Restoring full mongodump backups leaves your database at one consistent point in time, but never lets you choose when you want to restore. Point-in-time recovery (PITR) closes the gap, and it is the main addition in DMK 2.7.0.
With MongoDB, PITR restores a backup and then replays oplog history up to an exact target timestamp. DMK now supports this at three granularities: the whole instance, a single database, or a single collection.
A backup alone isn’t enough
In a MongoDB replica set, mongodump --oplog already captures the oplog while it runs, and mongorestore --oplogReplay can already replay it. That gives you a consistent snapshot of the exact moment the backup finished, which is the native PITR mechanism of MongoDB.
There are multiple ways of closing the gap between a full backup and any point in time. DMK does it through dmk_oplog_backup.py, a small cron-scheduled script. It archives the oplog entries written since its last successful run into incremental slices under $MONGO_BACKUP.
Setting up backups for PITR
A replica set is a prerequisite, since a standalone mongod has no oplog to archive. On mdbrs01, I added an entry to run every 5 minutes:
*/5 * * * * . $HOME/.dmk/DMK;. $DMK_HOME/bin/dmk.sh; $PYTHON_BIN $DMK_HOME/bin/dmk_oplog_backup.py -i mdbrs01 > /dev/null 2>&1
The output goes to /dev/null because every run already writes a log file in $DMK_HOME/log/backup/, named oplog_backup_<instance>_<timestamp>.log. The script also exits with 1 when a run fails, which a cron wrapper or a monitoring tool can check.
Questions about your own setup? A dbi services consultant can look at your case.
Talk to an expert (opens in a new tab)Each run checks two independent, per-instance settings, and archives if either is triggered:
OPLOG_BACKUP_INTERVAL_MINUTES(default240): a fixed-cadence fallback. It guarantees an upper bound on how large a PITR gap can ever be, even with no writes at all.OPLOG_BACKUP_CONSUMED_THRESHOLD_PCT(default80): archive early when the oplog window used since the last archive reaches this percentage of the configured size of the oplog. A burst of write volume then can’t push entries out of the oplog before they’re archived.
The first run for an instance has no last_archived_ts yet, so it archives the entire current oplog. For a long-lived instance with a large, mostly-full oplog, that first run can take longer. Every run after it only captures what is new.
State file content
Every run writes oplog_state.json next to the backups:
{
"last_archived_ts": {
"t": 1792836014,
"i": 1
},
"last_run_at": "2026-10-24T10:00:16.088027+00:00",
"last_interval_run_at": "2026-10-24T10:00:12.145177+00:00",
"slices": [
{
"file": "oplog_slice_20261024_100010_749651.archive.gz",
"from_ts": {
"t": 1792836004,
"i": 1
},
"to_ts": {
"t": 1792836011,
"i": 1
},
"gzip": true,
"created_at": "2026-10-24T10:00:12.145156+00:00"
},
{
"file": "oplog_slice_20261024_100014_640279.archive.gz",
"from_ts": {
"t": 1792836011,
"i": 1
},
"to_ts": {
"t": 1792836014,
"i": 1
},
"gzip": true,
"created_at": "2026-10-24T10:00:16.088006+00:00"
}
],
"replica_set_id": "6af75cb82539a1762c739aa7"
}
The first slice of a chain starts at the oldest entry of the oplog at that moment (from_ts is null only in a state file written by an older version of the DMK). The from_ts of every later slice matches the to_ts of the previous slice exactly. replica_set_id records the replica set that the slices belong to, and a restore refuses slices of another one. With this file, there is no room for a gap between two consecutive entries in the file.
Oplog entries are idempotent by design, which is the same guarantee the MongoDB replication depends on. Every boundary in the archiver is inclusive (ts >= last_archived_ts), so two consecutive slices always overlap by exactly one timestamp. Overlapping and re-applying an entry twice is safe. The state file itself is only updated after the slice is confirmed written and non-empty on disk. A crash may hit between writing the slice and updating the state (a killed cron job, an OOM, a reboot). The next run then just re-covers the same overlapping range, instead of skipping it.
Archiving falling behind
The oplog is a capped collection. dmk_oplog_backup.py may not run often enough, the host may be busy, or the oplog itself may be undersized. In that case, entries the archiver still needed can get evicted before they’re captured. DMK checks this on every run, by comparing last_archived_ts against the current oldest entry of the oplog:
2026-10-24 10:01:12 | ERROR | Oplog gap detected for 'mdbrs01': the last recorded archive boundary (last_archived_ts={'t': 1, 'i': 1}) is older than the oplog's current first entry (first_ts={'t': 1792836004, 'i': 1}): archiving has fallen behind far enough that the entries in between have already been evicted from local.oplog.rs. That window is unrecoverable: no file anywhere contains it, and any PITR target inside it can no longer be restored. The incremental oplog chain for this instance is broken and must not be extended as if it still covered that range. Refusing to record this slice. Take a full backup to restart the PITR chain (or set OPLOG_BACKUP_AUTO_FULL_BACKUP_ON_GAP=true in dmk.conf to have DMK do this automatically the next time this happens).
The run fails with an error, because it cannot be recovered. In this case, the only solution is to do a full backup again, which DMK only suggests, to avoid running a full backup during peak activity. However, you can decide whether DMK fixes the chain automatically. Setting OPLOG_BACKUP_AUTO_FULL_BACKUP_ON_GAP to true in the section of the instance in ~/.dmk/dmk.conf.local makes the script self-heal by taking a fresh full backup:
[mdbrs01]
var::OPLOG_BACKUP_AUTO_FULL_BACKUP_ON_GAP::=::nowarn::true::
After this full backup, the archiving restarts from a new starting point. DMK takes it from the last entry of the oplog.bson file that mongodump wrote during the backup, which is the exact point the backup is consistent to. The first slice starts from that entry, so no write falls between the backup and the first slice.
With OPLOG_BACKUP_AUTO_FULL_BACKUP_ON_GAP set to true, the same situation ends with a fresh full backup and a new baseline:
2026-10-24 10:01:13 | INFO | Backup completed successfully.
2026-10-24 10:01:14 | WARNING | PITR chain for 'mdbrs01' restarted after the oplog gap, new baseline ts={'t': 1792836064, 'i': 1}, taken from the fresh full backup's own captured oplog.bson. Any PITR target before this point that falls inside the gap remains permanently unavailable; targets before the gap are unaffected.
MongoDB also has storage.oplogMinRetentionHours, which keeps every oplog entry younger than that many hours. DMK reads it and then archives on the interval only. This setting does not replace the oplog slices, and can make the oplog size unpredictable.
Restoring to a point in time
With oplog slices archiving in the background, dmk_restore.py accepts --pitr with either an ISO-8601 timestamp or epoch seconds. For a full-instance restore:
$ dmk_restore.py -i mdbrs01 -s /u90/mongodb/backup/mdbrs01/backup_mdbrs01_20261024_100006 --pitr '2026-10-24T10:00:18'
2026-10-24 10:00:25 | INFO | PITR target '2026-10-24T10:00:18' is 2026-10-24T10:00:18+00:00 = 2026-10-24T10:00:18+00:00 (1792836018:0) (exclusive).
2026-10-24 10:00:26 | INFO | PITR target 2026-10-24T10:00:18+00:00 = 2026-10-24T10:00:18+00:00 (1792836018:0) is beyond the end of the oplog this backup captured (2026-10-24T10:00:05+00:00 = 2026-10-24T10:00:05+00:00 (1792836005:7)): it is reached through the archived oplog slices.
2026-10-24 10:00:26 | INFO | Whole-instance PITR to 2026-10-24T10:00:18+00:00 = 2026-10-24T10:00:18+00:00 (1792836018:0), beyond the oplog the base backup captured: replaying the archived oplog slices in a scratch instance (the target is not replayed into).
2026-10-24 10:00:26 | INFO | Starting scratch instance for the out-of-place restore on port 33581 (data: '/u90/mongodb/backup/mdbrs01/dmk_scratch_restore_mdbrs01_1695846/data') ...
2026-10-24 10:00:27 | INFO | Scratch instance on port 33581 is PRIMARY and ready.
2026-10-24 10:00:27 | INFO | Restoring the base backup '/u90/mongodb/backup/mdbrs01/backup_mdbrs01_20261024_100006' into the scratch instance (replaying its own captured oplog) ...
2026-10-24 10:00:27 | INFO | Replaying archived oplog slice 1/3 ('oplog_slice_20261024_100010_749651.archive.gz') ...
2026-10-24 10:00:27 | INFO | Replaying oplog slice 'oplog_slice_20261024_100010_749651.archive.gz' (2 entries in the remaining window) into the scratch instance ...
2026-10-24 10:00:27 | INFO | Replaying archived oplog slice 2/3 ('oplog_slice_20261024_100014_640279.archive.gz') ...
2026-10-24 10:00:27 | INFO | Replaying oplog slice 'oplog_slice_20261024_100014_640279.archive.gz' (2 entries in the remaining window) into the scratch instance ...
2026-10-24 10:00:28 | INFO | Replaying archived oplog slice 3/3 ('oplog_slice_20261024_100022_575920.archive.gz') ...
2026-10-24 10:00:28 | INFO | Scratch instance is now at 2026-10-24T10:00:18+00:00 = 2026-10-24T10:00:18+00:00 (1792836018:0).
2026-10-24 10:00:28 | INFO | Extracting the recovered instance from the scratch instance ...
2026-10-24 10:00:30 | INFO | Loading the recovered instance into 'mdbrs01' ...
2026-10-24 10:00:30 | INFO | Restore of MongoDB instance mdbrs01 (the whole instance) from '/u90/mongodb/backup/mdbrs01/dmk_scratch_restore_mdbrs01_1695846/work/extract'.
[...]
2026-10-24 10:00:30 | INFO | Restore summary: 1351 document(s) restored, 0 failed.
2026-10-24 10:00:30 | INFO | Restore completed successfully.
2026-10-24 10:00:30 | INFO | Removing scratch instance data directory '/u90/mongodb/backup/mdbrs01/dmk_scratch_restore_mdbrs01_1695846' ...
2026-10-24 10:00:30 | INFO | Whole-instance PITR completed successfully.
orders=1100 paid=1100 void=0 customers=201 other.items=50
Two mechanisms are used, and DMK picks the one that reaches the target time:
- A target time at the end of the oplog that the backup captured itself (its
oplog.bson, written while the dump ran) is the native MongoDB mechanism, unmodified: restore the base backup, then letmongorestore --oplogReplayreplay that oplog up to the target time. A target time inside the window in which the dump of that backup ran is refused: a collection dumped after the target already contains later changes, and replaying cannot undo them. In this situation, you need to restore from an earlier full backup instead, and let the slices extend the replay up to the target time. - A later target time is reached through the archived slices. DMK restores the base backup into a throwaway scratch
mongod, replays its captured oplog withmongorestore --oplogReplayand then every slice up to the target time through the sameapplyOpsengine as the namespace restores, and copies the recovered instance into the real one. The real instance is never an oplog replay target, so a refused or failed replay leaves it untouched.
Targets that cannot be reached
A target that neither the backup nor the slices reach is refused. Left alone, mongorestore would replay everything it has. It would still report success, having stopped at the end of what it had. DMK checks the chain first and refuses instead:
2026-10-24 10:00:43 | ERROR | PITR target 2026-10-24T11:00:42+00:00 = 2026-10-24T11:00:42+00:00 (1792839642:0) for the whole instance is beyond the furthest point continuously recoverable from this backup (2026-10-24T10:00:34+00:00 = 2026-10-24T10:00:34+00:00 (1792836034:1)). No gap was detected in the archived chain itself - nothing has been archived that far forward yet. Replaying to this target would silently stop at the reachable point and lose every write made after it while still reporting success. Choose a target at or before the reachable point, or run dmk_oplog_backup.py --force against a still-live instance to archive the missing window first, then retry.
Each archive run also writes a marker into the oplog with appendOplogNote before it reads the end of the oplog. On a quiet instance, the marker proves that nothing was written between the last write and the archive run, so a target in that gap is accepted.
Letting DMK select the base backup
The restore does not need -s. Without it, DMK takes the newest full-instance backup whose dump ended before the target, and whose archived slices continue from the end of that backup up to the target:
# Without a terminal (input redirected from /dev/null), DMK cannot ask for confirmation
$ dmk_restore.py -i mdbrs01 --pitr '2026-10-24T10:00:18' < /dev/null
2026-10-24 10:00:36 | INFO | No source given: selected the newest backup that reaches the target 2026-10-24T10:00:18+00:00 = 2026-10-24T10:00:18+00:00 (1792836018:0): '/u90/mongodb/backup/mdbrs01/backup_mdbrs01_20261024_100006'. Its dump ended at 2026-10-24T10:00:05+00:00 = 2026-10-24T10:00:05+00:00 (1792836005:7), the archived oplog covers up to 2026-10-24T10:00:34+00:00 = 2026-10-24T10:00:34+00:00 (1792836034:1).
2026-10-24 10:00:36 | ERROR | No terminal to confirm the selected backup on. Run again with --yes to accept it, or name the backup with -s.
2026-10-24 10:00:36 | ERROR | Restore cancelled: the selected backup was not confirmed.
# With --yes, DMK accepts the selected backup
$ dmk_restore.py -i mdbrs01 --pitr '2026-10-24T10:00:18' --yes
2026-10-24 10:00:36 | INFO | PITR target '2026-10-24T10:00:18' is 2026-10-24T10:00:18+00:00 = 2026-10-24T10:00:18+00:00 (1792836018:0) (exclusive).
2026-10-24 10:00:36 | INFO | No source given: selected the newest backup that reaches the target 2026-10-24T10:00:18+00:00 = 2026-10-24T10:00:18+00:00 (1792836018:0): '/u90/mongodb/backup/mdbrs01/backup_mdbrs01_20261024_100006'. Its dump ended at 2026-10-24T10:00:05+00:00 = 2026-10-24T10:00:05+00:00 (1792836005:7), the archived oplog covers up to 2026-10-24T10:00:34+00:00 = 2026-10-24T10:00:34+00:00 (1792836034:1).
2026-10-24 10:00:36 | INFO | --yes given: restoring from this backup without asking.
DMK tells which backup it selected and asks Restore from '<backup>'? [y/N] before it restores anything. --yes accepts the selection for a script, and without a terminal and without --yes DMK refuses. When no backup fits, it lists why each one was skipped. The search reads the end of the dump of each backup, starting from the newest, and the index of the slices. It stops at the first backup that fits and never opens a slice file.
Namespace-scoped restores
A database or a collection is restored to a point in time with the same slices:
$ dmk_restore.py -i mdbrs01 -s /u90/mongodb/backup/mdbrs01/backup_mdbrs01_20261024_100006 -d myapp -c orders --pitr '2026-10-24T10:00:18'
2026-10-24 10:00:49 | INFO | Restore summary: 1000 document(s) restored, 0 failed.
2026-10-24 10:00:49 | INFO | Restore completed successfully.
2026-10-24 10:00:49 | INFO | Applying 100 oplog entries to namespace 'myapp.orders' ...
2026-10-24 10:00:50 | INFO | Namespace-scoped PITR for 'myapp.orders' completed successfully.
orders=1100 paid=1100 void=0 customers=201 other.items=50
$ dmk_restore.py -i mdbrs01 -s /u90/mongodb/backup/mdbrs01/backup_mdbrs01_20261024_100006 -d myapp -c orders --pitr '2026-10-24T10:00:18' --out-of-place
2026-10-24 10:00:56 | INFO | Restore summary: 1100 document(s) restored, 0 failed.
2026-10-24 10:00:56 | INFO | Restore completed successfully.
2026-10-24 10:00:57 | INFO | Removing scratch instance data directory '/u90/mongodb/backup/mdbrs01/dmk_scratch_restore_mdbrs01_1699052' ...
2026-10-24 10:00:57 | INFO | Out-of-place PITR for namespace 'myapp.orders' completed successfully.
orders=1100 paid=1100 void=0 customers=201 other.items=50
mongorestore --oplogReplay has no namespace filter, so this path works differently internally. DMK restores the base backup scoped to that namespace and reads the relevant oplog entries. The starting point comes from the oplog captured by the backup, or, for a per-database backup, from a dmk_backup_start_ts.json file that DMK writes next to the backup and that stores its start time. DMK then pulls in whatever archived slices from dmk_oplog_backup.py cover the remaining window. It re-applies only the matching entries in original order via db.adminCommand({applyOps: [...]}). Namespace-scoped PITR needs a directory-format backup, not -a/--archive. The oplog entries inside an archive file cannot be read one by one. They can only be replayed all at once, by a full --oplogReplay restore.
Two details make that replay safe against the data mongorestore already wrote. Every oplog entry records the collection UUID as it existed at backup time. But a freshly restored collection always gets a new UUID. So DMK strips that field before replay, along with retryable-write/session fields tied to the original run. That lets applyOps resolve purely by namespace. And MongoDB writes a batch of statements committed together, like an insertMany() or a transaction, as one oplog entry. The individual operations are nested inside it. A naive per-entry filter would miss every write inside it, so DMK unpacks these recursively before applying its namespace filter.
Collection-level commands in the window
Collection-level commands (create, drop, collMod) inside the replay window are replayed in their original position. A collection dropped and recreated between the backup and the target ends up in the correct final state. It does not contain stale pre-drop documents. A dropDatabase inside the window is the one thing DMK refuses to replay automatically:
$ dmk_restore.py -i mdbrs01 -s /u90/mongodb/backup/mdbrs01/backup_mdbrs01_20261024_100006 -d other --pitr '2026-10-24T10:01:00'
2026-10-24 10:01:11 | ERROR | Refusing PITR for namespace 'other': the replay window contains a dropDatabase on 'other' at ts=(1792836058, 2). Replaying a dropDatabase automatically is too destructive: restore up to a target timestamp before this drop, or handle this window manually.
Replaying that command automatically would destroy the data the restore just wrote back. Picking a target before the drop, or handling that specific window by hand, is the only way.
Checking what is recoverable before you need it
Before relying on any of this during a real incident, dmk_pitr_status.py reports what is recoverable right now, per instance:
$ dmk_pitr_status.py -i mdbrs01
2026-10-24 10:01:11 | INFO | PITR availability for instance 'mdbrs01':
2026-10-24 10:01:11 | INFO | Full-instance PITR (no -d/-c): recoverable up to 2026-10-24T10:01:04+00:00 (from backup 'backup_mdbrs01_20261024_100006').
2026-10-24 10:01:11 | INFO | Namespace-scoped PITR (-d/-c): recoverable up to 2026-10-24T10:01:04+00:00 (from backup 'backup_mdbrs01_20261024_100006', cutoff 2026-10-24T10:00:05+00:00).
$ dmk_pitr_status.py -i mdbrs01 --detailed
2026-10-24 10:01:11 | INFO | PITR availability for instance 'mdbrs01':
2026-10-24 10:01:11 | INFO | - backup_mdbrs01_20261024_100006 [full-instance]
2026-10-24 10:01:11 | INFO | cutoff (base backup's own oplog position) : 2026-10-24T10:00:05+00:00
2026-10-24 10:01:11 | INFO | currently recoverable up to : 2026-10-24T10:01:04+00:00
2026-10-24 10:01:11 | INFO | - backup_mdbrs01_20261024_100006 [namespace]
2026-10-24 10:01:11 | INFO | cutoff (base backup's own oplog position) : 2026-10-24T10:00:05+00:00
2026-10-24 10:01:11 | INFO | currently recoverable up to : 2026-10-24T10:01:04+00:00
--detailed breaks that down per backup found under $MONGO_BACKUP, including any gap in the slice chain. This is built from the exact same logic the restore itself relies on. It walks the archived slice chain forward from each backup cutoff, and stops at the first missing file or break. So what it reports matches what a real restore would reach, not just what the state file optimistically claims.
Summary
dmk_oplog_backup.py, run frequently from cron, archives incremental oplog slices between backups, closing the window the--oplogcapture of a single backup leaves open.- Slice boundaries are inclusive by design, and an overlap is safe. The state file only advances once a slice is confirmed on disk.
- A detected gap fails the run with an error by default. With
OPLOG_BACKUP_AUTO_FULL_BACKUP_ON_GAP=true, DMK takes a fresh full backup instead. dmk_restore.py --pitrrestores the whole instance to a target at the end of the captured oplog of the backup with nativemongorestore --oplogReplay, and to a later target through the archived slices. A target that cannot be reached is refused.- With
--pitr,-s(--source, the backup to restore from) is optional: DMK selects the newest full-instance backup that reaches the target, asks for a confirmation, and--yesaccepts it. - Database and collection-scoped
--pitrreplays archived slices throughapplyOps, and the in-place mode refuses if the window contains adropDatabase. - Run
dmk_pitr_status.pybefore an incident, not during one, to know exactly how far back (or how recently) you can recover.
