mysqldump 实现分库分表备份

# 分库
vim db_dump.sh
#!/bin/sh
user=root
passwd=123456
path=/backup/
cmd="-u$user -p$passwd"
db="information_schema|performance_schema|sys|Database"
db_name=`mysql $cmd -e "show databases;" | egrep -v $db`
for i in `echo $db_name`
do
	[ -d $path ] || mkdir -p $path
	mysqldump $cmd -B $i | gzip > $path/${i}_`date +%Y%m%d`.sql.gz
done

# 检测
sh db_dump.sh
ll /backup/

# 分表
vim dt_dump.sh
#!/bin/sh
user=root
passwd=123456
path=/backup/
cmd="-u$user -p$passwd"
db="information_schema|performance_schema|sys|Database"
db_name=`mysql $cmd -e "show databases;" | egrep -v $db`
for i in `echo $db_name`
do
	[ -d $path/$i ] || mkdir -p ${path}/$i
	name=`mysql $cmd -e "use $i;show tables;" | grep -v "Tables_in"`
	for tables in `echo $name`
	do
		mysqldump $cmd $i $table --set-gtid-purged=OFF | gzip > $path/${i}_${table}.`date +%Y%m%d`.sql.gz
	done
done

# 检测
sh dt_dump.sh
ll /backup/

你可能感兴趣的:(linux,服务器,mysql)