目录
一,分库备份
二,分库操作
三,分库分表备份
四,备份还原
一,分库备份
#!/bin/bash
mysql_cmd='-uroot -pzly666666'
bak_path=/backup/db
[ -d ${bak_path} ] || mkdir -p ${bak_path}
mysql ${mysql_cmd} -e 'show databases' -N | egrep -v 'sys|mysql|information_schema|performance_schema' > dbname
while read line
do
mysqldump ${mysql_cmd} -B $line | gzip > ${bak_path}/${line}_$(date +%F).sql.gz
done < dbname
rm -rf dbname
二,分库操作
#!/bin/bash
mysql_cmd='-uroot -pzly666666'
bak_path=/backup/db
mysql -uroot -pzly666666 -e 'show tables from homework;' -N > tbname
#mysql ${mysql_cmd} -e 'show databases' | egrep -v 'sys|mysql|information_schema|performance_schema' > dbname
while read line
do
[ -d ${bak_path}/homework ] || mkdir -p ${bak_path}/homework
mysqldump ${mysql_cmd} homework $line | gzip > ${bak_path}/homework/homework_${line}_$(date +%F).sql.gz
done < tbname
rm -rf tbname
三,分库分表备份
#!/bin/bash
mysql_cmd='-uroot -pzly666666'
bak_path=/backup/db
exclude_db='sys|mysql|information_schema|performance_schema'
mysql ${mysql_cmd} -e 'show databases' -N | egrep -v ${exclude_db} > dbname
while read db
do
[ -d ${bak_path}/$db ] || mkdir -p ${bak_path}/$db
mysqldump ${mysql_cmd} -B $db | gzip > ${bak_path}/${db}/${db}_$(date +%F).sql.gz
mysql ${mysql_cmd} -e "show tables from ${db};" -N > tbname
while read tb
do
mysqldump ${mysql_cmd} $db $tb | gzip > ${bak_path}/${db}/${db}_${tb}_$(date +%F).sql.gz
done < tbname
done < dbname
rm -rf tbname
rm -rf dbname
四,备份还原
文章来源:https://www.toymoban.com/news/detail-620071.html
文章来源地址https://www.toymoban.com/news/detail-620071.html
zcat /backup/db/homework/homework_emp_2023-07-30.sql.gz | mysql -uroot -pzly666666 homework
到了这里,关于Shell脚本实现分库分表操作的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!