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

相关推荐
e***09634 分钟前
MySQL-递归查询
数据库·windows·mysql
('-')35 分钟前
《从根上理解MySQL是怎样运行的》第十三章笔记
数据库·笔记·mysql
翔云1234561 小时前
MySQL中,binlog文件开头的Previous_gtids_log_event是如何计算的
数据库·mysql·adb
Alex Gram1 小时前
Mysql增量同步到PostgreSQL实战
数据库·mysql·postgresql
O***P5712 小时前
【MySQL】MySQL内置函数--日期函数字符串函数数学函数其他相关函数
android·mysql·adb
z***43842 小时前
MySQL-mysql zip安装包配置教程
android·mysql·adb
x***13392 小时前
MySQL 篇 - Java 连接 MySQL 数据库并实现数据交互
java·数据库·mysql
0***v7774 小时前
MySQL四种备份表的方式
mysql·adb·oracle
盼哥PyAI实验室12 小时前
MySQL 数据库入门第一课:安装、账户、库、表与数据操作详解
数据库·mysql
h***593314 小时前
MySQL如何执行.sql 文件:详细教学指南
数据库·mysql