用shell實現Mysql延時複製

wenaini發表於2009-02-05
這個指令碼初步實現了mysql的延時複製,以後還會繼續加強[@more@]

lag_minute=30
sleep_time=5
binlogdir=/usr/local/mysql/data
cd $binlogdir
lag_time=`expr $lag_minute * 60`

while [ 0 -eq 0 ]
do

relay_pos_str=$(echo `mysql -e "show slave statusG"|grep -i Relay_Log_Pos`)
relay_pos_val=`expr substr "$relay_pos_str" 16 30`
echo $relay_pos_val>relay_pos_val.his

relay_file_str=$(echo `mysql -e "show slave statusG"|grep -i Relay_Log_File`)
relay_file_val=`expr substr "$relay_file_str" 17 50`
echo $relay_file_val>relay_file_val.his

cur_ts_string=`/usr/local/mysql/bin/mysqlbinlog -j "$relay_pos_val" "$relay_file_val"|grep "SET TIMESTAMP"|sed -n '1p'`
#echo $cur_ts_string
cur_ts=`expr substr "$cur_ts_string" 15 10`
echo "exec timestamp is $cur_ts"

while [ `echo ${#cur_ts}` -eq 0 ]
do
echo "can not get timestamp,wait and try again"
sleep 10
relay_pos_val=`cat relay_pos_val.his`
relay_file_val=`cat relay_file_val.his`
cur_ts_string=`/usr/local/mysql/bin/mysqlbinlog -j "$relay_pos_val" "$relay_file_val"|grep "SET TIMESTAMP"|sed -n '1p'`
cur_ts=`expr substr "$cur_ts_string" 15 10`
echo "exec timestamp is $cur_ts"
done

local_ts=`mysql -e "select unix_timestamp()"|sed -n '2p'`

target_ts=`expr $lag_time + $cur_ts`

echo "local timestamp is $local_ts"
echo "target timestamp is $target_ts"
slave_flag=`mysqladmin extended-status|grep Slave_running|grep -ic on`
if [ $target_ts -gt $local_ts ]
then
if [ $slave_flag -eq 1 ]
then
mysql -e "stop slave sql_thread"
echo "slave stopped"
fi
else
if [ $slave_flag -eq 0 ]
then
mysql -e "start slave sql_thread"
echo "slave started"
fi
fi

sleep $sleep_time
done

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/79686/viewspace-1016770/,如需轉載,請註明出處,否則將追究法律責任。

相關文章