shell脚本分库分表备份

1.取出数据库中的用户名称

[root@db01 ~]# mysql -uroot -e "show databases;" | sed 1d |egrep -v "*_schema|mysql|test"

脚本

[root@db01 ~]# cat mysql-bak.sh 
#!/usr/bin/bash

db_name=$(mysql -uroot -e "show databases;" | sed 1d |egrep -v "*_schema|mysql|test")
Date=$(date +%F)
db_path=/mysql/data
 
[ -d $db_path ] || mkdir -p $db_path
for i in $db_name
do
    mysqldump -uroot -B ${i} >$db_path/${i}_${Date}.sql
    if [ -f $db_path/${i}_${Date}.sql ];then
        echo "$i is ok"
    else 
        echo "$i is err"
    fi
    db_table=$(mysql -uroot -e "use $i; show tables;"|sed 1d)
    for j in $db_table
    do
        [ -d $db_path/$i/$Date ] || mkdir $db_path/$i/$Date -p
        mysqldump -uroot $i $j >$db_path/$i/$Date/${j}.sql
    done 
done 

你可能感兴趣的:(shell脚本分库分表备份)