Skip to content

feat: automated backups to S3 and restores - #394

Open
Tal-E wants to merge 4 commits into
ClickHouse:mainfrom
Tal-E:feat/backup-restore
Open

Tal-E wants to merge 4 commits 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


Note

High Risk
New data-plane operations (backup/restore to S3, credential handling, and cluster-wide SQL) can affect production data and availability; restore misuse or partial failures are high-impact.

Overview
Introduces backup and restore as first-class operator features via three new clickhouse.com/v1alpha1 resources: ClickHouseBackup (one-shot full or incremental backup), ClickHouseScheduledBackup (cron-driven creation of backups with optional full/incremental rotation and CR history pruning), and ClickHouseRestore (one-shot restore from a ClickHouseBackup or raw S3 path).

The API adds S3 storage (endpoint + optional Secret credentials), database/table scope, immutable specs, and shared status (Pending/Running/Completed/Failed, replica choice, system.backups operation id). RBAC, CRD manifests, Helm chart, OLM CSV, samples, and docs (guides/backup.mdx, README/AGENTS) are wired through alongside backup/restore condition and event types.

Per the architecture notes in this change, reconcilers run native BACKUP / RESTORE ... ON CLUSTER on a chosen replica and poll ClickHouse; scheduled backups use a cron library and manage baseBackupRef for incrementals.

Reviewed by Cursor Bugbot for commit 8a794c8. Bugbot is set up for automated code reviews on this repo. Configure here.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 3 potential issues.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 227ac33. Configure here.

@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

AI review gate — AI review passed

Clean review from: Cursor Bugbot.

@GrigoryPervakov GrigoryPervakov mentioned this pull request Dec 2, 2026
11 of 14 tasks

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