#!/bin/bash
#crontab -e
#55 23 * * * /app/mysql_backup/mysqldump.sh
#crontab -l
current_time=`date +"%Y%m%d"`
backpath=/app/mysql_backup/$current_time/
#package_list是二进制安装版本
#container_list是容器安装版本
#(ip:port:username:passwd ip2:port2:username2:passwd2)以空格分隔
package_list=(192.168.8.148:3306:root:xxxxxx )
container_list=(192.168.8.148:3306:root:xxxxxx )
del_backup(){
#删除7天前的日志备份
old_time=`date -d "-7 day" +"%Y%m%d"`
old_backpath=/app/mysql_backup/$old_time/
#echo $old_backpath
if [ -d $old_backpath ];then
rm -rf $old_backpath
echo "$old_backpath was clear"
fi
}
#备份所有数据库 二进制部署方式
package(){
for i in ${package_list[*]}; do
#echo $i
ipaddr=`echo $i|awk -F: '{print $1}'`
port=`echo $i|awk -F: '{print $2}'`
username=`echo $i|awk -F: '{print $3}'`
passwd=`echo $i|awk -F: '{print $4}'`
echo $ipaddr $port $username $passwd
dsturl=$backpath$ipaddr
echo $dsturl
[ -d $dsturl ] || mkdir -p $dsturl
mysqldump -u$username -p$passwd -h $ipaddr -P $port -A > $dsturl/package.sql
if [ $? -eq 0 ];then
echo "$current_time backup finish"
else
echo "$current_time backup error"
fi
done
}
#备份所有数据库 容器化部署
container(){
for i in ${container_list[*]}; do
#echo $i
ipaddr=`echo $i|awk -F: '{print $1}'`
port=`echo $i|awk -F: '{print $2}'`
username=`echo $i|awk -F: '{print $3}'`
passwd=`echo $i|awk -F: '{print $4}'`
echo $ipaddr $port $username $passwd
dsturl=$backpath$ipaddr
echo $dsturl
[ -d $dsturl ] || mkdir -p $dsturl
mysqldump -u$username -p$passwd -h$ipaddr -P$port -A > $dsturl/container.sql
if [ $? -eq 0 ];then
echo "$current_time backup finish"
else
echo "$current_time backup error"
fi
done
}
package
container
del_backup
mysql定时备份脚本
最新推荐文章于 2022-12-29 09:06:58 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)