Overview

ClickHouse has native BACKUP and RESTORE SQL commands that write a table, database or whole server to a backup destination (a local disk, S3, Azure Blob, or a file path). Backups can be incremental. Replication is not a backup; a bad DROP replicates.

Core concepts

  • Destinations must be allowed in server config: a backup disk plus <backups><allowed_disk> and <allowed_path>.
  • BACKUP TABLE db.t TO Disk('backups', 'name'), BACKUP DATABASE, BACKUP ALL. RESTORE TABLE db.t [AS db.t2] FROM Disk(...).
  • Incremental: SETTINGS base_backup = Disk('backups', 'previous') stores only the parts that are new compared with the base, referencing the rest.
  • Settings: compression_method, password (encrypted archive), async, structure_only.
  • Monitor in system.backups. Other approaches: ALTER TABLE ... FREEZE (hard-link snapshot of parts), the clickhouse-backup open-source tool, or object-storage snapshots.
  • Test restores, and restore into a new table name first to validate data.

Worked example

On events_p (4,553,600 rows after one partition drop):

BACKUP TABLE kb.events_p TO Disk('backups', 'events_p_full') SETTINGS compression_method = 'zstd';
-- BACKUP_CREATED   total 53.68 MiB   155 files   (55 MB on disk)
 
INSERT INTO events_p ...;          -- +100,000 rows
BACKUP TABLE kb.events_p TO Disk('backups', 'events_p_inc1') SETTINGS base_backup = Disk('backups', 'events_p_full');
-- BACKUP_CREATED   logical 54.47 MiB   new data 831.75 KiB   (912 KB on disk)
 
RESTORE TABLE kb.events_p AS kb.events_p_restored FROM Disk('backups', 'events_p_inc1');   -- 4,653,600 rows
DROP TABLE kb.events_p;
RESTORE TABLE kb.events_p FROM Disk('backups', 'events_p_full');                          -- 4,553,600 rows

The incremental backup stored only about 0.8 MiB of new data yet restored the full state at that point. Restoring the full backup after dropping the table returned the earlier row count, as expected.

Pitfalls and production notes

  • Keep backups off the server and in another failure domain; protect them with encryption and access control.
  • An incremental chain needs all its base backups; retention policies must respect that.
  • Replicated tables: back up one replica, or use BACKUP ... ON CLUSTER, and restore carefully into an empty replica set (ch-replication-keeper).
  • Account for the Keeper metadata: SYSTEM RESTORE REPLICA helps when Keeper data is lost.
  • Document RPO/RTO and rehearse a restore; compare with other engines (pg-backup-pitr, sqlserver-backup-recovery, oracle-rman-backup).

Practice questions

  1. How does an incremental backup avoid copying unchanged parts?
  2. Why restore into a different table name first?
  3. What server configuration is required before BACKUP TO Disk(...) works?

References

  • ClickHouse documentation, Backup and Restore

ch-replication-keeper · ch-partitions · ch-operations-monitoring · pg-backup-pitr