Skip to content

MongoDB point-in-time recovery with snapshots and oplog replay

Restore MongoDB to a specific timestamp by combining a base snapshot with oplog replay — Atlas/Ops Manager mechanics and the self-hosted equivalent.

Point-in-time recovery (PITR) restores MongoDB to a specific cluster timestamp — not just the snapshot you took at midnight, but 14:37:22 yesterday when the ill-fated migration ran. The mechanic is the same regardless of the platform: a base snapshot plus the oplog entries between the snapshot and the target time, replayed in order. Atlas and Ops Manager package this; if you self-host you build it yourself out of mongodump --oplog and mongorestore --oplogReplay. This article is the runbook.

How to verify

Before you need it: prove your snapshot + oplog window is actually wide enough to span the maximum tolerable recovery window:

mongosh --host primary.example.internal:27017
use local
db.oplog.rs.find().sort({ $natural: 1 }).limit(1).next().ts
db.oplog.rs.find().sort({ $natural: -1 }).limit(1).next().ts
rs.printReplicationInfo()
aws s3 ls s3://backups/mongo/ --recursive | tail -5

oplog first and oplog last define the PITR window for your most recent snapshot. If the difference is smaller than your “go back N hours” objective, the window must be expanded before you need to use it.

What’s happening

A base snapshot is a complete copy of the dataset at some ts. The oplog records every write since ts. To restore to ts + Δ, you restore the snapshot, then replay oplog entries with ts in the range (snapshot_ts, target_ts]. mongorestore --oplogReplay does exactly this when you pass it both a dump directory and the oplog file from a mongodump --oplog.

For continuous PITR (as opposed to “PITR within the last snapshot’s oplog window”), you need an out-of-band oplog archive — Atlas and Ops Manager run a backup agent that tails the oplog continuously and ships entries to durable storage. The PITR window then equals the oplog archive retention, not the database’s local oplog size. In self-hosted setups, the equivalent is a custom oplog-tailer that writes entries to S3 or another durable store.

The base snapshot can be a filesystem-level snapshot of the WiredTiger files (LVM snapshot, EBS snapshot, ZFS snapshot) or a mongodump --archive --oplog. The filesystem-level form restores faster but requires the same storage engine and version. The dump form is portable across versions but restores slower.

The procedure

Standard recovery (within local oplog window)

  1. Determine the target timestamp. From application logs, an incident report, or db.system.profile.

    Timestamp(1717272000, 1)  // unix epoch seconds, increment
  2. Find the most recent snapshot before the target time. Restoring forward is the only direction --oplogReplay runs.

  3. Restore the snapshot to a clean target. Do not restore over the live cluster — restore to a side-by-side environment, validate, then cut over.

    mongorestore --archive=backup-20260601-0000.archive.gz --gzip --oplogReplay \
      --uri="mongodb://restore:secret@restore-target:27017"
  4. --oplogReplay consumes the oplog.bson from the dump. It stops at the end of that file’s entries.

  5. For ranges that exceed the dump’s oplog, supply additional oplog as --oplogFile. Each mongorestore invocation can replay one oplog file; chain them in order.

    mongorestore --oplogReplay --oplogFile=oplog-20260601-0100.bson \
      --oplogLimit 1717272000:1 \
      --uri="mongodb://restore:secret@restore-target:27017"
  6. --oplogLimit ts[:inc] stops replay at the target. Without this, replay continues to the end of the oplog file.

Continuous PITR (Atlas / Ops Manager)

  1. From Atlas UI or API, request a PITR restore with the target timestamp.

    atlas backups restores create pointInTime --clusterName prod \
      --pointInTimeUTC 2026-06-01T14:37:22Z --targetClusterName prod-restore
  2. Atlas restores the latest snapshot before the target, then replays archived oplog entries to the target, into a new cluster — never in place over the existing one.

  3. Validate the restored cluster against application-side invariants — record counts, recent transactions, foreign-key-like consistency.

  4. Cut over by repointing application connection strings to the restored cluster, or by re-importing the affected collections into the live cluster.

Operational notes

  • PITR is per-replica-set on sharded clusters. Atlas handles cross-shard restore by snapshotting all shards at the same cluster time — self-hosted PITR across shards requires coordinating restores at the same clusterTime, which is non-trivial.
  • The oplog window must exceed the recovery objective by a safety margin. A 24-hour RPO with a 24-hour oplog window leaves no buffer for finding the bug and deciding to restore. Aim for 2-3× the recovery objective.
  • Snapshot consistency matters. A filesystem snapshot taken while mongod is writing is recoverable thanks to journaling, but quiescing the database (db.fsyncLock()) produces a cleaner snapshot.
  • Restore to a new cluster, validate, then cut over. Restoring over production prevents rollback and obscures the original state if the restore itself fails.
  • Test the runbook quarterly. PITR is the kind of operation that nobody remembers under pressure. Run a full drill — pick a random snapshot, restore to a sandbox, prove the data is correct — every quarter.

Stack Harbor maintains PITR runbooks for clients running self-hosted MongoDB and exercises them on a quarterly cadence. We pair filesystem snapshots from a backup secondary with a continuous oplog archive in S3, sized for 2-3× the contractual RPO. See /en/services/managed-operations/ for the engagement model and /en/knowledge-base/mongodb-mongodump-streaming-large/ for the streaming-dump techniques the base snapshots rely on.