Backups and a Point-in-Time Restore
pgBackRest to SeaweedFS: encrypted backups, the WAL archive, scheduled backups, and a restore to one second before a DROP TABLE
A backup you haven't restored is a hope. The cluster backs up with
pgBackRest to the pg-backups bucket in SeaweedFS, and
labs/backup_pitr.py proves it works by destroying data and getting it
back.
The setup
Two things run all the time:
- The WAL archive. Patroni sets
archive_commandtopgbackrest archive-pushon every node, so whichever node is primary ships each 16 MB WAL segment to the bucket.archive_timeout = 60forces a segment switch at least once a minute, which bounds how much an idle primary can lose. - Scheduled backups.
node/backup-loop.shruns on every node and acts only while its node is primary (it checks Patroni's/primary), so the schedule follows failovers. Daily incremental, weekly full, and after each check it pushes the newest backup's time to the Prometheus Pushgateway, which thePostgresBackupTooOldalert watches. The first time it ran it found an empty repository and took a full backup on its own.
The drill
docker compose -f compose_prod.yaml --profile restore up -d pg-restore
docker compose -f compose_prod.yaml run --rm labs python backup_pitr.py
1. Full and incremental backups
appdb is 237 MB
full backup in 2.9s: database 260 MB, changed 260 MB, stored in the repository 77 MB
incr backup in 2.5s: database 265 MB, changed 226 MB, stored in the repository 75 MB
2. Writes after the last backup, then an accident
2,001,000 rows (1,000 written after the backup); target time 2026-10-22 19:30:28.745580+00:00
DROP TABLE ledger
segment 0000000C0000000100000029 archived 0.5s after the switch
3. Point-in-time restore into pg-restore
pgbackrest restore (copy the backup files): 1.7s
WAL replay to the target, then promote: 3.1s
recovery stopping before commit of transaction 751756, time 2026-10-22 19:30:29.749836+00
selected new timeline ID: 13
archive recovery complete
restored 2,001,000 rows, 1000 of them written after the backup; sum matches: TrueThe repository holds 77 MB for a 260 MB database. zstd compression does most of that (the data is md5 strings and numbers), and it's encrypted on top. The security lab downloads a WAL file from the bucket and finds 8.000 bits of entropy per byte: indistinguishable from random.
The incremental backup stored 75 MB, barely less than the full. The
UPDATE changed 1 row in 20 (5%), but with dozens of rows on every 8 KB
page, almost every page held at least one updated row, and pgBackRest's
incrementals work on whole files. 5% of the rows was nearly 100% of the
files (changed 226 MB of 265). Block-level incrementals
(repo1-block=y, not enabled here) copy only changed blocks, but when
nearly every page changed that wouldn't help much either: what a backup
costs depends on how a change is spread, not how many rows it touches.
The 1,000 rows written after the last backup came back. They were in
no backup, only in the WAL archive. The restore copied the incremental
backup's files (1.7 s), then replayed archived WAL forward until the
target time and stopped just before the transaction that committed the
DROP (recovery stopping before commit of transaction 751756). It then
promoted onto a new timeline, 13: the restored database is a new history
branching off the old one, and its WAL must never mix with the live
cluster's.
Rules the drill enforces
- Restore somewhere else.
pg-restoreis a separate, empty node. You almost never want to rewind the whole production cluster to undo one bad statement: restore beside it, then copy the table back. - The restored copy must not archive. The node image's Postgres config
came from the backup, including
archive_command. Started as-is, the copy would push its timeline-13 WAL into the production repository. The lab starts it with-c archive_mode=off. - Force the WAL out before restoring. The
DROPsat in a WAL segment that wasn't full.pg_switch_wal()closed it, and the lab waited untilpg_stat_archiverreported that segment archived (0.5 s later) before restoring.
Two bugs in the first run of this lab. The archive wait compared against
the wrong segment name and passed immediately, so it would have restored
before the important WAL arrived; it now waits for the exact segment
pg_switch_wal() closed. And pgBackRest rejected the target time
2026-10-22T19:30:00+00:00: it wants a space, not ISO 8601's T.
Here the backups sit next to the database, on the same machine. In
production the repository is in another region or another account, a
second repository (repo2) gives you two independent copies, and the
restore drill runs on a schedule, with its duration recorded: that
number is your recovery time.