Skip to content

Instantly share code, notes, and snippets.

@97-109-107
Created July 7, 2014 08:59
Show Gist options
  • Save 97-109-107/90927f8d46740ee42d15 to your computer and use it in GitHub Desktop.
Save 97-109-107/90927f8d46740ee42d15 to your computer and use it in GitHub Desktop.
A hopefully working backup script for mongo. Use by providing names of dbs to backup, check if MONGODUMP_DEST exist prior to executing.
#!/bin/bash
for var in "$@"
do
#Force file syncronization and lock writes
mongo admin --eval "printjson(db.fsyncLock())"
#Keep the trailing slash
MONGODUMP_DEST="/home/ubuntu/mongobackups/"
MONGO_DATABASE="$var"
MONGODUMP_PATH="/usr/bin/mongodump"
MONGO_HOST="localhost" #replace with your server ip
MONGO_PORT="27017"
TIMESTAMP=`date +%F-%H%M`
# Create backup
$MONGODUMP_PATH -h $MONGO_HOST:$MONGO_PORT -d $MONGO_DATABASE -o $MONGODUMP_DEST
# Add timestamp to backup
cd $MONGODUMP_DEST
mv $MONGO_DATABASE mongodb-$HOSTNAME-$TIMESTAMP
tar cf mongodb-$MONGO_DATABASE-$TIMESTAMP.tar mongodb-$HOSTNAME-$TIMESTAMP
rm -rf mongodb-$HOSTNAME-$TIMESTAMP
#Unlock database writes
mongo admin --eval "printjson(db.fsyncUnlock())"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment