Skip to content

Instantly share code, notes, and snippets.

@dryan
Created September 6, 2009 18:59
Show Gist options
  • Save dryan/181921 to your computer and use it in GitHub Desktop.
Save dryan/181921 to your computer and use it in GitHub Desktop.
Django script for backing up database to S3
from django.core.management import setup_environ
import settings
setup_environ(settings)
# database backup routine
def database_backup():
import tarfile,subprocess,tempfile,os,S3
from datetime import datetime
now = datetime.utcnow()
archive_name = u'database-archive%s.tar.gz' % ( now.strftime('/%Y/%m/%d/%H-%M') )
which_mysqldump = subprocess.Popen('which mysqldump',shell=True,stdout=subprocess.PIPE)
mysqldump_path = which_mysqldump.communicate()[0].replace("\n","")
if settings.DATABASE_HOST:
host = settings.DATABASE_HOST
else:
host = 'localhost'
proc1 = subprocess.Popen(mysqldump_path + " -h " + host + " -u " + settings.DATABASE_USER + " -p" + settings.DATABASE_PASSWORD + " --add-drop-table --add-locks --complete-insert --extended-insert --single-transaction --database " + settings.DATABASE_NAME,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
t1 = tempfile.NamedTemporaryFile()
t1.write(proc1.communicate()[0])
tar = tarfile.open( (os.path.join(os.curdir, "temp-database-dump.tar.gz")), "w|gz")
tar.add(t1.name,now.strftime('%Y-%m-%d_%H-%M.sql'))
t1.close()
tar.close()
tardata = open(os.path.join(os.curdir, "temp-database-dump.tar.gz") , "rb").read()
conn = S3.AWSAuthConnection( settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY )
response = conn.put( settings.AWS_STORAGE_BUCKET_NAME, archive_name, S3.S3Object( tardata ),
{
'x-amz-acl': 'private',
'Content-type': 'application/x-tar-gz',
}
)
if response.http_response.status == 200 :
print "sucessfully uploaded the archive to Amazon S3"
else:
print "Uploading database dump to Amazon S3 is not successful"
print "Error : "+response.message
os.remove(os.path.join(os.curdir, "temp-database-dump.tar.gz"))
database_backup()
#!/bin/bash
export PYTHONPATH=[your python path]
export DJANGO_SETTINGS_MODULE=settings
export PYTHON_EGG_CACHE=[your project path]
/usr/bin/python /path/to/your/project/cron.py >> /path/to/your/project/dbcronlog
echo "cron job has run"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment