diff --git a/charts/backup/Chart.yaml b/charts/backup/Chart.yaml new file mode 100644 index 0000000..df9c5e9 --- /dev/null +++ b/charts/backup/Chart.yaml @@ -0,0 +1,3 @@ +apiVersion: v2 +version: 1.0.0 +name: backup diff --git a/charts/backup/templates/cron-job-backup.yaml b/charts/backup/templates/cron-job-backup.yaml new file mode 100644 index 0000000..e83dd00 --- /dev/null +++ b/charts/backup/templates/cron-job-backup.yaml @@ -0,0 +1,44 @@ +{{- $values := .Values -}} +{{- $release := .Release -}} + +--- +{{- range $key, $value := $values.jobs}} +apiVersion: batch/v1 +kind: CronJob +metadata: + name: "{{ $release.Name }}-{{ $key }}-backup" +spec: + schedule: "{{ $value.cron.backup }}" + concurrencyPolicy: Forbid + successfulJobsHistoryLimit: 3 + failedJobsHistoryLimit: 1 + jobTemplate: + spec: + template: + spec: + containers: + - name: "{{ $release.Name }}-{{ $key }}-backup" + image: ghcr.io/morten-olsen/homelab-operator-backup:main + command: ["/app/backup.sh"] + env: + - name: RESTIC_PASSWORD + valueFrom: + secretKeyRef: + name: "{{ $values.password.name }}" + key: "{{ $values.password.key }}" + volumeMounts: + - name: source + mountPath: "/mnt/source" + - name: target + mountPath: "/mnt/backups" + subPath: "{{ $release.Name }}-{{ $key }}" + volumes: + - name: source + persistentVolumeClaim: + claimName: "{{ $value.source }}" + - name: target + persistentVolumeClaim: + claimName: "{{ $values.target }}" + restartPolicy: OnFailure +--- +{{- end }} diff --git a/charts/backup/templates/cron-job-cleanup.yaml b/charts/backup/templates/cron-job-cleanup.yaml new file mode 100644 index 0000000..4f8cc15 --- /dev/null +++ b/charts/backup/templates/cron-job-cleanup.yaml @@ -0,0 +1,39 @@ +{{- $values := .Values -}} +{{- $release := .Release -}} + +--- +{{- range $key, $value := $values.jobs}} +apiVersion: batch/v1 +kind: CronJob +metadata: + name: "{{ $release.Name }}-{{ $key }}-cleanup" +spec: + schedule: "{{ $value.cron.cleanup }}" + concurrencyPolicy: Forbid + successfulJobsHistoryLimit: 3 + failedJobsHistoryLimit: 1 + jobTemplate: + spec: + template: + spec: + containers: + - name: "{{ $release.Name }}-{{ $key }}-cleanup" + image: ghcr.io/morten-olsen/homelab-operator-backup:main + command: ["/app/cleanup.sh"] + env: + - name: RESTIC_PASSWORD + valueFrom: + secretKeyRef: + name: "{{ $values.password.name }}" + key: "{{ $values.password.key }}" + volumeMounts: + - name: target + mountPath: "/mnt/backup" + subPath: "{{ $release.Name }}-{{ $key }}" + volumes: + - name: target + persistentVolumeClaim: + claimName: "{{ $values.target }}" + restartPolicy: OnFailure +--- +{{- end }} diff --git a/charts/backup/values.yaml b/charts/backup/values.yaml new file mode 100644 index 0000000..d468666 --- /dev/null +++ b/charts/backup/values.yaml @@ -0,0 +1,10 @@ +password: + name: backup + key: password +jobs: + pictures: + cron: + backup: "0 2 * * *" + cleanup: "0 4 * * SUN" + source: pictures +target: backups diff --git a/images/backup/backup.sh b/images/backup/backup.sh index 4fc7196..1b8f559 100644 --- a/images/backup/backup.sh +++ b/images/backup/backup.sh @@ -16,14 +16,13 @@ mkdir -p "/mnt/backup" echo "Starting Restic backup from $SOURCE_DIR to $RESTIC_REPOSITORY" echo "Checking/Initializing Restic repository..." -restic init --repository "$RESTIC_REPOSITORY" || true +restic init --repo "$RESTIC_REPOSITORY" || true echo "Running Restic backup..." restic backup \ "$SOURCE_DIR" \ --verbose \ --tag "daily" \ - --exclude-cache \ if [ $? -eq 0 ]; then echo "Restic backup completed successfully!"