Skip to content

Harbor Backups, Upgrade Gate and Restore Drill

A new Harbor chart is applied only after the running version has a backup that was uploaded and restored. Flux enforces this (Renovate architecture §11). The pattern is the Gitea gate's. The differences come from Harbor's state (Harbor): a bundled PostgreSQL whose image upgrades itself in place, and blobs in the S3 bucket skatzi-harbor-registry (Image storage in S3).

The gate and the drill use the backup-store secret from the backup store (Hetzner Object Storage, fsn1). The harbor and harbor-scratch namespaces carry the label backup.skatzi.com/store: enabled. harbor-preupgrade dependsOn the backup-store Kustomization, whose health check is the store's smoke Job, so the gate starts only once the store has taken a write.

Everything lives in components/harbor/backup/ and is applied by the Flux Kustomization harbor-preupgrade (cluster/hetzner-mgmt/harbor/kustomization.yaml). Every image the gate, the drill and the rollback run comes from Docker Hub (amazon/aws-cli, bitnami/kubectl, alpine/helm, python, goharbor/harbor-db, debian, rclone/rclone), none from Harbor.

The upgrade gate

The harbor Kustomization dependsOn harbor-preupgrade. That Kustomization's health check is the Job harbor/harbor-preupgrade. Flux applies a harbor change only after the Job has completed for the current Git revision.

The Job carries the target chart version in TARGET_VERSION and the annotation kustomize.toolkit.fluxcd.io/force: Enabled. A new version changes the pod template, so Flux recreates the Job and a new backup runs before the upgrade. Any other change to the pod template recreates it too: bumping an image in job.yaml (alpine/helm, amazon/aws-cli, bitnami/kubectl) leaves TARGET_VERSION the same. Nothing upgrades then, so the gate passes without a backup when Harbor already runs chart TARGET_VERSION (step 2 below), and Harbor's writers stay up (owner's decision #3453, 2026-11-22). Until then such a bump ran a full gate with the writers at 0: on 2026-11-18 Harbor's gate ran 1.19.4-to-1.19.4 three times (incident report). The drill CronJob runs the same three images, and Renovate bumps them in the same pull request, so the next weekly drill is their first run. The images in templates/ (python, debian, rclone/rclone) are in the harbor-backup-scripts ConfigMap, whose name is stable, so bumping them leaves the Job unchanged. Harbor's images follow the chart's appVersion (the values pin no tag), so the chart version is the one version that moves. TARGET_VERSION must equal chart.spec.version in components/harbor/base/helmrelease.yaml. The Job carries # renovate: datasource=helm --kube-context admin@mgmt-cluster depName=harbor registryUrl=https://helm.goharbor.io, so Renovate's harbor group bumps it with the chart in one PR.

