多主备份日志清理脚本

因为是多台服务器进行主从同步,由于硬件等因素影响,每台服务器的执行速度不一样,所有清理日志需要以最慢的为准。

#!/bin/bash

. /etc/profile
. ~/.bash_profile

cnt=$(ps -ef |grep -v grep|grep 'sh /root/clean_binlog.sh' -c)
if [ $cnt -gt 2 ];then
        exit
fi

rm -f /root/m.log /root/s.log

IMIP='192.168.0.41'
IMU='root'
IMP='123456'
MIP='192.168.0.43'
MU='root'
MP='123456'
SIP='192.168.0.42'
SU='root'
SP='123456'
mysql -h $MIP -u$MU -p$MP -e "
show slave status\G
quit" > /root/m.log
mysql -h $SIP -u$SU -p$SP -e "
show slave status\G
quit" >> /root/s.log


#show slave status\G
#S执行的M日志
m_log_pos=$(grep -w "Relay_Master_Log_File" /root/m.log|sed "s#Relay_Master_Log_File..##g"|sed "s#[ \t]##g"|sed "s#mysql-bin.##g"|bc)
s_log_pos=$(grep -w "Relay_Master_Log_File" /root/s.log|sed "s#Relay_Master_Log_File..##g"|sed "s#[ \t]##g"|sed "s#mysql-bin.##g"|bc)

if [ $m_log_pos -ge $s_log_pos ]
then
    s_log_id=$(printf "%06d" "$s_log_pos");
    mysql -h $IMIP -u$IMU -p$IMP -e "
purge binary logs to 'mysql-bin.$s_log_id';
quit" 
else
    m_log_id=$(printf "%06d" "$m_log_pos");
    mysql -h $IMIP -u$IMU -p$IMP -e "
purge binary logs to 'mysql-bin.$m_log_id';
quit"
fi


本文链接:https://jeff.xin/post/40.html

--EOF--

Comments

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。