Skip to content

Instantly share code, notes, and snippets.

@marvin
Created November 24, 2012 21:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marvin/4141497 to your computer and use it in GitHub Desktop.
Save marvin/4141497 to your computer and use it in GitHub Desktop.
quick prototype in python for taking a mysqldump, zip and encrypt it and store in couchdb
import couchdb
import random
import string
from beefish import encrypt_file
from subprocess import Popen
from subprocess import PIPE
# constants
SRC_DB_USER = "root"
SRC_DB_PASS = ""
SRC_DB_HOST = "localhost"
SRC_DB_TABLE = "testdb"
DST_DB_HOST = "http://localhost:5984"
DST_DB_TABLE = "dbbackuptest"
RND_LENGTH = 32
dump_cmd = "mysqldump -u %s -p%s -h %s %s > %s"
tar_cmd = "tar -czvf %s %s"
# dynamic name generator
def id_generator(size=RND_LENGTH, chars=string.ascii_uppercase + string.ascii_lowercase + string.digits):
return ''.join(random.choice(chars) for x in range(size))
## dump part
dump_name = id_generator()
dump_file = "tmp/" + dump_name + ".sql"
run_dump = dump_cmd % (SRC_DB_USER, SRC_DB_PASS, SRC_DB_HOST, SRC_DB_TABLE, dump_file)
print "-> running command: " + run_dump
pd = Popen(run_dump, shell=True, stdout=PIPE)
for ln in pd.stdout:
print '#',ln
print "-> dump finished"
## compression part
tar_file = "tmp/" + dump_name + ".tgz"
print "-> compressing dump to " + tar_file
tar_dump = tar_cmd % (tar_file, dump_file)
pt = Popen(tar_dump, shell=True, stdout=PIPE)
for ln in pt.stdout:
print '#',ln
print "-> compression finished"
## encrypting part
password = id_generator(size=16)
enc_file = "tmp/" + dump_name + ".enc"
print "-> encrypting compressed dump to %s using password %s" % (enc_file, password)
encrypt_file(tar_file, enc_file, password)
print "-> encryption completed"
## moving to couchdb
print "-> connecting to couch"
conn = couchdb.Server(DST_DB_HOST)
if DST_DB_TABLE not in conn:
db = conn.create(DST_DB_TABLE)
else:
db = conn[DST_DB_TABLE]
print "-> creating doc for new file"
doc = {'filename': dump_name, 'password': password}
db.save(doc)
print "-> attaching db dump to doc"
with open(enc_file) as f:
data = f.read()
db.put_attachment(doc, data, filename=dump_name + ".enc")
print "-> finished!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment