Created
January 9, 2015 19:47
-
-
Save jcinis/03cf797ac3f47a64c14b to your computer and use it in GitHub Desktop.
MongoDB backup script used at nubook.com
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 | |
''' | |
Script to backup mongodb data and push it out to s3 | |
Tarballs will remain so local backups will need to be removed manually in the case that the drive is full | |
Run from crontab | |
20 1 * * * /usr/local/bin/nubook-mongodb-backup >/dev/null 2>&1 | |
''' | |
import string, os, sys | |
import time | |
import boto | |
from socket import gethostname | |
AWS_ACCESS_KEY_ID = '{{AWS_ACCESS_KEY_ID}}' | |
AWS_SECRET_ACCESS_KEY = '{{AWS_ACCESS_KEY_SECRET}}' | |
# Removing checking for master and adding hostname to the tarball naming convention | |
#cmd = '/usr/bin/mongo --eval "db.isMaster()[\'ismaster\']"|tail -1' | |
#foo = os.popen(cmd).readlines()[0].strip() | |
#if foo == 'true': | |
# print "I'm the primary, I don't get backed up. Bye!" | |
# sys.exit(0) | |
today = time.strftime('%Y%m%d') | |
hostname = gethostname() | |
db_to_back_up ='nubook' | |
basename = '%s-%s-%s' % (today, db_to_back_up, hostname) | |
dest = '/opt/mongobackup/' + basename | |
cleanup_cmd = "/bin/rm -rfv %s" % (dest,) | |
backup_cmd = "/usr/bin/mongodump --out %s --db %s" % (dest, db_to_back_up) | |
tarball_cmd = "/bin/tar cvzf %s.tgz %s" % (dest, dest) | |
os.system(cleanup_cmd) | |
os.makedirs(dest, 0755) | |
os.system(backup_cmd) | |
os.system(tarball_cmd) | |
# SEND TO S3 | |
def percent_complete(complete, total): | |
sys.stdout.write('.') | |
sys.stdout.flush() | |
bucket_name = AWS_ACCESS_KEY_ID.lower() + '-nubook-mongodb-backups' | |
conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) | |
bucket = conn.create_bucket(bucket_name) | |
k = boto.s3.key.Key(bucket) | |
k.key = basename + '.tgz' | |
k.set_contents_from_filename(dest + '.tgz', | |
cb=percent_complete, num_cb=10) | |
os.system(cleanup_cmd) | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment