Skip to content

Instantly share code, notes, and snippets.

@fabdrol
Created February 15, 2016 20:35
Show Gist options
  • Select an option

  • Save fabdrol/599bbaa5daa9058b7af4 to your computer and use it in GitHub Desktop.

Select an option

Save fabdrol/599bbaa5daa9058b7af4 to your computer and use it in GitHub Desktop.
Daily Mongodb backup (all databses) to S3 using AWS CLI
#!/bin/bash
# Lock DB
mongo admin --eval "printjson(db.fsyncLock())"
MONGO_DUMP="/usr/bin/mongodump"
MONGO_HOST="localhost"
MONGO_PORT="27017"
TIMESTAMP=`date +%F-%H%M`
S3_BUCKET="ayers-rock"
S3_PATH="mongodb"
# Dump DB
$MONGO_DUMP -h $MONGO_HOST:$MONGO_PORT
# Rename & tar
mv dump mongodb-$HOSTNAME-$TIMESTAMP
tar cf mongodb-$HOSTNAME-$TIMESTAMP.tar mongodb-$HOSTNAME-$TIMESTAMP
# Copy to S3
aws s3 cp mongodb-$HOSTNAME-$TIMESTAMP.tar s3://$S3_BUCKET/$S3_PATH/mongodb-$HOSTNAME-$TIMESTAMP.tar
# Clean-up
rm -rf mongodb-$HOSTNAME-$TIMESTAMP.tar
rm -rf mongodb-$HOSTNAME-$TIMESTAMP
# Unlock DB
mongo admin --eval "printjson(db.fsyncUnlock())"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment