標籤:apt container data 資料 step host www 5.7 dock
step 1 建立dockfile
localhost:5.7.17 jack$ vi DockerfileFROM mysql:5.7.17MAINTAINER Jack.heENV TZ="Asia/Shanghai" LANG=zh_CN.UTF-8RUN 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/bashecho "================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.17echo "===============docker container started.==============="
step 4 建立Database Backup指令碼backup.sh
vi /export/mysql/mysqlbackup/script/backup.sh#!/bin/bashsource /etc/profileecho "......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.sqlecho "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容器定時備份資料到磁碟