Skip to content

Instantly share code, notes, and snippets.

@Bijendra
Created May 8, 2014 19:50
Show Gist options
  • Save Bijendra/a2d8f48d55271e2d226c to your computer and use it in GitHub Desktop.
Save Bijendra/a2d8f48d55271e2d226c to your computer and use it in GitHub Desktop.
Backup mongodb database installed on ec2 instance and store backup files over s3
#Force file syncronization and lock writes
mongo admin --eval "printjson(db.fsyncLock())"
#Above line can be commented in case of large databases as it stops write operations while making a db dump for proper sync.
MONGODUMP_PATH="/usr/bin/mongodump"
MONGO_HOST="10.0.0.51"
MONGO_PORT="27017"
MONGO_DATABASE="cv_appcloud_btoc"
MONGO_SAVE="/home/vol/expin-dump/"
TIMESTAMP=`date +%F-%H%M`
S3_BUCKET_PATH="bucket-name"
# Create backup
$MONGODUMP_PATH -h $MONGO_HOST:$MONGO_PORT -d $MONGO_DATABASE
mv dump/$MONGO_DATABASE mongodb-$MONGO_DATABASE-$TIMESTAMP
# Add timestamp to backup
tar cf mongodb-$MONGO_DATABASE-$TIMESTAMP.tar mongodb-$MONGO_DATABASE-$TIMESTAMP
#Unlock database writes
mongo admin --eval "printjson(db.fsyncUnlock())"
#If the lock in top is not enabled then comment this line
s3cmd put mongodb-$MONGO_DATABASE-$TIMESTAMP.tar s3://expin-db/
#s3cmd should be installed, for ubuntu+ec2, install using sudo apt-get install s3cmd, and provide the aws credentials to authenticate.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment