Shell脚本实现分库分表操作

目录

一,分库备份

二,分库操作

三,分库分表备份

四,备份还原


一,分库备份

python 复制代码
#!/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

二,分库操作

python 复制代码
#!/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

三,分库分表备份

python 复制代码
#!/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

四,备份还原

zcat /backup/db/homework/homework_emp_2023-07-30.sql.gz | mysql -uroot -pzly666666 homework

相关推荐
Ai 编码助手2 小时前
MySQL中distinct与group by之间的性能进行比较
数据库·mysql
白云如幻3 小时前
MySQL排序查询
数据库·mysql
苹果醋33 小时前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx
stars_User4 小时前
MySQL数据库面试题(下)
数据库·mysql
Yaml44 小时前
智能化健身房管理:Spring Boot与Vue的创新解决方案
前端·spring boot·后端·mysql·vue·健身房管理
yanwushu4 小时前
Xserver v1.4.2发布,支持自动重载 nginx 配置
mysql·nginx·php·个人开发·composer
蓝眸少年CY5 小时前
MySQL 【流程控制】函数
mysql
掘金-我是哪吒6 小时前
微服务mysql,redis,elasticsearch, kibana,cassandra,mongodb, kafka
redis·mysql·mongodb·elasticsearch·微服务
zmgst7 小时前
canal1.1.7使用canal-adapter进行mysql同步数据
java·数据库·mysql
令狐少侠20117 小时前
explain执行计划分析 ref_
mysql