Skip to content

Backup: the operator runs backups and restores itself through SQL on clickhouse-backup sidecars, multi-period schedules - #2148

Open
Slach wants to merge 1 commit into
0.28.0from
backup-config-file
Open

Slach wants to merge 1 commit into
0.28.0from
backup-config-file

Conversation

@Slach

@Slach Slach commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

fix #2146

What changes

The operator itself runs backups and restores. There is no orchestrator script, no Job, no CronJob and no backup Pod: the operator drives the clickhouse-backup sidecar of every host through SQL on the integration tables of that host's ClickHouse, as the Altinity usual backup workflow does.

  • Transport: sidecar only. Every Pod of a backup-enabled cluster gets a clickhouse-backup server sidecar. Its configuration, generated by the operator, always turns on api.create_integration_tables, so system.backup_actions and system.backup_list read http://127.0.0.1:7171. The sidecar runs with TZ=UTC, and restore_distributed_cluster defaults to {cluster}. backup.mode and pod mode are removed.

  • Credentials. The operator connects to each host by its FQDN, port, scheme and the cluster TLS, with the ClickHouse credentials of its own configuration (clickhouse.access or its Secret). They must be set explicitly: the built-in defaults do not count, and without them a backup or restore waits with reason NoClickHouseAccess.

  • Commands. A command is a row inserted into system.backup_actions of the host after now('UTC') of that host (async_insert = 0, http_receive_timeout/http_send_timeout from httpReceiveTimeout), and polled every 5 seconds, matched by its exact command text and start time, until it finishes. A busy sidecar (another operation is currently running) is retried with exponential backoff from 5 seconds up to 5 minutes. After an operator restart the leader resumes every run from its status: a running command is followed, a finished one is settled.

  • Timeouts. spec.timeouts of a ClickHouseBackup, ClickHouseRestore and ClickHouseBackupSchedule, defaults in the operator configuration backup.timeouts and in code, as Go durations: actionTimeout (12h), unreachableTimeout (10m), httpReceiveTimeout (4h), errorResponsesMax (5). Errors and empty polls are counted apart; the logs carry the response code.

  • Backup. create_remote --rbac on one replica per shard (replicaSelection: FirstPerShard, preferredReplica, fallback to another replica reported by OrchestratorReplicaFallback) or on every replica (AllReplicas). A shard is backed up once any of its replicas made the backup. A failed backup deletes only its local copy, once, and reports a remote leftover (OrchestratorFailedBackupLeft, OrchestratorFailedBackupRemoteLeft) with the SQL to delete it by hand.

  • Storage paths. Before any command, path and object_disks_path of every writer of a backup, with the macros of each host expanded, must be unique (AllReplicas: every replica; embedded backups and CAS disks included). Otherwise the backup or schedule is Aborted with StoragePathConflict, e.g. storage path 's3://backups/backup/main/0' is not unique: replicas ... both write a backup of one name there. Unknown macros give an OrchestratorStoragePathAdvice warning.

  • Backup names. spec.backup.naming / clusters[].backup.naming, a template such as shard{shardNum}-{YYYYMMDDHHIISS} with {shard} (the shard macro), {shardNum} (shard_num, from 1), {cluster}, {chi}, {namespace}, {prefix} and the time tokens. Retention, the incremental base and restore parse names back with it, so backups named by the Altinity usual backup workflow on a shared remote path are recognized. Empty keeps the current names.

  • Schedules. ClickHouseBackupSchedule.spec.schedules[] (minute, hour, 6hours, daily, weekly, monthly, with time, day, keep, incremental, configOverrides) and spec.timeZone:

    • the operator runs the slots of the finest entry itself; the heaviest matching entry decides full vs incremental;
    • no CronJob, Job or ClickHouseBackup is created per run: a run lives in status.active while it runs and in status.recentRuns afterwards, bounded by successfulRunsHistoryLimit (3) and failedRunsHistoryLimit (1); startingDeadlineSeconds skips a slot missed by more; runs of one cluster never overlap;
    • a ClickHouseBackup with scheduleRef runs the schedule now: its incremental entry, retention after it, a base of the next incremental backup.
  • Retention follows the Altinity usual backup workflow: each entry keeps its own keep newest backups (GFS); the newest backup is always kept; run-now backups live within the window of the scheduled ones; an incomplete backup is deleted only once a newer complete one exists and system.backup_actions shows no create_remote or upload of it on its shard; nothing is deleted during a restore; incremental bases are rebased by clickhouse-backup (forced backups_to_keep_remote: 0, rebase_*: true).

  • Schedule changes. spec.retention.onScheduleChange: Hold (default) records a change of the entries or the time zone in status.retention.previous[] with its expires. The backups the schedule kept before it and the current one would delete are held, listed in status.clusters[].heldBackups with expires: backup time + keep × the period of the longest old entry that kept it. Expired holds are deleted (OrchestratorRetentionHeldExpired), an expired change leaves previous[] (RetentionHeldExpired).

    • keepBackups: [{name, until}] pins backups: with until until then; without it a backup held by a change until its expiry, any other backup while it is listed. Pins protect incomplete backups too.
    • releaseHeld: <changeID> releases a change and all earlier ones, Prune applies a change at once.
  • Config overrides. schedules[].configOverrides and ClickHouseBackup.spec.configOverrides: up to 16 KEY=VALUE clickhouse-backup settings for one backup, sent as create_remote --env=.... Keys the operator pins (TZ included), API_*, paths, buckets, endpoints, the embedded backup disk and secrets are refused.

  • Restore, as in the Altinity usual backup workflow:

    1. download --hardlink-exists-files on every host (an incomplete local copy is resumed);
    2. restore --schema=1 --ignore-dependencies on every replica, the first replica of the first shard with RESTORE_SCHEMA_ON_CLUSTER={cluster}; skipped for partitions;
    3. spec.replicatedCopyToDetached (default true): the other replicas of each shard run restore --data=1 --ignore-dependencies --replicated-copy-to-detached at the same time, so the parts of replicated tables are already local; with false they download only the schema;
    4. restore --data=1 --ignore-dependencies on one replica per shard, the next answering replica when the first does not;
    5. delete local on every host after success; a failed restore keeps its downloads.

    Filters go on every command: tables, partitions, skipTables, skipTableEngines, databaseMapping, rbac. overwrite: true without tables adds --rm, which recreates the databases of the backup as a whole; with tables, or without overwrite, only the tables of the backup are recreated (clickhouse-backup drops the existing ones) and no database is dropped. A restore of access entities holds the backups and schedules of its own clusters only; restores of different clusters run at the same time.

  • Reruns: spec.taskID on all three kinds, with status.taskID, taskIDsStarted, taskIDsCompleted. A completed backup runs again under a new name (the old one in status.previousBackupNames), an unfinished one under its own name after its earlier copies are deleted. A failed restore runs again: spec.resumeDownload (default true) resumes its earlier downloads, false deletes them and starts over. An aborted schedule is validated again. A taskID set while a run is active is ignored with a TaskIDIgnored warning.

  • Deletion. Finalizers on ClickHouseBackup, ClickHouseRestore and ClickHouseBackupSchedule. Deleting an unfinished backup kills only its own running commands (kill "<command>") and deletes its local and remote copies; a completed one keeps its remote backup for retention. A restore and a schedule run are waited for, and a deleted schedule starts no new slot. Unreachable hosts are given up after unreachableTimeout, with a DeletionLeftover warning and the SQL to finish by hand.

  • Validation. Backup enabled without configSecretRef, a missing or invalid storage Secret, or an invalid backup block stops the CHI reconcile: status Aborted, reason BackupConfigInvalid, Warning event BackupStorageConfigInvalid.

  • Status and events. status.clusters[].run of a ClickHouseBackup / ClickHouseRestore records the step, the hosts and every command sent with its status. Phases Pending, Running, Completed, Failed, Aborted. The operator posts every event itself (Orchestrator* per step and command) on the ClickHouseBackup, ClickHouseBackupSchedule and ClickHouseRestore.

  • Removed: backup.mode and pod mode, backup.orchestrator, the bash and Python orchestrator scripts and their ConfigMaps, orchestratorConfigMapRef, backup.path of the operator configuration, jobName / cronJobName, concurrencyPolicy, backup verification (spec.verify, the Verified condition and its metric), the restore overwrite guard, and the operator RBAC rights for Jobs, CronJobs, Pod writes, ServiceAccounts, Roles and RoleBindings. The operator gains update/patch on its three backup kinds for the finalizers.

  • Manifests are regenerated with the current yq, which writes plain <<: merge keys in place of !!merge <<:; the documents are the same.

  • Docs: docs/backup.md (how the operator runs an operation, naming, timeouts, storage paths, migration from the Altinity usual backup workflow, schedule changes, reruns, deletion, config overrides, integration tables), docs/custom_resource_explained.md, docs/operator_configuration.md, docs/chb-examples/.

