How to back up MongoDB, and check that the backup restores
Which method to use.
- On Atlas M10 or larger: Atlas Cloud Backup is already on. Add a
mongodumpto storage outside Atlas if you need a copy that survives the project or the account. - Atlas free tier, or a self-hosted database under a few hundred GB:
mongodump --archive --gzipon a schedule, restored somewhere now and then to prove it works. That is most of this page. - A large self-hosted replica set: file-system snapshots (LVM, EBS, ZFS) of a secondary, with the journal on the same volume.
The supported ways to back up MongoDB
MongoDB's documentation lists these for a deployment you run yourself, plus Atlas's own backups for a managed cluster:
| Method | What it copies | Point in time | Best for |
|---|---|---|---|
mongodump / mongorestore | Documents and index definitions (BSON). One collection, one database or all | Only with --oplog, on a full dump of a replica set | Small and medium databases, Atlas free tier, moving between versions or hosts |
| File-system snapshot | The data files, at block level | The snapshot moment | Large self-hosted deployments with LVM, EBS or ZFS |
cp or rsync of the data directory | The data files | Only with writes stopped (db.fsyncLock() or a shut-down node) | A secondary you can pause |
| Atlas Cloud Backup | Snapshots kept by Atlas | With continuous cloud backup | Atlas M10 and larger (Flex gets daily snapshots, free clusters only through a private preview) |
| Cloud Manager | Continuous, from the oplog | Yes | Self-hosted, with MongoDB's paid hosted management service |
| Ops Manager | Continuous, from the oplog | Yes | Self-hosted, run on your own servers, with an Enterprise Advanced subscription |
The snapshot-based ones are faster to restore but restore the whole deployment, onto the same
major version. A mongodump archive restores a single collection, into any database
name, on a newer server, which is why it is the copy most teams keep somewhere else even when
they have snapshots.
Back up with mongodump
Install the MongoDB Database Tools on the machine that runs the backup. They are a separate
package from the server: mongodb-database-tools from MongoDB's apt or yum
repository (Ubuntu and Debian do not ship it), or brew install mongodb-database-tools
on macOS.
Create a user that can read and nothing else. The built-in backup role covers a
full dump; for one database, read on that database is enough:
use admin
db.createUser({ user: "backup", pwd: passwordPrompt(), roles: [ { role: "read", db: "app" } ] })
Then dump to a single compressed archive. One file is easier to encrypt, upload and lock than a directory of BSON files:
mongodump --uri "mongodb://localhost:27017/app" --archive=app.archive.gz --gzip
writing `app.orders` to "archive `app.archive.gz`"
writing `app.customers` to "archive `app.archive.gz`"
done dumping `app.customers` (1200 documents)
done dumping `app.orders` (5000 documents)
mongodb://backup:PASSWORD@db.internal:27017/app?authSource=admin.- On a replica set, read from a secondary so the dump does not compete with
the application: add
readPreference=secondaryto the URI. - On Atlas, use the
mongodb+srv://string from Connect, then Drivers, and add the machine's address to the IP Access List. - Keep the password out of the command line on a shared machine, where
psshows it to other users. Put the URI in a file readable only by you and pass--config.
The oplog catch
mongodump reads one collection after another. If the app writes while it runs, the
orders collection can be from 02:00:01 and customers from 02:00:09.
For most apps that is fine. If you need every collection from the same moment,
--oplog also records the writes made during the dump, and
mongorestore --oplogReplay replays them. It only works for a full dump of a replica
set member. Against a single database it refuses:
mongodump --uri "mongodb://localhost:27017/app" --oplog --archive=app.archive.gz
Failed: bad option: --oplog mode only supported on full dumps
Restore it, and check what came back
Restore into a new database name, never over the live one. --nsFrom and
--nsTo rename on the way in, so you can do this on the production server without
touching production data:
mongorestore --uri "mongodb://localhost:27017" --archive=app.archive.gz --gzip \
--nsFrom='app.*' --nsTo='app_check.*'
finished restoring `app_check.customers` (1200 documents, 0 failures)
finished restoring `app_check.orders` (5000 documents, 0 failures)
restoring indexes for collection `app_check.orders` from metadata
6200 document(s) restored successfully. 0 document(s) failed to restore.
"Restored successfully" means the archive was readable. It does not tell you the archive had what you expected. Count the documents in each collection and compare them with the source:
mongosh app_check --quiet --eval \
'db.getCollectionNames().sort().forEach(c => print(c, db[c].countDocuments()))'
customers 1200
orders 5000
Then drop it with db.dropDatabase() in app_check. Put it in the
calendar for once a month, or script it.
How to test that a backup restores goes
further, to comparing the data itself.
What a database dump leaves out
- Users and roles. They live in the
admindatabase, so a dump ofappdoes not carry them. Keep them in your provisioning code, or dumpadminas well. - Server settings, replica set configuration and anything in
mongod.conf. - Index data. Only the definitions are saved; indexes are rebuilt on restore, so restore time grows with the number and size of indexes, not just the data.
Run it every night
#!/bin/sh
set -eu
out=/var/backups/mongo/app-$(date +%F).archive.gz
mongodump --config /etc/mongo-backup.yaml --archive="$out" --gzip
test -s "$out"
find /var/backups/mongo -name 'app-*.archive.gz' -mtime +14 -delete
/etc/mongo-backup.yaml holds one line, uri: mongodb://backup:..., with
mode 600. This keeps two weeks on the same disk. Copy each archive off the server too, encrypted
if it holds customer data: the PostgreSQL to S3 guide
has an encrypted upload to a locked bucket that works the same way for this file.
How SafeGrd does it
SafeGrd runs the same mongodump --archive, streams it into an encrypted snapshot on
your host, and writes it to S3-compatible storage under Object Lock, so nobody can delete it
before a date set by your plan or your retention settings. The connection string goes to
mongodump in an
owner-only file, never on the command line. Document counts and collection checksums are
recorded as the archive streams.
Then it does the check above for you, on a schedule. A Fire Drill reads the archive to its end
and matches the counts and checksums. A sandbox drill restores it into a temporary database with
mongorestore, counts each collection there and drops it afterwards. Each result is
signed and added to your record, and a failed drill alerts your team.
curl -fsSL https://safegrd.dev/install.sh | sh
safegrd backup --database-url "mongodb://backup:$DB_PASSWORD@db.internal:27017/app?authSource=admin" --retention-days 14
No server to run it on? SafeGrd can take the backup on a machine it starts for
each run: Back up on SafeGrd under Surfaces in the console, engine MongoDB, and
the connection string. For Atlas, paste the mongodb+srv:// string with the password
filled in and the database name before the ?, and let the IP Access List admit the
backup machines. Where they connect from fixed addresses, the console lists them beside
Back up on SafeGrd. How it runs.
To restore, point safegrd restore at an empty database, under any name. It refuses
a database that already holds collections.
safegrd restore --snapshot SNAPSHOT_ID --target "mongodb://user:pass@host:27017/recovered"
Run your first Fire Drill Read the install guide
Questions
Can I restore it without SafeGrd?
Yes. The CLI is source-available and restores from your own bucket with or without an account.
After it decrypts a snapshot, the archive inside is the format mongorestore reads.
Who holds the decryption key?
You choose when the key is made. With a SafeGrd-managed key, SafeGrd keeps your key sealed and releases it only to your enrolled hosts, so you can restore even after losing a host. With a customer-managed key, only you can decrypt these backups.
Which MongoDB versions?
SafeGrd backs up MongoDB 7 and 8. The commands on this page work on any version the current Database Tools support.
Method list and Atlas tiers from MongoDB's documentation, read on 12 October 2026.
Related
- Backup file checker: does a mongodump archive end where it should
- Backup verification: what each kind of check proves
- MongoDB: every SafeGrd option, in the docs
- MongoDB: Backup methods