Cobalt Field Notes

Backups are a restore problem

· 2 min read

Every backup scheme looks fine until the day it is needed. The questions that matter are about the restore: how long it takes, what it loses, and whether anyone has done it recently.

Decide what you can lose

Two numbers drive the design. The recovery point objective is how much recent data you can afford to lose; an hourly backup means up to an hour. The recovery time objective is how long the service may stay down while you restore. A nightly copy to another region is cheap, but restoring 200 GB from it over a slow link can take most of a day.

Copies that are independent

The old 3-2-1 rule still holds: three copies of the data, on two different kinds of storage, one of them off site. Snapshots on the same disk or in the same account protect against mistakes, not against losing the disk or the account. At least one copy should be out of reach of the credentials the server itself holds, so that a compromised or misbehaving host cannot delete its own backups.

Consistent copies

Copying the files of a running database rarely produces something usable. Use the tool the database provides: pg_dump or pg_basebackup for PostgreSQL, the .backup command or VACUUM INTO for SQLite, and filesystem snapshots only when the application is known to recover from them as it would from a power loss.

Rehearse

# restore the newest archive into a scratch directory and check it
latest=$(ls -t /backups/app-*.tar.gz | head -n 1)
mkdir -p /tmp/restore-test
tar -xzf "$latest" -C /tmp/restore-test
sqlite3 /tmp/restore-test/app.db 'PRAGMA integrity_check;'

A restore test that runs on a schedule and raises an alert when it fails catches the slow failures: a job that has been writing empty archives for a month, credentials that expired, a disk that filled up. Write the steps down while doing it; the person restoring next time may be you, at night, in a hurry.