1、分库备份
bash
[root@localhost mysql-backup]# cat db_bak.sh
#!/bin/bash
k_user=root
bak_password=123456
bak_path=/root/mysql-backup/
bak_cmd="-u$bak_user -p$bak_password"
exc_db="Database|information_schema|mysql|performance_schema|sys"
dbname=`mysql $bak_cmd -e "show databases"| egrep -v $exc_db`
for db in `echo $dbname`
do
[ -d $bak_path ] || mkdir -p $bak_path
mysqldump $bak_cmd -B $db | gzip > $bak_path/${db}_`date +$Y%m%d`.sql.gz
done
2、分表备份
bash
[root@localhost mysql-backup]# cat tb_bak.sh
#!/bin/bash
k_user=root
bak_password=123456
bak_path=/root/mysql-backup/
bak_cmd="-u$bak_user -p$bak_password"
exc_db="Database|information_schema|mysql|performance_schema|sys"
db_name=`mysql $bak_cmd -e "show databases"| egrep -v $exc_db`
#echo $db_name
for db in `echo $db_name`
do
[ -d ${bak_path} ] || mkdir -p ${bak_path}
dt_name=`mysql $bak_cmd -e "use $db;show tables" | egrep -v "Tables_in_$db"`
#echo $db
#echo $dt_name
for dt in `echo $dt_name`
do
#echo $db $dt
mysqldump $bak_cmd $db ${dt} | gzip > $bak_path/${db}_${dt}.`date +%F%m%d`.sql.gz
done
done