apiVersion: batch/v1
kind: Job
metadata:
  generation: 1
  name: postgres-backup-restore-authentik
  namespace: backup-system
spec:
  suspend: true
  completions: 1
  parallelism: 1
  backoffLimit: 1
  activeDeadlineSeconds: 100
  ttlSecondsAfterFinished: 100
  template:
    metadata:
      name: postgres-backup-restore-authentik
    spec:
      containers:
      - image: postgres:14
        name: postgres-backup
        imagePullPolicy: IfNotPresent
        args:
          - -c
          - psql -U authentik -h ${SECRET_POSTGRES_HOST} authentik -f $(find /var/backups/authentik/ -size +100k -maxdepth 1 -name "*.sql" -type f -exec stat -c "%y %n" {} + | sort -r | head -n1 | cut -d " " -f 4-)
        command:
          - /bin/sh
        env:
        - name: PGPASSWORD
          value: ${SECRET_AUTHENTIK_POSTGRES_PASSWORD}
        
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /var/backups
          name: postgres-storage
      dnsPolicy: ClusterFirst
      restartPolicy: Never
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
      volumes:
      - name: postgres-storage
        persistentVolumeClaim:
          claimName: backup-system-nfs-tank-share-pvc

