cron to backup mongodb

This is a Note-to-Self for maintain my mongodb.

create folder for keep your backup and script. Ex. Your Home is /root, then create /root/mongodb_backups and /root/my_scripts

In /root/my_scripts create a file call db_backup.sh

Put below script inside db_backup.sh

TIMESTAMP=`date +%y%m%d_%H%M%S`
DEST=/root/mongodb_backups/$TIMESTAMP
DBNAME="YOUR_DB_NAME"
DBUNAME="YOUR_DB_USER"
DBPWORD="YOUR_DB_PASSWORD"
mkdir $DEST
mongodump --gzip -d $DBNAME -u $DBUNAME -p $DBPWORD --authenticationDatabase admin -o $DEST

Then make it executable

sudo chmod +X db_backup.sh

Setup cron

crontab -e
29 2 * * * sh /root/my_scripts/db_backup.sh

Above cron will run every day on 2:29