从库重建脚本(备忘sx)


#!/bin/bash

dbMaster='192.168.123.30'
dbSlave='192.168.123.29'
user='root'
bakPath='/data/mysql_back/xtrabackup'
tmpDir='fullBakFromMaster'
dataDir='/data/mysql3306/data'
mysqlHome='/data/mysql3306'
passwd='somepasswd'
zcuser='zhucong'
zcpasswd='zcpasswd'

cleanOldBakData(){
        /usr/bin/rm -rf ${bakPath}/${tmpDir}
        mkdir -p ${bakPath}/${tmpDir}
}

cleanMasterBackupData(){
        ssh root@$dbMaster << cyk
                cd ${bakPath} && /usr/bin/rm  -rf 2020*  backup* full* ./script/logs/*
cyk
}

startToFullBackupMasterData(){
        ssh root@$dbMaster << cyk
                sh ${bakPath}/script/full_mysql_backup.sh > ${bakPath}/script/logs/mysql_backup.log
cyk
}

cpMasterBakDataToSlava(){
        scp -r ${user}@${dbMaster}:${bakPath}/backup_now/full* ${bakPath}/${tmpDir}
}

stopDBandDeleteOldData(){
        pid=$(ps -ef |grep mysql |grep -v grep|awk '{print $2}')
        if [[ ! -z $pid ]];then
                kill -9 $pid
        fi
        /usr/bin/rm -rf $dataDir/*
        /usr/bin/rm -rf $mysqlHome/binlogs.*
}

getTmpDataDirName(){
        dirName=$(ls -l ${bakPath}/${tmpDir}|grep -v total|awk '{print $NF}')
        echo $dirName
}

recoveryData(){
        tmpDataDirName=$(getTmpDataDirName)
        chmod 666 ${bakPath}/${tmpDir}/${tmpDataDirName}/xtrabackup_checkpoints
        innobackupex --defaults-file=${mysqlHome}/conf/mysql3306.cnf --apply-log ${bakPath}/${tmpDir}/${tmpDataDirName} 2>&1 | tee /tmp/recover.log
        innobackupex --defaults-file=${mysqlHome}/conf/mysql3306.cnf --copy-back ${bakPath}/${tmpDir}/${tmpDataDirName} 2>&1 | tee /tmp/recover1.log
        chown -R mysql.mysql ${dataDir}
        cd ${dataDir} && find ./ -type f -exec chmod 666 {} \;
}

startSlave(){
        nohup /usr/local/mysql-5.7.25-linux-glibc2.12-x86_64/bin/mysqld --defaults-file=/data/mysql3306/conf/mysql3306.cnf &
}

getBinLogPos(){
        tmpDataDirName=$(getTmpDataDirName)
        binlogPosString=$(cat ${bakPath}/${tmpDir}/${tmpDataDirName}/xtrabackup_binlog_info)
        gtidPosString=$(echo $binlogPosString|sed "s/,\s/,/g"|awk '{print $3}')
        echo $gtidPosString
}

setSlaveDB(){
        binlogStr=`getBinLogPos`
        setMaster="CHANGE MASTER TO MASTER_HOST='${dbMaster}',  MASTER_USER='${zcuser}',  MASTER_PASSWORD='${zcpasswd}',  MASTER_PORT=3306,  MASTER_AUTO_POSITION=1"
        setReadOnly="show global variables like \"%read_only%\"; set global super_read_only=ON; flush privileges; "
        sql="show slave status; stop slave; reset master; reset slave all; SET GLOBAL gtid_purged='${binlogStr}'; ${setMaster}; ${setReadOnly}; start slave; show slave status \G"
        mysql -S ${dataDir}/mysql.sock -u${user} -pXX00 -e "$sql"

}
#cleanOldBakData
#cleanMasterBackupData
#startToFullBackupMasterData
#cpMasterBakDataToSlava
#stopDBandDeleteOldData
#recoveryData
#startSlave
#getBinLogPos
#setSlaveDB


你可能感兴趣的:(技术分享,技术研究,mysql,sql,脚本语言)