Backup & Recovery

Nobody needs backups; everybody needs restores. A backup strategy is judged only by one thing — whether the data can be brought back, to the right point in time, within the time the business can afford. This lesson covers the targets that drive a strategy, the kinds of backup available, point-in-time recovery, and the restore testing that turns a hope into a guarantee. Examples use PostgreSQL tools; Oracle's equivalent is RMAN.

Recovery Objectives

Before choosing tools, agree with the data owners on two numbers for each database. They turn a vague wish for “good backups” into requirements that can be tested.

RPO and RTO

ObjectiveQuestionDriven by
RPO — recovery point objectiveHow much recent data can we afford to lose?Backup frequency and continuous log archiving.
RTO — recovery time objectiveHow long may the database be unavailable while we recover?Restore speed, data size, automation and practice.

A nightly dump gives an RPO of up to 24 hours. Continuous WAL archiving brings it down to minutes or seconds. A standby replica can bring RTO down to minutes for a server failure — but not for a mistaken DELETE, which replicates instantly; that case needs a restore to an earlier point in time.

Plan for Each Failure Scenario

  • Human error: a dropped table or a bad update. Needs point-in-time recovery or a logical restore of one table.
  • Hardware or instance loss: needs a physical restore or failover to a replica (see Replication & High Availability).
  • Corruption: may silently propagate into recent backups; retention must reach back far enough to find a clean copy.
  • Site loss or ransomware: needs off-site copies that the compromised environment cannot modify or delete.

Kinds of Backup

Backups are either logical (the data as SQL statements or an archive of rows) or physical (a copy of the database files). Most production systems use both.

Logical Backups with pg_dump

pg_dump exports one database as a consistent snapshot without blocking normal reads and writes. The custom format (-Fc) is compressed and lets pg_restore select individual tables and restore in parallel:

# Consistent, compressed dump of one database.
pg_dump --format=custom --file=shop_2026-11-14.dump shop

# Global objects (roles, tablespaces) are not part of pg_dump; dump them separately.
pg_dumpall --globals-only --file=globals_2026-11-14.sql

# Restore into a new, empty database using four parallel jobs.
createdb shop_restore
pg_restore --jobs=4 --dbname=shop_restore shop_2026-11-14.dump

# Restore a single table from the same archive.
pg_restore --dbname=shop_restore --table=customer_order shop_2026-11-14.dump

Logical backups are portable across major versions and architectures, and ideal for restoring individual objects. They are slow to restore for large databases, because indexes must be rebuilt, and they only capture the moment of the dump.

Physical Base Backups

A physical backup copies the data directory of the whole cluster. pg_basebackup takes one from a running server over the replication protocol:

# Compressed tar-format base backup, streaming the WAL needed to make it consistent.
pg_basebackup --pgdata=/backups/base/2026-11-14 \
              --format=tar --gzip \
              --wal-method=stream \
              --checkpoint=fast --progress

Physical backups restore quickly, because files are copied rather than rebuilt, but they only restore to the same major version and platform. On their own they capture one moment; combined with archived WAL, they become the basis for point-in-time recovery.

Use a Backup Tool for Production

Dedicated tools such as pgBackRest and Barman manage base backups, WAL archiving, retention, compression, encryption, checksums and parallel restore for you. Managed cloud databases provide automated snapshots and point-in-time restore; understand their retention limits and test them like any other backup.

Point-in-Time Recovery

PostgreSQL writes every change to the write-ahead log (WAL) before applying it. If every WAL segment is archived, a base backup plus the archived WAL can replay the database to any moment after the backup — for example, one second before an accidental DROP TABLE.

Archive the WAL Continuously

# postgresql.conf on the primary
wal_level       = replica
archive_mode    = on
archive_command = 'test ! -f /archive/wal/%f && cp %p /archive/wal/%f'
archive_timeout = 60          # force a segment switch at least every minute

The archive_command above illustrates the contract — never overwrite an existing file, and return success only once the file is safely stored — but a plain cp to a local directory is not durable. In production, use the backup tool's archive command, which sends WAL to separate, durable storage. Monitor pg_stat_archiver: a failing archive command means the RPO is silently growing, and unarchived WAL accumulates until the disk fills.

Recover to a Target Time

To recover, restore the base backup into an empty data directory, tell the server where to fetch WAL and where to stop, and create the signal file that starts recovery mode (PostgreSQL 12+):

# postgresql.conf (or postgresql.auto.conf) of the restored copy
restore_command        = 'cp /archive/wal/%f %p'
recovery_target_time   = '2026-11-14 09:41:00+00'
recovery_target_action = 'pause'      # inspect before opening for writes

# then, in the data directory:
#   touch recovery.signal
#   start the server, verify the data, and run: SELECT pg_wal_replay_resume();

Always recover to a separate server first. Verify the data there, then either promote it or copy the lost rows back to production. Overwriting the only copy of the damaged database destroys the evidence and the chance to try a different target time.

Storage, Retention and Security

A backup stored next to the database fails together with it. Where and how long backups are kept is as important as how they are taken.

The 3-2-1 Rule

Keep at least three copies of the data, on two different kinds of storage, with one copy off-site. Against ransomware, make at least one copy immutable — for example, object storage with write-once retention locks — and use separate credentials, so that an attacker who controls the database server cannot delete its backups.

Retention and Encryption

Define retention from requirements: for example, daily backups for 14 days, weekly for 3 months, monthly for a year, with WAL kept back to the oldest base backup you intend to use for point-in-time recovery. Legal rules may set both minimum and maximum retention for personal data. Encrypt backups at rest and in transit, store the keys apart from the backups, and include the keys in the recovery runbook — an encrypted backup without its key is no backup.

Restore Testing

An untested backup is a hypothesis. Backups fail silently: a full disk, a changed path, an expired credential, a missing global object. Only a restore proves they work.

Automate Restore Tests

  1. On a schedule (at least weekly), restore the latest backup to an isolated server.
  2. Run integrity checks: the server starts, row counts of key tables are within expected ranges, and application smoke queries succeed.
  3. Record the elapsed time and compare it with the RTO; restore time grows with the data.
  4. Alert on any failure exactly as you would on a production outage.

Run Recovery Drills

Automated tests prove the files are usable; drills prove the people and the runbook are. Several times a year, have someone other than the author follow the written runbook to recover a scenario — a dropped table to a point in time, or a lost server from off-site storage — and fix every step where the runbook was wrong or unclear.

Practice Lab

Use a disposable local PostgreSQL cluster with WAL archiving enabled to a local directory.

Logical Backup and Restore

  1. Dump a sample database with pg_dump -Fc and its roles with pg_dumpall --globals-only.
  2. Restore both onto a fresh cluster and compare row counts of every table.
  3. Drop one table in the restored copy and restore only that table from the archive.

Point-in-Time Recovery

  1. Take a base backup, then insert rows for a few minutes, noting the time of each batch.
  2. Run an accidental DELETE and note its time.
  3. Recover a copy to one minute before the delete with recovery_target_action = 'pause', verify the rows are present, and record how long the whole recovery took.