-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from divyenduz/db_interface
DB interface for mysql, sqlite3
- Loading branch information
Showing
7 changed files
with
62 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,7 @@ xcuserdata | |
# svn & cvs | ||
.svn | ||
CVS | ||
|
||
# python files | ||
*.pyc | ||
__pycache__/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import db_interface |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from exceptions import ValueError | ||
class DumpProtocol: | ||
|
||
def dump(self, config=None, verbose=False): | ||
raise ValueError('DumpProtocol not followed') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import subprocess | ||
|
||
from .dump_protocol import DumpProtocol | ||
|
||
class MySQLDump(DumpProtocol): | ||
|
||
def dump(self, config, s3_bucket, s3_bucket_key_name, filepath, verbose=False, upload_callback=None): | ||
sqldump_cmd = ['mysqldump', config['NAME'], '-h', config['HOST'], '-P', config['PORT'], '-u', config['USER'], '-p{password}'.format(password=config['PASSWORD'])] | ||
proc = subprocess.Popen(sqldump_cmd, stdout=subprocess.PIPE) | ||
|
||
if verbose: | ||
print('Dumping MySQL database: {database} to file {filepath}'.format(database=config['NAME'], filepath=filepath)) | ||
|
||
with open(filepath, 'w+') as f: | ||
while True: | ||
buf = proc.stdout.read(4096*1024) # Read 4 MB | ||
if buf != '': | ||
f.write(buf) | ||
if verbose: | ||
print('- Written 4 MB') | ||
else: | ||
break | ||
|
||
if verbose: | ||
print('+ Dump finished') | ||
|
||
if upload_callback is not None: | ||
upload_callback(f, s3_bucket, s3_bucket_key_name, verbose) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import subprocess | ||
|
||
from shutil import copy2 as copyfile | ||
|
||
from .dump_protocol import DumpProtocol | ||
|
||
class SQLiteDump(DumpProtocol): | ||
|
||
def dump(self, config, s3_bucket, s3_bucket_key_name, filepath, verbose=False, upload_callback=None): | ||
if verbose: | ||
print('Copying SQLite file to {filepath}'.format(filepath=filepath)) | ||
|
||
copyfile(config['NAME'], filepath) | ||
|
||
with open(filepath, 'r') as f: | ||
if upload_callback is not None: | ||
upload_callback(f, s3_bucket, s3_bucket_key_name, verbose) |