Backups & Disaster Recovery
Design reliable backups using RPO/RTO targets, the 3-2-1 rule, logical vs physical PostgreSQL backups, regular restore drills, and pragmatic DR tiers.
TL;DR
Define RPO (how much data you can afford to lose) and RTO (how long you can stay down) before choosing backup tools. Follow the 3-2-1 rule, pick logical (pg_dump) for small DBs or physical (pg_basebackup + WAL) for PITR, and schedule automated restore drills—an untested backup is a wish, not a plan.
Why it exists
RAID mirrors, database replication, and git push protect against a single disk dying—not against an accidental DROP TABLE, a bad migration, or ransomware deleting your primary and replica together. Backups and Disaster Recovery (DR) exist to recover point-in-time state when replication faithfully copies your mistake.
Mental model
1. RPO, RTO, and the 3-2-1 rule
- RPO (Recovery Point Objective): Maximum data loss in time (e.g.,
24hwith nightly snapshots,< 1mwith continuous WAL archiving). - RTO (Recovery Time Objective): Maximum time from incident start until service is restored (e.g.,
4hfor cold restore,< 5mfor multi-region failover). - 3-2-1 rule: 3 copies of data, on 2 separate systems/media, with 1 off-site (in a different cloud account/region with object-lock or immutable retention).
2. Logical vs Physical backups (PostgreSQL)
| Type | Tool | Pros | Cons |
|---|---|---|---|
| Logical | pg_dump -Fc / pg_restore | Portable across PG major versions; can restore one table | Slow on multi-hundred-GB DBs; no sub-snapshot PITR |
| Physical | pg_basebackup + WAL archiving | Fast binary restore; Point-in-Time Recovery (PITR) to any second | Tied to same PG major version & architecture; restores whole cluster |
3. DR tiers (match spend to risk)
- Backup & Restore (cheap, RTO hours): Nightly snapshots + WAL in off-site object storage; spin up a fresh instance on failure.
- Pilot Light / Warm Standby (medium, RTO minutes): Small replica running in a second region, scaled up on failover.
- Multi-Region Active-Active (expensive, RTO seconds): Simultaneous writes across regions—rarely needed below huge scale.
Do it
Run a compressed custom-format logical dump and verify restoring it into a temporary database:
# Create a compressed logical backup (not run)
pg_dump --format=custom --file=app_$(date +%F).dump "$DATABASE_URL"
# Restore drill into a scratch database (not run)
createdb restore_drill
pg_restore --dbname=restore_drill --no-owner app_2026-11-26.dumpWhen you DON'T need this
- Git-backed static sites (like this one): Every page, config, and script in Mengty Learns lives in Git (already replicated across developer laptops and GitHub). There is no production database to back up—rebuilding from
mainrestores the entire site. - Ephemeral caches (Redis pure cache): If all cached data can be recomputed from Postgres, backing up Redis snapshots adds storage cost with little benefit.
- Cost / complexity trade-off: Automated backups + off-site object storage are very cheap (pennies per GB/month). Paying for cross-region warm standbys or active-active clusters multiplies compute cost 2x+ and adds replication lag bugs—only pay for low RTO when downtime actually costs more than the standby infrastructure.
Gotchas
- Confusing replication with backup: A read replica immediately applies
DROP TABLEorDELETE FROM users;—it does not replace point-in-time backups. - Storing backups in the same cloud account with the same admin key: If an attacker compromises your primary account, they can delete your backups too. Use cross-account replication or WORM/Object Lock.
- Never testing
pg_restore: Silent cron failures, truncated dumps, or missing KMS permissions are only caught by running periodic restore drills.
Production checklist
- Written RPO and RTO targets exist for every stateful system.
- Backups follow 3-2-1 with off-site, immutable (or separate-account) storage.
- Backups are encrypted at rest and in transit, with keys stored separately from the data.
- Automated restore drill runs on a schedule (monthly/quarterly) into an isolated scratch DB and verifies row counts.
Check yourself
Check Yourself
1. Your nightly pg_dump runs at 02:00 UTC and the database is corrupted at 14:00 UTC. What is your actual RPO if you only have nightly dumps?
2. Why is a PostgreSQL read replica not a substitute for backups?
Sources
Related Notes
Reverse Proxy & TLS Termination
How a reverse proxy terminates TLS with ACME/Let's Encrypt, routes traffic, sets forwarding headers, and compares Nginx, Caddy, and managed edges.
Observability: Logs, Metrics & Traces
Use logs, metrics, and traces with RED/USE, symptom-based alerts, SLO error budgets, and OpenTelemetry—and know what a static site needs vs a backend.