如何进行实时备份mysql binlog日志

今天就跟大家聊聊有关如何进行实时备份mysql binlog日志,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

在要备份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 &

看完上述内容,你们对如何进行实时备份mysql binlog日志有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注云搜网行业资讯频道,感谢大家的支持。


【AD】美国洛杉矶/香港/日本VPS推荐,回程电信CN2 GIA线路,延迟低、稳定性高、免费备份_搬瓦工

【AD】炭云:36元/年/1GB内存/20GB SSD空间/500GB流量/5Gbps端口/KVM/香港/国际线路LUMEN