How do I create a MongoDB dump of my database?

前端 未结 19 2473
既然无缘
既然无缘 2020-12-22 15:06

What command do I use and run?

19条回答
  •  Happy的楠姐
    2020-12-22 15:55

    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.

    ref: https://www.digitalocean.com/community/tutorials/how-to-back-up-restore-and-migrate-a-mongodb-database-on-ubuntu-14-04

提交回复
热议问题