實時備份mysql binlog日誌

abin1703發表於2016-08-11

在要備份binlog伺服器(192.168.1.248

grant REPLICATION SLAVE on *.* 'repl'@'192.168.1.250' identified by 'repl';


以下操作都是在備份binlog伺服器操作(192.168.1.250

mkdir /backup/binlog

cat backup_binlog.sh

#!/bin/sh
#指定mysqlbinlog的位置
BACKUP_BIN=/usr/local/mysql/bin/mysqlbinlog
#指定將遠端binlog備份到本地的位置
LOCAL_BACKUP_DIR=/backup/binlog/
#指定備份binlog時的日誌
BACKUP_LOG=/backup/binlog/backup.log
#指定要備份哪臺binlog的ip 
REMOTE_HOST=192.168.1.248
REMOTE_PORT=3306
REMOTE_USER=repl
REMOTE_PASS=repl
FIRST_BINLOG=mysql-bin.000001


#time to wait before reconnecting after failure
SLEEP_SECONDS=10


##create local_backup_dir if necessary
mkdir -p ${LOCAL_BACKUP_DIR}
cd ${LOCAL_BACKUP_DIR}


## 執行while迴圈,連線斷開後等待指定時間,重新連線
while :
do
if [ `ls -A "${LOCAL_BACKUP_DIR}" |wc -l` -eq 0 ];then
    LAST_FILE=${FIRST_BINLOG}
else
LAST_FILE=`ls -l ${LOCAL_BACKUP_DIR} | grep -v backup.log |tail -n 1 |awk '{print $9}'`
 fi
#${BACKUP_BIN} --raw --read-from-remote-server --stop-never --host=${REMOTE_HOST} --port=${REMOTE_PORT} --user=${REMOTE_USER} --password=${REMOTE_PASS} ${LAST_FILE}


/usr/local/mysql/bin/mysqlbinlog  --raw --read-from-remote-server --stop-never --host=${REMOTE_HOST} --port=${REMOTE_PORT} --user=${REMOTE_USER} --password=${REMOTE_PASS} ${LAST_FILE}
echo "`date +"%Y/%m/%d %H:%M:%S"` mysqlbinlog停止,返回程式碼:$?" | tee -a ${BACKUP_LOG} 
echo "${SLEEP_SECONDS}秒後再次連線並繼續備份" | tee -a ${BACKUP_LOG}  
sleep ${SLEEP_SECONDS}
done

後臺執行該備份指令碼
nohup sh  backup_binlog.sh &

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

相關文章