Backups and disaster recovery
RPO and RTO first, a backup designed to fail loudly, the same-host gap, 3-2-1, restoring into a new database, and the drill that proves it.
A backup you have never restored is not a backup. It is a file you are hoping about, and the day you find out whether the hope was justified is the worst possible day to find out.
This lesson uses one real system as its example — this site's own database backup — because a designed backup is more instructive than a described one, and because it has a documented gap worth learning from.
Two numbers decide everything
Before choosing a mechanism, agree two numbers with whoever owns the business:
- RPO — recovery point objective. How much data may you lose? "Everything since last night" is an RPO of 24 hours. "Nothing" is an RPO of zero, and it costs a great deal.
- RTO — recovery time objective. How long may you be down while you restore? Minutes, hours, a day?
Everything else follows from those. A nightly dump gives an RPO of up to 24 hours — fine for a learning site, unacceptable for a payment ledger. Streaming replication to a standby gives an RPO of seconds and a much lower RTO, and costs a second database running all the time.
The mistake is choosing the mechanism first and discovering the RPO after the incident. "We back up nightly" is a sentence that means we accepted losing up to a day of orders, and somebody should have agreed to that on purpose.
A backup that is designed, not just scheduled
This site's database is backed up by a small script on a systemd timer. Its design choices are worth reading as a checklist, because each one closes a specific failure:
- Every night,
pg_dump --format=custom. The custom format is compressed and restorable selectively — one table, not only the whole database. - Verified by listing the dump's table of contents before anything else happens. A dump that ran but produced a truncated or empty file is the most common silent failure. Checking the contents is what distinguishes "the job ran" from "the backup is usable".
- Old dumps are removed only after the new one is verified. The obvious ordering — prune, then dump — means a failing dump quietly leaves you with fewer backups each night until there are none.
- The password goes through a temporary
PGPASSFILE, never the command line, because a command line is visible to every user on the host through the process list. - Fourteen days of retention, because the corruption you discover today may have happened last week, and a backup of already-corrupted data is not a recovery.
The gap: a backup on the same machine
That script writes its dumps to the same host as the database. Its documentation is explicit about the consequence: an off-host copy to S3 happens only when a credentials file exists, and until it does, the journal says off-host copy skipped.
That is worth dwelling on, because it is the most common real gap in small production systems. A backup on the same disk protects you from a bad query and not from losing the machine. Disk failure, a deleted volume, a compromised host, a provider problem — every one of those takes the database and its backups together.
The rule has a name, and it is a good one to be able to recite:
3-2-1: three copies of the data, on two different kinds of storage, with one of them off-site.
The off-host design here is also worth copying for how it limits damage. The upload credentials belong to a dedicated IAM user with permission to put and delete objects and no permission to list the bucket — so a leaked key cannot enumerate what is there, which is also why old copies are pruned by exact name. That is the least-privilege principle from the security course, applied to a backup.
Restore is the product
Nobody needs backups. Everybody needs restores. So the restore procedure is the thing to design, write down, and rehearse.
This site's documented restore goes into a new database first, never the live one:
createdb -U code10x code10x_restore
pg_restore -U code10x -d code10x_restore --no-owner --no-privileges latest.dumpThen point a copy of the application at it, or check the migration state, before deciding anything about the live database. Restoring straight over production during an incident is how a recoverable situation becomes an unrecoverable one — if the backup turns out to be bad, you have now destroyed the only copy you were not sure about.
Rehearse it on a schedule. A restore drill answers questions you cannot answer any other way:
- Does the backup actually restore?
- How long does it take at today's data size — and is that inside your RTO?
- Does the person on call know the commands, or only the person who wrote them?
- What is missing — a secret, an extension, a sequence value, the files in object storage the rows point at?
That last one catches almost everybody: the database restores perfectly and every uploaded file is gone, because the files lived in a bucket nobody backed up.
Disaster recovery is more than the database
A full recovery needs everything the service needs to run, and the database is only one entry:
| You need back | Where it usually lives |
|---|---|
| data | database backups — tested |
| files | object storage, with versioning and replication |
| secrets | a secret manager, or you cannot start the app |
| configuration | version control and infrastructure-as-code |
| the build | the pipeline, and the ability to rebuild an image |
| the knowledge | a written runbook somebody other than the author can follow |
The practical test for an entire DR plan is blunt: could someone who did not build this system restore it from nothing, using only what is written down? If the answer depends on one person remembering something, that person is the single point of failure.
Under the hood: WAL, and why continuous archiving buys a much better RPO than a nightly dump
pg_dump (what this site's nightly backup uses) is a logical backup: it reconnects to the database and asks it to reconstruct SQL statements (or a custom-format equivalent) that would recreate the current state. That is exactly why its RPO is bounded by how often you run it — a dump taken once a night can never promise better than "up to 24 hours of loss", no matter how fast the dump itself runs, because nothing is captured about what happens between dumps.
PostgreSQL's own crash-recovery mechanism offers a fundamentally different, much finer-grained data source: the write-ahead log (WAL). Before PostgreSQL changes a single data page on disk, it first writes a record of that change to the WAL and ensures the WAL record is durable (fsynced) — this is what lets PostgreSQL recover cleanly after a crash: on restart, it replays the WAL forward from the last checkpoint to reconstruct any changes that were logged but not yet reflected in the data files. Continuous archiving repurposes this same mechanism for backup: PostgreSQL is configured to ship each completed WAL segment to an archive (archive_command, or a tool like pgBackRest/WAL-G that automates it) as soon as it is written, rather than only at backup time. Combined with an occasional base backup (a full copy of the data directory), you get point-in-time recovery (PITR): restore the base backup, then replay archived WAL segments forward to any specific moment — not just to whenever the last full backup happened to run.
The RPO difference is the entire point. A nightly pg_dump has an RPO of "up to 24 hours, whatever time of day the incident happens to occur." Continuous WAL archiving has an RPO bounded by how often a WAL segment gets shipped to the archive — commonly seconds, and tunable down further with archive_timeout forcing a segment to close and ship even during quiet periods. The trade-off is real: logical dumps are portable across major versions and simple to reason about, while WAL-based PITR requires more infrastructure (an archive location, a tool to manage segment retention, and — critically — periodic restore testing of the replay path, not just the base backup) and ties you to the same major PostgreSQL version on both ends.
Try it yourself
Work out whether a mechanism can actually meet a stated RPO
A team states an RPO of 5 minutes for their order database, and their current backup mechanism is a pg_dump cron job running every hour. Their monitoring shows the dump job itself completes successfully every time, in about 90 seconds.
Does this mechanism actually meet the 5-minute RPO? What would have to change, and why can no amount of making the dump itself faster fix it?
Why a faster dump does not close the gap
No — an hourly pg_dump, however fast the dump itself runs, has an RPO of up to roughly one hour: if the database is lost 58 minutes after the last successful dump, everything written in those 58 minutes is gone, regardless of whether the dump job takes 90 seconds or 9 seconds to complete. The dump's own speed only affects how long the job takes to run, not how much data could have been written between one dump starting and the next one starting. To meet a genuine 5-minute RPO, the team needs a mechanism where the gap between recovery points is 5 minutes or less — either running pg_dump every 5 minutes (usually impractical at any real data size, since a dump reads the whole database) or, far more realistically, continuous WAL archiving with a short archive_timeout, which creates a new recoverable point continuously rather than only at the moment each scheduled dump happens to run.
Misconceptions
- "A backup running successfully on a schedule is the same as meeting an RPO." A scheduled job's success only tells you the mechanism worked; the RPO is a property of the gap between recovery points, which is bounded by the schedule's frequency, not by whether any individual run succeeded. A perfectly reliable hourly dump still has an hour-scale RPO.
- "Streaming replication to a standby is a backup." A hot standby (or synchronous replica) protects against hardware failure and gives fast failover, but it replicates mistakes just as faithfully as it replicates good data — a
DELETEwith noWHEREclause, or a bad migration, arrives on the standby within seconds too. A standby is a high-availability mechanism, not a substitute for point-in-time-recoverable backups that let you go back to before the mistake.
Sources
- PostgreSQL documentation, Chapter 26, "Reliability and the Write-Ahead Log", and Chapter 26.3, "Continuous Archiving and Point-in-Time Recovery (PITR)".
- pgBackRest and WAL-G documentation — production tools for WAL archiving, base backups and PITR.
- Google, Site Reliability Engineering, the "Data Integrity" chapter, on RPO/RTO and testing recovery paths.