Created
February 15, 2016 20:35
-
-
Save fabdrol/599bbaa5daa9058b7af4 to your computer and use it in GitHub Desktop.
Daily Mongodb backup (all databses) to S3 using AWS CLI
This file contains hidden or 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
| #!/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