编写脚本,使用mysqldump实现分库分表备份。
1、获取分库备份的库名列表
[root@web01 scripts]# mysql -uroot -p123456 -e "show databases;" | egrep -v "Database|information_schema|mysql|performance_schema|sys"
mysql: [Warning] Using a password on the command line interface can be insecure.
db1
it
wp
2、编写备份脚本
#!/bin/bash
##############################################################
# File Name: db_table2.sh
# Version: V1.0
# Author: wp
# Email: wp@163.com
# Organization: http://www.xx.com/xx/
# Created Time : 2023-07-31 06:54:13
# Description:
##############################################################
#定义数据库连接信息
bak_user=root
bak_passwd=123456
#定义备份文件存储目录
bak_path=/server/dbbak
bak_cmd="-u$bak_user -p$bak_passwd"
#定义要排除的数据名
exclude_db="Database|information_schema|mysql|performance_schema|test"
db_name=`mysql $bak_cmd -e "show databases;" | egrep -v $exclude_db`
for db in `echo $db_name`
do[ -d ${bak_path}/$db ] || mkdir -p ${bak_path}/$dbtable_name=`mysql $bak_cmd -e "use $db;show tables;" | grep -v "Tables_in"`for table in `echo ${table_name}`domysqldump $bak_cmd $db $table |gzip > $bak_path/${db}/${db}_${table}.`date +%Y%m%d`.sql.gzdone
done
3、执行脚本,查看备份情况
[root@web01 scripts]# vim db_table2.sh
[root@web01 scripts]# ll /server/dbbak
total 12
drwxr-xr-x. 2 root root 96 Jul 31 06:56 db1
drwxr-xr-x. 2 root root 6 Jul 31 06:56 it
drwxr-xr-x. 2 root root 8192 Jul 31 06:56 sys
drwxr-xr-x. 2 root root 6 Jul 31 06:56 wp