How to Backup and Restore MongoDB Databases
In this tutorial, you'll learn about How to Backup and Restore MongoDB Databases. We cover key concepts, practical examples, and best practices.
The Problem
You need to back up your MongoDB database or restore it after data loss. Without a backup strategy, one corruption event or accidental drop can destroy days of work.
Quick Fix
Step 1: Create a backup with mongodump
mongodump --uri="mongodb://localhost:27017/mydb" --out=/backups/mongodb/2024-06-24
This exports all collections from mydb into a BSON dump at the specified path. Each collection gets its own .bson and .metadata.json file.
Expected output:
2024-06-24T10:00:00.000+0000 writing mydb.users to /backups/mongodb/2024-06-24/mydb/users.bson
2024-06-24T10:00:00.100+0000 done dumping mydb.users (1245 documents)
Step 2: Restore from a backup
mongorestore --uri="mongodb://localhost:27017" /backups/mongodb/2024-06-24
This reads the BSON files and inserts all documents into the target database. Collections are created if they do not exist.
Expected output:
2024-06-24T11:00:00.000+0000 preparing collections to restore from
2024-06-24T11:00:00.050+0000 restoring mydb.users from /backups/mongodb/2024-06-24/mydb/users.bson
2024-06-24T11:00:00.800+0000 done restoring mydb.users (1245 documents)
Step 3: Back up a single collection
mongodump --uri="mongodb://localhost:27017/mydb" --collection=users --out=/backups/mongodb/single
Use this when you only need one collection — it is faster and produces a smaller dump.
Step 4: Restore a single collection
mongorestore --uri="mongodb://localhost:27017/mydb" --collection=users --drop /backups/mongodb/single/mydb/users.bson
The --drop flag removes the existing collection before restoring, ensuring a clean replacement.
Step 5: Authenticated backup
mongodump --uri="mongodb://myuser:password@localhost:27017/mydb?authSource=admin" --out=/backups/mongodb/auth
Include credentials and the auth source in the connection string when authentication is enabled.
Step 6: Compress the backup
mongodump --uri="mongodb://localhost:27017/mydb" --archive=/backups/mongodb/mydb.gz --gzip
The --archive flag with --gzip produces a single compressed file instead of a directory of BSON files.
Common Errors
mongodump not found: If mongodump is not installed, install the MongoDB database tools: sudo apt install <a href="/databases/mongodb/">mongodb</a>-database-tools.
Authentication failure during dump: Add --username and --password flags or include credentials in the URI: mongodump --uri="<a href="/databases/mongodb/">mongodb</a>://user:pass@host/db".
Disk space exhausted: Backups fail silently when the disk is full. Check available space with df -h /backups before running large dumps.
Version mismatch: Restoring a dump from a newer MongoDB version into an older one fails. Ensure the source and target MongoDB versions are compatible.
Oplog-aware backups for replica sets: For point-in-time recovery, use mongodump --oplog which captures oplog entries during the dump. This allows restoring to a specific timestamp rather than just the dump time.
Handling large databases: For databases over 100 GB, mongodump may be too slow. Consider using file-system snapshots (LVM, EBS) or MongoDB Atlas backups instead.
Alternative Solutions
Use mongoexport for JSON export:
mongoexport --uri="mongodb://localhost:27017/mydb" --collection=users --out=users.json
Restore JSON with mongoimport:
mongoimport --uri="mongodb://localhost:27017/mydb" --collection=users --file=users.json
Common Errors
mongodump not found: If mongodump is not installed, install the MongoDB database tools: sudo apt install <a href="/databases/mongodb/">mongodb</a>-database-tools.
Authentication failure during dump: Add --username and --password flags or include credentials in the URI: mongodump --uri="<a href="/databases/mongodb/">mongodb</a>://user:pass@host/db".
Disk space exhausted: Backups fail silently when the disk is full. Check available space with df -h /backups before running large dumps.
Version mismatch: Restoring a dump from a newer MongoDB version into an older one fails. Ensure the source and target MongoDB versions are compatible.
Prevention
- Schedule daily backups with
cronusingmongodump. - Store backups off-server (S3, rsync to another machine).
- Test restores periodically — an untested backup is no backup.
- Use
--gzipto save disk space and transfer time. - For replica sets, back up from a secondary node to avoid primary load.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro