job-backup.yaml 3.99 KB
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: {{ include "postgresql-rclone.fullname" . }}-backups
spec:
  storageClassName: {{ .Values.backup.pvc.storageClassName }}
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: {{ .Values.backup.pvc.size }}
---
apiVersion: batch/v1
kind: CronJob
metadata:
  name: {{ include "postgresql-rclone.fullname" . }}-backup
  labels:
    {{- include "postgresql-rclone.labels" . | nindent 4 }}
spec:
  schedule: {{ .Values.backup.schedule }}
  startingDeadlineSeconds: 3600
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 10
  failedJobsHistoryLimit: 10
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          imagePullSecrets:
            {{- toYaml .Values.imagePullSecrets | nindent 12 }}
          volumes:
            - name: backups
              persistentVolumeClaim:
                claimName: {{ include "postgresql-rclone.fullname" . }}-backups
            - name: postgresql-auth
              secret:
                secretName: {{ include "postgresql-rclone.fullname" . }}-auth
            - name: local-config
              configMap:
                name: {{ include "postgresql-rclone.fullname" . }}-local
            {{- if .Values.rclone.enabled }}
            - name: rclone-auth
              secret:
                secretName: {{ include "postgresql-rclone.fullname" . }}-rclone
            - name: rclone-config
              configMap:
                name: {{ include "postgresql-rclone.fullname" . }}-rclone
            {{- end }}
          initContainers:
          - name: dump-databases
            image: "{{ .Values.local.image.repository }}:{{ .Values.local.image.tag }}"
            imagePullPolicy: {{ .Values.local.image.pullPolicy }}
            volumeMounts:
              - name: backups
                mountPath: /backups
              - name: local-config
                mountPath: /config
              - name: postgresql-auth
                mountPath: /secret
            env:
              - name: BACKUP_KEEP_DAYS
                valueFrom:
                  configMapKeyRef:
                    name: {{ include "postgresql-rclone.fullname" . }}-local
                    key: BACKUP_KEEP_DAYS
              - name: BACKUP_DIR
                value: /backups
              - name: POSTGRES_HOST
                value: {{ .Values.postgresql.host }}
              - name: POSTGRES_DB_FILE
                value: /config/POSTGRES_DB
              - name: POSTGRES_USER_FILE
                value: /secret/POSTGRES_USER
              - name: POSTGRES_PASSWORD_FILE
                value: /secret/POSTGRES_PASSWORD
            command: ["/backup.sh"]
          {{- if .Values.rclone.enabled }}
          - name: rclone
            image: "{{ .Values.rclone.image.repository }}:{{ .Values.rclone.image.tag }}"
            imagePullPolicy: {{ .Values.rclone.image.pullPolicy }}
            volumeMounts:
              - name: backups
                mountPath: /backups
              - name: rclone-config
                mountPath: /config
              - name: rclone-auth
                mountPath: /secret
            envFrom:
              - configMapRef:
                  name: {{ include "postgresql-rclone.fullname" . }}-rclone
              - secretRef:
                  name: {{ include "postgresql-rclone.fullname" . }}-rclone
            {{- if .Values.rclone.crypt.enabled }}
            env:
              - name: RCLONE_CRYPT_REMOTE
                value: ":s3:$(S3_BUCKET)/$(S3_PREFIX)/"
            {{- end }}
            {{- if .Values.rclone.crypt.enabled }}
            args: ["copy", "/backups/", ":crypt:"]
            {{- else }}
            args: ["copy", "/backups/", ":s3:$(S3_BUCKET)/$(S3_PREFIX)/"]
            {{- end }}
          {{- end }}
          containers:
          - name: show-dumps
            image: bash
            volumeMounts:
              - name: backups
                mountPath: /backups
            command: ["ls"]
            args: ["-alR", "/backups"]