NewsBlur/utils/backups/backup_mongo.py

40 lines
1.1 KiB
Python
Raw Normal View History

import os
import sys
2011-05-18 14:59:05 -04:00
import shutil
2013-08-01 18:15:40 -07:00
CURRENT_DIR = os.path.dirname(__file__)
NEWSBLUR_DIR = ''.join([CURRENT_DIR, '/../../'])
sys.path.insert(0, NEWSBLUR_DIR)
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import time
import s3
COLLECTIONS = "classifier_tag classifier_author classifier_feed classifier_title userstories shared_stories category category_site sent_emails social_profile social_subscription social_services statistics feedback"
if False:
COLLECTIONS += " starred_stories"
date = time.strftime('%Y-%m-%d-%H-%M')
2011-05-18 14:59:05 -04:00
collections = COLLECTIONS.split(' ')
2011-10-05 19:30:21 -07:00
db_name = 'newsblur'
dir_name = 'backup_mongo_%s' % date
filename = '%s.tgz' % dir_name
2011-05-18 14:59:05 -04:00
os.mkdir(dir_name)
for collection in collections:
cmd = 'mongodump --db %s --collection %s -o %s' % (db_name, collection, dir_name)
2011-05-18 14:59:05 -04:00
print "Dumping %s: %s" % (collection, cmd)
os.system(cmd)
2013-04-20 15:06:32 -07:00
print "Compressing %s..." % filename
cmd = 'tar -zcf %s %s' % (filename, dir_name)
os.system(cmd)
print 'Uploading %s to S3...' % filename
2013-12-12 15:35:32 -08:00
try:
s3.save_file_in_s3(filename)
except Exception, e:
print " ****> Exceptions: %s" % e
2011-05-18 14:59:05 -04:00
shutil.rmtree(dir_name)
os.remove(filename)