Files
HetznerTerra/infrastructure/addons/cnpg/pgdump-cronjob.yaml
MichaelFisher1997 9d601dc77c
Some checks failed
Deploy Cluster / Terraform (push) Successful in 4m16s
Deploy Cluster / Ansible (push) Failing after 12m27s
feat: Add CloudNativePG with B2 backups for persistent Rancher database
- Add Local Path Provisioner for storage
- Add CloudNativePG operator (v1.27.0) via Flux
- Create PostgreSQL cluster with B2 (Backblaze) auto-backup/restore
- Update Rancher to use external PostgreSQL via CATTLE_DB_CATTLE_* env vars
- Add weekly pg_dump CronJob to B2 (Sundays 2AM)
- Add pre-destroy backup hook to destroy workflow
- Add B2 credentials to Doppler (B2_ACCOUNT_ID, B2_APPLICATION_KEY)
- Generate RANCHER_DB_PASSWORD in Doppler

Backup location: HetznerTerra/rancher-backups/
Retention: 14 backups
2026-03-25 23:06:45 +00:00

61 lines
2.0 KiB
YAML

apiVersion: batch/v1
kind: CronJob
metadata:
name: pgdump-rancher
namespace: cnpg-cluster
spec:
schedule: "0 2 * * 0"
concurrencyPolicy: Forbid
successfulJobsHistoryLimit: 4
failedJobsHistoryLimit: 4
jobTemplate:
spec:
backoffLimit: 3
template:
spec:
restartPolicy: OnFailure
containers:
- name: pgdump
image: ghcr.io/cloudnative-pg/pgbackrest:latest
command:
- /bin/sh
- -c
- |
set -e
export AWS_ACCESS_KEY_ID=$(cat /etc/b2/credentials/B2_ACCOUNT_ID)
export AWS_SECRET_ACCESS_KEY=$(cat /etc/b2/credentials/B2_APPLICATION_KEY)
export AWS_ENDPOINT=https://s3.us-east-005.backblazeb2.com
BACKUP_FILE="rancher-backup-$(date +%Y%m%d-%H%M%S).sql.gz"
pg_dump -h cnpg-cluster-rw.cnpg-cluster.svc -U postgres -d postgres --no-owner --clean | gzip | \
aws s3 cp - s3://HetznerTerra/rancher-backups/$BACKUP_FILE
echo "Backup completed: $BACKUP_FILE"
env:
- name: PGPASSWORD
valueFrom:
secretKeyRef:
name: rancher-db-password
key: password
volumeMounts:
- name: b2-credentials
mountPath: /etc/b2/credentials
readOnly: true
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 512Mi
volumes:
- name: b2-credentials
secret:
secretName: b2-credentials
nodeSelector:
kubernetes.io/hostname: k8s-cluster-cp-1
tolerations:
- key: node-role.kubernetes.io/control-plane
operator: Exists
effect: NoSchedule