对于部署在Docker容器里的数据库进行定时备份操作。
查看mysql容器ID
创建存储备份文件的文件夹
1 2
| cd /data mkdir -p mysqlBak/data mysqlBak/log
|
修改备份脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| #!/bin/bash
container_name="73eadb6f35e2"
mysql_name="root"
mysql_pwd="123456"
backup_dir="/data/mysqlBak/data" bakup_log="/data/mysqlBak/log"
DATABASES=$(/usr/bin/docker exec -i ${container_name} mysql -u${mysql_name} -p${mysql_pwd} -e "show databases" | grep -Ev "Database|sys|information_schema|performance_schema|mysql|brp_log")
backup_clean_day=5
if [ ! -d $backup_dir ]; then mkdir -p $back_dir; fi
for db in $DATABASES do echo echo ----------$BACKUP_FILEDIR/${db}_`date "+%Y_%m_%d_%H_%M_%S"`.sql.gz BEGIN----------
/usr/bin/docker exec -i ${container_name} mysqldump -u${mysql_name} -p${mysql_pwd} --default-character-set=utf8mb4 --single-transaction --source-data=2 --flush-logs --hex-blob --triggers --routines --events --databases ${db} | gzip > ${backup_dir}/${db}`date "+%Y_%m_%d_%H_%M_%S"`.sql.gz
echo "create $bakup_log/${db}-`date "+%Y_%m_%d_%H_%M_%S"`.dupm" >> $bakup_log/log.txt echo ----------$BACKUP_FILEDIR/${db}_`date "+%Y_%m_%d_%H_%M_%S"`.sql.gz COMPLETE---------- echo done echo "done"
find ${backup_dir} -type f -mtime +${backup_clean_day} -delete
|
上传和运行脚本
1 2 3 4
| chmod a+x sqlbak.sh
./sqlbak.sh
|
设置每天凌晨一点执行
1 2 3 4 5
| crontab -b
0 1 * * * /data/mysqlBak/sqlbak.sh
|
可能出现的问题
1
| mysqldump: [ERROR] unknown variable 'source-data=2'
|
原因:版本问题,5.7版本参数为**–master-data=2**
解决方式:替换备份参数
1 2
| /usr/bin/docker exec -i ${container_name} mysqldump -u${mysql_name} -p${mysql_pwd} --default-character-set=utf8mb4 --single-transaction --master-data=2 --flush-logs --hex-blob --triggers --routines --events --databases ${db} | gzip > ${backup_dir}/${db}`date "+%Y_%m_%d_%H_%M_%S"`.sql.gz
|