Skip to main content
On a self-managed cluster, BACKUP ... ON CLUSTER makes every host of the cluster write its tables into one backup, and RESTORE ... ON CLUSTER creates the tables again on every host. Two rules apply: the destination must be storage that all hosts share, and it cannot be an archive file.
This article is about self-managed ClickHouse. In ClickHouse Cloud, backups are taken by the service. See Backups.

What ON CLUSTER does

The host that receives the statement sends it to the other hosts of the cluster as a distributed DDL query. The hosts coordinate their work through ClickHouse Keeper, under the path set by zookeeper_path in the backups server settings, and all of them write into the same destination:
  • For replicated tables (the ReplicatedMergeTree family), the replicas agree on which of them stores each data part, and a file that several replicas have is stored once. The backup also contains the parts that had not reached every replica yet.
  • For non-replicated tables (MergeTree), every host stores its own data separately, under its shard number and replica number in the cluster (shard_num and replica_num in system.clusters).
  • A table that you name in BACKUP TABLE must exist on every host of the cluster. Otherwise the statement fails with the error of the host that does not have it.
Without ON CLUSTER, BACKUP stores only what the host that runs the statement has. For a replicated table in a cluster with one shard, that is a complete copy as long as the replica is in sync. In a cluster with several shards, or for non-replicated tables, it is the data of that host only.

The destination must be shared by all hosts

Disk('backups', ...) and File(...) are resolved by each host on its own. If they point to a local directory, every host looks at a different place. The host that received the statement creates a lock file in the destination, the other hosts do not find it, and the backup fails:
Use a destination that is the same storage for every host:

Archives are not supported

A destination with an archive extension, such as .zip or .tar, is rejected:
Use a name without an extension. The backup is then stored as a directory of files. The check is made on every host that takes part in a BACKUP ... ON CLUSTER or a RESTORE ... ON CLUSTER. A single host can write and read an archive without ON CLUSTER.

Restoring with ON CLUSTER

RESTORE ... ON CLUSTER creates the tables on every host of the cluster and then restores the data:
  • Replicated tables. One replica attaches the parts from the backup, and the other replicas receive them through replication. Wait for replication before you compare the hosts, for example with SYSTEM SYNC REPLICA ON CLUSTER 'my_cluster' db.events_restored.
  • Non-replicated tables. Every host restores the data that was stored for its own shard number and replica number. A host whose table was empty when the backup was taken has an empty table after the restore. ON CLUSTER does not copy the rows of one host to the others.

Restoring without ON CLUSTER

Without ON CLUSTER, RESTORE creates and fills the table only on the host that runs the statement:
  • A replicated table whose Keeper path contains {uuid}, as the default path /clickhouse/tables/{uuid}/{shard} does, gets a new UUID and therefore a new path. The result is a separate table with one replica on that host.
  • A replicated table with a fixed Keeper path is restored with the path of the original table. Restoring it under another name on a host that still has the original fails with REPLICA_ALREADY_EXISTS. If you dropped the table on this host only, the restored table joins the remaining replicas and receives their data through replication. If they have any data, RESTORE then fails with Cannot restore the table ... because it already contains some data (CANNOT_RESTORE_TABLE). The table exists after this error, but it holds the current data of the other replicas, not the data of the backup: rows that were deleted or changed after the backup was taken are not brought back. If a new replica with the current data is what you want, restore only the definition, which gives the same table without the error:
    To get the rows of the backup, create an empty table that differs from the original only in its Keeper path (or in its engine: MergeTree instead of ReplicatedMergeTree), and restore into it:
    Do not use allow_non_empty_tables = true on the re-created replica for this. It adds the parts of the backup to the data received from the other replicas, so rows that are in both are stored twice.
  • A non-replicated table from an ON CLUSTER backup is filled with the data that was stored for the first replica of the first shard. To take the data of another host, name it with the RESTORE settings shard_num_in_backup and replica_num_in_backup: