通过嵌套循环实现
#! /bin/bash
back_user="-uroot -pRoot=12345."
back_dir=/backup/one
#获取数据库名
mysql ${back_user} -N -e 'show databases' | grep -Ev 'sys|mysql|information_schema|performance_schema' > dbs
while read dbname
do
[ -d ${back_dir}/$dbname ] || mkdir -p ${back_dir}/$dbname
#库备份
mysqldump ${back_user} $dbname | gzip > ${back_dir}/$dbname/"$dbname"_$(date +%F).sql.gz
#获取数据库中表名
mysql ${back_user} -N -e "show tables from $dbname" > tbs
while read tbname
do
#表备份
mysqldump ${back_user} $dbname | gzip > ${back_dir}/$dbname/"$dbname"-"$tbname"_$(date +%F).sql.gz
done < tbs
done < dbs
rm -rf dbs tbs
后续进行删除数据库中的部分数据检测是否备份成功