?查看數(shù)據(jù)和數(shù)據(jù)表
mysql -uroot -p'123456' -e 'show databases'
mysql -uroot -p'123456' -e 'show tables from cb_d'
刪除頭部Database和數(shù)據(jù)庫自帶的表
mysql -uroot -p'123456' -e 'show databases' -N | egrep -v "information_schema|mysql|performance_schema|sys"
?編寫腳本
#!/bin/bash
#********************************************************************
#Author: cb
#Date: 2023-11-02
#FileName: cbmysql.sh
#Description: The test script
#********************************************************************
#除去系統(tǒng)自帶的庫以外,再將存在的數(shù)據(jù)庫名分別保存到dbname文件中
mysql -uroot -p'123456' -e 'show databases' -N | egrep -v 'information_schema|mysql|performance_schema|sys' > dbname
# 判斷是否有/backup/db目錄,沒有則創(chuàng)建
[ -d /backup/db ] || mkdir -p /backup/db
#一行一行讀取dbname中的庫名,然后開始備份數(shù)據(jù)庫,進(jìn)而分別備份數(shù)據(jù)庫中的表
while read line
do
#備份數(shù)據(jù)庫
mysqldump -uroot -p'123456' -B $line | gzip > /backup/db/${line}_$(date +%F).sql.gz
#分別將數(shù)據(jù)庫中的表名保存到tbname文件中
mysql -uroot -p'123456' -e "show tables from $line" -N > tbname
#判斷是否有/backup/db/$line目錄,沒有則創(chuàng)建
[ -d /backup/db/$line ] || mkdir /backup/db/$line
#讀取tbname中的表名,備份數(shù)據(jù)表
while read line2
do
#備份數(shù)據(jù)表
mysqldump -uroot -p'123456' $line $line2 | gzip > /backup/db/$line/${line2}_$(date +%F).sql.gz
done < tbname
done < dbname
檢查腳本運(yùn)行備份數(shù)據(jù)庫
tree /backup/db
分表分庫備份成功
還原檢測
刪除數(shù)據(jù)庫并查看庫
mysql -uroot -p'123456' -e 'drop database cb_d'
mysql -uroot -p'123456' -e 'show databases'
開始還原
使用備份的庫進(jìn)行還原,由于是壓縮文件,使用壓縮還原
gunzip < /backup/db/cb_d_2023-11-02.sql.gz | mysql -uroot -p'123456' -B
查看數(shù)據(jù)庫
文章來源:http://www.zghlxwxcb.cn/news/detail-742739.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-742739.html
到了這里,關(guān)于編寫shell腳本,利用mysqldump實(shí)現(xiàn)MySQL數(shù)據(jù)庫分庫分表備份的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!