The Job runs these steps (scripts/preupgrade.sh):

  1. Preflight: checks that the store answers. If it does not, the Job stops here, before any downtime.
  2. Nothing to upgrade? When the running chart version (the HelmRelease's status.history[0].chartVersion) equals TARGET_VERSION, the gate logs Harbor already runs chart <version>: nothing upgrades, nothing to back up and passes: it stops nothing and writes no latest.env, so publish has nothing to publish. The annotation platform.skatzi.com/preupgrade-force on deployment/harbor-core overrides this (see Forcing a backup).
  3. Record: the running chart version (the HelmRelease's status.history[0].chartVersion), the harbor-database image, the HelmRelease's spec.values (values.json), the replica counts, and the registry's bucket, endpoint, region and root directory from persistence.imageChartStorage (registry_storage in scripts/lib.sh). Unless the storage type is s3 with a bucket, the Job stops here, before any downtime.
  4. Stop writes: scales harbor-core, harbor-jobservice and harbor-registry to 0. Core takes pushes and deletes, jobservice runs garbage collection, retention and replication, and the registry writes the blobs to the bucket. Image pulls from Harbor fail until step 6. Harbor's read-only mode was not used: it is stored in the database, so the backup, and a rollback restored from it, would come back read-only.
  5. Dump pod (harbor-preupgrade-dump), with the running harbor-db image, so pg_dump matches the server, rclone for the bucket, and debian for the tar (harbor-db has no tar):

    • pg_dump --format=custom of the registry database, the project, repository and artifact counts, and the smallest single-image artifact, which the smoke check pulls (SMOKE_REPOSITORY, SMOKE_DIGEST);
    • registry-copy (scripts/registry-s3.sh fetch): rclone copy of <rootdirectory>/docker/ (registry/docker/) from the bucket to the work volume, with the harbor-registry-s3 keys. Unfinished uploads (_uploads/) are skipped. A bucket that can't be read fails the pod; an empty prefix (nothing pushed yet) gives an empty tar;
    • a tar of that copy, in the filesystem driver's layout (docker/registry/v2/...), and the blob count. tar --remove-files deletes each file once archived, so the volume holds the blob store about once. The backup format is the one the volume-based gate wrote, so older backups restore the same way.

    The files go to a temporary 110 GiB volume, harbor-preupgrade-work. With the 100 Gi scratch registry volume below, that bounds the blob store the gate can back up to about 100 GiB. When the bucket grows past it, the gate fails closed (no space left) and KubePersistentVolumeFillingUp fires on harbor-preupgrade-work; raise both volumes, the restore and rollback pods' work volumes (the rollback pod holds the tar and the extracted blob store, so twice the bound) and the time limits before that. The bucket's size is hetzner_object_storage_version_bytes{bucket="skatzi-harbor-registry"} (Image storage in S3).

    The bound was 20 GiB until 2026-11-17, when the bucket outgrew the 30 GiB work volume (incident report). The copy ran at about 62 MiB/s (29.3 GiB in 8 minutes), and Harbor's writers stay at 0 for all of it, so the downtime of step 4 grows with the bucket: about half an hour for the copy alone at 100 GiB. The tar of the copy takes about as long again: the re-run of 2026-11-17 (#3314) copied 32.0 GiB in 7m39s, then tarred it in about 10 minutes, and the writers were at 0 for 18m50s. The dump pod may take 2 hours. Tag retention never deletes an immutable tag, and the release tags (*.*.*) of the webshop images and charts are immutable, so the bucket grows with every release until those tags are pruned.

    The weekly prune bounds the copy time. On 2026-11-18 the owner chose to bound the gate's downtime by pruning the webshop release tags that nothing has pinned for a set time (#3316), over copying the bucket incrementally before the writers stop or accepting the growing downtime. On 2026-11-19 the owner made the prune weekly (Mondays 08:00 Europe/Copenhagen) with a 30-day window instead of monthly with 90 days (#3437): the bucket grows about 2 GiB a day and would otherwise reach the 100 GiB bound around mid-December. The list is made weekly; approving and deleting the tags stay manual: see Tag retention. The 100 GiB bound stays. The warning HarborBucketNearUpgradeGateBound fires when the bucket passes 80 GiB, so the volumes are raised or the tags pruned before a gate fails. 6. Resume: scales the three back. Downtime lasts only for the dump, now including the download of the bucket. 7. Upload pod: uploads registry.pgdump, registry.tar and metadata.env (versions, counts, smoke artifact, SHA-256) to s3://skatzi-backups/<cluster>/harbor/preupgrade/<timestamp>-<chart>-to-<target>/, as 1 GiB parts with Content-MD5, as the Gitea gate does. metadata.env goes last and marks a complete upload. 8. Verify: runs the restore check on that prefix. 9. Publish: uploads values.json next to the backup and writes <cluster>/harbor/latest.env (prefix, chart version, database image, target, smoke artifact), so it points only at verified backups. Then it prunes older gate backups.

Time limits

Each step of the gate waits a bounded time: 600 s for the writers to stop, 7200 s each for the dump and upload pods, and 11700 s for the restore check (900 s each for the scratch database, registry and core, 7200 s for the restore pod, 1800 s for the smoke pod). The Job's activeDeadlineSeconds (8h) is longer than their sum (26700 s), so a slow step fails with its own message and the trap below runs, and harbor-preupgrade's timeout (8h5m) outlasts the Job. At the 100 GiB bound, the 7200 s waits allow about 15 MiB/s for the upload and about 30 MiB/s for the restore pod's download plus extraction, against the 62 MiB/s the copy from the bucket ran at. When the bound grows, raise these waits first, then the deadline and the timeout above their sum. The drill has its own deadline.

Whether a step succeeds or fails, a trap scales the three back, deletes the pods and the work volume, and removes the scratch copy. Each pod's log is printed in the Job's log.

On failure the Job fails (backoffLimit: 0), harbor-preupgrade is not Ready, and harbor stays on the old version. The Flux alert taskbase-platform turns the failed Kustomization into a Taskbase task. To retry after a fix, delete the Job, and Flux recreates it:

kubectl --context admin@mgmt-cluster -n harbor logs job/harbor-preupgrade --all-containers
kubectl --context admin@mgmt-cluster -n harbor delete job harbor-preupgrade
flux --context admin@mgmt-cluster reconcile kustomization harbor-preupgrade

The gate runs whenever the Job is (re)created, not only for version changes: on first deploy and after a manual delete too. It takes a backup only when Harbor runs a chart other than TARGET_VERSION, or when the backup is forced. With no deployment/harbor-core (a fresh cluster), it passes without a backup.

Forcing a backup of the running chart

To back up the chart Harbor already runs, for a first backup on a new cluster (see the drill) or a rehearsal of the rollback, annotate deployment/harbor-core, then rerun the gate:

kubectl --context admin@mgmt-cluster -n harbor annotate deployment harbor-core platform.skatzi.com/preupgrade-force=true
kubectl --context admin@mgmt-cluster -n harbor delete job harbor-preupgrade
flux --context admin@mgmt-cluster reconcile kustomization harbor-preupgrade

The gate logs Forced by the annotation … and runs every step, the S3 check included. Once the backup is verified it removes the annotation (the Role harbor-preupgrade may patch deployment/harbor-core for this), so the next gate on the same chart passes without a backup again. A failed gate leaves the annotation, so a retry is forced too. Helm leaves an annotation it did not set in place across upgrades, and an annotation on the Deployment's metadata starts no rollout.

The restore check

The gate and the drill share one restore check (restore_check in scripts/lib.sh). It starts the same chart version with the same values in the namespace harbor-scratch, restores into it and runs the smoke check:

  1. Removes any earlier scratch copy, then creates the volumes harbor-database-pvc (10 Gi) and harbor-registry-pvc (100 Gi) in harbor-scratch, under the claim names the values use: production's for the database, scratch-values.yaml's for the registry.
  2. Renders the chart with helm template from https://helm.goharbor.io, with values.json and scratch-values.yaml laid over it, and applies it. scratch-values.yaml starts core and the registry at 0, so core never runs on an empty database. It never starts jobservice, which would run production's replication, retention, garbage collection and webhooks from the copy. It sets the registry's storage to filesystem on the scratch harbor-registry-pvc and clears s3.existingSecret, so the copy never reads or writes production's bucket. The chart mounts that secret whatever the storage type (templates/registry/registry-dpl.yaml), and harbor-registry-s3 does not exist in harbor-scratch. The override applies to values from before the move to S3 too. It also turns the metrics exporter off. tests/test-scratch-values.sh renders the chart with hetzner-mgmt's values and scratch-values.yaml and fails if the copy names production's bucket, endpoint or harbor-registry-s3, or stores anywhere but the scratch harbor-registry-pvc; ci-component-tests.yaml runs it on every pull request. It tries the chart download 3 times, because helm.goharbor.io sometimes drops the connection.
  3. Once harbor-database is ready, the restore pod (templates/restore-pod.yaml) runs:
    • fetch: downloads the backup and checks both SHA-256 sums;
    • restore-db: keeps the copy's fresh registry database as registry_replaced_<stamp>, creates an empty one, runs pg_restore and checks the project, repository and artifact counts against the dump's;
    • restore-files (debian): extracts the blobs onto the registry volume, checks the blob count, and checks the SHA-256 of 10 random blobs against the digest in their path.
  4. Scales the copy's registry and core to 1 and waits for them.
  5. The smoke pod runs smoke.py check against http://harbor.harbor-scratch.svc. GET /api/v2.0/health must report every component healthy except jobservice. The smoke artifact must then pull the way a client pulls it: a token from /service/token as admin, then the manifest and every blob it names, each checked against its digest. The admin password comes from harbor-admin-secret, which the ExternalSecret in harbor-scratch takes from the same OpenBao path as production. The restored copy holds production's users, so it must still match the admin password in Harbor's database.

The copy is removed at the end (scratch_clean: the chart's objects by the label release=harbor, the restore pods and volumes by app.kubernetes.io/name=harbor-restore). The gate and the drill use the same names. If one starts while the other's check runs, the later one removes the earlier copy and the earlier check fails. The drill runs on Mondays at 04:00 UTC, so this is rare. A failed gate is retried by deleting its Job, as above.

The chart's random secrets (core, jobservice, registry) differ in the copy, which does not matter. The values set no secretKey, so the copy uses the chart's default, as production does, and can read the fields Harbor encrypts in the database. If the values ever set existingSecretSecretKey or another existing secret, that secret has to exist in harbor-scratch too.

Tested in CI

On every pull request, the harbor-backup-files-test job of .gitea/workflows/ci-component-tests.yaml runs components/harbor/backup/tests/test-files.sh inside the debian image: dump-files.sh then restore-files.sh as uid 10000 on a fake blob store of three blobs, then again with one blob corrupted, which restore-files.sh must fail. CI jobs can't reach the runner's Docker daemon, so the image runs as a docker:// step, whose tag is written in the workflow. The job's first step fails unless that tag is the image of dump-pod.yaml, restore-pod.yaml and rollback-pod.yaml. So a bump of debian in the templates fails CI until the workflow names the same tag, and is then tested in it.

Weekly restore drill

The CronJob harbor-scratch/harbor-restore-drill runs on Mondays at 04:00 UTC. It reads latest.env and the backup's values.json, runs the restore check with the chart version and database image recorded there, and on success writes <cluster>/harbor/drill/last-success.env (pod name, time, prefix, chart version). Its Job's activeDeadlineSeconds (4h) is longer than the restore check's waits (11700 s, Time limits).

PR evidence for a Harbor upgrade cites the last passing drill:

kubectl --context admin@mgmt-cluster -n harbor-scratch get cronjob harbor-restore-drill -o jsonpath='{.status.lastSuccessfulTime}'
kubectl --context admin@mgmt-cluster -n harbor-scratch get jobs -l batch.kubernetes.io/cronjob-name=harbor-restore-drill   # Complete = passed
aws s3 cp "s3://skatzi-backups/hetzner-mgmt/harbor/drill/last-success.env" -                # with the backup-store keys

To run a drill now: kubectl --context admin@mgmt-cluster -n harbor-scratch create job --from=cronjob/harbor-restore-drill harbor-restore-drill-manual.

The drill fails until a gate run has published <cluster>/harbor/latest.env. drill.sh sources it, and nothing else writes it. On a new cluster, or after a gate that passed without a backup (a fresh install), take the first backup by forcing a gate run on the chart already running, then run a drill:

kubectl --context admin@mgmt-cluster -n harbor annotate deployment harbor-core platform.skatzi.com/preupgrade-force=true
kubectl --context admin@mgmt-cluster -n harbor delete job harbor-preupgrade     # Flux recreates it with the current TARGET_VERSION
flux --context admin@mgmt-cluster reconcile kustomization harbor-preupgrade
kubectl --context admin@mgmt-cluster -n harbor get job harbor-preupgrade        # wait for Complete, then:
kubectl --context admin@mgmt-cluster -n harbor-scratch create job --from=cronjob/harbor-restore-drill harbor-restore-drill-manual

Image pulls from Harbor fail while the gate dumps, so do it when no CI is in flight. On hetzner-mgmt the first backup was taken this way on 2026-10-25 on chart 1.18.1, and the drill passed (#1623). The gate run of 2026-10-21 had passed without a backup: it ran the script from before the move to S3.

A failed drill shows as a failed Job. As for Gitea, KubeJobFailed sends it to Mattermost and opens a Taskbase incident (Backups: failed drills). Renovate holds no Harbor PR on the Dependency Dashboard: the backup gate guards the upgrade (Renovate architecture).

Retention

As for Gitea: the gate keeps every backup younger than 30 days, and always the newest three. <cluster>/harbor/ must not be added to backup_expiring_prefixes: an expiry would remove the backup that latest.env and the drill depend on when no upgrade has run for 30 days (Backups: protection and retention).

Manual restore

After a failed upgrade, the rollback Job does this on its own. These steps are its manual fallback. The aws commands need the store's endpoint and path-style addressing: source components/harbor/backup/scripts/s3.sh first, as the Jobs do. On hetzner-mgmt, with PREFIX, CHART_VERSION and DB_IMAGE taken from latest.env or the gate's log:

  1. Hold Flux and stop the writers.

    flux --context admin@mgmt-cluster suspend kustomization harbor
    flux --context admin@mgmt-cluster suspend helmrelease harbor -n harbor
    
  2. Run the chart the backup was taken on, with nothing that writes. The database must run DB_IMAGE before the restore: a newer harbor-db has upgraded the data directory in place, and the older image starts an empty cluster next to it.

    kubectl --context admin@mgmt-cluster -n harbor patch helmrelease harbor --type merge -p \
      '{"spec":{"suspend":false,"chart":{"spec":{"version":"<CHART_VERSION>"}},"values":{"core":{"replicas":0},"jobservice":{"replicas":0},"registry":{"replicas":0}}}}'
    kubectl --context admin@mgmt-cluster -n harbor get statefulset harbor-database -o jsonpath='{.spec.template.spec.containers[0].image}'   # must be DB_IMAGE
    
  3. Restore the database. From a pod in harbor with DB_IMAGE and the backup-store secret, join the registry.pgdump.part-* objects and check the result against PGDUMP_SHA256 in metadata.env, then:

    export PGHOST=harbor-database PGUSER=postgres PGPASSWORD=<POSTGRES_PASSWORD from secret harbor-database>
    psql -d postgres -c 'ALTER DATABASE registry RENAME TO registry_replaced_<stamp>' -c "CREATE DATABASE registry ENCODING 'UTF8'"
    pg_restore --no-owner --no-acl --exit-on-error -d registry registry.pgdump
    
  4. Restore the blobs into the bucket. In a pod in harbor with the rclone/rclone image, envFrom the secret harbor-registry-s3 and a volume big enough for the tar and its contents, join registry.tar.part-*, check it against REGISTRY_SHA256, and run tar -C /work/storage -xf registry.tar. Then, with BUCKET, ENDPOINT, REGION, ROOT (the HelmRelease's persistence.imageChartStorage.s3 values) and STAMP set, run sh registry-s3.sh restore from components/harbor/backup/scripts/. It syncs /work/storage/docker into <bucket><ROOT>/docker and moves every object it replaces or removes to rollback-aside/<STAMP>/docker in the same bucket; do not delete that until Harbor is back.

  5. Start Harbor and pin the version in Git: remove the three replicas overrides (kubectl --context admin@mgmt-cluster -n harbor patch helmrelease harbor --type merge -p '{"spec":{"values":{"core":{"replicas":1},"jobservice":{"replicas":null},"registry":{"replicas":1}}}}'), set chart.spec.version and both TARGET_VERSIONs to CHART_VERSION in Git (reverting the upgrade commit does it), then flux --context admin@mgmt-cluster resume kustomization harbor.