Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DB interface for mysql, sqlite3 #10

Merged
merged 6 commits into from
Jan 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ env/
# backup config files / dumps
*.json
*.sql
*.sqlite

# osx noise
.DS_Store
Expand All @@ -25,3 +26,7 @@ xcuserdata
# svn & cvs
.svn
CVS

# python files
*.pyc
__pycache__/
57 changes: 7 additions & 50 deletions db_s3_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import random, string
import os
import re
from shutil import copy2 as copyfile

from db_s3_backup.db_interface.mysql_dump import MySQLDump
from db_s3_backup.db_interface.sqlite_dump import SQLiteDump

intervals = [
# For 2 days, 1 backup per 1 hour
Expand Down Expand Up @@ -36,53 +38,6 @@ def connect_to_s3(aws_config, verbose = False):

return (s3_connection, s3_bucket,)

#
# Create MySQL dump
#

def create_mysql_dump(mysql_config, s3_bucket, s3_bucket_key_name, filepath, verbose = False):
sqldump_cmd = ['mysqldump', mysql_config['NAME'], '-h', mysql_config['HOST'], '-P', mysql_config['PORT'], '-u', mysql_config['USER'], '-p{password}'.format(password=mysql_config['PASSWORD'])]

proc = subprocess.Popen(sqldump_cmd, stdout=subprocess.PIPE)

# Write to file

if verbose:
print('Dumping MySQL database: {database} to file {filepath}'.format(database=mysql_config['NAME'], filepath=filepath))

f=open(filepath, 'w+')
i=0
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')

# Send file to S3
upload_dump_s3(f, s3_bucket, s3_bucket_key_name, verbose)

f.close()

#
# Create SQLite dump
#

def create_sqlite_dump(sqlite_config, s3_bucket, s3_bucket_key_name, filepath, verbose = False):
if verbose:
print('Copying SQLite file to {filepath}'.format(filepath=filepath))

copyfile(sqlite_config['NAME'], filepath)

# Send file to S3
f=open(filepath, 'r')
upload_dump_s3(f, s3_bucket, s3_bucket_key_name, verbose)
f.close()

#
# Upload dump to Amazon S3
Expand Down Expand Up @@ -254,9 +209,11 @@ def delete_local_backups(dir_path, backup_prefix, backup_extension, verbose = Fa

if args.create_dump:
if config['database']['ENGINE'] == 'mysql':
create_mysql_dump(config['database'], s3_bucket, filename, filepath, verbose=args.verbose)
mysql_dump = MySQLDump()
mysql_dump.dump(config['database'], s3_bucket, filename, filepath, verbose=args.verbose, upload_callback=upload_dump_s3)
elif config['database']['ENGINE'] == 'sqlite':
create_sqlite_dump(config['database'], s3_bucket, filename, filepath, verbose=args.verbose)
sqlite_dump = SQLiteDump()
sqlite_dump.dump(config['database'], s3_bucket, filename, filepath, verbose=args.verbose, upload_callback=upload_dump_s3)

if args.delete_remote:
cleanup_old_backups(backup_prefix, backup_extension, intervals, s3_bucket, verbose=args.verbose, action = (not args.simulate_delete))
Expand Down
1 change: 1 addition & 0 deletions db_s3_backup/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import db_interface
Empty file.
5 changes: 5 additions & 0 deletions db_s3_backup/db_interface/dump_protocol.py
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')
28 changes: 28 additions & 0 deletions db_s3_backup/db_interface/mysql_dump.py
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)
17 changes: 17 additions & 0 deletions db_s3_backup/db_interface/sqlite_dump.py
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)