Created
April 11, 2017 11:08
-
-
Save mxmader/bb646907496982e9ec1da58c38432061 to your computer and use it in GitHub Desktop.
Upload a gzip-compressed mysql dump to google drive via pipes, generate an email on any failure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import datetime | |
import mandrill | |
import os | |
import subprocess | |
import sys | |
import time | |
class GDriveUploadError(Exception): | |
pass | |
class DbDumpError(Exception): | |
pass | |
def send_warning_email(subject, message): | |
mandrill_client = mandrill.Mandrill(mandrill_api_key) | |
print "sending warning email to %s with subject: %s" % (admin_email, subject) | |
message = { | |
'from_email': bot_email, | |
'from_name': bot_name, | |
'text' : message, | |
'subject' : subject, | |
'to': [{'email': admin_email, | |
'type': 'to'}] | |
} | |
try: | |
return mandrill_client.messages.send(message) | |
except mandrill.Error, e: | |
print 'A mandrill error occurred: %s - %s' % (e.__class__, e) | |
raise | |
admin_email = "[email protected]" | |
bot_email = "[email protected]" | |
bot_name = "My backup bot" | |
database = "mydb" | |
gdrive_folder_id = "<google drive folder id>" | |
mandrill_api_key = "<mandrill api key>" | |
timestamp = datetime.datetime.fromtimestamp(time.time()).strftime('%Y%m%d%H%M%S') | |
dump_file = "%s-%s.sql.gz" % (database, timestamp) | |
dump_command = "mysqldump --skip-extended-insert %s" % database | |
upload_command = "gdrive upload - --parent %s %s" % (gdrive_folder_id, dump_file) | |
try: | |
print "dumping database: %s through gzip" % (database) | |
print "uploading database dump to google drive as %s" % dump_file | |
dump_process = subprocess.Popen(dump_command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
gzip_process = subprocess.Popen("gzip --stdout".split(), stdin=dump_process.stdout, stdout=subprocess.PIPE) | |
upload_process = subprocess.Popen(upload_command.split(), stdin=gzip_process.stdout, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
dump_return_code = dump_process.wait() | |
if dump_return_code != 0: | |
raise DbDumpError(dump_process.stderr.read()) | |
gzip_return_code = gzip_process.wait() | |
if gzip_return_code != 0: | |
raise DbDumpError(gzip_process.stderr.read()) | |
# upload the compressed database dump to google drive | |
upload_return_code = upload_process.wait() | |
if upload_return_code != 0: | |
raise GDriveUploadError(upload_process.stdout.read()) | |
print "upload successful" | |
except DbDumpError as e: | |
print "database dump failed - sending email" | |
send_warning_email("Failed to write database dump: %s" % database, str(e)) | |
sys.exit(1) | |
except GDriveUploadError as e: | |
print "gdrive upload failed - sending email" | |
send_warning_email("Failed to upload to google drive: %s" % database, str(e)) | |
sys.exit(1) | |
except Exception as e: | |
print "unknown error - sending email" | |
send_warning_email("Database backup general error for: %s" % database, str(e)) | |
sys.exit(1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment