skip to content

A nightly mongodump of a busy MongoDB replica set restores with documents that disagree with each other; what went wrong, and what do --oplog and --oplogReplay guarantee?

level: seniorimportance: must knowfreq 52%

answer

  1. collections are read one after another
  2. a timestamp noted before the first read
  3. the moment the dump finished
  4. what stops the mode from running
  5. a privilege the restore role lacks

basics

~20 s

Without --oplog a dump reads collections at different moments, so writes during the run land in some and not others. --oplog captures those writes and --oplogReplay applies them, giving the state at the dump's end, not a time you choose.

solid answer

~40 s

`mongodump` copies collections while writes continue, so without `--oplog` the output doesn't represent one moment. An order read late can reference a payment written after `payments` had already been read. With `--oplog` it notes a start timestamp, moved back to the oldest active transaction if one is running, dumps the collections, then copies every oplog entry up to the newest one into `oplog.bson`. `mongorestore --oplogReplay` replays them after loading the data, so the restore reflects the moment the dump finished, and nothing later. The pair needs a full dump of a replica set member: `--db` is refused, a `mongos` is refused, and the run fails if the oplog rolls past its start or a rename occurs. The restoring user needs a custom role with `anyAction` on `anyResource`, because the `restore` role isn't enough.

code

bash · 5 lines
bash
mongodump --uri="mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017/?replicaSet=rs0" \
  --oplog --gzip --archive=/backups/nightly.archive.gz

mongorestore --uri="mongodb://restore0.example.com:27017" \
  --oplogReplay --gzip --archive=/backups/nightly.archive.gz

go deeper

for a junior

Recall that a dump isn't one moment unless something makes it so, that --oplog goes with mongodump and --oplogReplay with mongorestore, and that the file is called oplog.bson.

for a middle

Explain the sequence: start timestamp, collections, end timestamp, copy of the entries, replay on restore. Say which moment the result reflects, and why that is the end of the dump.

for a senior

Diagnose from the symptom and the message: overflow means the oplog rolled past the start, a rename fails the run late, and a mongos refuses the flag. Mention the anyAction role before someone hits it mid-restore.

for a principal

Treat recurring overflow as a capacity signal, not a flag problem. The pair narrows inconsistency inside the dump and does nothing for the hours after it, so it doesn't change the recovery point a nightly schedule gives you.

## What went wrong `mongodump` reads a live deployment, collection by collection, with `--numParallelCollections` defaulting to 4. The whole run can take hours. Writes keep arriving, so each collection is captured at its own moment, and a document may be read before or after a related write. The tools docs put it plainly: if writes occur during the dump, the backup might not represent a single point in time. Suppose a checkout writes to `orders` and then to `payments`. If `payments` was read at 01:00 and `orders` at 03:00, the restored `orders` can reference payments that were never captured. Nothing errors, and every file is valid. The inconsistency shows up only when the application reads the data. The docs offer three ways out: block writes at the application, take an fsync lock, or use `--oplog` for a full replica set dump. ## What `--oplog` does The **oplog** is the replica set's capped log of write operations. Reading Database Tools 100.20.1, a run goes like this: 1. Note a **start timestamp**: the `startOpTime` of the oldest prepared or in-progress transaction in `config.transactions`, or else the newest oplog entry. 2. Dump all the collections. 3. Note an **end timestamp**, the newest oplog entry at that point. 4. Check that the start entry still exists in the oplog. 5. Copy every entry from start to end, both inclusive, to `oplog.bson`, or into the archive when `--archive` is used. 6. Check the start entry again, because it may have rolled off during the copy. `mongorestore --oplogReplay` loads the collections and then applies those entries. The manual's claim is that the restore reflects the exact moment `mongodump` completed. It doesn't dump the oplog collection itself, only the entries from the run. ## What the pair doesn't give you - **Nothing after the dump.** Writes after the end timestamp aren't in the file, so a nightly dump can still lose up to a day. - **No free choice of moment.** The manual says it doesn't support restoring to an arbitrary point. `--oplogLimit` can stop the replay early, applying only entries strictly before the given timestamp, and an entry at exactly that timestamp isn't applied. The docs warn that hand-picking entries this way might cause corruption and inconsistencies. - **No partial restore.** `--oplogReplay` refuses `--db`, `--nsInclude`, `--nsExclude` and `--nsFrom`. - **No sharded cluster.** Both tools refuse a `mongos` for this. ## How it fails | Trigger | What happens | |---|---| | `--db`, and so `--collection` or `--query` | `--oplog mode only supported on full dumps` | | Connected to a `mongos` | `can't use --oplog option when dumping from a mongos` | | Oldest oplog entry newer than the start | `oplog overflow` | | Rename during the run, including `$out` and `mapReduce` per the docs | `cannot dump with oplog while renames occur` | | Changes to users, roles or cluster-wide settings | The docs say the dump fails | The oplog checks run after the collections are dumped, so a six-hour dump can fail at the very end. If the start entry is exactly the oldest in the oplog the run passes. Only an oldest entry newer than the start counts as overflow. When overflow recurs, the dump has become slower than the oplog's retention allows, which is a strong sign that the dump has outgrown this deployment. ## On the restore side - **Privileges.** The `restore` role covers a restore without replay. For `--oplogReplay` the docs say to create a user-defined role with `anyAction` on `anyResource`, and to grant it only to users who need it. - **Files.** `--oplogFile` names an oplog file explicitly and refuses `--archive`. Having both it and a top-level `oplog.bson` is an error. - **Versions.** Oplog replay files are outside the guarantee for restoring into a different version, and the docs warn that such a restore could end in failure, silent failure or corrupted metadata. ## The 8.x to 9.0 trap In MongoDB 9.0, changing the feature compatibility version between 8.x and 9.0 converts time series collections, and `mongorestore --oplogReplay` can't replay across that. From Database Tools 100.18.0, `mongodump --oplog` fails rather than producing a dump you can't restore. So hold FCV changes until the dump ends. To restore a range that spans one, replay up to `--oplogLimit`, run `setFeatureCompatibilityVersion` on the target, then replay the rest.

  • Can mongodump --oplog read from a secondary to spare the primary?
    Yes. The docs say `--oplog` works against any node that maintains an oplog, which includes all replica set members, and `mongodump` defaults to primary read preference until you override it. Connect to one member with `--host`, or set `--readPreference=secondary`. It spares the primary's working set, but a slow dump still risks overflow on the member it reads.
  • How do you restore to a moment later than the end of the dump?
    Capture the oplog separately with `mongodump --db=local --collection=oplog.rs`, then restore a directory dump with `--oplogReplay --oplogFile=<path>` and `--oplogLimit` set to the bad operation's timestamp, since the limit is exclusive. The tool refuses `--oplogFile` with `--archive` or beside a top-level `oplog.bson`. The docs warn that hand-picking entries might cause corruption, so rehearse this rather than improvising during an incident.
  • Why does mongodump move its start timestamp back when transactions are running?
    A transaction that began before the dump can commit during it. Starting from the newest oplog entry could lose that transaction's earlier entries, so the tool reads `config.transactions` and starts at the oldest `prepared` or `inProgress` transaction's `startOpTime`. The oplog file can therefore begin before the run did.

Photographing a warehouse aisle by aisle while forklifts keep moving, and keeping the gate log for the whole shoot. Replaying the log over the photos tells you where everything stood when the last photo was taken, and nothing about what moved afterwards.

  • mongodump --oplog lets you recover to any moment since the last dump.
  • --oplog dumps the entire local.oplog.rs collection into the backup.
  • You can add --oplog when dumping a single database to keep it consistent.
  • The built-in restore role is enough to run mongorestore --oplogReplay.
  • A dump without --oplog is a single consistent snapshot because WiredTiger uses snapshots.
  • Running mongodump --oplog through mongos gives a consistent copy of the whole cluster.

Assumes MongoDB 9.0 and Database Tools 100.20.1; the 8.x to 9.0 FCV conversion rule is new with 9.0, and the flags otherwise behave the same against an 8.0 server.