Files
cozystack/examples/backups/postgres/README.md
T
Alexey Artamonov 6d1eeba65b fix(postgres): bound a backup whose cnpg Backup stalls in pending
CloudNativePG keeps a Backup pending either behind a started or running
backup of the same Cluster, which is healthy, or because the instance to
run it on cannot be found or is not ready, which lasts as long as the
Cluster is fenced. The BackupJob waited on both forever.

The driver now tells the two apart by whether any backup of the Cluster
is started or running. It does not model which pending Backup
CloudNativePG lets through first. With none running, the job records a
CNPGBackupStalled condition and fails once that has lasted the backup
deadline. Before it gives up it reads the Cluster's backups again from
the apiserver, since the cache can still show pending one that has just
started. Its own Backup is deleted, conditionally on the version read so
a Backup CloudNativePG has just started is kept, because otherwise it
would run later with no Backup recording it. The give-up is persisted
before the delete, so a Backup found missing afterwards fails the job
only when the driver removed it; one deleted by hand is replaced and its
stall counts afresh.

The reason CloudNativePG gives exists only as an event on a Backup, so
the driver reads the newest Warning on its own Backup, or on another
pending backup of the Cluster when its own carries none, by UID and
uncached, which needs list on events but no watch.

A job is no longer held on the plugin rollout another job started when
the Cluster is fenced and every instance pod postdates that attach: a
fenced Cluster is neither healthy nor ready until the fence is lifted,
so waiting on that would only delay the bound by up to the rollout
deadline.

The postgres e2e fences the source Cluster, checks that a BackupJob
reports the stall with that reason, then lifts the fence and checks the
same job completes with the stall ended. That exercises the events grant
and the involvedObject.uid selector against a real apiserver. The step
creates the BackupJob only once every instance pod has gone NotReady,
since the fence takes effect asynchronously and CloudNativePG starts a
Backup on a target that is still Ready.

Assisted-by: LLM
Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
Signed-off-by: Andrey Kolkov <androndo@gmail.com>
2026-11-25 16:01:48 +03:00

4.3 KiB

PostgreSQL backup & restore (barman-cloud plugin)

End-to-end demo of backing up a cozystack Postgres application to S3 and restoring it, using the CloudNativePG barman-cloud plugin (the native spec.backup.barmanObjectStore path is deprecated in CNPG 1.27 and removed in 1.29). Backups flow through the platform BackupClass + CNPG strategy so one strategy CR serves every Postgres instance in the tenant.

What it provisions

The numbered manifests are the documented flow; a human can read and apply them by hand, and run-all.sh drives the same files for a smoke test.

  • 00-bucket.yaml — a cozystack-managed S3 Bucket for the backups, with a backup user.
  • 05-postgres-src.yaml — the source Postgres, with backup.enabled: true so archive_command streams WALs from helm-install onwards.
  • 10-cnpg-strategy.yaml — the CNPG strategy templated against the live application.
  • 15-backupclass.yaml — maps Kind=Postgres to that strategy.
  • 20-plan.yaml — an optional cron Plan for recurring backups.
  • 25-backupjob-adhoc.yaml — an ad-hoc BackupJob.
  • 30-postgres-target.yaml + 40-restorejob-to-copy.yaml — restore into a fresh copy, leaving the source running.
  • 35-restorejob-in-place.yaml — the destructive in-place restore variant.
  • 45-restorejob-pitr.yaml — point-in-time recovery: restore into the copy as of a specific spec.options.recoveryTime (RFC3339).

Placeholders and derived Secrets

05-postgres-src.yaml and 10-cnpg-strategy.yaml carry REPLACE_WITH_COSI_BUCKET_NAME and REPLACE_WITH_S3_ENDPOINT (the demo app user's password is chart-generated, not substituted). They also reference two Secrets, <app>-cnpg-backup-creds (the S3 credentials the barman-cloud sidecar reads) and <app>-cnpg-backup-ca (the CA it trusts for a self-signed endpoint). The strategy template renders those names against whichever application it drives, so the restore target needs its own pair too — a restore against pg-target looks up pg-target-cnpg-backup-*. run-all.sh resolves the placeholders from the provisioned Bucket's BucketInfo Secret and materialises the pair for both the source and the target; editing by hand, you copy the coordinates from kubectl -n <ns> get secret bucket-<name>-backup -o jsonpath='{.data.BucketInfo}' and the CA from the seaweedfs CA secret in tenant-root (seaweedfs-ca-cert by default; run-all.sh auto-discovers it from the cert-manager Certificate if the name differs).

Drop the endpointCA block from 05/10 (and set S3_CA_SECRET="" for run-all.sh) when the S3 endpoint is signed by a publicly-trusted CA.

Run it

# Defaults to NAMESPACE=tenant-root; override for a tenant namespace.
NAMESPACE=tenant-root examples/backups/postgres/run-all.sh
# Tear everything down afterwards (idempotent).
NAMESPACE=tenant-root examples/backups/postgres/cleanup.sh

run-all.sh writes a sentinel row into the source, waits for the BackupJob to reach Succeeded, then fences the source Cluster for a while: a second BackupJob must report the stall (CNPGBackupStalled) with the reason CloudNativePG recorded, and complete once the fence is lifted. It then restores to a copy, and asserts the sentinel round-tripped through S3 into the restored copy. It then runs a point-in-time recovery: it writes a before marker, captures the server timestamp, writes an after marker, and restores the copy to that timestamp (45-restorejob-pitr.yaml), asserting the before row survived and the after row did not. Finally it submits a RestoreJob with a recoveryTime an hour in the future and asserts it fails fast with status.phase: Failed and reason RecoveryTargetUnreachable (rather than hanging to the restore deadline). Set SKIP_RESTORE=1 to stop after a successful backup, or SKIP_PITR=1 to stop after the latest-point restore.

Automated e2e

The Chainsaw suite at hack/e2e-chainsaw/postgres/ drives this same run-all.sh as a second test (postgres-2-backup-roundtrip), selected by CI whenever the postgres app (or these scripts) change and on every release cut. It runs in tenant-root against the in-cluster seaweedfs endpoint — the isolated e2e tenant cannot reach it across the Cilium egress policy, and the external ingress endpoint is an unroutable placeholder in the sandbox.