Add mysqlbackup

This commit is contained in:
Bart Geesink 2023-06-05 21:34:03 +02:00
parent a0c8a0f2d9
commit 257c00fe0c
4 changed files with 105 additions and 0 deletions

View File

@ -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

39
mysqlbackup/cronjob.yml Normal file
View File

@ -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

39
mysqlbackup/job.yml Normal file
View File

@ -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

11
mysqlbackup/pvc.yaml Normal file
View File

@ -0,0 +1,11 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: backup-pvc
spec:
accessModes:
- ReadWriteOnce
storageClassName: "longhorn"
resources:
requests:
storage: 5Gi