Backup & Recovery — Database

A backup is a separate copy for a restore (bringing the data back) after data is lost or damaged. RPO is how much of the latest data may be lost, and RTO is…

At 14:30, somebody at Sunset Coffee runs DELETE FROM orders; and forgets to write the WHERE. Every order is gone in an instant. In the Updating & Deleting Data lesson of the SQL track, you saw for yourself that the database doesn't ask "are you sure?" and has no undo button. The only thing that can help now is a backup.

A backup is a copy of the database kept somewhere else, so the data can be brought back (restore or recovery) after it's lost or damaged. The cause can be a broken disk, a deleted server, or that DELETE without a WHERE. Restore here means bringing data back from a backup, not the restore of the Soft Delete & Archiving lesson (making a soft-deleted row active again).

Picture Sunset Coffee's cash sheets being photocopied every midnight. At 14:30, coffee is spilled all over the original sheets. You can copy them again from last night's photocopy, but every sale since midnight is gone, unless you also kept the receipts. The cash sheets are the database. The photocopy is a full backup: a copy of the whole database at one moment. The pile of receipts is a change log: a record of every change after the backup was made.

One day at Sunset Coffee

This page's Lab replays that day in real SQLite:

Time What happens
00:00 full backup: 3 customers, 4 products, 3 orders (#1 to #3)
07:20 order #4: Andi, 1 Milk Tea
09:15 order #5: Siti, 2 Palm Sugar Milk Coffee
11:40 order #6: Budi, 1 Toast
13:05 Toast's price goes up from $5.00 to $5.50
14:10 order #7: Budi, 1 Milk Coffee
14:30 DELETE FROM orders; every order is gone

RPO and RTO

There are two numbers you agree on with the business owner before any disaster:

If a full backup is made only at 00:00, the recovery point is 00:00: a restore brings the database back to how it was at 00:00. Only 3 orders come back (#1 to #3). Orders #4 to #7 and Toast's price rise are lost, so 14 hours 30 minutes of data is gone. A daily backup means an RPO of up to 24 hours, which is usually too much for an online shop.

Full, incremental, differential

For a smaller RPO, backups have to be made more often. But copying the whole database every hour is heavy. That's why there are three kinds of backup:

In the Lab, a full backup is made at 00:00, then small backups at 04:00, 08:00 and 12:00. This is what each small backup holds:

Backup Incremental holds Differential holds
04:00 0 changes 0 changes
08:00 #4 #4
12:00 #5, #6 #4, #5, #6

PITR: back to the second before the disaster

PostgreSQL writes every change to the WAL (Write-Ahead Log) first, and only then applies it to the tables. The WAL plays the part of the pile of receipts. If you keep archiving those WAL files (copying them to other storage), you can use PITR (Point-in-Time Recovery). This is how: first restore a base backup (the full backup you start from), then replay the WAL up to the second you choose. The settings go in postgresql.conf:

# postgresql.conf on the server being restored (PostgreSQL 12 and later)
restore_command = 'cp /backup/wal/%f %p'
recovery_target_time = '2026-10-08 14:29:59'
# then create an empty recovery.signal file in the data folder, and start the server

PostgreSQL's tools: pg_dump and pg_basebackup

These are the commands for both tools, plus the settings for archiving the WAL:

# logical backup: an SQL file with CREATE TABLE and all the data
pg_dump -h localhost -U postgres mydb > backup.sql
# restoring it: run that SQL file
psql mydb < backup.sql

# physical backup: a copy of PostgreSQL's data files as they are
pg_basebackup -D /backup/base -Ft -z -P

# continuous WAL archiving (postgresql.conf), needed for PITR
archive_mode = on
archive_command = 'cp %p /backup/wal/%f'

How to choose, exactly as in the exercise. The second column asks whether the app may be down for a long time while restoring:

Database size Long downtime while restoring allowed? Pick
small (under 1 GB) either pg_dump
medium (1 to 100 GB) allowed pg_dump
medium (1 to 100 GB) not allowed pg_basebackup
big (over 100 GB) either pg_basebackup + WAL archiving

Other options you will meet outside the exercise:

A backup that's never tested isn't a backup

Picture a backup job (an automatic task that makes the backup every night) that is set up wrong. Its command is pg_dump --schema-only, so the file holds only CREATE TABLE and not a single row of data. A file appears every night, the job is marked "succeeded", and nobody suspects anything until the day of the disaster. The only way to discover this is a restore drill, a practice restore. Regularly (for example, once a month), restore the backup onto a separate server, then check what's in it by counting the rows:

SELECT (SELECT COUNT(*) FROM customers) AS customers,
       (SELECT COUNT(*) FROM orders) AS orders;

Compare the numbers with the production database. The time the drill takes is your real RTO. Keep the copies with the 3-2-1 rule: 3 copies of the data, on 2 different kinds of storage, and 1 off site. Set an alert that goes off when the backup job fails, or when its file suddenly gets smaller.

⚠️ Backups that do not help in the end

TL;DR: A backup is a separate copy for a restore (bringing the data back) after data is lost or damaged. RPO is how much of the latest data may be lost, and RTO is how long the app may be down. A full backup copies the whole database. An incremental holds only the changes since the last backup of any kind, so a restore needs every file in order. A differential holds every change since the last full backup, so a restore needs only the full plus the latest differential. PITR (Point-in-Time Recovery) restores a base backup (the full backup you start from), then replays the WAL, PostgreSQL's record of every change, up to the second before the disaster. In PostgreSQL, pick pg_dump (an SQL file) for a small database, or a medium one that may be down a while during a restore. Pick pg_basebackup (a copy of the data files) for a medium one that may not, and pg_basebackup + WAL archiving (plus a WAL archive) for a big one. Keep copies by the 3-2-1 rule (3 copies, 2 kinds of storage, 1 off site), and run restore drills (practice restores) regularly: a backup that's never tested isn't a backup.

⚡ RPO and RTO

Guess first: a full backup (a copy of the whole database) runs only at 00:00, then at 14:30 every order is deleted. How much data is lost?

About 14 hours 30 minutes. A restore (bringing data back from a backup) only brings back the 00:00 state, so every order after it is gone.

Meaning: RPO is how much of the latest data may be lost, and RTO is how long the app may be down. You agree on both numbers before any disaster.

⚡ Full, incremental, differential

Meaning: A full backup copies the whole database. An incremental holds only the changes since the last backup of any kind, so a restore needs every file in order. A differential holds every change since the last full backup, so a restore needs only the full plus the latest differential.

⚡ PITR and restore drills

Write:

pg_basebackup -D /backup/base -Ft -z -P

Meaning: pg_basebackup makes a physical backup, a copy of PostgreSQL's data files, in the folder /backup/base (-D). -Ft -z makes it a compressed tar file, and -P shows the progress. With WAL archiving (keeping every file of the WAL, PostgreSQL's record of every change), you can restore this backup and then replay the WAL up to the second before the disaster. This is called PITR. Run restore drills regularly (restore the backup onto a separate server, then check what's in it): a backup that's never tested isn't a backup.

In the exercise, return the exact text, including "pg_basebackup + WAL archiving" with a space on each side of the +.

What you will learn