[MySQL-MM] 生產(chǎn)環(huán)境自動恢復(fù)MM中一臺M2庫的過程,分享從零開始寫的自動化重建腳本以及思路
1 簡介
有N0+組MM節(jié)點,如果每次都是手動修復(fù),一次兩次還好,次數(shù)多了難免有些無用功了,因為建立自動腳本,避免每次都在M庫手動備份,手動copy,手動建立連接,手動設(shè)置復(fù)制信息的過程,我的目的,一個sh run command,所有的事情都準備妥當了,然后就泡一杯雀巢咖啡或者普洱茶,在一邊靜靜的等待或者還可以借此小憩片刻,看著M2修復(fù)成功:
2 環(huán)境
必須是MM架構(gòu),而且一臺主庫M1是完好無損的,一臺主庫M2可以根據(jù)M1主庫來進行重建;如果MS架構(gòu),自己可以稍微做一下腳本修改動作,也能使用,架構(gòu)如下圖所示:
3 總體思路,建立主腳本a_build_rep.sh
思路分為13個步驟,如a_build_rep.sh腳本中的備注
#!/bin/bash
set -x
set -e
set -u
#(1)準備好目錄以及全局變量
BACKUP_FOLDER=/mysqldata/shared/backup2
TEMP_SETUP_FOLDER=/tmp/sharding_setup_1
MYSQL_EXEC=/opt/mysql/product/mysql/bin/mysql
#(2)設(shè)置參數(shù),$1是要修復(fù)的M2庫,$2是正在運行良好的M1庫
db1=$2
db2=$1
#(3)在這里設(shè)置好一些所必用的環(huán)境變量,比如備份用戶,備份用戶密碼,以及復(fù)制帳號密碼,甚至一些super帳號
BACKUP_USER="backupuser"
BACKUP_PASSWORD="#xx$"
AGENT_PASSWORD='#xx$'
REPLICATION_USER=replication
REPLICATION_PASSWORD='#xx$'
REPLICA_PASSWORD='#xx$'
MONITOR_PASSWORD='#xx$'
WRITER_ETH='eth0'
SUPER_USER=backupuser
SUPER_PASSWORD='#xx$'
#(4)由于磁盤空間所限,建立這些目錄,如果目錄已經(jīng)存在,則清空這些目錄。
ssh -t $db1 "rm -rf $TEMP_SETUP_FOLDER && mkdir -p $TEMP_SETUP_FOLDER && chmod 777 $TEMP_SETUP_FOLDER"
ssh -t $db2 "rm -rf $TEMP_SETUP_FOLDER && mkdir -p $TEMP_SETUP_FOLDER && chmod 777 $TEMP_SETUP_FOLDER"
#(5)將在線備份腳本 create_hot_backup.sh copy到即將要進行在線備份的M1庫上面,并且調(diào)用此腳本進行在線備份,此備份用來去另一個M2庫上進行恢復(fù)。
echo "Taking hotbackup on db1..."
scp create_hot_backup.sh $db1:$TEMP_SETUP_FOLDER/create_hot_backup.sh
ssh -t $db1 "sudo -u mysql mkdir -p $BACKUP_FOLDER"
ssh -t $db1 "sudo -i -u mysql $TEMP_SETUP_FOLDER/create_hot_backup.sh '$BACKUP_FOLDER' '$BACKUP_USER' '$BACKUP_PASSWORD' N Y"
#(6)停止M2的MySQL服務(wù)
echo "Hotbackup completed. Now restore the hotbackup on db2..."
ssh -t $db2 "sudo /etc/init.d/mysql stop"
#(7)將restore_hot_backup.sh腳本copy到M2庫上,并調(diào)用此腳本在M2庫上面進行恢復(fù)操作。
scp restore_hot_backup.sh $db2:$TEMP_SETUP_FOLDER/restore_hot_backup.sh
ssh -t $db2 "sudo -i -u mysql $TEMP_SETUP_FOLDER/restore_hot_backup.sh '$BACKUP_FOLDER' '$BACKUP_USER' '$BACKUP_PASSWORD' '$TEMP_SETUP_FOLDER' Y"
#(8)恢復(fù)完,啟動M2庫。
ssh -t $db2 "sudo /etc/init.d/mysql start"
echo "Restore completed. Now rebuild replication between db1 and db2..."
#(9)建立M2上面的復(fù)制功能,M1為master,M2為slave;
echo "Setup replication from db1 to db2"
echo "Setup replication from db1 to db2"
scp $db2:$TEMP_SETUP_FOLDER/xtrabackup_binlog_info.master xtrabackup_binlog_info.db1
binlog_filename=`cat xtrabackup_binlog_info.db1 | awk '{print $1}'`
binlog_pos=`cat xtrabackup_binlog_info.db1 | awk '{print $2}'`
$MYSQL_EXEC -h$db2 -ummm_agent -p"$AGENT_PASSWORD" -e "SLAVE STOP;"
$MYSQL_EXEC -h$db2 -ummm_agent -p"$AGENT_PASSWORD" -e "CHANGE MASTER TO MASTER_HOST='$db1', MASTER_PORT=3306, MASTER_USER='replication', MASTER_PASSWORD='$REPLICATION_PASSWORD', MASTER_LOG_FILE='$binlog_filename', MASTER_LOG_POS=$binlog_pos;"
$MYSQL_EXEC -h$db2 -ummm_agent -p"$AGENT_PASSWORD" -e "SLAVE START;"
#(10)Check M2庫的復(fù)制功能狀態(tài),是否搭建成功
echo "Check db2 replication status."
if ! $MYSQL_EXEC -h$db2 -ummm_agent -p"$AGENT_PASSWORD" -e "SHOW SLAVE STATUS/G" | grep "Slave_SQL_Running: Yes"
then
echo "[ERROR] Cannot start slave on db2!"
exit -1
fi
#(11)建立M1上面的復(fù)制功能,M2為master,M1為slave;
echo "Setup replication from db2 to db1"
./build_replication.sh $db2 $db1
#(12)Check M1庫的復(fù)制功能狀態(tài),是否搭建成功
echo "Check db1 replication status."
if ! $MYSQL_EXEC -h$db1 -ummm_agent -p"$AGENT_PASSWORD" -e "SHOW SLAVE STATUS/G" | grep "Slave_SQL_Running: Yes"
then
echo "[ERROR] Cannot start slave on db1!"
exit -1
fi
#(13)清除臨時目錄的備份文件,釋放磁盤空間
echo "Clean up the shared storage folder and tmp folder."
ssh -t $db1 "rm -rf $TEMP_SETUP_FOLDER"
ssh -t $db2 "rm -rf $TEMP_SETUP_FOLDER"
ssh -t $db1 "sudo -u mysql rm -rf $BACKUP_FOLDER"
echo 'Rebuild secondary done!'
4 分腳本(1),M1庫上的在線備份腳本create_hot_backup.sh詳情
#!/bin/bash
set -x
set -e
# !!! This file need to be run in mysql !!!
#(1)設(shè)置一些基本參數(shù),$1:備份文件;$2:備份用戶名;$3:備份用戶名密碼;$4:是否需要安全模式備份; $5:是否需要rsync信息
BACKUP_FOLDER=$1
HOTBACKUP_USER=$2
HOTBACKUP_PASSWORD=$3
NEED_SAFE_SLAVE=$4
NEED_RSYNC=$5
#(2)參數(shù)驗證,如果需要,則在接下來的備份命令中加上--safe-slave-backup或者--rsync參數(shù)
INNOBACKUP_OPT=""
if [[ $NEED_SAFE_SLAVE == "Y" ]]
then
INNOBACKUP_OPT=$INNOBACKUP_OPT" --safe-slave-backup"
fi
if [[ $NEED_RSYNC == "Y" ]]
then
INNOBACKUP_OPT=$INNOBACKUP_OPT" --rsync"
fi
#(3)開始執(zhí)行備份命令
echo "Run xtrabackup to take hotbackup..."
export MYSQL_HOME=/opt/mysql/product/mysql
innobackupex $INNOBACKUP_OPT --user=$HOTBACKUP_USER --password=$HOTBACKUP_PASSWORD $BACKUP_FOLDER
5 分腳本(2)restore_hot_backup.sh,在M2庫上進行數(shù)據(jù)恢復(fù)操作。
#!/bin/bash
set -x
set -e
#(1)$1:備份數(shù)據(jù)存放的目錄;$2:恢復(fù)用戶;$3:恢復(fù)用戶密碼; $4:臨時文件夾目錄; $5:是否需要resync;
SHARED_STORAGE_PATH=$1
HOTBACKUP_USER=$2
HOTBACKUP_PASSWORD=$3
TEMP_SETUP_FOLDER=$4
NEED_RSYNC=$5
#(2)是否需要加上resync參數(shù)
INNOBACKUP_OPT=""
if [[ $NEED_RSYNC == "Y" ]]
then
INNOBACKUP_OPT="--rsync"
fi
#(3)把MYSQL_HOME放入環(huán)境變量中
export MYSQL_HOME=/opt/mysql/product/mysql
#(4)如果目錄存在的話,刪除這些數(shù)據(jù)庫目錄下的舊文件
echo "Delete existing mysql instance..."
rm -rf /mysqldata/data
rm -rf /mysqldata/shared/restore
rm -rf /mysqldata/binlog/*
rm -rf /mysqldata/iblogs/*
#(5)開始準備數(shù)據(jù)目錄以及備份數(shù)據(jù)目錄,如今不存在就新建,并且賦予linux系統(tǒng)帳號mysql的操作權(quán)限。
echo "Apply and copy back backup files..."
mkdir -p /mysqldata/data && chown -R mysql:mysql /mysqldata/data && chmod 700 /mysqldata/data
mkdir -p /mysqldata/shared/restore
backup_folder=$(ls -1 $SHARED_STORAGE_PATH | sort -rn | head -n1) #這個ls的找最新生成的文件的命令很實用,可以借鑒。
#(6)如果為了安全起見可以備份文件數(shù)據(jù)目錄copy到指定的恢復(fù)臨時目錄,然后在臨時目錄進行--apply-log以及--copy-back等操作,但是我為了效率,就去掉了copy的時間(copy時間大概需要1小時左右),直接在原來的備份數(shù)據(jù)目錄進行--apply-log以及--copy-back操作
# cp -r $SHARED_STORAGE_PATH/${backup_folder}/* /mysqldata/shared/restore/
# innobackupex --user=$HOTBACKUP_USER --password=$HOTBACKUP_PASSWORD --ibbackup xtrabackup --apply-log /mysqldata/shared/restore/
# innobackupex --user=$HOTBACKUP_USER --password=$HOTBACKUP_PASSWORD $INNOBACKUP_OPT --copy-back /mysqldata/shared/restore/
innobackupex --user=$HOTBACKUP_USER --password=$HOTBACKUP_PASSWORD --ibbackup xtrabackup --apply-log $SHARED_STORAGE_PATH/${backup_folder}/
innobackupex --user=$HOTBACKUP_USER --password=$HOTBACKUP_PASSWORD $INNOBACKUP_OPT --copy-back $SHARED_STORAGE_PATH/${backup_folder}/
#(7)將原來的備份中的復(fù)制點信息copy到臨時目錄下,并賦予對copy后的文件賦予相應(yīng)的權(quán)限。
cp -f $SHARED_STORAGE_PATH/${backup_folder}/xtrabackup_binlog_info $TEMP_SETUP_FOLDER/xtrabackup_binlog_info.master
chmod 644 $TEMP_SETUP_FOLDER/xtrabackup_binlog_info.master
#(8)恢復(fù)成功后,刪除原來的臨時目錄的備份數(shù)據(jù),釋放磁盤空間,如果你磁盤空間足夠,這一步可以省略。
echo "Remove backup files..."
rm -rf $SHARED_STORAGE_PATH/${backup_folder}/
6 搭建M1上面的復(fù)制,M2為master,M1為slave,MM架構(gòu)。
#!/bin/sh
set -x
set -u
set -e
#(1)$1:master主庫服務(wù)器主機名或者IP地址; $2:slave主庫服務(wù)器主機名或者IP地址;
MASTER_SERVER=$1
SLAVE_SERVER=$2
MYSQL_CNF_DIR='/opt/mysql/product/mysql/etc'
MYSQL_EXEC='/opt/mysql/product/mysql/bin/mysql'
#(2)從$1主庫上面獲取復(fù)制點信息
master_file=$(ssh -t $MASTER_SERVER "$MYSQL_EXEC -u$SUPER_USER -p$SUPER_PASSWORD -e 'SHOW MASTER STATUS/G'" | grep "File" | awk '{print $2}')
master_file=$(sed -e 's/[/r/n]//' <<<"$master_file")
master_pos=$(ssh -t $MASTER_SERVER "$MYSQL_EXEC -u$SUPER_USER -p$SUPER_PASSWORD -e 'SHOW MASTER STATUS/G'" | grep "Position" | awk '{print $2}')
master_pos=$(sed -e 's/[/r/n]//' <<<"$master_pos")
#(3)執(zhí)行搭建復(fù)制sql命令操作:
ssh -t $SLAVE_SERVER "$MYSQL_EXEC -u$SUPER_USER -p$SUPER_PASSWORD -e /"
STOP SLAVE;
RESET SLAVE;
CHANGE MASTER TO master_host='$MASTER_SERVER', master_port=3306, master_user='$REPLICATION_USER',
master_password='$REPLICATION_PASSWORD', master_log_file='$master_file', master_log_pos=$master_pos;
START SLAVE;
/""
7 執(zhí)行操作
在根目錄下,執(zhí)行
[root@xx-control xx]# nohup /home/cc/a_build_rep.sh m2.xx.com m1-.xx.com . > rebuild.log &,放在后臺執(zhí)行,使用nohup以防止屏幕突然失效。
然后用 tail -f rebuild.log 查看進展:
[root@xx-control xx]# tail -f rebuild.log
+ set -e
+ BACKUP_FOLDER=/mysqldata/shared/backup
Run xtrabackup to take hotbackup...
+ HOTBACKUP_USER=backupuser
+ HOTBACKUP_PASSWORD='#xxx$'
+ NEED_SAFE_SLAVE=N
+ NEED_RSYNC=Y
+ INNOBACKUP_OPT=
+ [[ N == /Y ]]
+ [[ Y == /Y ]]
+ INNOBACKUP_OPT=' --rsync'
+ echo 'Run xtrabackup to take hotbackup...'
+ export MYSQL_HOME=/opt/mysql/product/mysql
+ MYSQL_HOME=/opt/mysql/product/mysql
+ innobackupex --rsync --user=user'--password=#xxx$' /mysqldata/shared/backup
.................................................................................
................................................................................
Rebuild secondary done!
bitsCN.com聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com