因为现在MySQL是跑在Kubernetes集群里面的,所以这边的定时备份是用的Kubernetes的CronJob来做。具体步骤如下。
创建PVC
新建yml配置文件
mysql-backup-pvc.yml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mysql-backup
namespace: test
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: db-nfs
执行
kubectl apply -f /home/mysql-backup-k8s/mysql-backup-pvc.yml
创建CronJob
新建yml配置文件
mysql-full-backup-cron-job.yml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: mysql-backup
namespace: test
spec:
schedule: "0 0 * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: mysql-backup
imagePullPolicy: IfNotPresent
image: mysql
env:
- name: MYSQL_BACKUP_USER
value: root
- name: MYSQL_BACKUP_USER_PASSWORD
value: test123456
- name: MYSQL_HOST
value: mysql
command:
- /bin/sh
- -c
- |
set -ex
number=30
mysqldump --host=$MYSQL_HOST --user=$MYSQL_BACKUP_USER \
--password=$MYSQL_BACKUP_USER_PASSWORD \
--routines --all-databases --single-transaction \
> /mysql-backup/test-backup-full-`date +"%Y%m%d"`.sql
echo "create /mysql-backup/test-backup-full-`date +"%Y%m%d"`.sql" >> /mysql-backup/log.txt
delfile=`ls -l -crt /mysql-backup/*.sql | awk '{print $9 }' | head -1`
count=`ls -l -crt /mysql-backup/*.sql | awk '{print $9 }' | wc -l`
if [ $count -gt $number ]
then
rm $delfile
echo "delete $delfile" >> /mysql-backup/log.txt
fi
volumeMounts:
- name: mysql-backup
mountPath: /mysql-backup
- name: local-time
mountPath: /etc/localtime
readOnly: true
restartPolicy: OnFailure
volumes:
- name: mysql-backup
persistentVolumeClaim:
claimName: mysql-backup
- name: local-time
hostPath:
path: /etc/localtime
如果mysql的文件使用的是指定映射路径的方式,则配置如下
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: mysql-backup
namespace: test
spec:
schedule: "0 0 * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: mysql-backup
imagePullPolicy: IfNotPresent
image: mysql
env:
- name: MYSQL_BACKUP_USER
value: root
- name: MYSQL_BACKUP_USER_PASSWORD
value: test123456
- name: MYSQL_HOST
value: mysql
command:
- /bin/sh
- -c
- |
set -ex
mysqldump --host=$MYSQL_HOST --user=$MYSQL_BACKUP_USER \
--password=$MYSQL_BACKUP_USER_PASSWORD \
--routines --all-databases --single-transaction \
> /mysql-backup/test-backup-full-`date +"%Y%m%d"`.sql
echo "create /mysql-backup/test-backup-full-`date +"%Y%m%d"`.sql" >> /mysql-backup/log.txt
volumeMounts:
- name: mysql-backup
mountPath: /mysql-backup
- name: local-time
mountPath: /etc/localtime
readOnly: true
restartPolicy: OnFailure
volumes:
- hostPath:
path: /opt/test/mysql_backup
type: DirectoryOrCreate
name: mysql-backup
- name: local-time
hostPath:
path: /etc/localtime
执行
kubectl apply -f /home/mysql-backup-k8s/mysql-full-backup-cron-job.yml
管理定时任务
查看定时任务
[root@master mysql-backup-k8s]# kubectl get cronjob mysql-backup -n test
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
mysql-backup 0 0 * * * False 0 <none> 8s
查看执行情况
[root@master mysql-backup-k8s]# kubectl get jobs -n test
NAME COMPLETIONS DURATION AGE
mysql-backup-1585731000 1/1 16s 89s
删除定时任务
方法1:
kubectl delete -f /home/mysql-backup-k8s/mysql-full-backup-cron-job.yml
方法2:
kubectl delete cronjob mysql-backup