From 64e3d5809acf46c93e7a0b2725dc9185f1c67afa Mon Sep 17 00:00:00 2001 From: John Hsu Date: Tue, 5 Mar 2024 09:06:20 -0800 Subject: [PATCH] ADD ipeer db backup cronjob The old email cronjob template was moved from cronjob.yaml to cronjob-email.yaml and the new db cronjob template is named cronjob-backup.yaml. This migrates the old manually created prod db backup cronjob to one that's managed by the chart. The old cronjob was no longer working ever since we started using replication since the db pod names were changed. New config values were added for this feature under 'cronjob.backup'. The most important is the ones specifying the nfs mount where we store the backups, 'cronjob.backup.nfs.path' and 'cronjob.backup.nfs.server' must be set properly in the env value files. --- ipeer/Chart.yaml | 2 +- ipeer/templates/cronjob-backup.yaml | 54 +++++++++++++++++++ .../{cronjob.yaml => cronjob-email.yaml} | 2 +- ipeer/values.yaml | 9 +++- 4 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 ipeer/templates/cronjob-backup.yaml rename ipeer/templates/{cronjob.yaml => cronjob-email.yaml} (98%) diff --git a/ipeer/Chart.yaml b/ipeer/Chart.yaml index 33b7276..d4c7f02 100644 --- a/ipeer/Chart.yaml +++ b/ipeer/Chart.yaml @@ -15,7 +15,7 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. # Versions are expected to follow Semantic Versioning (https://semver.org/) -version: 0.1.9 +version: 0.1.11 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. Versions are not expected to diff --git a/ipeer/templates/cronjob-backup.yaml b/ipeer/templates/cronjob-backup.yaml new file mode 100644 index 0000000..df7a65b --- /dev/null +++ b/ipeer/templates/cronjob-backup.yaml @@ -0,0 +1,54 @@ +{{- if .Values.cronjob.backup.enabled }} +apiVersion: batch/v1 +kind: CronJob +metadata: + name: {{ include "ipeer.fullname" . }}-backup-cronjob +spec: + schedule: {{ .Values.cronjob.backup.schedule | quote }} + jobTemplate: + spec: + template: + spec: + containers: + - name: {{ include "ipeer.fullname" . }}-backup-cronjob + image: "{{ .Values.db.image.repository }}:{{ .Values.db.image.tag }}" + args: + - /bin/sh + - -c + - | + set -e + echo "[$(date)] - Starts backing up ipeer database!" + CURRENT_YEAR=$(date +%Y) + MYSQLDMP_FILE=/db_archives/$CURRENT_YEAR/$IPEER_ENV_NAME-db-$(date +%F_%T).sql.gz + mkdir -p /db_archives/$CURRENT_YEAR + mysqldump -u"$IPEER_DB_USER" -p"$IPEER_DB_PASSWORD" -h"$IPEER_DB_HOST" ipeer --single-transaction --quick | gzip > $MYSQLDMP_FILE + zgrep "Dump completed" $MYSQLDMP_FILE + env: + - name: IPEER_ENV_NAME + value: {{ include "ipeer.fullname" . }} + - name: IPEER_DB_HOST + value: {{ template "ipeer.db.fullname" . }} + - name: IPEER_DB_PORT + value: {{ .Values.db.service.port | quote }} + - name: IPEER_DB_USER + value: {{ .Values.db.auth.username | quote }} + - name: IPEER_DB_PASSWORD + valueFrom: + secretKeyRef: + {{- if .Values.db.disableExternal }} + name: {{ template "ipeer.db.secretName" . }} + key: mariadb-password + {{- else }} + name: {{ template "ipeer.fullname" . }} + key: db_password + {{- end }} + volumeMounts: + - mountPath: /db_archives + name: nfs-volume + restartPolicy: Never + volumes: + - name: nfs-volume + nfs: + path: {{ .Values.cronjob.backup.nfs.path | quote }} + server: {{ .Values.cronjob.backup.nfs.server | quote }} +{{- end }} diff --git a/ipeer/templates/cronjob.yaml b/ipeer/templates/cronjob-email.yaml similarity index 98% rename from ipeer/templates/cronjob.yaml rename to ipeer/templates/cronjob-email.yaml index 5970d06..eaf26f7 100644 --- a/ipeer/templates/cronjob.yaml +++ b/ipeer/templates/cronjob-email.yaml @@ -1,4 +1,4 @@ -{{- if .Values.cronjob.enabled }} +{{- if .Values.cronjob.email.enabled }} apiVersion: batch/v1 kind: CronJob metadata: diff --git a/ipeer/values.yaml b/ipeer/values.yaml index 6d65949..40cf3c0 100644 --- a/ipeer/values.yaml +++ b/ipeer/values.yaml @@ -25,7 +25,14 @@ worker: enabled: false cronjob: - enabled: true + email: + enabled: true + backup: + enabled: false + schedule: "00 09 * * *" + nfs: + path: 'REPLACEME' + server: 'REPLACEME' imagePullSecrets: [] nameOverride: ""