SafeGrd / MongoDB backup

How to back up MongoDB, and check that the backup restores

Updated 12 October 2026 ยท commands run against MongoDB 8.0 with Database Tools 100.16.1

Which method to use.

  • On Atlas M10 or larger: Atlas Cloud Backup is already on. Add a mongodump to 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 --gzip on 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:

MethodWhat it copiesPoint in timeBest for
mongodump / mongorestoreDocuments and index definitions (BSON). One collection, one database or allOnly with --oplog, on a full dump of a replica setSmall and medium databases, Atlas free tier, moving between versions or hosts
File-system snapshotThe data files, at block levelThe snapshot momentLarge self-hosted deployments with LVM, EBS or ZFS
cp or rsync of the data directoryThe data filesOnly with writes stopped (db.fsyncLock() or a shut-down node)A secondary you can pause
Atlas Cloud BackupSnapshots kept by AtlasWith continuous cloud backupAtlas M10 and larger (Flex gets daily snapshots, free clusters only through a private preview)
Cloud ManagerContinuous, from the oplogYesSelf-hosted, with MongoDB's paid hosted management service
Ops ManagerContinuous, from the oplogYesSelf-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)
A test database with 6,200 documents in a local container. The archive came to 32 KB. Timestamps trimmed from the output. On a real server the URI carries the backup user: mongodb://backup:PASSWORD@db.internal:27017/app?authSource=admin.

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
And against a standalone server with no oplog: "error getting oplog start".

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.
Same test database. Indexes are rebuilt from their definitions, which is where a large restore spends its time.

"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

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