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:
- RPO (Recovery Point Objective): how much of the latest data may be lost, measured as time back from the disaster. "RPO 1 hour" means at most the last hour of data may be lost.
- RTO (Recovery Time Objective): how long the app may be down until it works again. "RTO 30 minutes" means the restore has to finish within 30 minutes.
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:
- Full backup: copies the whole database. The most complete, but also the biggest and the slowest to make.
- Incremental backup: copies only the changes since the last backup of any kind (full or the previous incremental).
- Differential backup: copies every change since the last full 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 |
- Restoring with incrementals needs the full backup plus every incremental, in order: 4 files. The files are small, but if just one is damaged, the chain breaks: the files after it can't be used.
- Restoring with differentials needs only the full backup plus the latest differential: 2 files. The files grow through the day, but the restore is quicker.
- Both bring the database back to how it was at 12:00: 6 orders, Toast still at $5.00. The data lost is 2 hours 30 minutes (the price rise and order #7).
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
restore_commandtells PostgreSQL how to fetch WAL files from the archive.recovery_target_timemakes the replay stop at 14:29:59, right before theDELETEat 14:30.- The result in the Lab: all 7 orders come back and Toast costs $5.50. The data lost is a matter of seconds.
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'
pg_dumpproduces a logical backup: SQL statements that rebuild the database. It is easy to read and to move, so it's a good fit for a small database. Restoring it inserts every row again and rebuilds every index, so for a big database a restore can take hours (a long RTO). This Lab uses SQLite. The Lab's backup file is an SQL export like sqlite3's.dumpcommand, the equivalent ofpg_dump.pg_basebackupproduces a physical backup: a copy of PostgreSQL's data files. In the command above,-Dis the target folder,-Ft -zmakes a compressed tar file, and-Pshows the progress. Restoring it is just copying the files back, much faster for a big database.pg_basebackup+ WAL archiving (archiving the WAL continuously) lets you do PITR, with an RPO of only seconds.
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:
pg_dump -Fc, thenpg_restore -j 4.-Fcmakes a compressed file in the custom format. Its restore can run as 4 processes at the same time, so the RTO is shorter than with a plain.sqlfile.pg_basebackup --incremental(PostgreSQL 17 and later, withsummarize_wal = on). With it, PostgreSQL can make incremental backups by itself. Before a restore, the chain of backups is joined back together withpg_combinebackup.- pgBackRest or WAL-G. Tools that manage full and incremental backups, the WAL archive, compression and storage on S3 (Amazon's file storage service) in one place. Ready-made database services such as AWS RDS or Cloud SQL already provide daily backups and PITR. You only set how many days the backups are kept.
- In MySQL:
mysqldumpfor a logical backup (likepg_dump), the binary log for PITR (like WAL), and Percona XtraBackup for a physical backup without stopping the database.
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
- The backup on the same server. When the disk breaks, the database and its backup are lost together. Use the 3-2-1 rule.
- Never testing a restore. An empty, broken or unreadable backup is only found when you need it.
- A broken incremental chain. If one incremental file is lost, every file after it is useless.
- Misspelling the answers in the exercise. Return the exact text:
"pg_dump","pg_basebackup", and"pg_basebackup + WAL archiving"with a space on each side of the+.
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 +.