Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
March 3, 2021 02:22 pm GMT

Backup MySQL Databases in Kubernetes

In this post, we will show you how to create a MySQL server backup using Kubernetes CronJobs.

In our case, we do not have a managed MySQL server. But we want to backup it to our NAS, so that we have a backup in case of emergency.
For this we first build a container that can execute our tasks, because we will certainly need several tasks to backup our cluster.

CronJob Agent Container

First, we'll show you our Dockerfile so you know what we need.

FROM alpine:3.10# UpdateRUN apk --update add --no-cache bash nodejs-current yarn curl busybox-extras vim rsync git mysql-client openssh-client RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.18.0/bin/linux/amd64/kubectl && chmod +x ./kubectl && mv ./kubectl /usr/local/bin/kubectl# ScriptsRUN mkdir /srv/jobsCOPY jobs/* /srv/jobs/# Backup FolderRUN mkdir /var/backupRUN mkdir /var/backup/mysql
Enter fullscreen mode Exit fullscreen mode

Backup Script

And now our backup script which the container executes.

Our script is quite simple, we get all tables with the mysql client, export them as sql file, pack them in a zip file and send them in a 8 hours interval to our NAS.

#!/bin/bash############# SET VARIABLES ############## Env VariablesBACKUPSERVER="8.8.8.8" # Backup Server IpBACKUPDIR=/var/backup/mysqlBACKUPREMOTEDIR="/mnt/backup/kubernetes/"HOST="mariadb.default"NOW="$(date +"%Y-%m-%d")"STARTTIME=$(date +"%s")USER=mysqlUserPASS=mysqlPassword############# BUILD ENVIROMENT ############## Check if temp Backup Directory is emptymkdir $BACKUPDIRif [ "$(ls -A $BACKUPDIR)" ]; then    echo "Take action $BACKUPDIR is not Empty"    rm -f $BACKUPDIR/*.gz    rm -f $BACKUPDIR/*.mysqlelse    echo "$BACKUPDIR is Empty"fi############# BACKUP SQL DATABASES #############for DB in $(mysql -u$USER -p$PASS -h $HOST -e 'show databases' -s --skip-column-names); do    mysqldump -u$USER -p$PASS -h $HOST --lock-tables=false $DB > "$BACKUPDIR/$DB.sql";done############# ZIP BACKUP #############cd $BACKUPDIRtar -zcvf backup-${NOW}.tar.gz *.sql############# MOVE BACKUP TO REMOTE #############rsync -avz $BACKUPDIR/backup-${NOW}.tar.gz root@$BACKUPSERVER:$BACKUPREMOTEDIR# done
Enter fullscreen mode Exit fullscreen mode

Kubernetes CronJob Deployment

Finally we show you the kubernetes deployment for our agent.

In the deployment, our agent is defined as a CronJob that runs every 8 hours.
In addition, we have added an SSH key as a Conifg map so that this can write to the NAS and a certain security is given.

apiVersion: batch/v1beta1kind: CronJobmetadata:  name: backup-mariadb  namespace: defaultspec:  schedule: "0 8 * * *"  successfulJobsHistoryLimit: 1  failedJobsHistoryLimit: 1  jobTemplate:    spec:      template:        spec:          containers:            - name: cronjob-agent              image: xxx/cronjob-agent              command: ["bash",  "/srv/jobs/backup-mariadb.sh"]              volumeMounts:                - mountPath: /root/.ssh/id_rsa.pub                  name: cronjob-default-config                  subPath: id_rsa.pub                - mountPath: /root/.ssh/id_rsa                  name: cronjob-default-config                  subPath: id_rsa                  readOnly: true                - mountPath: /root/.ssh/config                  name: cronjob-default-config                  subPath: config          volumes:            - name: cronjob-default-config              configMap:                name: cronjob-default-config                defaultMode: 256          restartPolicy: Never
Enter fullscreen mode Exit fullscreen mode

Original Link: https://dev.to/n00d13/backup-mysql-databases-in-kubernetes-2g6k

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To