MongoDB
You back up MongoDB 7 and 8 with one command, through mongodump, and restore a database with another.
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:
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:
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.
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
- Each collection is read consistently, but not every collection at the same moment,
because
mongodump --oplogworks only for a whole server. A document written to one collection during the backup may be in it while a related document in another collection is not. A point-in-time restore brings every collection to one moment. - The backup keeps indexes and view definitions. Users and roles are managed at the server level, so a single-database archive does not include them.
- Each backup records every collection's document count, size and shape. The shape is the
collection's top-level fields across every document, each with its BSON type
(
mixedwhere documents disagree)._idis recorded as the primary key, and a field every document carries as not null. The console's Tables tab shows them. Threat Shield warns when a field is gone from every document in the next backup (what each snapshot records). For a collection with more than 500 top-level field names, SafeGrd records the first 500 and no hash. - Restore into an empty database. It can have any name. The restore checks document counts against the snapshot manifest. If a restore fails partway, drop the target database before you run it again.
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:
- The check reads
mongodump --archivefiles, gzipped or not. Coolify'smongo-dump-*.tar.gzis one gzipped archive, not a tar. The check counts each collection's documents and checks each collection's CRC as mongodump wrote it. An archive that was cut short or damaged in the upload counts as a failed backup. An archive of the whole server is checked, but drilled in memory only, because one sandbox takes one database. - A Fire Drill loads the archive into the sandbox with
mongorestore, renaming the archive's database to the sandbox's, and compares every collection's documents. mongorestore stops at the first error and writes as it goes, so a failed load leaves part of the archive in the sandbox. The drill empties it afterwards.drill.sandbox_urlmust name a MongoDB database. A sandbox of another engine is refused before anything connects. - Under one Coolify tree, a
type: mongodbsurface reads only themongo-dump-*files. The PostgreSQL and MySQL backups beside them belong to surfaces of their own type. - If you have no host, add the surface in the console under Back up on SafeGrd and tick Check the backups another tool makes, as the PostgreSQL page describes. SafeGrd holds the bucket key and runs the checks and drills on machines it starts.
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.
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:
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.
- Every 5 seconds (
interval_seconds), the daemon reads the oplog entries for this database since its last read. It compresses and encrypts them on the host and pushes them to your bucket unlocked. Once a day it joins them into one locked bundle and deletes the unlocked copies. On hosted storage, this comes with Growth (7 days) and Scale (30 days). - A base backup is a
mongodumpof the database, taken between two reads of the oplog's newest entry. A restore replays every entry after the first of those reads over it, which brings every collection to the same moment. The daemon takes a base when it starts following and every 7 days (base_every_days). To take one now, runsafegrd wal base --surface app. - Only the resident daemon follows the oplog. When it starts again, it carries on where the
archive ends. If the oplog has rolled past that point in the meantime, the daemon says so,
starts again from the newest entry and takes a new base. The window then starts from there.
Size the oplog to hold at least a day.
safegrd doctorshows how much it holds. - Once a day the daemon records a restore point: every collection's document count, read at
one moment of the oplog. Every 7 days it restores the archive into the surface's
drill.sandbox_url, to the newest restore point and to the end, and checks the counts. To run that drill now, usesafegrd wal drill --surface app. - On MongoDB Atlas free clusters, a user can read the oplog and nothing else in
local. The daemon's query meets their rule that an oplog namespace match starts with^.
To restore into an empty database:
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: