Files
cozystack/packages/apps/postgres/templates/backup.yaml
T
Yan Bondarenko 982db83dd2 fix(postgres): accept a standard five-field backup schedule
The mongodb app and backup Plans take a standard five-field cron, but
the postgres chart hands backup.schedule to a CloudNativePG
ScheduledBackup, which parses it with robfig/cron v1 Parse. That parser
reads the first field as seconds and makes day-of-week optional, so a
five-field value passes admission with only a warning and runs one
field off. 0 2 * * * runs hourly and 5 * * * * every minute.

A five-field schedule now gets a leading 0 seconds field. Six fields
and descriptors pass through, and any other field count fails the
render. A release that already sets five fields gets the standard
meaning on upgrade, so 0 2 * * * goes from hourly to daily.

Assisted-by: LLM
Signed-off-by: Yan Bondarenko <202671653+yankawai@users.noreply.github.com>
2026-10-26 20:10:59 +03:00

59 lines
2.8 KiB
YAML

{{/*
Legacy chart-managed scheduled backup.
Emits a cnpg.io/ScheduledBackup so the chart can drive recurring backups
on its own schedule. Superseded by the BackupClass + Plan flow under
`backups.cozystack.io` (see packages/apps/postgres/README.md), which is
the recommended path for tenant-managed backup orchestration.
Gating: rendered only when both `backup.enabled` and `backup.schedule`
are non-empty. `backup.enabled=true` alone is the documented way to
bootstrap WAL archiving on the source cluster (the BackupClass driver
SSA-applies the barman-cloud ObjectStore and SSA-patches spec.plugins
with ForceOwnership); leaving `backup.schedule=""` lets users opt into
archive without also getting a chart-emitted ScheduledBackup that
competes with their BackupClass policy. CNPG admission rejects an empty
`schedule` field, so the gate keeps invalid manifests from reaching the
cluster.
Also excluded when `backup.useSystemBucket=true`: in that flow the
Cluster's `spec.plugins` (and its barman-cloud ObjectStore) are
intentionally left unset until the BackupClass driver SSA-applies them
at first BackupJob time, so a chart-emitted ScheduledBackup would fire
against a Cluster with no backup configuration and fail. This mirrors
the ClickHouse `useSystemBucket` gates on the chart-managed backup
resources.
CNPG parses `schedule` with robfig/cron v1 `Parse`, which takes the first
field as seconds and makes day-of-week optional. A standard five-field
cron passes its webhook with only a warning and then runs one field off,
so `5 * * * *` fires every minute instead of hourly. A five-field value
therefore gets a leading `0` seconds field. Six fields and descriptors
such as `@daily` pass through as they are, and any other field count
fails the render, which CNPG would reject at admission anyway.
*/}}
{{- if and .Values.backup.enabled .Values.backup.schedule (not .Values.backup.useSystemBucket) }}
{{- $schedule := .Values.backup.schedule }}
{{- $fields := regexFindAll `\S+` $schedule -1 }}
{{- if not (hasPrefix "@" (trim $schedule)) }}
{{- if eq (len $fields) 5 }}
{{- $schedule = printf "0 %s" (join " " $fields) }}
{{- else if ne (len $fields) 6 }}
{{- fail (printf "postgres: backup.schedule %q is not a cron schedule with 5 or 6 fields (it has %d). Use a standard five-field cron such as \"0 2 * * *\", a six-field cron with leading seconds such as \"0 0 2 * * *\", or a descriptor such as \"@daily\"." $schedule (len $fields)) }}
{{- end }}
{{- end }}
---
apiVersion: postgresql.cnpg.io/v1
kind: ScheduledBackup
metadata:
name: {{ .Release.Name }}
spec:
schedule: {{ $schedule | quote }}
backupOwnerReference: self
method: plugin
pluginConfiguration:
name: barman-cloud.cloudnative-pg.io
cluster:
name: {{ .Release.Name }}
{{- end }}