{{ if .Values.backup.enabled }}
apiVersion: batch/v1
kind: CronJob
metadata:
  name: backup
spec:
  schedule: "{{ .Values.backup.schedule }}"
  suspend: {{ .Values.backup.suspend }}
  timeZone: "{{ .Values.timeZone }}"
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      ttlSecondsAfterFinished: 3600 # 1 hpur
      backoffLimit: 0
      completions: 1
      parallelism: 1
      template:
        spec:
          imagePullSecrets:
          - name: regcred
          containers:
          - name: backup
            image: goalprofit/backup:{{ .Values.backup.version }}
            imagePullPolicy: Always
            volumeMounts:
            - mountPath: /data
              name: data
            env:
              - name: BACKUP_SERVER
                value: {{.Values.backup.server}}
              - name: BACKUP_FOLDER
                value: {{.Values.backup.folder}}
              - name: BACKUP_PRIVATE_KEY
                valueFrom:
                  secretKeyRef:
                    name: backup-private-key
                    key: key
                    optional: false
      {{ if .Values.local_user_id }}
              - name: LOCAL_USER_ID
                value: "{{.Values.local_user_id}}"
      {{ end }}
      {{ if .Values.local_user_name }}
              - name: LOCAL_USER_NAME
                value: "{{.Values.local_user_name}}"
      {{ end }}
          restartPolicy: Never
          volumes:
          - name: data
            hostPath:
              path: /data/{{.Values.customer}}
              type: Directory
      {{ include "app.affinity" . | nindent 6}}
{{ end }}