---
title: Backups & recovery
summary: A backup you've never restored is a hope, not a backup.
section: data
phase: operate
order: 3
last_reviewed: 2026-11-20
parent: databases
---

# Backups & recovery

Agents can and do run destructive commands. Beyond that, bad migrations, mistakes and provider outages happen. Backups decide whether one of those is an incident or a disaster.

## You'll be able to
- Define how much data you can afford to lose and how long you can be down
- Configure automated backups and point-in-time recovery
- Run and document a restore drill

## The mental model
Two numbers drive everything:
- **RPO (Recovery Point Objective):** How much data you can afford to lose. "5 minutes" means you need continuous backups.
- **RTO (Recovery Time Objective):** How long you can afford to be down while restoring.

## Key concepts
- **Logical backup:** A SQL dump (`pg_dump`). It's portable and slow for big databases.
- **Physical backup / snapshot:** A copy of the raw storage. It's fast and tied to the engine version.
- **Point-in-time recovery (PITR):** A base backup plus the write-ahead log (WAL), letting you restore to any second within the retention window. Most managed Postgres services offer it.
- **Branching:** Some providers (Neon, Supabase, PlanetScale) can instantly fork a database. This is great for testing migrations against real-shaped data.
- **3-2-1 rule:** 3 copies, on 2 types of media, with 1 off-site (a different provider or region).
- **Retention:** How long backups are kept (for example, daily for 30 days, monthly for a year).
- **Encryption:** Backups contain everything, so encrypt them and restrict who can access them.
- **Restore drill:** Actually restoring into a scratch environment, on a schedule, and timing it.
- **Beyond the database:** Uploaded files (S3/R2), environment config and secrets need backing up too.

> **Note:** The PostgreSQL manual cautions that `pg_dump` is generally not the right choice for regular backups of production databases, except in simple cases. Its backup chapter describes continuous archiving: a base backup plus archived write-ahead log, which gives point-in-time recovery. A `pg_dump` can still be a useful portable, off-site copy. It covers one database only; roles and tablespaces come from `pg_dumpall`.

## Decisions you make (not the agent)
| Decision | Sensible default | Choose otherwise when… |
|---|---|---|
| Backup type | Managed PITR (7+ days) | Self-hosted → pgBackRest/WAL-G |
| Off-site copy | Nightly `pg_dump` to object storage in another provider/region | Strict compliance requirements → follow those |
| Drill frequency | Quarterly, documented in a runbook | Regulated industries → monthly |
| Agent access to prod DB | **None** by default; read-only replica if needed | — |

## What good output looks like

- RPO and RTO are written down, and the setup can actually meet them
- Backups run automatically and **tell a person when they fail**
- The backup tool matches the database server's version, and someone checks that each run produced a real, non-empty file
- At least one encrypted copy lives somewhere else, with access limited
- A runbook exists, and a timed restore drill has been run in a scratch environment and recorded

## Red flags
- "The provider handles backups," with no one knowing the retention period or how to restore
- Backups stored in the same account or region as production, and nowhere else
- An agent given production database credentials with write access
- No restore ever tested
- **Replication or snapshots counted as backups.** GitLab's 2017 postmortem notes their streaming replication was meant for failover, not disaster recovery
- **A failure alert nobody receives.** In the same incident, the failure emails from the daily dump were being rejected, so nobody saw it fail
- **A dump restored from an untrusted source without inspecting it.** Restoring a dump runs whatever the source put in it, according to the pg_dump documentation

## Worked example

**Task:** Have the agent set up nightly off-site backups of the production Postgres database.

You prompt: *"Set up backups for our database."* The agent writes a scheduled workflow that runs `pg_dump` and uploads the result. The workflow is green and the setup looks done.

**What a good result contains:** a dump from a tool whose version matches the server, encrypted and stored off-site; an alert to a person when a run fails; roles and permissions that a restore will need; and a restore drill that has been run and timed against your RTO.

**The wrong turn:** nobody restores a backup until the day it matters, and this one wouldn't work. The runner uses a different `pg_dump` version from the server, so every run fails and the bucket stays empty, and the failure notice goes somewhere nobody reads. Even a successful dump covers one database and not the roles it needs. GitLab's 2017 postmortem describes a very similar combination: an empty bucket from a dump job that failed on a version mismatch, failure emails that were never seen, and recovery procedures that nobody had tested because nobody owned them.

**How review catches it:** do the drill. Restore the latest backup into a scratch environment, time it, and compare the time with your RTO. Check that the files exist, aren't empty, and are recent. Break the job on purpose and confirm that a human gets told. Confirm that the restored database has the roles and extensions the app needs.

## Sample prompts

### Weak vs strong
```compare
## Weak
Set up backups for our database.
## Why it fails
It doesn't say where backups go, how they're encrypted, how failures are reported or how they're tested. The agent will make something that runs, which is not the same as something you can restore from.
## Strong
Write a GitHub Actions workflow that runs nightly: pg_dump our Postgres
(DATABASE_URL from secrets) in custom format, compress, encrypt with age using a
public key from secrets, upload to Cloudflare R2 bucket <name> with the date in
the key, and delete objects older than 30 days. Fail loudly and notify on error.
## Why it works
It names the tool, the schedule, the encryption, the destination, the retention and what happens on failure. Those are the places backup jobs usually go wrong.
## What changed
- **Schedule and tool** are named, with the connection string kept in secrets
- **Encryption** is specified, with only a public key in the workflow
- **Destination** is a different provider from production
- **Retention** is stated, so old copies are deleted
- **Failure** must be loud, which is what stops a silent dead job
```

```text
Write a restore runbook (markdown) for our <Neon/Supabase/RDS> Postgres:
how to do point-in-time recovery to a new branch/instance, how to verify data,
how to cut the app over, and how to restore from the off-site pg_dump if the
provider itself is unavailable. Include estimated time for each step.
```
*Why it works:* A runbook written before the incident, with a time estimate per step, is what lets you meet your recovery-time target. Check the estimates in a drill.

### Run a restore drill
```text
Run a restore drill. Take the most recent off-site backup and restore it into a new scratch database that is not production. Record the start and end time of each step, then verify the data: row counts for <key tables>, the newest record's timestamp, and that the app's roles and extensions exist. Compare the total time with our RTO of <time> and our RPO of <time>, and list every step that failed or needed a manual fix. Don't touch production.
```
*Why it works:* A restore you have timed is the only proof of your RTO. The list of failures and manual fixes is the real work to do.

### Audit the backup setup
```text
Audit our backup setup. List every mechanism (backups, replicas, snapshots, dumps) and say for each: what it is for (backup, failover or something else), where it is stored, who can access it, how it is monitored, when it was last restored successfully, and who is told when it fails. Check that the dump tool's version matches the server and that roles are covered. Don't change anything.
```
*Why it works:* It separates real backups from replication and snapshots, which is the confusion behind many data-loss incidents, and it asks for the last successful restore.

### Explain what we can recover
```text
Explain, for our current setup, what we could recover after each of these: someone drops a table at 14:03; the provider loses the region; a bad migration corrupts data a day ago. For each, say the point in time we can restore to, how long it would take, and the first command you would run. Mark any answer you couldn't verify.
```
*Why it works:* Scenarios turn RPO and RTO from abstract numbers into answers, and "couldn't verify" marks the gaps to test.

## Check your understanding

```quiz
Q: Why is a streaming replica not a replacement for backups?
- [ ] Replicas are slower
- [x] It is built for failover, not for recovering from mistakes or disasters
- [ ] Replicas can't hold a full copy
> Why: A replica serves a different purpose. A real backup is a separate, tested copy you can restore from.

---

Q: What two things combine to give point-in-time recovery?
- [ ] A dump and a snapshot
- [x] A base backup and the archived write-ahead log, replayed up to the moment you choose
- [ ] Two replicas
> Why: Replaying the archived log on top of a base backup lets you stop at any point in time.

---

Q: Does pg_dump of one database include its roles?
- [ ] Yes, everything in the cluster
- [x] No. It dumps one database, and roles and tablespaces come from pg_dumpall
- [ ] Only superuser roles
> Why: A restore into a fresh instance can fail or lack permissions if the roles weren't recreated.

---

Q: A nightly backup job has been failing for weeks and nobody noticed. What would have caught it?
- [ ] Larger disks
- [x] An alert that reaches a person on failure, and regular restore drills
- [ ] Running it twice a night
> Why: A backup that hasn't been restored is unproven, and a failure nobody sees is the same as no backup.
```

## Checklist
- [ ] RPO and RTO written down
- [ ] PITR enabled; retention known
- [ ] An off-site copy exists
- [ ] A restore has been tested within the last quarter
- [ ] Agents cannot write to the production database
- [ ] A failed backup alerts a person who is expected to act
- [ ] The restore time from the last drill is compared with the RTO
- [ ] The backup tool's version matches the server, and roles are covered

## Go deeper
- [PostgreSQL: Backup and Restore](https://www.postgresql.org/docs/current/backup.html)
- [pgBackRest](https://pgbackrest.org)
- [PostgreSQL: pg_dump](https://www.postgresql.org/docs/current/app-pgdump.html): what a dump covers and how to restore it
- [PostgreSQL: Continuous Archiving and PITR](https://www.postgresql.org/docs/current/continuous-archiving.html)
- [GitLab: Postmortem of database outage of January 31](https://about.gitlab.com/blog/postmortem-of-database-outage-of-january-31/): a real incident where several backup mechanisms were not working
- [Databases](/docs/data/databases.md) and [Schema & migrations](/docs/data/schema-and-migrations.md)
