Advanced
Searching inLabs
How to search
  • Search a task or a tool (lvm, selinux, @podman); filter by level and by book or course.
  • With an empty search box, a filter alone lists the labs — for example all hard labs of one course.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Lab 11.1Engineering practice, Chapter 11

Prove the backup restores: a timed restore drill with RPO and RTO

Level
Medium
60 min
Machines
a Linux machine with Docker (or a VM with PostgreSQL), a stopwatch
Status
Not started

Goal

"We have backups" means nothing until a restore worked. Run a small database with live writes, take backups the way production would, then destroy the database and restore it while the clock runs. You measure the real RPO (how much data you lost) and RTO (how long you were down), compare them with the targets, and write the drill report.

Before you start

  • Docker works. Write the targets down before the drill: RPO 15 minutes, RTO 30 minutes.
  • Start the database and a writer that inserts one row every second (this is your "business"):
    docker run -d --name pg -e POSTGRES_PASSWORD=lab -v pgdata:/var/lib/postgresql/data postgres:16
    sleep 5; docker exec pg psql -U postgres -c "create table orders(id serial primary key, at timestamptz default now());"
    (while true; do docker exec pg psql -qU postgres -c "insert into orders default values;" >/dev/null 2>&1; sleep 1; done) &

Tasks

  1. Scope: write in 3 lines what you test (one database), what "restored" means (the app can read and write, the newest row is known) and who decides the all-clear.
  2. Take a logical backup with pg_dump -Fc into a folder outside the container, with the date in the name, and record its checksum.
  3. Let the writer run for 2 more minutes (these rows will be lost — that is your RPO).
  4. Disaster: stop the writer, remove the container and the volume. Start the stopwatch (T+0).
  5. Restore: new container, new volume, pg_restore, check the row count and the newest timestamp. Restart the writer. Stop the stopwatch at the first successful write.
  6. Calculate the real RPO (newest row before the disaster minus newest restored row) and the real RTO (T+0 to the first write). Did you meet the targets?
  7. Write the drill report: timeline, numbers, what went wrong, three actions (for example: backups every 5 minutes, WAL archiving for point-in-time restore, a scripted restore).

Check your work

ls -l ~/drill-backups/
sha256sum -c ~/drill-backups/*.sha256
docker exec pg psql -U postgres -tc "select count(*), max(at) from orders;"
cat ~/drill-backups/REPORT.md
  • The checksum check prints OK.
  • After the restore the table has rows, and new rows arrive again.
  • The report has T+0, the restore end, the measured RPO and RTO, and whether each target was met.

Hints

Show the hints

Solution

Show the solution
  1. Backup with checksum.
    mkdir -p ~/drill-backups && cd ~/drill-backups
    F=pg-$(date +%F-%H%M).dump
    docker exec pg pg_dump -U postgres -Fc postgres > $F
    sha256sum $F > $F.sha256
  2. Wait 2 minutes, then note the last row and cause the disaster.
    sleep 120
    docker exec pg psql -U postgres -tc "select max(at) from orders;" | tee last-before.txt
    kill %1
    T0=$(date +%s); docker rm -f pg && docker volume rm pgdata
  3. Restore.
    docker run -d --name pg -e POSTGRES_PASSWORD=lab -v pgdata:/var/lib/postgresql/data postgres:16
    sleep 5; docker exec -i pg pg_restore -U postgres -d postgres --clean --if-exists < $F
    docker exec pg psql -U postgres -tc "select count(*), max(at) from orders;"
    docker exec pg psql -qU postgres -c "insert into orders default values;" && T1=$(date +%s)
    echo "RTO: $((T1 - T0)) s"
    RTO: 41 s
  4. RPO = max(at) before the disaster minus max(at) after the restore, here about 2 minutes (the rows written after the dump). The RTO of under a minute is far below the target of 30 minutes, but only because you knew every command; a real team also needs time to detect, decide and communicate.
  5. The report.
    ~/drill-backups/REPORT.md
    # Restore drill 2026-11-14 — orders database
    Targets: RPO 15 min, RTO 30 min.  Result: RPO 2 min (met), RTO 41 s (met, technical part only).
    Timeline: 10:02 dump taken · 10:04 disaster (T+0) · 10:04:41 first write after restore.
    Went wrong: restore steps were typed by hand; no alert would have told us the DB was gone.
    Actions: 1) dumps every 5 min + WAL archiving (owner: ops, by 30 Nov)
             2) script the restore and run this drill every quarter
             3) alert on "no new orders for 2 min"

My notes

No notes yet.

Lessons for this lab

Commands in this lab