# How a backup actually brings your data back

Alfonso Campodonico · 2026-11-30 · https://alfonsocampodonico.com/posts/how-a-backup-actually-brings-your-data-back

> A backup is a snapshot plus a log. Restoring means replaying the log up to the second before the mistake.

## Why "we have backups" isn't the answer

Someone runs `DELETE FROM orders` and forgets the `WHERE`. Everyone relaxes because "we have backups". Then the questions start. Backups from when? Can we get back to 14:31, a minute before the mistake? How long will it take? What about the orders that came in after?

Those questions have real answers, and they all come from how a backup is built. This post walks through it: what's actually stored, how a restore puts it back together, and the two numbers that decide how bad your bad day is.

The short version: a modern database backup is two things, a full copy taken now and then and a log of every change since. A restore copies the full copy back and replays the log on top, stopping wherever you tell it to.

## How it works, in one minute

Picture a busy pantry.

1. **A photo every night.** At closing time you photograph every shelf. That's the **snapshot**, or base backup.
2. **A receipt for every change.** All day, every time someone takes or adds something, a receipt prints: "added 4 kg of flour, 09:12". The receipt roll is the **log**. The database writes it anyway, for its own crash safety. A backup just keeps a copy.
3. **Someone knocks over a shelf.** At 14:32, an entire shelf ends up on the floor.
4. **Put it back.** You stock an empty pantry exactly as last night's photo shows it. Then you redo every receipt, in order, up to 14:31. That's **point-in-time recovery**.
5. **Two numbers.** How much you can lose is **RPO**: how far back your last safe receipt is. How long it takes is **RTO**: restocking from the photo, then working through the receipts.

The photo alone only gets you back to last night. The receipts alone are useless without something to apply them to. You need both.

## The deep dive

The rest of this post takes each step with real names. Postgres calls the log the write-ahead log (WAL). MySQL keeps two: InnoDB's redo log for crash recovery, and the binary log, the one you keep for point-in-time restores. Most databases have something like them, and managed databases run the same machinery behind a restore button.

## The log was already there

Before a database changes a page on disk, it writes the change to its log and flushes it. If the server crashes mid-write, it reads the log on startup and redoes whatever didn't reach the data files. That's crash recovery, and it's why the log exists at all.

A backup reuses it. Copy the log somewhere safe as it's written, and you have a record of every change since the last snapshot. In Postgres, that's WAL archiving: each finished 16 MB segment goes to `archive_command` or an archive module, usually off to object storage. In MySQL, it's the binary log you keep: you copy its files off the server.

## The snapshot doesn't have to be perfect

A snapshot is a copy of the data files: a `pg_basebackup`, a Percona XtraBackup run, or a disk snapshot. The database keeps writing while it's taken, so the copy is a little inconsistent: some pages are from before a change, some from after.

That's fine, for the same reason crash recovery works. When you start a database from that snapshot, it sees an unclean copy, opens the log at the last checkpoint before the snapshot started, and replays forward until everything agrees. A snapshot of a running database is just a crash, recovered later, as long as you also keep the log written while it was being taken.

## Restoring to a point in time

So a restore has three steps:

1. **Copy the snapshot back**, onto a new server.
2. **Replay the log**, change by change, in order.
3. **Stop at the target.** In Postgres, that's `recovery_target_time`. With MySQL, `mysqlbinlog --stop-datetime` writes the changes up to that moment, and you pipe them into the server.

Try it below. The snapshot is from midnight. The log has the day's changes, and the bad `DELETE` at 14:32. Drag the target and watch the table come back.

*Interactive diagram on the page: Rebuilding the table.*

Notice the order that came in at 14:40. Restore to 14:31 and it's gone too, because it came after the target. That's why you restore to a *new* server, not over the live one. You get the old data back next to the current data, and you copy across what you need: the deleted rows into production, or the later orders out of it.

## RPO: how much you can lose

The recovery point objective is how much recent data a restore can't bring back. It depends on how often the log leaves the server:

- **A nightly dump** (`pg_dump`, `mysqldump`) is a full copy, but no log. Lose the database at 17:00 and the last 17 hours are gone. Dumps are still worth having: they're portable and easy to inspect. They just can't do point-in-time.
- **An archived log** leaves in chunks. Postgres only ships a WAL segment once it's full, so on a quiet database the last segment could sit there for hours. `archive_timeout` forces a switch every few minutes. You can lose up to one chunk.
- **A streamed log** leaves as it's written, to a standby or to a tool like `pg_receivewal`. You lose seconds at most.

*Interactive diagram on the page: What you lose.*

## RTO: how long it takes

The recovery time objective is how long you're down. A restore spends it in two places: copying the snapshot back, then replaying the log. The second one is the one people forget.

Postgres replays its log in a single process, and piping `mysqlbinlog` into a server applies the changes one at a time. Either way, it re-does every write since the snapshot. On a busy database, 14 hours of log can take longer to replay than the snapshot takes to copy. The fix is boring: take snapshots more often, so there's less log to replay.

*Interactive diagram on the page: How long a restore takes.*

These numbers are made up. Yours depend on your disks, your network and your write load, and the only way to know them is to time a real restore.

## A replica isn't a backup

A replica follows the primary in real time. That's what makes it good for failover, and what makes it useless here: it replays the `DELETE` a few milliseconds after the primary does. A replica protects you from a dead server. It doesn't protect you from a bad query, a bad migration or a bad deploy. Those need a backup you can rewind.

## What I'd check

- **The log leaves the server.** Archived or streamed, somewhere the database server can't reach and the database's own credentials can't delete. Object storage with versioning or object lock is the usual answer.
- **You know your two numbers.** How far back you can lose, and how long a restore takes, written down.
- **You've restored, recently.** Pick a time, restore to a new server, check a table, time it. Put it in the calendar. A backup you've never restored is a guess.

Managed databases do most of this for you, and point-in-time restore is usually a field with a date and time in it. It's still worth knowing what happens when you press the button. It copies a snapshot and replays a log, and that log replay is why it takes as long as it does.
