MongoDB Backup

Intermediate
8 min read
Updated: Nov 2026
Getting Started

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

ReasonBenefit
Protects against human errorAccidental drops and bad updates are common, and replicas copy them instantly.
Enables recoveryBackups restore data after corruption, ransomware, or a lost cluster.
Supports cloningDumps create copies for staging or analysis.
ComplianceMany policies require retained, tested backups.

How To Use It

  1. Install the MongoDB Database Tools so mongodump and mongorestore are available.
  2. Run mongodump with a connection string and an output location, for example --archive with --gzip.
  3. Use --oplog on a replica set for a consistent point-in-time dump of all databases.
  4. Copy the backup files to storage that is separate from the database server.
  5. Restore into a scratch deployment with mongorestore to verify it works.
  6. For large datasets, consider filesystem or volume snapshots with journaling enabled.
  7. Automate the schedule and monitor for failures; on Atlas, configure the backup policy instead.

mongosh Examples

shell
# 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

TopicDetail
ReplicationHigh availability; mirrors mistakes, so it does not replace backups
mongodumpCreates a logical BSON export of databases or collections
mongorestoreLoads a mongodump output back into a deployment
--uriConnection string option for both tools
--db / --collectionLimit the dump to one database or collection
--gzip / --archiveCompress output or write a single archive file
--oplogInclude oplog entries for a consistent replica set dump
Filesystem snapshotBlock-level copy of the data files; fast for large data when journaling is enabled
Atlas backupsManaged snapshots and, on higher tiers, continuous backups
Restore testPeriodically 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.

MongoDB Replication →

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful