From 257c00fe0cf28eef1f792a8de661942e67c2406d Mon Sep 17 00:00:00 2001 From: Bart Geesink Date: Mon, 5 Jun 2023 21:34:03 +0200 Subject: [PATCH] Add mysqlbackup --- mysqlbackup/backup-script.sh | 16 +++++++++++++++ mysqlbackup/cronjob.yml | 39 ++++++++++++++++++++++++++++++++++++ mysqlbackup/job.yml | 39 ++++++++++++++++++++++++++++++++++++ mysqlbackup/pvc.yaml | 11 ++++++++++ 4 files changed, 105 insertions(+) create mode 100644 mysqlbackup/backup-script.sh create mode 100644 mysqlbackup/cronjob.yml create mode 100644 mysqlbackup/job.yml create mode 100644 mysqlbackup/pvc.yaml diff --git a/mysqlbackup/backup-script.sh b/mysqlbackup/backup-script.sh new file mode 100644 index 0000000..80ab23c --- /dev/null +++ b/mysqlbackup/backup-script.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# Set backup directory and file name format +BACKUP_DIR=/backup +DATE=$(date +"%Y-%m-%d-%H-%M-%S") +BACKUP_RETENTION_DAYS=7 + +# Get list of databases +DATABASES=$(mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -h $MYSQL_HOST -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema|mysql)") + +# Loop through databases and dump them to separate files +for DB in $DATABASES; do + mysqldump --single-transaction --quick -u $MYSQL_USER -p$MYSQL_PASSWORD -h $MYSQL_HOST $DB | gzip >$BACKUP_DIR/$DB-$DATE.sql.gz +done +# Remove backups older than 7 days +find $BACKUP_DIR -name "*.gz" -type f -mtime +$BACKUP_RETENTION_DAYS -delete diff --git a/mysqlbackup/cronjob.yml b/mysqlbackup/cronjob.yml new file mode 100644 index 0000000..19b8c7b --- /dev/null +++ b/mysqlbackup/cronjob.yml @@ -0,0 +1,39 @@ +apiVersion: batch/v1 +kind: CronJob +metadata: + name: mysql-backup +spec: + schedule: "0 0 * * *" + jobTemplate: + spec: + template: + spec: + containers: + - name: mysql-backup + image: mariadb:latest + command: ["sh", "/backup-script.sh"] + env: + - name: MYSQL_HOST + value: galera-mariadb-galera + - name: MYSQL_USER + value: backupuser + - name: MYSQL_PASSWORD + valueFrom: + secretKeyRef: + name: backupscrets + key: backupsecret + volumeMounts: + - name: backup-volume + mountPath: /backup + - name: backup-script + mountPath: /backup-script.sh + subPath: backup-script.sh + readOnly: true + restartPolicy: OnFailure + volumes: + - name: backup-volume + persistentVolumeClaim: + claimName: backup-pvc + - name: backup-script + configMap: + name: backup-script diff --git a/mysqlbackup/job.yml b/mysqlbackup/job.yml new file mode 100644 index 0000000..1bcb2e6 --- /dev/null +++ b/mysqlbackup/job.yml @@ -0,0 +1,39 @@ +apiVersion: batch/v1 +kind: Job +metadata: + name: mysql-backup +spec: + template: + spec: + containers: + - name: mysql-backup + image: mariadb:latest + command: [ "/bin/sh" ] + args: [ "-c", "sleep 3600" ] + env: + - name: MYSQL_HOST + value: galera-mariadb-galera + - name: MYSQL_USER + value: backupuser + - name: MYSQL_PASSWORD + valueFrom: + secretKeyRef: + name: backupscrets + key: backupsecret + volumeMounts: + - name: backup-volume + mountPath: /backup + - name: backup-script + mountPath: /backup-script.sh + subPath: backup-script.sh + readOnly: true + restartPolicy: OnFailure + volumes: + - name: backup-volume + persistentVolumeClaim: + claimName: backup-pvc + - name: backup-script + configMap: + name: backup-script + backoffLimit: 1 + diff --git a/mysqlbackup/pvc.yaml b/mysqlbackup/pvc.yaml new file mode 100644 index 0000000..47e1e54 --- /dev/null +++ b/mysqlbackup/pvc.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: backup-pvc +spec: + accessModes: + - ReadWriteOnce + storageClassName: "longhorn" + resources: + requests: + storage: 5Gi