使用docker-compose搭建的mysql中自带了mysqldump,所以在服务器上如何使用容器中的mysqldump命令是实现备份的原理,下面是主要实现的命令
docker exec -it mysql mysqldump -u root -p$mysql_password $database_name > $backup_file
vim db_backup.sh
#!/bin/bash
# 数据库名称
database_name="say"
mysql_password="Dalian.123"
# 备份文件保存地址
backup_dir="/var/zrs/mysql/backup"
# 备份文件名前缀
backup_prefix="buckup"
# 备份文件的有效期,单位为day
backup_days="30"
dd=`date +%Y-%m-%d-%H-%M-%S`
backup_file="$backup_dir/$backup_prefix-$dd.sql"
if [ ! -d $backup_dir ];
then
mkdir -p $backup_dir;
fi
#mysqldump -u root -p$mysql_password $database_name > $backup_file
# 使用docker容器中命令
docker exec -it mysql mysqldump -u root -p$mysql_password $database_name > $backup_file
# 压缩sql文件
gzip -f $backup_file
# 写创建备份日志
echo "create $backup_dir/$database_name-$dd.dupm" >> $backup_dir/log.txt
# 清除过期的文件
find $backup_dir -name "$backup_prefix*.sql.gz" -mtime +$backup_days -exec rm {} \;
用到了请点个赞,感谢!!