Backup MongoDB using mongodump

Backup a Whole Database

mongodump --db mydb
sudo mongodump --db mydb --out /var/backups/mongo

Backup a Single Collection

mongodump -d mydb -o /var/backups/mongo --collection users

Restore a Whole Database

sudo mongorestore --db mydb /var/backups/mongo/mydb

Restore a whole collection

mongorestore -d mydb -c users mydb/users.bson

Taking Regular Backups Using Mongodump/Mongorestore

sudo crontab -e

backup every day at 4:30 AM

30 4 * * * mongodump --db mydb

Archive output

mongodump --db monitoring --archive=monitoring_20190604.archive

Gzip output

mongodump --db monitoring --archive=monitoring_20190604.archive.gz --gzip

References
https://severalnines.com/blog/review-mongodb-backup-options
https://docs.mongodb.com/manual/reference/program/mongodump/

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.