# Nightly logical backup of BOTH databases — the *standard shape* of the client's # stated practice (bash script on cron that dumps the database). # # This is NOT a copy of the client's script: theirs has not been seen yet # (Week 3 read-only audit item). It implements what Lance described — a nightly # `pg_dump` per database, timestamped files, an age-based retention sweep and a # per-run log line — and nothing more. # # Deliberately absent, because the client's method has none of it: # - WAL archiving / PITR -> worst-case RPO is 24 h (see README) # - filestore (attachment bytes)-> the dump carries rows, not files (see README) # - off-box copy -> dumps live on siriussec's local-path disk # (the Garage S3 API at http://10.10.0.1:3900 is reachable from a pod, but it # needs a bucket + credentials that are not part of this namespace; copy is # therefore NOT implemented and backups die with the node — on purpose, so # the replica shows the real exposure.) # # No RBAC: the default ServiceAccount, with its token not even mounted. This pod # talks to Postgres over the ClusterIP Service and writes to its own PVC. --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: backup-data namespace: customer1 labels: app.kubernetes.io/part-of: customer1 spec: accessModes: [ReadWriteOnce] storageClassName: local-path resources: requests: storage: 10Gi --- apiVersion: v1 kind: ConfigMap metadata: name: pg-backup-script namespace: customer1 labels: app.kubernetes.io/part-of: customer1 data: backup.sh: | #!/bin/sh # Nightly pg_dump -Fc of each database + age-based retention. # Exit non-zero if ANY dump fails or comes back with an empty TOC, so a # broken backup turns the Job red instead of silently succeeding. set -eu : "${PGHOST:=pg}" : "${PGPORT:=5432}" : "${PGUSER:=postgres}" : "${BACKUP_DIR:=/backups}" : "${RETENTION_DAYS:=14}" DATABASES="${DATABASES:-odoo metabase}" ts="$(date -u +%Y%m%dT%H%M%SZ)" mkdir -p "$BACKUP_DIR" run_start="$(date +%s)" rc=0 echo "backup: run start ts=$ts databases='$DATABASES' retention=${RETENTION_DAYS}d dir=$BACKUP_DIR" for db in $DATABASES; do file="$BACKUP_DIR/$db-$ts.dump" start="$(date +%s)" if pg_dump -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" -d "$db" -Fc -f "$file"; then dur=$(( $(date +%s) - start )) bytes="$(stat -c %s "$file")" toc="$(pg_restore --list "$file" | wc -l)" if [ "$toc" -gt 0 ]; then echo "backup: ok db=$db file=$db-$ts.dump size=${bytes}B toc_entries=$toc duration=${dur}s" else echo "backup: FAIL db=$db file=$db-$ts.dump empty archive TOC" >&2 rc=1 fi else echo "backup: FAIL db=$db pg_dump did not complete" >&2 rc=1 fi done echo "backup: retention sweep, deleting *.dump older than ${RETENTION_DAYS} days" find "$BACKUP_DIR" -maxdepth 1 -type f -name '*.dump' -mtime +"$RETENTION_DAYS" -print -delete || rc=1 echo "backup: run complete rc=$rc total=$(( $(date +%s) - run_start ))s dumps_on_disk=$(ls -1 "$BACKUP_DIR"/*.dump 2>/dev/null | wc -l)" exit "$rc" --- apiVersion: batch/v1 kind: CronJob metadata: name: pg-backup namespace: customer1 labels: app.kubernetes.io/part-of: customer1 spec: # Parked with the stack (see odoo.yaml): no dumps while Postgres is at 0. suspend: true # 03:17 America/Los_Angeles — one run per night, off the hour so it does not # collide with Odoo's own cron threads or Metabase syncs at :00. schedule: "17 3 * * *" timeZone: America/Los_Angeles concurrencyPolicy: Forbid startingDeadlineSeconds: 600 successfulJobsHistoryLimit: 3 failedJobsHistoryLimit: 3 jobTemplate: spec: backoffLimit: 1 activeDeadlineSeconds: 1800 template: metadata: labels: app: pg-backup app.kubernetes.io/part-of: customer1 spec: restartPolicy: Never automountServiceAccountToken: false nodeSelector: kubernetes.io/hostname: siriussec tolerations: - key: dedicated operator: Exists effect: PreferNoSchedule # local-path hands out root-owned directories; the pod writes the PVC # as root. It only runs pg_dump + pg_restore (client tools) and find. securityContext: runAsUser: 0 runAsGroup: 0 containers: - name: pg-dump image: postgres:16 imagePullPolicy: IfNotPresent command: ["/bin/sh", "/opt/backup/backup.sh"] env: - name: PGHOST value: pg.customer1.svc.cluster.local - name: PGPORT value: "5432" - name: PGUSER valueFrom: secretKeyRef: name: pg-app key: username - name: PGPASSWORD valueFrom: secretKeyRef: name: pg-app key: password - name: RETENTION_DAYS value: "14" - name: TZ value: America/Los_Angeles resources: requests: cpu: 50m memory: 128Mi limits: memory: 512Mi volumeMounts: - name: backups mountPath: /backups - name: script mountPath: /opt/backup readOnly: true volumes: - name: backups persistentVolumeClaim: claimName: backup-data - name: script configMap: name: pg-backup-script defaultMode: 0555