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/sqlvi /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 #启动定时任务
原文地址:https://www.cnblogs.com/hwp0710/p/7743613.html
时间: 2024-10-08 04:32:49