参数名称
db_user:数据库账号
db_passwd:数据库密码
time:当前日期(20210522)
backup_dir:备份文件路径
db_name_list:数据库名称
脚本效果
备份db_name_list所有库中的表结构和数据,--ignore-table=$db_name.xxx_xxx_xxx 排除xxx_xxx_xxx表,备份文件保留当前日期和七天以前的备份文件。
db_user="xxx"
db_passwd="xxx"
time=`date --date='0 days ago' "+%Y%m%d"`
backup_dir="/data/mysql-backup/data"
db_name_list='db1 db2 db3 db4'
for db_name in $db_name_list
do
back_path=$backup_dir/$db_name
mkdir -p $back_path
mysqldump $db_name --ignore-table=$db_name.xxx_xxx_xxx >"$back_path/$time.sql"
find $back_path -mtime +7 -type f | xargs rm -f
done
如果mysql安装在docker内
db_user="xxx"
db_passwd="xxx"
time=`date --date='0 days ago' "+%Y%m%d"`
backup_dir="/data/mysql-backup/data"
db_name_list='db1 db2 db3 db4'
for db_name in $db_name_list
do
back_path=$backup_dir/$db_name
mkdir -p $back_path
docker exec -i mysql mysqldump $db_name --ignore-table=$db_name.xxx_xxx_xxx >"$back_path/$time.sql"
find $back_path -mtime +7 -type f | xargs rm -f
done