Backup Lab: Full, Incremental, Drop, Restore
BACKUP and RESTORE ON CLUSTER to S3, as a least-privilege user, restored and checked to the row
A backup you haven't restored is a hope. labs/backup_restore.py loads
data, backs it up, drops the tables, and restores.
docker compose -f compose_prod.yaml run --rm labs python backup_restore.py1. Data
5,000,000 rows
2. Full backup ON CLUSTER (user: backup)
took 1.9s
3. 500,000 more rows, then an incremental backup
took 1.6s
rows: 5,500,000, sum(duration_ms) = 5,497,250,000
stored in S3: full 62.3 MB, incremental 6.4 MB
4. The accident
DROP TABLE events, events_local ON CLUSTER lab
5. RESTORE from the incremental backup
took 2.0s
restored rows: 5,500,000, sum matches: True
per node: ch-1 2,747,289, ch-2 2,747,289, ch-3 2,752,711, ch-4 2,752,711The statements:
BACKUP DATABASE analytics ON CLUSTER lab TO S3(ch_backups, 'full-<stamp>/');
BACKUP DATABASE analytics ON CLUSTER lab TO S3(ch_backups, 'incr-<stamp>/')
SETTINGS base_backup = S3(ch_backups, 'full-<stamp>/');
RESTORE DATABASE analytics ON CLUSTER lab FROM S3(ch_backups, 'incr-<stamp>/');ch_backups is a named collection holding the S3 URL and keys, so they
appear in no statement and no log. ON CLUSTER backs up one replica of
each shard and restores to every replica.
How big is an incremental backup?
system.backups reported the incremental as 137 MiB, bigger than the
full. That number counts every file the backup refers to, including the
ones it reuses from the base. Summing the objects in the bucket gives what
was really stored: 6.4 MB, about a tenth of the full backup, for a tenth
more data. Restoring the incremental needs the full backup to still be
there.
The first run said the restore was wrong
It reported 2.16 million rows after loading 5 million, and a restored
table with more rows than before the backup. Both counts were read too
early: the Distributed inserts used the 26.2 async_insert default, and
the counts came from replicas still fetching parts. The lab now inserts
synchronously and runs SYSTEM SYNC REPLICA on every node before
counting. With that, the row counts and the sum of a column match exactly.
The lab also dropped one setting: an earlier version restored with
allow_non_empty_tables = true. Restoring into tables that still hold
data appends to them and can silently duplicate rows. Restore into empty
tables, or into a new database, and swap.
Not covered by BACKUP DATABASE: users, roles and grants, which live in
Keeper here. They come back by re-running scripts/init.sql from git,
which is a good reason to manage them as code. Keep
the backups in a different failure domain from the cluster, and run a
restore on a schedule, not only in a lab.