apiVersion: batch/v1
kind: CronJob
metadata:
  name: postgres-backup
spec:
  # Backup the database every day at 1AM
  schedule: "0 1 * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: postgres-backup
            image: postgres:15
            command: ["/bin/sh"]
            args:
              - '-c'
              - 'pg_dumpall -U postgres -h postgres-postgresql.postgres > /backup/backup-$(date +"%m-%d-%Y-%H-%M").sql'
            env:
            - name: PGPASSWORD
              valueFrom:
                secretKeyRef:
                  name: postgres-password
                  key: password
            volumeMounts:
            - mountPath: /backup
              name: postgres-backup-claim
          restartPolicy: Never
          volumes:
          - name: postgres-backup-claim
            persistentVolumeClaim:
              claimName: postgres-backup-claim