MongoDB Backup
What Is Backup?
Replication keeps copies of your data for availability, but it is not a backup: an accidental delete or a bad deploy is replicated to every member immediately. A backup is an independent, restorable copy from a point in time. Common self-managed methods are mongodump/mongorestore for logical backups and filesystem snapshots for large deployments. MongoDB Atlas provides managed backups.
Why It Matters
| Reason | Benefit |
|---|---|
| Protects against human error | Accidental drops and bad updates are common, and replicas copy them instantly. |
| Enables recovery | Backups restore data after corruption, ransomware, or a lost cluster. |
| Supports cloning | Dumps create copies for staging or analysis. |
| Compliance | Many policies require retained, tested backups. |
How To Use It
- Install the MongoDB Database Tools so mongodump and mongorestore are available.
- Run mongodump with a connection string and an output location, for example --archive with --gzip.
- Use --oplog on a replica set for a consistent point-in-time dump of all databases.
- Copy the backup files to storage that is separate from the database server.
- Restore into a scratch deployment with mongorestore to verify it works.
- For large datasets, consider filesystem or volume snapshots with journaling enabled.
- Automate the schedule and monitor for failures; on Atlas, configure the backup policy instead.
mongosh Examples
# Dump everything to a compressed single file
mongodump --uri="mongodb://backupUser:PASS@localhost:27017/?authSource=admin" --gzip --archive=backup-2026-11-28.gz
# Dump a single database
mongodump --uri="mongodb://localhost:27017" --db=shop --out=./dump
# Consistent dump of a replica set including oplog
mongodump --uri="mongodb://localhost:27017/?replicaSet=rs0" --oplog --out=./dump-full
# Restore from the archive into another deployment
mongorestore --uri="mongodb://localhost:27018" --gzip --archive=backup-2026-11-28.gz
# Restore a full dump that includes oplog entries
mongorestore --uri="mongodb://localhost:27018" --oplogReplay ./dump-full
# Restore into new namespaces
mongorestore --uri="mongodb://localhost:27018" --nsFrom="shop.*" --nsTo="shop_copy.*" ./dump Edge Cases & Pitfalls
Replication is not backup
Dropping a collection on the primary drops it on every secondary within moments.
Dumps of live data may be inconsistent
Without --oplog on a replica set, writes during the dump can produce a state that never existed at one moment.
mongodump is slow on huge data
Logical dumps read all data and rebuild indexes on restore. Use snapshots for very large deployments.
Snapshots need care
A filesystem snapshot must capture a consistent state, with journal and data on the same volume or a coordinated method.
Backups need security too
Dump files contain your data. Encrypt them and restrict access.
Cheat Sheet
| Topic | Detail |
|---|---|
| Replication | High availability; mirrors mistakes, so it does not replace backups |
| mongodump | Creates a logical BSON export of databases or collections |
| mongorestore | Loads a mongodump output back into a deployment |
| --uri | Connection string option for both tools |
| --db / --collection | Limit the dump to one database or collection |
| --gzip / --archive | Compress output or write a single archive file |
| --oplog | Include oplog entries for a consistent replica set dump |
| Filesystem snapshot | Block-level copy of the data files; fast for large data when journaling is enabled |
| Atlas backups | Managed snapshots and, on higher tiers, continuous backups |
| Restore test | Periodically restore to a scratch environment to verify backups |
Key Takeaways
A backup you have never restored is only a hope. Schedule backups, store them off the server, and practice restoring.
Frequently Asked Questions
Is replication the same as a backup?
No. Replication provides availability and copies every change, including mistakes. A backup is a separate, restorable point-in-time copy.
How do I back up MongoDB?
Use mongodump for logical backups, filesystem or volume snapshots for larger deployments, or Atlas managed backups.
How do I restore from mongodump?
Use mongorestore with the same dump directory or archive file, and add --oplogReplay if you used --oplog.
Does mongodump include indexes?
It saves index definitions in metadata files, and mongorestore rebuilds the indexes during the restore.
How often should I back up?
It depends on how much data loss you can tolerate. Daily is common, with more frequent or continuous backups for critical data.
Where should I store backups?
Off the database server and ideally in another location or account, with encryption and access control.
Next: MongoDB Replication
Continue the MongoDB learning path.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
8 people found this page helpful
