因为现在MySQL是跑在Kubernetes集群里面的,所以这边的定时备份是用的Kubernetes的CronJob来做。具体步骤如下。

创建PVC

新建yml配置文件

mysql-backup-pvc.yml

  1. kind: PersistentVolumeClaim
  2. apiVersion: v1
  3. metadata:
  4. name: mysql-backup
  5. namespace: test
  6. spec:
  7. accessModes:
  8. - ReadWriteOnce
  9. resources:
  10. requests:
  11. storage: 10Gi
  12. storageClassName: db-nfs

执行

  1. kubectl apply -f /home/mysql-backup-k8s/mysql-backup-pvc.yml

创建CronJob

新建yml配置文件

mysql-full-backup-cron-job.yml

  1. apiVersion: batch/v1beta1
  2. kind: CronJob
  3. metadata:
  4. name: mysql-backup
  5. namespace: test
  6. spec:
  7. schedule: "0 0 * * *"
  8. jobTemplate:
  9. spec:
  10. template:
  11. spec:
  12. containers:
  13. - name: mysql-backup
  14. imagePullPolicy: IfNotPresent
  15. image: mysql
  16. env:
  17. - name: MYSQL_BACKUP_USER
  18. value: root
  19. - name: MYSQL_BACKUP_USER_PASSWORD
  20. value: test123456
  21. - name: MYSQL_HOST
  22. value: mysql
  23. command:
  24. - /bin/sh
  25. - -c
  26. - |
  27. set -ex
  28. number=30
  29. mysqldump --host=$MYSQL_HOST --user=$MYSQL_BACKUP_USER \
  30. --password=$MYSQL_BACKUP_USER_PASSWORD \
  31. --routines --all-databases --single-transaction \
  32. > /mysql-backup/test-backup-full-`date +"%Y%m%d"`.sql
  33. echo "create /mysql-backup/test-backup-full-`date +"%Y%m%d"`.sql" >> /mysql-backup/log.txt
  34. delfile=`ls -l -crt /mysql-backup/*.sql | awk '{print $9 }' | head -1`
  35. count=`ls -l -crt /mysql-backup/*.sql | awk '{print $9 }' | wc -l`
  36. if [ $count -gt $number ]
  37. then
  38. rm $delfile
  39. echo "delete $delfile" >> /mysql-backup/log.txt
  40. fi
  41. volumeMounts:
  42. - name: mysql-backup
  43. mountPath: /mysql-backup
  44. - name: local-time
  45. mountPath: /etc/localtime
  46. readOnly: true
  47. restartPolicy: OnFailure
  48. volumes:
  49. - name: mysql-backup
  50. persistentVolumeClaim:
  51. claimName: mysql-backup
  52. - name: local-time
  53. hostPath:
  54. path: /etc/localtime

如果mysql的文件使用的是指定映射路径的方式,则配置如下

  1. apiVersion: batch/v1beta1
  2. kind: CronJob
  3. metadata:
  4. name: mysql-backup
  5. namespace: test
  6. spec:
  7. schedule: "0 0 * * *"
  8. jobTemplate:
  9. spec:
  10. template:
  11. spec:
  12. containers:
  13. - name: mysql-backup
  14. imagePullPolicy: IfNotPresent
  15. image: mysql
  16. env:
  17. - name: MYSQL_BACKUP_USER
  18. value: root
  19. - name: MYSQL_BACKUP_USER_PASSWORD
  20. value: test123456
  21. - name: MYSQL_HOST
  22. value: mysql
  23. command:
  24. - /bin/sh
  25. - -c
  26. - |
  27. set -ex
  28. mysqldump --host=$MYSQL_HOST --user=$MYSQL_BACKUP_USER \
  29. --password=$MYSQL_BACKUP_USER_PASSWORD \
  30. --routines --all-databases --single-transaction \
  31. > /mysql-backup/test-backup-full-`date +"%Y%m%d"`.sql
  32. echo "create /mysql-backup/test-backup-full-`date +"%Y%m%d"`.sql" >> /mysql-backup/log.txt
  33. volumeMounts:
  34. - name: mysql-backup
  35. mountPath: /mysql-backup
  36. - name: local-time
  37. mountPath: /etc/localtime
  38. readOnly: true
  39. restartPolicy: OnFailure
  40. volumes:
  41. - hostPath:
  42. path: /opt/test/mysql_backup
  43. type: DirectoryOrCreate
  44. name: mysql-backup
  45. - name: local-time
  46. hostPath:
  47. path: /etc/localtime

执行

  1. kubectl apply -f /home/mysql-backup-k8s/mysql-full-backup-cron-job.yml

管理定时任务

查看定时任务

  1. [root@master mysql-backup-k8s]# kubectl get cronjob mysql-backup -n test
  2. NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
  3. mysql-backup 0 0 * * * False 0 <none> 8s

查看执行情况

  1. [root@master mysql-backup-k8s]# kubectl get jobs -n test
  2. NAME COMPLETIONS DURATION AGE
  3. mysql-backup-1585731000 1/1 16s 89s

删除定时任务

方法1:

  1. kubectl delete -f /home/mysql-backup-k8s/mysql-full-backup-cron-job.yml

方法2:

  1. kubectl delete cronjob mysql-backup