...
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=backup DB_PWD=****** HOST=host.docker.internal PORT=3355 # cleanup old backups removeOldBackups() { # remove backups older than -mmin for minutes, -mtime for days for i in `find $BACKUP_ARCHIVEDIR/*.tar.gz $BACKUP_ARCHIVEDIR/*.dmp $BACKUP_ARCHIVEDIR/*.err \ 2>/dev/null -type f -mmin +60 -ls | grep -o '\S\+$'` do rm $i >/dev/null 2>/dev/null done } cd / echo "Backup Started" # call backup utility { "$MARIADB_BACKUP_EXE_DIR"/mysqldump --all-databases --host=$HOST --port=$PORT \ --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 rm $BACKUP_ARCHIVEDIR/$ARCHIVE >/dev/null 2>/dev/null exit 1 else rm $BACKUP_WORKDIR/$DMPFILE \ $BACKUP_WORKDIR/$ERRFILE >/dev/null 2>/dev/null removeOldBackups exit 0 fi |
* Old backups will only be removed if the backup succeeds
docker build . -t ktimoney/mysqldump_backup
...
Code Block | ||||
---|---|---|---|---|
| ||||
apiVersion: batch/v1beta1 kind: CronJob metadata: name: batch-every-10-minutes spec: schedule: "*/10 * * * *" concurrencyPolicy: Forbid jobTemplate: spec: template: spec: volumes: - name: backup-volume persistentVolumeClaim: claimName: backup-claim containers: - name: mysqldump-backup image: ktimoney/mysqldump_backup volumeMounts: - mountPath: "/tmp" name: backup-volume restartPolicy: Never backoffLimit: 3 |
* backoffLimit sets the number of times (+1) the cron will retry the job before it stops
kubectl create -f backupCronjob.yaml
...