Skip to content

Instantly share code, notes, and snippets.

@abulte
Created November 15, 2012 17:09
Show Gist options
  • Save abulte/4079819 to your computer and use it in GitHub Desktop.
Save abulte/4079819 to your computer and use it in GitHub Desktop.
Simple AWS Glacier backup for files and MySQL DB
#!/usr/bin/env python
import sys
from datetime import datetime
import subprocess
DB_LOGIN = 'root'
DB_PASSWD = 'XXXX'
VAULT_NAME = 'backup_sunny'
HOSTNAME = 'Sunny'
backup_string = ''
db_string = ''
today = datetime.today()
date_string = '%s%s%s-%s%s%s' % (today.year, today.month, today.day, today.hour, today.minute, today.second)
def execute_cmd(cmd):
cmd = ' '.join(cmd)
print "Executing %s..." % (cmd)
log.write("Executing %s..." % (cmd))
ret = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
log.write(ret.stdout.read())
print "Done."
with open('./to_backup.txt') as f, open('./log.txt', 'a+') as log, open('./to_backup_mysql.txt') as fdb:
print "Working..."
for line in f:
backup_string += line.rstrip('\r\n') + ' '
for line in fdb:
db_string += line.rstrip('\r\n') + ' '
backup_string += 'backup.temp.sql'
log.write('\n-------------- BACKUP START FOR %s----------------\n' % date_string)
# MYSQL DUMP
cmd = ['mysqldump', '-u%s'%DB_LOGIN, '-p%s'%DB_PASSWD, '--databases %s'%db_string.strip(),'--result-file=backup.temp.sql']
execute_cmd(cmd)
# COMPRESS
cmd = ["tar", 'cfz', '%s.tar.gz'%date_string, backup_string.strip()]
execute_cmd(cmd)
# SEND TO GLACIER
cmd = ["glacier-cmd", 'upload', VAULT_NAME, '%s.tar.gz'%date_string, "--description='%s backup for %s'"%(HOSTNAME, date_string)]
execute_cmd(cmd)
# CLEAN
execute_cmd(['mv', '%s.tar.gz'%date_string, 'last_backup.tar.gz'])
execute_cmd(['rm', './backup.temp.sql'])
Requirements:
https://github.com/uskudnik/amazon-glacier-cmd-interface
List of stuff to save (files in CWD):
- to_backup.txt: one file/dir per line
- to_backup_mysql.txt : one DB name per line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment