docker快速搭建mysql容器定时备份数据到磁盘
Posted hwp0710
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker快速搭建mysql容器定时备份数据到磁盘相关的知识,希望对你有一定的参考价值。
step 1 创建dockfile
localhost:5.7.17 jack$ vi Dockerfile FROM mysql:5.7.17 MAINTAINER Jack.he ENV TZ="Asia/Shanghai" LANG=zh_CN.UTF-8 RUN cp /usr/share/zoneinfo/$TZ /etc/localtime && apt-get update && apt-get install -y vim && apt-get install -y cron
step 2 创建image并推送到私服
docker build -t 10.10.101.199:5000/ab-mysql-backup:5.7.17 . docker push 10.10.101.199:5000/ab-mysql-backup:5.7.17
step 3 创建启动脚本run.sh
$ mkdir -p /export/mysql/mysqlbackup/script/sql
vi /export/mysql/mysqlbackup/run.sh
#!/bin/bash echo "================start docker container...===============" docker run -d --restart=always --name mysqlbackup -e MYSQL_ROOT_PASSWORD=12345678 -v /etc/localtime:/etc/localtime:ro -v /export/mysql/mysqlbackup/script:/root 10.10.101.199:5000/ab-mysql-backup:5.7.17 echo "===============docker container started.==============="
step 4 创建数据库备份脚本backup.sh
vi /export/mysql/mysqlbackup/script/backup.sh #!/bin/bash source /etc/profile echo "......begin backup databases......" currentTime=`date "+%Y%m%d%H%M%S"` echo ‘current time = ‘$currentTime
#192.168.2.22 为需要备份服务器ip以及mysql用户名和密码 /usr/bin/mysqldump -h192.168.2.22 -uroot -p12345678 testdb > /root/sql/testdb_$currentTime.sql echo "backup databases end !!!"
step 5 启动容器并创建crontab任务
[[email protected] mysqlbackup]$sh /export/mysql/mysqlbackup/run.sh [[email protected]-ip199 mysqlbackup]$ docker exec -it mysqlbackup bash [email protected]:/# crontab -l */1 * * * * sh /root/backup.sh [email protected]:/#service cron start #启动定时任务
以上是关于docker快速搭建mysql容器定时备份数据到磁盘的主要内容,如果未能解决你的问题,请参考以下文章
docker应用-6(mysql+mycat 搭建数据库集群)
利用 Docker 快速实现 MySQL binlog 主从备份