检查实验MySQL主从备份是不是运营,shell脚本修复

2019-12-31 06:17 来源:未知

经过查阅 slave  状态,确认保证 Slave_IO_Running: Yes Slave_SQL_Running: Yes

发布:thebaby   来源:net     【大 中 小】

#!/bin/bash
#Author:Darius-D
mysql -uroot -p123123 -e 'show slave statusG' >a.txt
IO=`head -12 a.txt | tail -1|awk -F ':' '{print $2}'`
SQL=`head -13 a.txt | tail -1|awk -F ':' '{print $2}'`
if [ $IO == 'Yes' ];then
echo "Slave_IO_Running:Yes"
if [ $SQL == 'Yes' ];then
echo "Slave_SQL_Running:Yes"
else
echo "Slave_SQL_Running:No"
fi
else
echo "Slave_IO_Running:No"
fi
有关于mysql主从备份相关原理及备份链接步向查看。

享用生龙活虎例shell脚本,用于修改mysql的骨干同步难点,有必要的爱人参照他事他说加以考察下吧。

多少个足以改正mysql骨干同步的shell脚本。

例子:  

复制代码代码示例:

#!/bin/sh
#修补mysql主从同步
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
LOGFILE=/data/repair_mysql_sync_`date +%F`.log
SQLCMD1="show slave status"
#查阅MySQL是不是运转
retval=`ps aux | grep mysqld | grep -v grep`
if [ "${retval}X" = "X" ]; then
    echo The MySQL is not running at: `date +%F" "%H-%M-%S` >> ${LOGFILE}
    exit 1
fi
#获得MySQL从端Relay binlog的路径
retval=`grep "^relay-log" /etc/my.cnf | grep -v relay-log- | grep '/'`
if [ "${retval}" = "X" ]; then
    RELAY_BINLOG_PATH=`ps aux | grep -w mysqld | grep -v grep | awk '{print $13}' | awk -F '=' '{print $2}'`
else
    RELAY_BINLOG_PATH=`dirname $(echo ${retval} | awk -F '=' '{print $2}')`
fi
#搜寻master.info文件,用于固定Binlog新闻
MASTER_FILE=`ps aux | grep -w mysqld | grep -v grep | awk '{print $13}' | awk -F '=' '{print $2}'`/master.info
if [ ! -e ${MASTER_FILE} ]; then
   echo This Server is not MySQL Slave at: `date +%F" "%H-%M-%S` >> ${LOGFILE}
   exit 1
fi
#收获当前的联名状态
IO_STATUS=`mysql -uroot -e "${SQLCMD1}G;" | awk '$1=="Slave_IO_Running:" {print $2}'`
SQL_STATUS=`mysql -uroot -e "${SQLCMD1}G;" | awk '$1=="Slave_SQL_Running:" {print $2}'`
if [[ "${IO_STATUS}" = "Yes" && "${SQL_STATUS}" = "Yes" ]]; then
   echo Now, The MySQL Replication is synchronous at: `date +%F" "%H-%M-%S` >> ${LOGFILE}
   exit 0
fi
#从master.info文件中,获得MySQL主端的一齐音讯
REPLI_INFO=`sed '/^$/d' ${MASTER_FILE} | tail +2 | head -5`
REPLI_BINLOG_FILE=`echo ${REPLI_INFO} | awk '{print $1}'`
REPLI_IPADDR=`echo ${REPLI_INFO} | awk '{print $3}'`
REPLI_USER=`echo ${REPLI_INFO} | awk '{print $4}'`
REPLI_PWD=`echo ${REPLI_INFO} | awk '{print $5}'`
#除去无用的Relay binlog
rm -rf ${RELAY_BINLOG_PATH}/*-relay-bin.*
#直白从0地点上马协同
SQLCMD2="change master to master_host='${REPLI_IPADDR}', master_user='${REPLI_USER}', master_password='${REPLI_PWD}',"
SQLCMD2="${SQLCMD2} master_log_file='${REPLI_BINLOG_FILE}', master_log_pos=0"
mysql -uroot -e "stop slave;"
mysql -uroot -e "${SQLCMD2};"
mysql -uroot -e "start slave;"
#设若同步的进程中,出现重复记录引致同步失利,就跳过
while true
do
   sleep 2
   IO_STATUS=`mysql -uroot -e "${SQLCMD1}G;" | awk '$1=="Slave_IO_Running:" {print $2}'`
   SQL_STATUS=`mysql -uroot -e "${SQLCMD1}G;" | awk '$1=="Slave_SQL_Running:" {print $2}'`
   BEHIND_STATUS=`mysql -uroot -e "${SQLCMD1}G;" | awk '$1=="Seconds_Behind_Master:" {print $2}'`
   SLAVE_BINLOG1=`mysql -uroot -e "${SQLCMD1}G;" | awk '$1=="Master_Log_File:" {print $2}'`
   SLAVE_BINLOG2=`mysql -uroot -e "${SQLCMD1}G;" | awk '$1=="Relay_Master_Log_File:" {print $2}'`
   #出现谬误,就将错误音讯记录到日志文件,并跳过不当继续联手
   if [[ "${IO_STATUS}" != "Yes" || "${SQL_STATUS}" != "Yes" ]]; then
       ERRORINFO=`mysql -uroot -e "${SQLCMD1}G;" | awk -F ': ' '$1=="Last_Error" {print $2}'`
       echo "The MySQL synchronous error information: ${ERRORINFO}" >> ${LOGFILE}
       mysql -uroot -e "stop slave;"
       mysql -uroot -e "set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;"
       mysql -uroot -e "start slave;"
       #已做到大器晚成道,如同常退出
   elif [[ "${IO_STATUS}" = "Yes" && "${SQL_STATUS}" = "Yes" && "${SLAVE_BINLOG1}" = "${SLAVE_BINLOG2}" && ${BEHIND_STATUS} -eq 0 ]]; then
      echo The MySQL synchronous is ok at: `date +%F" "%H-%M-%S` >> ${LOGFILE}
      break
 fi
done

你只怕感兴趣的小说:
自动配置mysql主从的shell脚本
监察mysql主从正规情状的shell脚本
mysql主从监察的shell脚本

正文地址:

TAG标签:
版权声明:本文由金沙澳门唯一官网发布于金沙澳门唯一官网,转载请注明出处:检查实验MySQL主从备份是不是运营,shell脚本修复