MongoDB backup script

September 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"

Notes: