Skip to content

Instantly share code, notes, and snippets.

@alexcroox
Last active July 23, 2017 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexcroox/2b8fd5f4280429ba75ac4b98e6d25f40 to your computer and use it in GitHub Desktop.
Save alexcroox/2b8fd5f4280429ba75ac4b98e6d25f40 to your computer and use it in GitHub Desktop.
Automated DB Backups to AWS Glacier
#!/usr/bin/env python
import sys
from datetime import datetime
import subprocess
DB_LOGIN = 'root'
DB_PASSWD = ''
VAULT_NAME = 'r3-backup'
HOSTNAME = 'Titanmods'
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 = ["aws", 'glacier', 'upload-archive', '--account-id -', '--vault-name %s'%VAULT_NAME, '--body', '%s.tar.gz'%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:
http://docs.aws.amazon.com/cli/latest/reference/glacier/index.html#cli-aws-glacier
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