...
Dockerfile
Code Block | ||||
---|---|---|---|---|
| ||||
FROM mariadb:latest RUN apt update COPY mysqldump_backup.sh . ENTRYPOINT [ "/bin/sh" ] CMD [ "./mysqldump_backup.sh" ] |
Shell script
Code Block | ||||
---|---|---|---|---|
| ||||
#!/bin/sh FILENAME=$(basename $0 .sh) BACKUP_TS=$(date +%F-%T) ARCHIVE=${FILENAME}_${BACKUP_TS}.tar.gz ERRFILE=${FILENAME}_${BACKUP_TS}.err DMPFILE=${FILENAME}_${BACKUP_TS}.dmp LOG_DIR=/tmp BACKUP_WORKDIR=/tmp BACKUP_ARCHIVEDIR=/tmp MARIADB_BACKUP_EXE_DIR="/usr/bin" DB_USER=policy DB_PWD=******* HOST=host.docker.internal PORT=3355 # cleanup old backups removeOldBackups() { for i in `ls $BACKUP_ARCHIVEDIR/$FILENAME*.tar.gz | grep -v $ARCHIVE` do rm $i >/dev/null 2>/dev/null done } cd / echo "Backup Started" # call backup utility { "$MARIADB_BACKUP_EXE_DIR"/mysqldump --all-databases --add-drop-database --compact --routines --host=$HOST --user=$DB_USER --password= $DB_PWD --log-error=/tmp/$ERRFILE > /tmp/$DMPFILE } echo >&2 "Backup Complete" # create a tar archive from the backup directory tar cvzf $BACKUP_ARCHIVEDIR/$ARCHIVE $BACKUP_WORKDIR/$DMPFILE $BACKUP_WORKDIR/$ERRFILE >/dev/null 2>/dev/null # check for errors if [ -s "$BACKUP_WORKDIR/$ERRFILE" ] then exit 1 else rm $BACKUP_WORKDIR/$DMPFILE $BACKUP_WORKDIR/$ERRFILE removeOldBackups exit 0 fi |
...
Your backup will be available in the tar.gz file in the c:\mariadb\backup directory once docker completes
The kubernetes cron can be created using the following 3 files:
Code Block | ||||
---|---|---|---|---|
| ||||
apiVersion: v1
kind: PersistentVolume
metadata:
name: local-pv
spec:
capacity:
storage: 500Mi
volumeMode: Filesystem
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
storageClassName: local-storage
local:
path: /run/desktop/mnt/host/c/mariadb/backup
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- docker-desktop |
kubectl create -f backupPersistentVolume.yaml
Code Block | ||||
---|---|---|---|---|
| ||||
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: backup-claim
spec:
storageClassName: local-storage
accessModes:
- ReadWriteMany
resources:
requests:
storage: 500Mi |
kubectl create -f backupPersistentClaim.yaml
Code Block | ||||
---|---|---|---|---|
| ||||
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: batch-every-10-minutes
spec:
schedule: "*/10 * * * *"
concurrencyPolicy: Forbid
jobTemplate:
spec:
template:
spec:
restartPolicy: OnFailure
containers:
- name: mysqldump-backup
image: ktimoney/mysqldump_backup
volumeMounts:
- mountPath: "/tmp"
name: backup-volume
volumes:
- name: backup-volume
persistentVolumeClaim:
claimName: backup-claim |
kubectl create -f backupCronjob.yaml
This will back up the local copy of mariadb every 10 minutes to the c:\mariadb\backup folder.
Links
Backing Up and Restoring Databases
...