MySQL分库备份与分表备份

1、分库备份

要求:将mysql数据库中的用户数据库备份,备份的数据库文件以时间命名

脚本内容如下:

[[email protected] scripts]# vim backup_database.sh

#!/bin/bash

mysql_user=root

mysql_pass=123456

mkdir -p /backup

for n in `mysql -u$mysql_user -p$mysql_pass -e 'show databases;' 2>/dev/null|grep -Ev '_schema|mysql'|sed '1d'`;

do

    mysqldump -u$mysql_user -p$mysql_pass -B $n 2>/dev/null>/backup/${n}_`date +%Y_%m_%d`.sql

done

 

执行脚本进行测试:

[[email protected] scripts]# sh -x backup_database.sh

+ mysql_user=root

+ mysql_pass=123456

+ mkdir -p /backup

++ mysql -uroot -p123456 -e 'show databases;'

++ grep -Ev '_schema|mysql'

++ sed 1d

+ for n in '`mysql -u$mysql_user -p$mysql_pass -e '\''show databases;'\'' 2>/dev/null|grep -Ev '\''_schema|mysql'\''|sed '\''1d'\''`'

+ mysqldump -uroot -p123456 -B oldboy

+ for n in '`mysql -u$mysql_user -p$mysql_pass -e '\''show databases;'\'' 2>/dev/null|grep -Ev '\''_schema|mysql'\''|sed '\''1d'\''`'

+ mysqldump -uroot -p123456 -B oldgirl

+ for n in '`mysql -u$mysql_user -p$mysql_pass -e '\''show databases;'\'' 2>/dev/null|grep -Ev '\''_schema|mysql'\''|sed '\''1d'\''`'

+ mysqldump -uroot -p123456 -B test

[[email protected] scripts]#

 

检查备份情况:

[[email protected] scripts]# tree /backup/

/backup/

├── oldboy_2018_07_04.sql

├── oldgirl_2018_07_04.sql

└── test_2018_07_04.sql

 

0 directories, 3 files

[[email protected] scripts]#

 

2、分库分表备份

要求:备份每个数据库的表,同一个库中的表,放在对应数据库名字命名的目录下

脚本内容如下:

[[email protected] scripts]# vim backup_tables.sh

#!/bin/bash

mysql_user=root

mysql_pass=123456

mkdir -p /backup

for n in `mysql -u$mysql_user -p$mysql_pass -e 'show databases;' 2>/dev/null|grep -Ev '_schema|mysql'|sed '1d'`;

do

    mkdir -p /backup/$n

    for m in `mysql -u$mysql_user -p$mysql_pass $n -e "show tables;" 2>/dev/null|sed '1d'`;

    do

        mysqldump -u$mysql_user -p$mysql_pass $n $m 2>/dev/null>/backup/${n}/${m}_`date +%Y_%m_%d`.sql

    done

done

 

执行脚本进行测试:

[[email protected] scripts]# sh -x backup_tables.sh

+ mysql_user=root

+ mysql_pass=123456

+ mkdir -p /backup

++ mysql -uroot -p123456 -e 'show databases;'

++ grep -Ev '_schema|mysql'

++ sed 1d

+ for n in '`mysql -u$mysql_user -p$mysql_pass -e '\''show databases;'\'' 2>/dev/null|grep -Ev '\''_schema|mysql'\''|sed '\''1d'\''`'

+ mkdir -p /backup/oldboy

++ sed 1d

++ mysql -uroot -p123456 oldboy -e 'show tables;'

+ for m in '`mysql -u$mysql_user -p$mysql_pass $n -e "show tables;" 2>/dev/null|sed '\''1d'\''`'

+ mysqldump -uroot -p123456 oldboy students

+ for m in '`mysql -u$mysql_user -p$mysql_pass $n -e "show tables;" 2>/dev/null|sed '\''1d'\''`'

+ mysqldump -uroot -p123456 oldboy test

+ for m in '`mysql -u$mysql_user -p$mysql_pass $n -e "show tables;" 2>/dev/null|sed '\''1d'\''`'

+ mysqldump -uroot -p123456 oldboy test2

+ for n in '`mysql -u$mysql_user -p$mysql_pass -e '\''show databases;'\'' 2>/dev/null|grep -Ev '\''_schema|mysql'\''|sed '\''1d'\''`'

+ mkdir -p /backup/oldgirl

++ mysql -uroot -p123456 oldgirl -e 'show tables;'

++ sed 1d

+ for m in '`mysql -u$mysql_user -p$mysql_pass $n -e "show tables;" 2>/dev/null|sed '\''1d'\''`'

+ mysqldump -uroot -p123456 oldgirl test3

+ for n in '`mysql -u$mysql_user -p$mysql_pass -e '\''show databases;'\'' 2>/dev/null|grep -Ev '\''_schema|mysql'\''|sed '\''1d'\''`'

+ mkdir -p /backup/test

++ mysql -uroot -p123456 test -e 'show tables;'

++ sed 1d

+ for m in '`mysql -u$mysql_user -p$mysql_pass $n -e "show tables;" 2>/dev/null|sed '\''1d'\''`'

+ mysqldump -uroot -p123456 test test4

[[email protected] scripts]#

 

检查备份情况:

[[email protected] scripts]# tree /backup/

/backup/

├── oldboy

│   ├── students_2018_07_04.sql

│   ├── test_2018_07_04.sql

│   └── test2_2018_07_04.sql

├── oldgirl

│   └── test3_2018_07_04.sql

└── test

   └── test4_2018_07_04.sql

 

3 directories, 5 files

[[email protected] scripts]#