Created
July 12, 2017 16:27
-
-
Save rjmacarthy/36ffbfb12269bba6ce351bb7eba6491d to your computer and use it in GitHub Desktop.
Mongo backup
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 | |
#Force file syncronization and lock writes | |
mongo -u "username" -p "password" --authenticationDatabase "admin" --eval "printjson(db.fsyncLock())" | |
MONGODUMP_PATH="/usr/bin/mongodump" | |
MONGO_HOST="127.0.0.1" #replace with your server ip | |
MONGO_PORT="27017" | |
MONGO_DATABASE="dbname" #replace with your database name | |
TIMESTAMP=`date +%F-%H%M` | |
S3_BUCKET_NAME="s3bucketname" #replace with your bucket name on Amazon S3 | |
# Create backup | |
$MONGODUMP_PATH -h $MONGO_HOST:$MONGO_PORT -d $MONGO_DATABASE -u "username" -p "password" | |
# Add timestamp to backup | |
mv dump mongodb-$HOSTNAME-$TIMESTAMP | |
tar cf mongodb-$HOSTNAME-$TIMESTAMP.tar mongodb-$HOSTNAME-$TIMESTAMP | |
# Upload to S3 | |
s3cmd put mongodb-$HOSTNAME-$TIMESTAMP.tar s3://yours3url/mongodb-$HOSTNAME-$TIMESTAMP.tar #replace with s3 url | |
#Unlock database writes | |
mongo -u "username" -p "username" --authenticationDatabase "admin" --eval "printjson(db.fsyncUnlock())" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment