← Back to SQL, page 18

sql-0356 · Medium mid

A stored backup turned out to be unusable during an incident. How should backups for a production database be set up?

Combine regular full backups with continuous archiving of the write-ahead log, which allows point-in-time recovery to just before a mistake. Keep copies in a separate location and account, encrypt them, and above all test restores on a schedule. A backup that has never been restored is an assumption, not a guarantee.

Full answer

Elements of a sound setup:

  • Physical base backups (for PostgreSQL, pg_basebackup or a tool such as pgBackRest) plus WAL archiving, enabling recovery to any moment, for example one minute before an accidental DELETE.
  • Logical dumps (pg_dump) for moving single databases or tables between versions; slower to restore at scale.
  • Off-site and separate credentials: a compromised production account should not be able to delete backups.
  • Retention matching business needs, such as daily for 30 days and monthly for a year.
  • Restore drills: automated restores into a scratch server, with row counts or checksums checked, and the time taken recorded against the recovery time objective.
  • Monitoring: alerts when a backup or WAL archive fails.

Replicas are not backups: a destructive statement replicates within seconds. For SQLite, the online backup API or VACUUM INTO produces consistent copies while the database is in use.

Key points

  • Full backups plus WAL archiving enable point-in-time recovery
  • Store backups off-site with separate credentials
  • Test restores regularly
  • Replicas are not backups
  • Monitor backup jobs

Common mistakes

  • Never testing restores
  • Treating a replica as a backup

Follow-ups

  • What are RPO and RTO?
  • How would you recover a single dropped table?

Tips

  • Stress restore testing