Skip to content

MySQL CronJob backup file

You might guess from the article title, we launched MySQL in the Kubernetes. Although some people DO NOT suggest run MySQL in the Kubernetes instead of run as a standalone because of IO, performance, and reliability, we still run it as Statefulset in the Kubernetes. it looks good right now and hasn’t encountered any problems.

The MySQL cronjob backup YAML is presented below.

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: mysql-backup
  namespace: trainingmaster
spec:
  schedule: "0 2 * * *"
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          containers:
          - name: mysql
            image: imega/mysql-client
            imagePullPolicy: IfNotPresent
            command: 
              - sh
              - -c
              - >
                 now=`date +"%Y-%m-%d-%H-%M-%S"`;
                 echo "${now}";
                 mysqldump -h mysql-svc -P 3306 -uuser -ppassword trainingmaster> /sql/mysqlbackup/voicemaster-${now}.sql;
                 mysqldump -h mysql-svc -P 3306 -uuser -ppassword lite > /sql/mysqlbackup/vprc_lite-${now}.sql;
                 mysqldump -h mysql-svc -P 3306 -uuser -ppassword --all-databases > /sql/mysqlbackup/mysql_all-${now}.sql;
                 ls -l /sql ;
            volumeMounts:
            - name: sql
              mountPath: /sql
            - name: host-time
              mountPath: /etc/localtime
              readOnly: true
          nodeSelector:
            node-role/ops-host: true
          volumes:
          - name: sql
            hostPath:
              path: /root
          - name: host-time
            hostPath:
              path: /etc/localtime

Leave a Reply