Docs / Surfaces / Databases / MongoDB

MongoDB

You back up MongoDB 7 and 8 with one command, through mongodump, and restore a database with another.

# MongoDB 7 and 8 (each run by the test suite)
safegrd backup --database-url "mongodb://backup:$DB_PASSWORD@db.internal:27017/app?authSource=admin" --retention-days 14

The backup runs mongodump --archive and reads its output as a stream. As the stream passes, SafeGrd counts each collection's documents and checks the CRC mongodump writes for it. The recorded counts match what is in the backup, even while the database is being written to.

The user to back up as

Back up as a user with MongoDB's built-in read role on the database. That is enough to back up every collection with its indexes and views. Create the user in admin, so the connection string's authSource=admin finds it. In mongosh:

db.getSiblingDB("admin").createUser({user: "safegrd_backup", pwd: passwordPrompt(), roles: [{role: "read", db: "shop"}]})

If the surface's user holds a role that can write, such as readWrite, dbOwner or root, safegrd doctor warns and prints the line above.

The sandbox for Fire Drills

A sandbox drill (drill.sandbox_url on the host, or --sandbox-target) loads the archive with mongorestore into a database you use for nothing else. It counts each collection there, then drops them. The database has to be empty before each drill. Give the sandbox user readWrite on it to load and drop the collections, and dbAdmin so the drill can validate them and build their indexes:

db.getSiblingDB("admin").createUser({user: "safegrd_sandbox", pwd: passwordPrompt(), roles: [{role: "readWrite", db: "shop_sandbox"}, {role: "dbAdmin", db: "shop_sandbox"}]})

TLS and a provider's own CA

Atlas signs with a public CA, so mongodb+srv:// strings verify as they are. If your server's certificate is signed by a CA of its own, give SafeGrd that CA. On a host, add ?tls=true&tlsCAFile=/path/ca.pem to the URL, or set ca_file on the surface in the config. For a database SafeGrd backs up for you, paste the CA certificate when you add it, or later with CA certificate on the surface. SafeGrd refuses a URL that also says tlsInsecure=true or tls=false.

Incremental backups

A backup uploads only the collections that changed since the last run, and only the parts of them that changed. Each backup is a run of the surface's repository, like a PostgreSQL backup. SafeGrd splits the archive by collection. The header and every collection's options and indexes go in one small file. Each collection's documents go in a file of their own, in the order mongodump read them.

A collection nobody wrote to since the last run has the same bytes, so it uploads nothing. A collection that changed uploads the 64 KiB chunks around the documents that moved. Every run reads the whole database and is a restore point. The snapshot detail in the console shows what each collection uploaded.

Measured on a 290 MB database, a run with no writes uploaded 0.1 MB, and 2,000 documents appended to one collection uploaded 0.5 MB. Updating 400 documents across a collection of 300,000 uploaded 19 MB, the chunks around each one. An inserted document costs only itself, whatever its _id, because mongodump reads in storage order and a new document sits at the end.

To take one archive per backup instead, use --format tar, or format: tar on a surface. When you add a surface in the console, Backup type sets this. Each backup is then mongodump's own archive, which mongorestore --archive loads without SafeGrd once it is decrypted.

To restore or drill a run, SafeGrd puts the archive back together, one block per collection, and hands it to mongorestore. For a run, mongodump reads one collection at a time.

A whole run restores into an empty database: safegrd restore --snapshot <id> --target mongodb://…. You can also restore one collection of a run into a database that has other collections, with safegrd restore --snapshot <id> --table orders --target mongodb://…. SafeGrd hands mongorestore an archive that holds only that collection's documents, options and indexes. The collection has to be missing from the target, or empty. In the console, the Tables tab has Restore collection for this. A full backup (tar) restores only whole.

Upload per run on a 290 MB MongoDB database, bars to scale. A run with no writes uploaded 0.1 MB. 2,000 appended documents uploaded 0.5 MB. 400 documents updated across a collection of 300,000 uploaded 19 MB. The database itself is 290 MB. No writes since the last run 0.1 MB 2,000 documents appended 0.5 MB 400 of 300,000 documents updated 19 MB The database, for scale 290 MB
The measured runs above, drawn to scale against the 290 MB database. The first two bars are too thin to see.

What the host needs

Install the MongoDB Database Tools (mongodb-database-tools) on the host, from MongoDB's apt or yum repository or its download page. Ubuntu and Debian do not ship them. To point SafeGrd at specific binaries, set SAFEGRD_MONGODUMP and SAFEGRD_MONGORESTORE. SafeGrd passes them the connection string in a private file only the owner can read, never on their command line.

What a backup holds

Check the backups another tool makes

If Coolify or a cron mongodump already backs up the database, SafeGrd can check those backups where that tool stores them instead of taking its own. Set it up as for a PostgreSQL surface, with type: mongodb:

surfaces:
- id: shop-db
type: mongodb
schedule: daily
watch:
url: s3://my-backups/data/coolify/backups/databases/team-0/shop-db
endpoint: https://ACCOUNT.r2.cloudflarestorage.com
access_key: env:R2_KEY_ID
secret_key: env:R2_SECRET

Restore to a point in time

You can restore the database to any second inside a window, 7 days by default. Add a wal: block to the surface, and the resident daemon follows the replica set's oplog. Without it, a backup restores the database as it was when the run started.

surfaces:
  - id: app
    type: mongodb
    database_url: mongodb://safegrd_backup:...@db.internal:27017/app?authSource=admin
    wal: {}

This needs a replica set, because only a replica set keeps an oplog. To run a single server as a replica set of one member, start mongod with --replSet rs0 and run rs.initiate() once. A standalone server and a sharded cluster (mongos) are refused, and the daemon says why. The backup user reads the oplog with read on local, the narrowest built-in role that can:

Which MongoDB deployments point-in-time restore takes. A replica set: the daemon follows its oplog, with the read role on local. A single server: start it with --replSet rs0 and run rs.initiate() once, and it becomes a replica set of one. A sharded cluster through mongos: refused, and the daemon says why. Replica set Oplog followed with read on local Single server Make it a set of one start with --replSet rs0 then rs.initiate() once Sharded (mongos) Refused the daemon says why
A single server becomes a replica set of one member with two steps, and then works like any replica set.
db.getSiblingDB("admin").grantRolesToUser("safegrd_backup", [{role: "read", db: "local"}])

The oplog belongs to the whole replica set, so with this role the user can read the changes to every database on it. The daemon keeps only this database's entries.

To restore into an empty database:

safegrd wal restore --surface app --to "2026-10-17 14:52:00" --target-url "mongodb://restorer:...@db.internal:27017/app_restored?authSource=admin"

The surface's Point in time tab in the console writes this command for a moment you pick.

The command loads the newest base backup from before that moment (UTC). It then replays this database's oplog entries up to that moment with mongorestore --oplogReplay. Oplog entries are stamped to the second, so an entry from the same second as the moment you give is not replayed. To restore to a restore point, use --to-point <name>. To restore everything archived, use --to latest. The target database can have a different name. The user you restore with needs readWrite on it and the built-in restore role, which lets mongorestore apply oplog entries:

db.getSiblingDB("admin").createUser({user: "restorer", pwd: passwordPrompt(), roles: [{role: "readWrite", db: "app_restored"}, "restore"]})