Gitea Backups, Upgrade Gate and Restore Drill¶
Gitea's database migrations run one way. A new Gitea version is applied only after the running version has a backup that was uploaded and restored. Flux enforces this (Renovate architecture §11). Gitea is the first stateful gate. Keycloak, Mattermost and Harbor copy the pattern.
The gate and the drill use the backup-store secret from the backup store (Hetzner Object Storage, fsn1). gitea-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/gitea/backup/ and is applied by the Flux Kustomization gitea-preupgrade (cluster/hetzner-mgmt/gitea/kustomization.yaml).
The upgrade gate¶
The gitea Kustomization dependsOn gitea-preupgrade. That Kustomization's health check is the Job gitea/gitea-preupgrade. Flux applies a gitea change only after the Job has completed for the current Git revision.
The Job carries the target 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. TARGET_VERSION must equal image.tag in components/gitea/base/helmrelease.yaml. Both carry # renovate: datasource=docker depName=gitea/gitea, so Renovate's gitea group bumps them in one PR.
Any other change to the Job's pod template recreates it as well, against the unchanged TARGET_VERSION. Examples are a new image of the preflight or publish container (amazon/aws-cli) or of the gate container (bitnami/kubectl), such as a Renovate digest bump. Nothing upgrades then, so the gate passes without a backup when Gitea already runs TARGET_VERSION (step 2 below): Gitea stays up, and latest.env keeps naming the backup taken before the last upgrade. Such a pull request is therefore not drain-class (owner's decision #3453, 2026-11-22). Until then it ran a full gate with Gitea down for about 10 minutes, and three such bumps on 2026-11-18 each held agent work for over an hour (incident report). The drill CronJob runs the same amazon/aws-cli and bitnami/kubectl images, and Renovate bumps them in the same pull request, so the next weekly drill is the first run of the new image, as for the images in templates/; the next Gitea upgrade is the first gate to run it. A change to scripts/ or templates/ alone does not recreate the Job: the gitea-backup-scripts ConfigMap keeps a stable name (disableNameSuffixHash in kustomization.yaml).
The Job runs these steps (scripts/preupgrade.sh):
- Preflight: checks that the store answers. If it does not, the Job stops here, before any downtime.
- Nothing to upgrade? Reads the running image. When its version (the tag without
-rootless) equalsTARGET_VERSION, the gate logsGitea already runs <version>: nothing upgrades, nothing to back upand passes: it stops nothing and writes nolatest.env, sopublishhas nothing to publish. The annotationplatform.skatzi.com/preupgrade-forceondeployment/giteaoverrides this, for a rehearsal or another backup of the running version (see Forcing a backup). - Stop writes: records the running image and replica count, then scales
deployment/giteato 0. Gitea before 1.26 has no maintenance mode. - Dump pod (
gitea-preupgrade-dump), with thegitea-shared-storagevolume mounted read-only: pg_dump --format=customofgitea-postgresql, plus the repository and user counts;gitea dump --type tar --skip-logwith the running image, becausegitea dumpreads the database with that version's models. The files go to a temporary 40 GiB volume,gitea-preupgrade-work.- Resume: scales Gitea back to its replica count. Downtime lasts only for the two dumps. Agent work is held and drained around it (Drain).
- Upload pod: uploads
gitea.pgdump,gitea-dump.tarandmetadata.env(versions, counts, SHA-256) tos3://skatzi-backups/<cluster>/gitea/preupgrade/<timestamp>-<from>-to-<target>/.metadata.envgoes last and marks a complete upload. The bucket's object lock requiresContent-MD5on every upload, soscripts/s3.shuploads each object withput-object --content-md5, and the two dumps as 1 GiB parts (gitea-dump.tar.part-0000, …), below put-object's 5 GiB limit.fetchjoins the parts again, andunpackchecks the SHA-256 of the joined file. - Verify: runs the restore check on that prefix in
gitea-scratch, with the running image. - Publish: writes
<cluster>/gitea/latest.env(prefix, version, image), so it points only at verified backups. Then it prunes older gate backups.
Whether a step succeeds or fails, a trap scales Gitea back and deletes the pods and the work volume. Each pod's log is printed in the Job's log.
On failure the Job fails (backoffLimit: 0), gitea-preupgrade is not Ready, and gitea 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 gitea logs job/gitea-preupgrade --all-containers
kubectl --context admin@mgmt-cluster -n gitea delete job gitea-preupgrade
flux --context admin@mgmt-cluster reconcile kustomization gitea-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 Gitea runs a version other than TARGET_VERSION, or when the backup is forced. With no deployment/gitea (a fresh cluster), it passes without a backup.
Forcing a backup of the running version¶
To back up the version Gitea already runs, for the rollback rehearsal or a first backup on a new cluster, annotate the Deployment, then rerun the gate. Gitea is down for the dumps, so drain agent work first as for a drain-class merge:
kubectl --context admin@mgmt-cluster -n gitea annotate deployment gitea platform.skatzi.com/preupgrade-force=true
kubectl --context admin@mgmt-cluster -n gitea delete job gitea-preupgrade
flux --context admin@mgmt-cluster reconcile kustomization gitea-preupgrade
The gate logs Forced by the annotation … and runs every step. Once the backup is verified it removes the annotation (the Role gitea-preupgrade may patch deployment/gitea for this), so the next gate on the same version 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 (templates/restore-pod.yaml). It is a single pod in namespace gitea-scratch with a 60 GiB ephemeral volume, removed with the pod. Its steps run in order:
fetch: downloads the backup.unpack: checks both SHA-256 sums and lays the dump out as the chart's/data(data directory, repositories in/data/git/gitea-repositories,app.ini). Then it runsgit fsckon a random sample of 10 repositories.postgres: a throwaway PostgreSQL sidecar. Thepostgresimage in the dump, restore and rollback templates keeps the major ofgitea-postgresql(17):pg_dumprefuses a server newer than itself, and a restore into an older major is not supported. Renovate enforces this with anallowedVersions: "<18"rule for this path inrenovate.json; raise it in the same pull request that movesgitea-postgresqlto PostgreSQL 18. A minor bump of that image changes only the backup tools: it does not touchgitea-postgresqlor recreate the gate Job (TARGET_VERSIONis unchanged), so the next weekly drill is the first run of the new image.restore-db:pg_restore, then checks that the repository and user counts match the counts taken at dump time.gitea: the Gitea version that made the backup, started on the restored data. Mirrors, mail, Actions and the SSH server are off, and webhooks may only reach loopback, so the copy cannot push to the GitHub mirror or call production webhooks. From Gitea 28 the loopback-only list holds only with[security] EGRESS_MODE = strict, which the pod sets: in the defaultlaxmode the list restricts private hosts alone and every public host stays reachable (go-gitea/gitea#39426).smoke:GET /api/healthzand aGET /api/v1/versionsent withAuthorization: token. The token is the push mirror reconciler's Gitea access token (OpenBao<cluster>/gitea/github-mirror, propertygitea-token, synced by thegitea-tokenExternalSecretingitea-scratch; see the Flux page). The restored database holds production's access tokens, so the token works only on a backup taken after it was created: a drill of an older backup fails with401until the next backup. The admin password is not used, since Gitea refused basic auth with it on 2026-10-20 (#1375). The version must equal the backup's.
Weekly restore drill¶
The CronJob gitea-scratch/gitea-restore-drill runs on Mondays at 03:00 UTC. It reads latest.env, runs the restore check with the image recorded there, and on success writes <cluster>/gitea/drill/last-success.env (pod name, time, prefix, version).
PR evidence for a Gitea upgrade cites the last passing drill:
kubectl --context admin@mgmt-cluster -n gitea-scratch get cronjob gitea-restore-drill -o jsonpath='{.status.lastSuccessfulTime}'
kubectl --context admin@mgmt-cluster -n gitea-scratch get jobs -l batch.kubernetes.io/cronjob-name=gitea-restore-drill # Complete = passed
aws s3 cp "s3://skatzi-backups/hetzner-mgmt/gitea/drill/last-success.env" - # with the backup-store keys
To run a drill now: kubectl --context admin@mgmt-cluster -n gitea-scratch create job --from=cronjob/gitea-restore-drill gitea-restore-drill-manual.
A failed drill shows as a failed Job. The default Prometheus rule KubeJobFailed fires for it after 15 minutes, and Alertmanager sends it to Mattermost and to Taskbase, which opens an incident task in the Platform project (Backups: failed drills). Delete the failed Job once the cause is fixed and a drill has passed; until then the alert keeps firing.
Retention¶
The gate keeps every backup younger than 30 days, and always the newest three. A bucket lifecycle expiry is not used for this prefix: it would expire the backup that latest.env and the drill depend on when no upgrade has run for 30 days. Under the bucket's object lock a prune only adds delete markers; the lifecycle rule for non-current versions removes the data 31 days later (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 here and under the drill need the store's endpoint and path-style addressing: source components/gitea/backup/scripts/s3.sh first, as the Jobs do. By hand, on hetzner-mgmt, with PREFIX taken from latest.env or the gate's log:
-
Stop Gitea and Flux's hold on it.
-
Restore the database. From a pod in
giteathat has thebackup-storesecret and thepostgresimage oftemplates/restore-pod.yaml, run:for part in $(aws s3 ls "s3://$BUCKET/$PREFIX/gitea.pgdump.part-" | awk '{print $4}' | sort); do aws s3 cp "s3://$BUCKET/$PREFIX/$part" - >> /tmp/gitea.pgdump done PGPASSWORD=<postgres-password from secret gitea-postgresql> \ psql -h gitea-postgresql -U postgres -c 'DROP DATABASE gitea WITH (FORCE)' -c 'CREATE DATABASE gitea OWNER gitea' PGPASSWORD=<password> pg_restore -h gitea-postgresql -U gitea -d gitea --no-owner --exit-on-error /tmp/gitea.pgdump -
Restore the files. Join the
gitea-dump.tar.part-*objects the same way and check the result againstDUMP_SHA256inmetadata.env. From a pod that mountsgitea-shared-storageat/dataand runs as uid 1000, extractgitea-dump.tarasunpack.shdoes. Move the current/datacontents aside first. Do not delete them until Gitea is back.mkdir /tmp/dump && tar -xf gitea-dump.tar -C /tmp/dump # entry by entry: copying /tmp/dump/data/. would also try to set the volume root's owner (shopt -s dotglob; for e in /tmp/dump/data/*; do [ "${e##*/}" = lost+found ] || cp -a "$e" /data/; done) mkdir -p /data/git && cp -a /tmp/dump/repos /data/git/gitea-repositories cp /tmp/dump/app.ini /data/gitea/conf/app.ini -
Pin the versions the backup was made with: in Git, set
image.tagand bothTARGET_VERSIONs toVERSIONinmetadata.env, andchart.spec.versionto the chart it ran on (CHART_VERSIONin<cluster>/gitea/latest.envwhenPREFIXis the latest backup, otherwise the version before the upgrade commit). Reverting the whole upgrade commit does both. Then resume:
Gitea is Flux's source, so if Gitea is down, Git changes cannot reach the cluster. Patch the HelmRelease's image.tag and chart.spec.version directly instead, or switch Flux to the fallback source (decision #1161).