{{- if .Values.backup.enabled -}} apiVersion: batch/v1 kind: CronJob metadata: name: {{ include "home-assistant.fullname" . }}-backup {{- if .Values.namespaceOverride }} namespace: {{ .Values.namespaceOverride }} {{- end }} labels: {{- include "home-assistant.labels" . | nindent 4 }} spec: successfulJobsHistoryLimit: 1 failedJobsHistoryLimit: 1 schedule: {{ .Values.backup.schedule | quote }} jobTemplate: spec: template: spec: containers: - name: postgres-backup image: {{ .Values.backup.image }} command: - /bin/sh - -c - | # Backup Home Assistant database pg_dump -h {{ .Values.backup.postgres.host }} -U {{ .Values.backup.postgres.user }} -d $DB_NAME | gzip > /backup/$DB_NAME-$(date +%Y%m%d-%H%M%S).sql.gz # Retention: Delete backups older than specified days find /backup -name "$DB_NAME-*.sql.gz" -mtime +$RETENTION_DAYS -delete env: - name: PGPASSWORD valueFrom: secretKeyRef: name: {{ .Values.backup.postgres.existingSecret }} key: {{ .Values.backup.postgres.secretKey }} - name: DB_NAME value: {{ .Values.backup.postgres.db | quote }} - name: RETENTION_DAYS value: {{ .Values.backup.retentionDays | quote }} volumeMounts: - name: backup-storage mountPath: /backup {{- if .Values.backup.persistence.enabled }} subPath: postgres {{- end }} volumes: - name: backup-storage {{- if .Values.backup.persistence.enabled }} persistentVolumeClaim: claimName: {{ .Values.backup.persistence.existingClaim | default (printf "%s-db-backup" (include "home-assistant.fullname" .)) }} {{- else }} hostPath: path: {{ .Values.backup.storagePath }} type: DirectoryOrCreate {{- end }} restartPolicy: OnFailure {{- if .Values.backup.node }} affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: kubernetes.io/hostname operator: In values: - {{ .Values.backup.node }} {{- else }} {{- with .Values.nodeSelector }} nodeSelector: {{- toYaml . | nindent 10 }} {{- end }} {{- with .Values.affinity }} affinity: {{- toYaml . | nindent 10 }} {{- end }} {{- end }} {{- with .Values.tolerations }} tolerations: {{- toYaml . | nindent 10 }} {{- end }} {{- end }}