Docker容器定时备份数据库并发送到指定邮箱(设计思路)
项目地址及使用说明:https://gitee.com/noovertime/mysqlmail
一、背景:
一开始的初衷是,想写一个脚本来监控服务器的高占用率进程并通过邮件发送给我,然后突发奇想,可以使用这种方式来备份我的数据库,开始动手!
二、设计思路:
通过编写shell脚本,调用linux的mail工具,mysqldump的方式来保存数据库的sql文件,通过mail工具添加到附件,最后发送到我的邮箱。
三、编写启动脚本
首先我们来编写一个启动脚本
为了方便以后的个性化配置,我们将脚本中的变量都提取到一个application.yml文件中,文件如下:
runtime: 084900 ##启动时间,因为容器时区问题,需要当前时间减去8小时 host: 172.17.0.3 ##数据库ip地址 user: root ##数据库用户 password: 123456 ##数据库密码 database: solo ##数据库名 targetmail: 1849539179@qq.com ##发送的邮箱地址
接下来我们来写一下shell脚本,逻辑也很简单,当前时间与启动时间相同时,则调用sendmail函数发送邮件
#!/bin/bash #author: chenteng 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 --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
四、构建镜像
因为我们最后要放到k8s平台上的,所以我们要构建一个镜像,在构建镜像之前,请先把application.yml demo.sh dockerfile放在同一目录下
dockerfile如下:
ps:添加了mysql的客户端,邮件mail客户端
from centos run mkdir /app && yum install -y mysql.x86_64 sendmail mailx libreport-plugin-mailx workdir /app copy 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连接主容器的数据库。
5.1 创建配置文件
为了方便调试,我把里面的shell脚本也挂载出来。
创建两个configmap,分别对应容器内的配置文件与shell脚本,后面如果不需要调试可以取消mysqlshell的挂载。
apiversion: v1 kind: configmap metadata: name: mysqlmail-conf namespace: solo data: application.yml: | runtime: 105800 host: 127.0.0.1 user: root password: 123456 database: solo targetmail: 1849539179@qq.com --- apiversion: v1 kind: configmap metadata: name: mysqlmail-shell namespace: solo data: demo.sh: | #!/bin/bash #author: chenteng 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
5.2 创建有状态服务部署文件
我们的deploy文件使用的是上篇文章中创建的mysql有状态服务的yaml,有兴趣的可以看下我上篇迁移的文章
apiversion: apps/v1 kind: statefulset metadata: name: mysql namespace: solo spec: 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: v1 kind: service metadata: name: mysql-service namespace: solo labels: app: mysql spec: 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 5 105755 105756 105757 105758 105759 starting bak mysql database mysqldump: [warning] using a password on the command line interface can be insecure. 105801 105802
从日志可以看到,邮件已经发送成功了!我们来去邮箱看一下,发现也已经成功了,至此我们的实验完美完成!
到此这篇关于docker容器定时备份数据库并发送到指定邮箱的文章就介绍到这了,更多相关docker定时备份数据库内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: python练习题