码迷,mamicode.com
首页 > 数据库 > 详细

docker快速搭建mysql容器定时备份数据到磁盘

时间:2018-06-11 17:09:02      阅读:2011      评论:0      收藏:0      [点我收藏+]

标签:apt   container   data   数据   step   host   www   5.7   dock   

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任务

[www@cent7-ip199 mysqlbackup]$sh /export/mysql/mysqlbackup/run.sh
[www@cent7-ip199 mysqlbackup]$ docker exec -it mysqlbackup bash
root@f1ae964e9f21:/# crontab -l
*/1 * * * * sh /root/backup.sh
root@f1ae964e9f21:/#service cron start #启动定时任务

 

 
 

docker快速搭建mysql容器定时备份数据到磁盘

标签:apt   container   data   数据   step   host   www   5.7   dock   

原文地址:https://www.cnblogs.com/hwp0710/p/7743613.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!