MongoDB backup scriptSeptember 16 2016
Here's a little script I wrote yesterday which creates a mongodump of a database, tar's it, gzip's it and then deletes the original dump from /temp. I installed this in my crontab as a nightly backup strategy.
CURDATE="$(date +%F_%R)" DB="backup_test" TEMP_DIR="temp" BACKUP_LOC="$DB.tar.gz" TEMP_BACKUP_LOC="$TEMP_DIR/$DB" USER="backup" PASS="backup" mongodump -d $DB -o $TEMP_DIR -u $USER -p $PASS tar -czf $BACKUP_LOC -C $TEMP_BACKUP_LOC . rm -R $TEMP_BACKUP_LOC echo "Done"
- You can decide to use $CURDATE in $BACKUP_LOC if you don't want the tar to be replaced every time it's executed.
- Make sure to cd into the directory before executing the script as it does use relative paths by default. 😊