Skip to content

Instantly share code, notes, and snippets.

@hoffrocket
Created May 16, 2011 21:33
Show Gist options
  • Save hoffrocket/975419 to your computer and use it in GitHub Desktop.
Save hoffrocket/975419 to your computer and use it in GitHub Desktop.
import commands
import pymongo
import time
from pymongo import Connection
from boto.ec2.connection import EC2Connection
host = '127.0.0.1'
timestamp = time.strftime("%Y-%m-%d-%H-%M")
CONFIGS_HOST = 'mongoc-server'
CONFIGS_PORT = 27019
shards = [
{
'name': 'shard-0',
'port': 27017,
'volume': 'vol-1212'
},
{
'name': 'shard-1',
'port': 27018,
'volume': 'vol-23323'
},
{
'name': 'shard-2',
'port': 27019,
'volume': 'vol-3434'
}
]
ec2 = EC2Connection('key', 'secret')
# create a connection to the mongo-configs server and get the current
# balancer status
db = Connection(CONFIGS_HOST, CONFIGS_PORT)['config']
pre_backup_balancer_status = db.settings.find({'_id': 'balancer'}).next()
# stop the autobalancer
db.settings.update({'_id': 'balancer'}, {'$set': {'stopped': True}})
try:
for shard in shards:
print '%s: starting backup' % shard['name']
mongodb = Connection(host, shard['port'], slave_okay=True)
volume = ec2.get_all_volumes(shard['volume'])[0]
print '%s: locking mongodb' % shard['name']
mongodb.admin.command('fsync', lock=True)
print '%s: initiating volume snapshot' % shard['name']
description = "%s-%s" % (shard['name'], timestamp)
try:
print '%s: %s' % (shard['name'], volume.create_snapshot(description))
except:
print '%s: could not initiate snapshot' % shard['name']
print '%s: unlocking mongodb' % shard['name']
mongodb.admin['$cmd'].sys.unlock.find_one()
print '%s: backup complete' % shard['name']
# back up the mongo configs server
status, output = commands.getstatusoutput(
'mongodump -h %s:%d -d config -o /backup/mongoc/%s' \
% (CONFIGS_HOST, CONFIGS_PORT, timestamp))
if status: print 'error dumping config db: %s' % o
except:
print 'oops, something bad happened during the shard backups :('
if pre_backup_balancer_status['stopped'] is False:
db.settings.update({'_id': 'balancer'}, {'$set': {'stopped': False}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment