Skip to content

feat: automated backups to S3 and restores - #394

Open
Tal-E wants to merge 1 commit into
ClickHouse:mainfrom
Tal-E:feat/backup-restore
Open

Tal-E wants to merge 1 commit into
ClickHouse:mainfrom
Tal-E:feat/backup-restore

Conversation

@Tal-E

@Tal-E Tal-E commented Nov 12, 2026

Copy link
Copy Markdown

Why

Roadmap item 7 in #65 asks for automated backups to S3. Today the operator has no way to back up or
restore a cluster, so users run their own CronJobs or sidecars.

This PR adds backups and restores built on the native BACKUP / RESTORE statements. The data goes
from the ClickHouse pods to the storage directly: there is no sidecar, no extra pod, and the operator
needs no access to the storage.

What

Three new resources in clickhouse.com/v1alpha1. They are separate kinds, so ClickHouseCluster and
KeeperCluster are not changed.

  • ClickHouseBackup runs one backup. The controller submits
    BACKUP ... ON CLUSTER 'default' TO S3(...) SETTINGS id = '<object UID>' ASYNC to the first reachable
    replica and polls system.backups on that replica until the operation finishes.
    • spec.scope selects databases and tables. The default is ALL EXCEPT DATABASES system.
    • spec.baseBackupRef makes the backup incremental (base_backup).
    • spec.storage.s3.credentials references Secret keys. When omitted, the server credentials are used
      (IRSA, instance profile). s3_allow_server_credentials_in_user_queries is added on servers that
      know the setting.
    • The spec is immutable and a failed backup is not retried.
  • ClickHouseScheduledBackup creates ClickHouseBackup resources on a cron schedule.
    • fullBackupInterval: N takes one full backup followed by N - 1 incremental backups based on it.
    • History limits prune finished ClickHouseBackup resources and never remove the latest full backup
      or the base of a kept incremental backup.
    • A run is skipped while the previous backup is not finished.
  • ClickHouseRestore runs one RESTORE ... ON CLUSTER from a ClickHouseBackup (backupRef) or from a
    location in the storage (source), which covers restoring into a new Kubernetes cluster.

Behavior worth reviewing:

  • The object UID is used as the operation id. A repeated submission (operator restart between the
    statement and the status update) is rejected by ClickHouse with "another backup with the same ID" and
    is treated as already submitted.
  • system.backups is in memory. If the replica that executes the operation restarts, the operation is
    reported as Failed with reason StateLost. backup_log is not enabled by the operator, and enabling
    it would restart existing clusters, so it is not used.
  • Credentials are passed as statement arguments. ClickHouse masks them in logs and system tables. The
    operator never logs the statement and removes the secret from error messages before storing them.
  • Validation is done with CEL rules in the CRDs. No admission webhooks are added for the new kinds.

Not in this PR:

  • Deleting backup data from the storage. History limits remove Kubernetes resources only. Bucket
    lifecycle rules are expected to expire the data.
  • Cancelling a running operation when its resource is deleted.
  • Storage types other than S3. spec.storage is a union so that more can be added.

New dependency: github.com/robfig/cron/v3 to parse cron schedules, the parser Kubernetes CronJobs use.

Tests:

  • Unit tests for statement rendering and helpers.
  • envtest tests for the three controllers with a fake executor, including the CEL rules.
  • Integration tests in the existing commander suite that run backup, incremental backup, restore and
    a failing backup against real ClickHouse and an S3 mock (adobe/s3mock).
  • An e2e spec (Label("clickhouse", "backup")) that takes a full and an incremental backup, restores a
    dropped table, reports a failed backup and runs a schedule.

Docs: docs/guides/backup.mdx, events reference, API reference, examples/scheduled_backup_s3.yaml.

Run locally: make lint, make test, make check-crd-compat, and the new e2e spec on a kind cluster
(--ginkgo.focus="Backup controller"). The full e2e suite was not run. All runs used an S3 mock, so
the server-credentials path is not exercised against real AWS.

Related Issues

Fixes #393
Related to #65

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Automated backups to S3 and restores

1 participant