一开始的初衷是,想写一个脚本来监控服务器的高占用率进程并通过邮件发送给我,然后突发奇想,可以使用这种方式来备份我的数据库,开始动手!
通过编写Shell脚本,调用Linux的mail工具,MySQLDump的方式来保存数据库的sql文件,通过Mail工具添加到附件,最后发送到我的邮箱。
首先我们来编写一个启动脚本 为了方便以后的个性化配置,我们将脚本中的变量都提取到一个application.yml文件中,文件如下:
RUNTIME: 084900 HOST: 172.16.1.2USER: rootPORT: 3306PASSWORD: 123456DATABASE: deanTARGETMAIL: deanmr@qq.com
接下来我们来写一下shell脚本,逻辑也很简单,当前时间与启动时间相同时,则调用sendmail函数发送邮件
#!/bin/bashRUNTIME=$(cat ./application.yml | grep RUNTIME| awk '{print $2}')HOST=$(cat ./application.yml | grep HOST| awk '{print $2}')USER=$(cat ./application.yml | grep USER| awk '{print $2}')PORT=$(cat ./application.yml | grep PORT| awk '{print $2}')PASSWORD=$(cat ./application.yml | grep PASSWORD| awk '{print $2}')DATABASE=$(cat ./application.yml | grep DATABASE| awk '{print $2}')TARGETMAIL=$(cat ./application.yml | grep TARGETMAIL| awk '{print $2}')function sendmail(){ echo -e "mysqlbak_$CURRENT_TIME" |mail -s "mysqlbak_$CURRENT_TIME" -a $DATABASE.sql $TARGETMAIL sleep 1}while truedo CURRENT_TIME=$(date +%H%M%S) if [ $CURRENT_TIME = $RUNTIME ];then echo "starting bak mysql database" sendmail continue else echo $CURRENT_TIME sleep 1 fidone
因为我们最后要放到k8s平台上的,所以我们要构建一个镜像,在构建镜像之前,请先把application.yml demo.sh Dockerfile放在同一目录下 Dockerfile如下:PS:添加了mysql的客户端,邮件mail客户端
FROM centosRUN mkdir /app && yum install -y mysql.x86_64 sendmail mailx libreport-plugin-mailx WORKDIR /appCOPY demo.sh .COPY application.yml .CMD ["/bin/sh","demo.sh"]
使用docker build命令构建镜像,要记得加一下最后的点
docker build -t mysqlmail-bak:1.0.1 .
边车容器(sidecar):边车容器就是与主容器一起在一个pod中运行的容器,为业务容器赋能,共享一个网络空间,所以可以用127.0.0.1:3306连接主容器的数据库。
为了方便调试,我把里面的shell脚本也挂载出来。创建两个configmap,分别对应容器内的配置文件与shell脚本,后面如果不需要调试可以取消mysqlshell的挂载。
apiVersion: v1kind: ConfigMapmetadata: name: mysqlmail-conf namespace: deandata: application.yml: | RUNTIME: 105800 HOST: 127.0.0.1 USER: root PASSWORD: 123456 DATABASE: dean TARGETMAIL: deanmr@qq.com---apiVersion: v1kind: ConfigMapmetadata: name: mysqlmail-shell namespace: deandata: demo.sh: | #!/bin/bash RUNTIME=$(cat ./application.yml | grep RUNTIME| awk '{print $2}') HOST=$(cat ./application.yml | grep HOST| awk '{print $2}') USER=$(cat ./application.yml | grep USER| awk '{print $2}') PASSWORD=$(cat ./application.yml | grep PASSWORD| awk '{print $2}') DATABASE=$(cat ./application.yml | grep DATABASE| awk '{print $2}') TARGETMAIL=$(cat ./application.yml | grep TARGETMAIL| awk '{print $2}') function sendmail(){ mysqldump -h$HOST -u$USER -p$PASSWORD --complete-insert --skip-add-drop-table --column-statistics=0 --hex-blob $DATABASE > $DATABASE.sql echo -e "mysqlbak_$CURRENT_TIME" |mail -s "mysqlbak_$CURRENT_TIME" -a $DATABASE.sql $TARGETMAIL sleep 1 } while true do CURRENT_TIME=$(date +%H%M%S) if [ $CURRENT_TIME = $RUNTIME ];then echo "starting bak mysql database" sendmail continue else echo $CURRENT_TIME sleep 1 fi done
我们的deploy文件使用的是上篇文章中创建的mysql有状态服务的yaml
apiVersion: apps/v1kind: StatefulSetmetadata: name: mysql namespace: deanspec: serviceName: mysql-service selector: matchLabels: app: mysql replicas: 1 template: metadata: labels: app: mysql spec: containers: - name: mysqlmail-bak imagePullPolicy: IfNotPresent image: mysqlmail-bak:1.0.1 volumeMounts: - name: mysqlmail-conf mountPath: /app/application.yml subPath: application.yml - name: mysqlmail-shell mountPath: /app/demo.sh subPath: demo.sh - name: mysql-pod imagePullPolicy: IfNotPresent image: mysql:5.7 env: - name: MYSQL_ROOT_PASSWORD value: "123456" ports: - containerPort: 3306 name: msyql-listin volumeMounts: - name: mysql-data mountPath: /var/lib/mysql subPath: mysql-data - name: mysql-conf mountPath: /etc/mysql/conf.d/my.cnf subPath: my.cnf volumes: - name: mysql-data hostPath: path: /data/mysql - name: mysql-conf configMap: name: mysql-conf - name: mysqlmail-conf configMap: name: mysqlmail-conf - name: mysqlmail-shell configMap: name: mysqlmail-shell---apiVersion: v1kind: Servicemetadata: name: mysql-service namespace: dean labels: app: mysqlspec: ports: - targetPort: 3306 port: 3306 clusterIP: None selector: app: mysql
我们上面给他定的时间是RUNTIME: 105800,上海时区也就是18点58分,我们来看一下效果 查看日志, 注意:当一个pod包含多个容器时,要使用 -c 参数指定查看哪个容器
[root@VM-24-15-centos solo]# kubectl logs -n solo mysql-0 -c mysqlmail-bak | grep mysql -C 5105755105756105757105758105759starting bak mysql databasemysqldump: [Warning] Using a password on the command line interface can be insecure.105801105802
从日志可以看到,邮件已经发送成功了!我们来去邮箱看一下,发现也已经成功了,至此我们的实验完美完成!
本文链接:http://www.28at.com/showinfo-26-55252-0.html我们一起聊聊K8s定时备份MySQL并发送到指定邮箱
声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。邮件:2376512515@qq.com