Tests

  • go build ./... && go test ./..., -race for pkg/controller/chbackup and pkg/model/chbackup: runs against fake sidecars (command text, polling, timeouts, resume after restart, busy backoff, replica fallback, copy to detached, finalizers, taskID reruns, path uniqueness, per-cluster gates), retention plans, schedule changes and their expiry, naming, slots, incremental base.
  • helm template of the regenerated chart.
  • Native e2e on orbstack:
    • test_010087: backup, schedule, retention and restore of two clusters through real sidecars;
    • test_010088: recovery of a new CHI from another one's backup;
    • test_010089: retention across a schedule change: held backups expiring by the old schedule, keepBackups pins without until, with until in the past and in the future.

Related, in clickhouse-backup:

deploy/operatorhub is not regenerated (done at release). dev/run_code_generator.sh does not build with the current Go (#2152); deepcopy was generated with deepcopy-gen v0.34.1.

🤖 Generated with Claude Code

@Slach
Slach force-pushed the backup-config-file branch from d0d0ec5 to d81d518 Compare November 12, 2026 10:51
@Slach Slach changed the title Backup: sidecar or per-shard Pod transport, multi-period schedules, orchestrator script as operator config Backup: sidecar or per-shard Pod transport, multi-period schedules, bash or Python orchestrator as operator config Nov 13, 2026
@Slach
Slach force-pushed the backup-config-file branch from ddc3980 to c3e4b29 Compare November 16, 2026 14:54
@Slach Slach changed the title Backup: sidecar or per-shard Pod transport, multi-period schedules, bash or Python orchestrator as operator config Backup: the operator runs backups and restores itself through SQL on clickhouse-backup sidecars, multi-period schedules Nov 17, 2026
@Slach
Slach force-pushed the backup-config-file branch from c3e4b29 to 3c76ca0 Compare November 17, 2026 13:26
@Slach
Slach force-pushed the backup-config-file branch from eaf9557 to a9e00ff Compare November 18, 2026 10:07
@alex-zaitsev

Copy link
Copy Markdown
Member

@Slach , for consistency sake, could you change the secret reference to valueFrom syntax? Like:

backup:
  enabled: "yes"
  storageConfig:
    valueFrom:
      secretKeyRef:
        name: backup-storage
        key: config.yml

It has been missed in secrets review.

@denisok

denisok commented Nov 19, 2026

Copy link
Copy Markdown

claude AI review, maybe slop but JFYI:

Blockers

1. Deleting a failed backup CR can delete someone else's backup. A ClickHouseBackup refused in preflight with "backup X already exists in the storage" never sent a command. When that CR is deleted, the finalizer still sends delete remote X on every host and removes the pre-existing backup.
   - unfinishedHosts (pkg/controller/chbackup/finalizer.go:220) falls back to cluster.Hosts whenever a run exists but recorded no hosts, which is exactly the preflight-failure case.
   - The docs promise deletion only on "every host it sent a command to".
   - Confirmed with a repro test, kept outside the repo at /tmp/claude-1000/-home-stdden-Workspace-clickhouse-operator/d0327138-a99a-438b-adb1-d10ccc1ed6b9/scratchpad/review_repro_refused_backup_delete_test.go. Copied into pkg/controller/chbackup/, it fails with delete remote manual sent to both hosts.
2. Losing the leader lease disables backup until the pod restarts. The backup manager is now leader-elected (cmd/operator/app/thread_backup.go:153). On a lost lease, controller-runtime returns "leader election lost" from Start and expects the process to exit.
   - launchBackup (cmd/operator/app/main.go:182) only logs "Starting backup FAILED" and returns, while the CHI controller keeps the pod alive.
   - With one replica, a brief API-server outage (the default renew deadline is 10s) is enough to trigger it.
   - Afterwards no backup, restore or schedule runs, in-flight runs stay Running forever, and CRs with finalizers hang in Terminating.
   - Confirmed by reading both sides; not reproduced live.

Should fix

3. DST gap slots match no schedule entry (reviewer, confirmed by test). A slot at a wall time that does not exist is shifted by Go, so its tag matches no entry and the next run's retention deletes that backup. Example: daily plus weekly Sunday 02:30 in Europe/Berlin on 2026-03-29. See pkg/model/chbackup/schedule.go:125.
4. Adding a shard wipes backup history at the next scheduled run. Backups made before the shard existed are not "complete on every shard", so retention deletes all of them older than the newest one, whatever keep says (retention.go:223, backup_orchestrator_backup.go:476). The docs describe this as intended; I think it is too destructive for a routine scale-out.
5. Storage path check has false negatives (reviewer, confirmed by test). The storage identity is compared as a raw string, so https://minio:9000 and https://minio:9000/, or an explicit versus unset endpoint, count as different storages. Two clusters can then share a path and prune each other's backups (pathcheck.go:371). A storage path is also never compared with an object disk path.
6. Aborted CHI is not retried after the storage Secret is fixed (reviewer, confirmed by test). isAbortedOnInvalidBackupConfig (pkg/controller/chi/controller-backup-config.go:430) checks only status.errors[0], and the "FAILED to reconcile" line is prepended above the tagged error. This happens with the shipped status.fields.errors: true.
7. Generated Secret name collides and is overwritten without an ownership check (reviewer, confirmed by test). CHI a-b with cluster c and CHI a with cluster b-c both map to a-b-c-backup-config. A user Secret with that name has its data replaced. The apply function already exists on origin/0.28.0, so this may predate these commits.
8. Storage Secret content leaks through YAML errors (reviewer, confirmed by test). A decode error quotes up to 10 characters of the offending value into CHI status, an Event and the log (backupconfig.go:154).
9. mutated is not set when a resumed run adopts a command. In submit (backup_orchestrator.go:390) the adopt branch returns before Mutated is set. A restore that restarted between the insert and the status write can then fail as FailedUnchanged, which releases the schedule hold a changed cluster should keep. The window is narrow.

Lower priority

- Config overrides use a denylist. S3_ACL=public-read and S3_DISABLE_CERT_VERIFICATION=true pass validation (type_backup_overrides.go:49), so anyone who can create a CHB can set them. An allowlist would be safer.
- Leases RBAC is cluster-wide (reviewer). In the ClusterRole variants, create/get/update on all Leases is granted where one named Lease is needed.
- Data-restore fallback may double-apply. If the submit on the first replica fails after the command reached the sidecar, runData hands the same restore to the next replica (backup_orchestrator_restore.go:287). Plausible only.
- Fall-back hour name collision (reviewer). Hourly and minute entries produce the same backup name twice, so the second run fails with "already exists".
- Unbounded deadline seconds (reviewer, plausible). activeDeadlineSeconds and the schedule's startingDeadlineSeconds have no upper bound and overflow time.Duration above about 9.2e9.
- Nits.
  - In runs.go:268 the final notification fires before done closes.
  - A reconcile can start a run before the registry has the manager's context.
  - The comment on parseClickHouseTime names the wrong function.
  - The comment at type_backup.go:65 is stale.

@denisok

denisok commented Nov 19, 2026 •

Copy link
Copy Markdown

API:

  • type. Lets do it explicit and required? type: full, type:incremental. That way ppl wouldn’t be confused.
  • period  maybe hourly . Just adding every:X give us hourly (if absent) or 6 or what ever
  • day : weekly: 0..6, 0 being Sunday; monthly: 1..31. This doesn’t look good. Day like Mon, Tue and etc is better. For month backup it is weird, don’t know yet (first, last maybe)…
  • keep is tight to schedule. Currently retention checks backup times against it’s entries as I understand. Logically separate it will be better, so Retention looks on it’s configuration and existing backups instead of slots:
retention:
  keepLast: 3
  keepHourly: 24
  keepDaily: 7
  keepWeekly: 4
  keepMonthly: 6

also slot matching is fragile because of DST time changes.

Missed runs. Say operator was down, when backup will be done? another point to the bucketed retention above is better control that nothing will be deleted if there was a miss.

types:

  • tables and skipTables should be []string.
  • configOverrides should be map?

Jitter, we start backup on all replicas without delay now as I understand?

One-off backups, no pruning strategy for them? ttl or some keep until?

  schedules:
    - period: hourly
      every:1
      type: incremental
    - period: daily
      time: "02:30"
      type: incremental
    - period: weekly
      time: "02:30"
      day: Monday
      type: full
    - period: monthly
      time: "02:30"
      day: first #or last
      type: full
  retention:
    keepLast: 3
    keepHourly: 24
    keepDaily: 7
    keepWeekly: 4
    keepMonthly: 6

@Slach

Slach commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator Author

@denisok thanks for review

Thanks, I went through both comments point by point against the code.

AI review

Real, will be fixed before merge:

  • Finalizer can delete someone else's backup. A backup refused in preflight ("already exists in the storage") records no hosts, so unfinishedHosts falls back to every host of the cluster and delete remote removes the pre-existing backup. It will delete only on hosts a command was actually sent to.
  • Lost leader lease disables backups until the pod restarts. launchBackup only logs the error. It will re-enter leader election with backoff.
  • DST. A spring-forward daily slot (02:30 → named …033000) matches no entry and is pruned by the next run; a fall-back hour gives two slots one name and the second run fails with "already exists". Slot classification becomes zone-aware, and a repeated wall-clock slot runs once.
  • Generated <chi>-<cluster>-backup-config Secret name collision (a-b/c vs a/b-c), plus an ownership check before overwriting.
  • Storage Secret content leaking through YAML decode errors.
  • Mutated not set when a resumed run adopts a command.
  • Overflow of unbounded activeDeadlineSeconds / startingDeadlineSeconds (schema maximum added).
  • Cluster-wide leases RBAC → namespaced Role only.
  • Storage path check: endpoint normalization (https://minio:9000 vs https://minio:9000/).
  • configOverrides: the denylist gets S3_ACL, S3_DISABLE_CERT_VERIFICATION and their GCS/Azure/COS/FTP/SFTP analogues.
  • The nits.

Not a bug:

  • Aborted CHI after the Secret is fixed. An aborted CHI skips reconcile, so the [BackupConfigInvalid] marker stays first in status.errors, and the config sync re-enqueues the CHI once the Secret is usable.
  • Adding a shard prunes older backups. This is intended and documented: those backups cannot restore the new shard. It needs a newer complete backup first, and retention.keepBackups pins protect them. I'll make the warning more prominent and add a test.

API

Changing now, while unreleased:

  • tables / skipTables become []string, rendered comma-joined like partitions already is.
  • skipTables and skipTableEngines ([]string) on backups and schedules, so "skip Kafka" no longer needs configOverrides. The same key in configOverrides is refused.

Keeping, with the reasons:

  • Per-entry keep, no keepLast/keepHourly/... block. That block is a different policy, not a safer implementation of this one:

    • it changes which backups existing users keep;
    • it loses the per-entry incremental / configOverrides and the hold on schedule change;
    • it does not fix the DST name collision, which happens before retention runs.

    The DST problems are fixed in slot matching instead.

  • No every: X. {period: hour, incremental: true, keep: N} already means "hourly incrementals, a full one every N". The incremental chain is bounded by the entry's keep.

  • No type: full|incremental. The kind of a run is derived: the heaviest entry matching the slot decides, and an incremental run still falls back to full when the chain reaches keep or has no base. A required per-entry type would be wrong on every overlapping slot.

  • Numeric day. Weekly 0..6 with 0 = Sunday, as cron and CronJob. Monthly 1..31, where a day past the month's end means its last day, so 31 is "last" (cron cannot express that). Cloud maps MONDAY → 1 trivially. I'll document it more clearly.

  • No text schedule format (skedule-like) in the CRD. These CRs will be generated by Cloud, so a typo should fail at kubectl apply through the schema, not later as an Aborted event. A text syntax fits better one level up, in the UI/API, emitting these structured entries.

  • configOverrides stays a list. It is one --env flag per item, validated per item.

  • Missed runs. As CronJob: only the latest missed slot runs, late, within startingDeadlineSeconds. Retention is count-based over existing backups, so a miss never deletes anything earlier; it only keeps older backups longer. Will document.

  • Jitter. By default one replica per shard runs (FirstPerShard), all shards together for a consistent snapshot, as CronJob without jitter. Will document.

  • One-off backups are kept until deleted on purpose. scheduleRef (lives within the schedule's retention) and retention.keepBackups[].until cover expiry today. A TTL can come later as an opt-in.

Agreed that consistency with the Altinity usual backup workflow is the baseline, not the goal. The PR already goes beyond it, e.g. held backups survive a retention change until they expire by the old schedule, and the DST fixes above are cases the old workflow never handled.

@denisok denisok 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.

I am still thinking separating retention from backup schedule might be the right thing to do.

It makes backups more reliable so retention is actually based on time and presence and not on slots.

Also I am not sure what is wrong with literals like Monday/Tuesday/...

clickhouse-backup is current tool to do it but Interface shouldn't depend on it.

Let's discuss and come to conclusion before merging this.

@Slach
Slach force-pushed the backup-config-file branch from 46069a9 to 586c246 Compare November 23, 2026 06:51
- period: hour
keep: 24
- period: daily
time: "02:30"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why it has to start the same time ... if time has to be same globally, would it make sense to make it global instead of relying on people repeating the same value?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Entries share one slot, so one run counts for daily, weekly and monthly at once (GFS) and the weekly backup is also a daily one. Agree, a schedule-level time would be simpler than repeating it.

# - releaseHeld: releases the holds of a change, and of all earlier changes, at once.
# retention:
# onScheduleChange: Hold
# keepBackups:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

who is responsible for deleting keep records, maybe it would make sense to store the information of when it expires on the backup itself, then this wont be needed because backups will expire with the expiration time configured when they were made

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Normal expiration needs no record: retention computes it from the schedule entries on every run. keepBackups is only for manual exceptions, the user owns it. An entry whose until passed is ignored, so leaving it there is harmless.

@Slach
Slach force-pushed the backup-config-file branch from e5e8d01 to 7f94cde Compare November 23, 2026 07:47
@ondrej-smola

ondrej-smola commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

I am trying to decode if {period: daily, keep: 7, incremental: true} is supported and what happens when I configure such schedule, it looks to me full is never made after first one and we will do infinite rebase. Can you please point me to the docs where it is explained?

@ondrej-smola

Copy link
Copy Markdown
Contributor
  • also we need to make clear to explain the rebase feature to our users because even if rebase is done on remote storage we still pay for operations, so the rebase on incremental can have the same cost as full (- network costs)

@Slach

Slach commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator Author

@ondrej-smola

  1. {period: daily, keep: 7, incremental: true}: right, there is no periodic full backup. Retention rebases the oldest kept backup before it deletes its base, so the storage always has 1 full + 6 incremental backups, and the server uploads only new parts each day. This is intended (forever-incremental with a synthetic full). I fixed the docs, which said otherwise: Incremental backups.

  2. Rebase cost: clickhouse-backup rebases with server-side CopyObject per part (upload_by_part: true), no data leaves S3. For 1 TiB with ~100 MB objects in us-east-1:

    • rebase: ~10.5k COPY requests ≈ $0.05, no transfer;
    • full upload of the same 1 TiB: ~230k PUT-class requests (5 MB parts) ≈ $1.15, plus NAT/inter-region/egress if any (≈ $40-90 per TiB).

    Rebase also copies only the parts still needed, not the whole base.

@Slach

Slach commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator Author

@ondrej-smola diagrams for full vs incremental and for the rebase of a daily, keep: 7 chain: Incremental backups.

Comment thread pkg/controller/chbackup/task.go Outdated
}
run = run.DeepCopy()
for _, host := range cluster.Hosts {
run.Hosts = append(run.Hosts, api.BackupRunHost{Host: host, Backup: chb.Status.BackupName})

@ondrej-smola ondrej-smola Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should this be hostsOfInsertedCommands so we e.g. on manual duplicate backup name incorrectly attribute clsuter hosts

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Right, a delete could remove another backup of the same name. Fixed in 7513322, it uses hostsOfInsertedCommands.

spec := newRunSpec(api.BackupRunOperationRestore, topology, chr.Spec.GetActiveDeadlineSeconds())
spec.BackupName = cmp.Or(chr.Status.BackupName, chr.Spec.BackupName)
spec.SchemaHosts = topology.AllReplicas()
spec.DataHosts = topology.FirstPerShard()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

does this download data even when only schema is set?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, it did: the first replica of each shard downloaded the data. Fixed in f84123a, it downloads only the schema now.

// The T keeps a ClickHouseBackup name apart from the names of scheduled runs.
func BackupTag(t time.Time, atOnce bool) string {
if atOnce {
return t.Format("20060102T150405")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

looks like the same constant is present on multiple places in the codebase (retention, builder)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Now api.BackupTagLayout and api.BackupTagLayoutAtOnce, used everywhere. 91869f4.

Comment thread pkg/model/chbackup/builder.go Outdated
// scheduleTimestampLayout formats the time a scheduled run was due into the "-YYYYMMDD-HHMMSS"
// suffix of its backup name, as the Job script does.
scheduleTimestampLayout = "20060102-150405"
scheduleTimestampLayout = "20060102150405"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this looks like unused outside tests

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Removed in 91869f4.

// Actions keep the last command per host and step.
Actions []BackupRunAction `json:"actions,omitempty" yaml:"actions,omitempty"`
// Plan holds decisions made before anything changed, so steps after a restart follow them.
Plan map[string]string `json:"plan,omitempty" yaml:"plan,omitempty"`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why this needs to be "stringly typed"?

Comment on lines +126 to +155
type orchestrator struct {
spec *model.RunSpec
sc sidecars
// mu guards status, which the controller reads while the run goes on.
mu *sync.Mutex
status *api.BackupRunStatus
event func(eventType, reason, message string)
changed func()
logPrefix string
clock clock.WithTicker
poll time.Duration
startedAt time.Time
resumed bool
elapsed time.Duration

lastWarning string
bestEffort bool
outcome runOutcome
sourceEnv string
sourceShown string
sourceOwnPath bool
sourceHint string
sourceCopies []string
noLocalCopy map[string]bool
ownBackups map[string]map[string]bool
pathResult model.PathCheckResult
backupNames map[string]string
resumable map[string]bool
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

looks to me like many fields are just some form of scratch/strange values - makes it really hard for human to fully understand, e.g. resumable, ownBackups, noLocalCopy

return strings.HasPrefix(command, commandDeleteLocal+" ") || strings.HasPrefix(command, commandDeleteRemote+" ")
}

func (o *orchestrator) lost(ctx context.Context, action *api.BackupRunAction, name string) (bool, bool, error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this is also example where actuall documentation would really help to make it clear

Comment on lines +155 to +159
for i := 1; i < len(hosts); i++ {
if slices.Contains(hosts[:i], hosts[i]) {
return nil, failf("the backup lists %s twice: hosts of one backup must not share a storage path", hosts[i])
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

do we need this check, looks to me like dead safecode?

Comment on lines +166 to +181
case spec.BackupName != "":
key, prefix, err := naming.Key("backup name", spec.BackupName, spec.Prefix)
if err != nil {
return nil, failf("%v", err)
}
plan.key, plan.namingPrefix = key, prefix
case naming.IsTemplate():
if naming.HoldsPrefix() && (spec.Prefix == "") {
return nil, failf("the naming template '%s' holds {prefix}, which the backup name prefix gives", naming.Template)
}
plan.key, plan.namingPrefix = slotTag, spec.Prefix
case spec.Prefix != "":
plan.key = spec.Prefix + "-" + slotTag
default:
return nil, failf("the backup has no name")
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

looks like to me we have always backupname or prefix, also this code is extremely hard to reason about

…clickhouse-backup sidecars, multi-period schedules

- ClickHouseBackup, ClickHouseRestore and ClickHouseBackupSchedule resources; the operator drives every run
  through SQL on the clickhouse-backup sidecar of each host
- per-cluster backup settings in the ClickHouseInstallation: sidecar, storage config from a Secret, naming,
  preferred replica
- schedules with several periods, full or incremental backups, retention with keep counts, pins and held backups
- restore of a new installation from a backup (recovery), schema-only restore, table filters, configOverrides
- reruns through spec.taskID, starting deadlines, missed slots warnings, DST-aware slots
- status.backups lists the backups in storage
- docs, CRD descriptions and e2e tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Slach
Slach force-pushed the backup-config-file branch from b423f88 to 1a29fa0 Compare November 24, 2026 05:33

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.

Backup: operator-run backup and restore through clickhouse-backup sidecars, multi-period schedules

4 participants