Skip to content

Instantly share code, notes, and snippets.

@rjmacarthy
Created July 12, 2017 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjmacarthy/36ffbfb12269bba6ce351bb7eba6491d to your computer and use it in GitHub Desktop.
Save rjmacarthy/36ffbfb12269bba6ce351bb7eba6491d to your computer and use it in GitHub Desktop.
Mongo backup
#!/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