Skip to content

Database point-in-time recovery (PITR) — Postgres, MySQL, MongoDB

Configure point-in-time recovery for Postgres (WAL archiving), MySQL (binlogs), and MongoDB (oplog), and run a real recovery to a chosen timestamp.

A nightly database dump is enough to get back to last midnight. It is not enough when at 14:00 someone runs DELETE FROM users without a WHERE clause and you need 13:59:50 — twelve hours of data, not zero. Point-in-time recovery is what closes that gap: a base backup plus a continuous stream of write-ahead logs that can be replayed to any chosen timestamp. This article covers PITR for Postgres, MySQL, and MongoDB — what to enable, what to ship offsite, and how the actual recovery walks.

How to verify

For each database, the existence and currency of the log stream is what tells you PITR is functional, not just configured:

# Postgres
sudo -u postgres psql -c "SHOW archive_mode;" -c "SHOW archive_command;"
sudo -u postgres psql -c "SELECT pg_walfile_name(pg_current_wal_lsn());"
ls -lt /var/backups/postgres/wal/ | head

# MySQL
mysql -e "SHOW VARIABLES LIKE 'log_bin';" -e "SHOW BINARY LOGS;"
ls -lt /var/lib/mysql/ | grep binlog | head

# MongoDB
mongo --eval "rs.status().members.map(m => ({name: m.name, state: m.stateStr, optimeDate: m.optimeDate}))"
mongo --eval "db.oplog.rs.find().sort({\$natural: -1}).limit(1).pretty()" local

A binlog with mtime older than five minutes on a busy server suggests log rotation has stalled. An oplog window smaller than the time between full backups means the oplog cannot bridge two snapshots.

What’s happening

PITR has two ingredients: a base backup (a consistent dump or filesystem snapshot at time T) and a continuous log of every write since T. The recovery procedure restores the base backup, then replays the log up to a target timestamp.

In Postgres, the log is the Write-Ahead Log (WAL). With archive_mode = on and an archive_command, every completed WAL segment is shipped to an archive (local directory, S3, NFS). Recovery replays the WAL until it hits the target time or LSN.

In MySQL, the log is the binary log (binlog). With log_bin = ON and binlog rotation, the binlogs accumulate. Recovery loads the base dump, then mysqlbinlog --stop-datetime replays the binlogs up to the target time.

In MongoDB, the log is the replica set’s oplog — a capped collection on the primary that records every operation. A replica restoring from a snapshot uses the oplog to roll forward; PITR to an arbitrary point uses mongorestore --oplogReplay --oplogLimit=<timestamp>.

The procedure

Postgres PITR

  1. Enable WAL archiving. In postgresql.conf:

    archive_mode = on
    archive_command = 'test ! -f /var/backups/pg/wal/%f && cp %p /var/backups/pg/wal/%f'
    wal_level = replica
    max_wal_senders = 3

    Restart Postgres. For offsite, use pgbackrest, wal-g, or barman — they ship WAL to S3 with compression and verification rather than relying on cp.

  2. Take a base backup.

    sudo -u postgres pg_basebackup -D /var/backups/pg/base-2026-06-02 -F tar -z -P
  3. Recovery to a target time. Restore the base into a fresh data directory, drop a recovery.signal (Postgres 12+) and set restore_command plus recovery_target_time in postgresql.conf.

    restore_command = 'cp /var/backups/pg/wal/%f %p'
    recovery_target_time = '2026-06-02 13:59:50 UTC'
    recovery_target_action = 'pause'

    Start Postgres. It replays WAL up to the target, pauses, and lets you SELECT to verify the state before committing with pg_wal_replay_resume().

MySQL PITR

  1. Enable binlog and a sensible expiry. In my.cnf:

    log_bin = /var/lib/mysql/mysql-bin
    binlog_format = ROW
    binlog_expire_logs_seconds = 1209600   # 14 days
    sync_binlog = 1

    Restart MySQL.

  2. Take a logical backup.

    mysqldump --single-transaction --master-data=2 \
      --routines --triggers --events --all-databases \
      > /var/backups/mysql/dump-2026-06-02.sql

    --master-data=2 records the binlog coordinates at dump time as a comment in the SQL.

  3. Recovery to a target time. Restore the dump, then replay binlogs.

    mysql < /var/backups/mysql/dump-2026-06-02.sql
    mysqlbinlog --start-position=<from-dump> \
      --stop-datetime='2026-06-02 13:59:50' \
      /var/lib/mysql/mysql-bin.{000123,000124} | mysql

MongoDB PITR

  1. Confirm replica set with sufficient oplog. Without a replica set, MongoDB has no oplog and no PITR.

    mongo --eval "rs.printReplicationInfo()"

    oplog window shorter than your backup interval means you cannot replay across the gap; increase oplogSize in mongod.conf.

  2. Take a snapshot. mongodump --oplog captures the data plus the oplog at dump time.

    mongodump --uri "mongodb://primary:27017" --out /var/backups/mongo/2026-06-02 --oplog
  3. Recovery to a target time. Restore with --oplogReplay --oplogLimit.

    mongorestore --uri "mongodb://dr-primary:27017" \
      --oplogReplay --oplogLimit "1717336790:1" \
      /var/backups/mongo/2026-06-02

Common pitfalls

  • Postgres archive_command that fails silently (returns 0 but didn’t actually ship) creates gaps in the WAL chain — pgbackrest is preferable to a hand-rolled cp.
  • MySQL binlog with binlog_format = STATEMENT cannot reliably replay non-deterministic statements; use ROW.
  • MongoDB mongodump --oplog captures the oplog at the moment of dump; PITR to a later timestamp requires a separate oplog tail since the snapshot.
  • The base backup and the logs must come from the same lineage — restoring a 30-day-old base and trying to replay last week’s logs will fail.
  • A PITR target time in the future is silently accepted by Postgres and the recovery never stops; double-check the timestamp.

In the engagements we run, PITR is configured for every customer database whose data-loss tolerance is sub-day — WAL/binlog shipped to S3 with object-lock, monthly recoveries to scratch hosts to confirm the chain is intact, and the recovery procedure documented in the /en/knowledge-base/disaster-recovery-drill/ runbook. See /en/services/managed-operations/.