Backup/Restore Mongodb with timing.
Backup:
sudo mongodump --db db_name --out /path_of_your_backup/`date +"%m-%d-%y"`
--db
argument for databse name
--out
argument for path of output
Restore:
sudo mongorestore --db db_name --drop /path_of_your_backup/01-01-19/db_name/
--drop
argument for drop databse before restore
Timing:
You can use crontab for timing backup:
sudo crontab -e
It opens with editor(e.g. nano)
3 3 * * * mongodump --out /path_of_your_backup/`date +"%m-%d-%y"`
backup every day at 03:03 AM
Depending on your MongoDB database sizes you may soon run out of disk space with too many backups. That's why it's also recommended to clean the old backups regularly or to compress them. For example, to delete all the backups older than 7 days you can use the following bash command:
3 1 * * * find /path_of_your_backup/ -mtime +7 -exec rm -rf {} \;
delete all the backups older than 7 days
Good Luck.