#!/bin/bash
#Author:royoy
#Date:20170606
#Desc: backup for sc xtmgame
BACKUP_BIN=/home/royoy/mysqlbinlog
LOCAL_BACKUP_DIR=/data/backup/binlog/sc/xtmgame/
BACKUP_LOG=/data/backup/binlog/backup.log
REMOTE_HOST="10.14.11.111"
REMOTE_PORT="3306"
REMOTE_USER="repl"
REMOTE_PASS="IdhvRWrKsMV23wJvbTeCrUwAWvN5PhA2"
FIRST_BINLOG="mysql-bin.008424"
#time to wait before reconnecting after failure
SLEEP_SECONDS=10
##create local_backup_dir if necessary
[ -d ${LOCAL_BACKUP_DIR} ] || mkdir -p ${LOCAL_BACKUP_DIR}
cd ${LOCAL_BACKUP_DIR}
## 运行while循环,连接断开后等待指定时间,重新连接
while :
do
if [ `ls -A "${LOCAL_BACKUP_DIR}" |wc -l` -le 1 ];then
LAST_FILE=${FIRST_BINLOG}
else
LAST_FILE=`ls -l ${LOCAL_BACKUP_DIR} | awk '!/backuplog/END{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}
echo "`date +"%Y/%m/%d %H:%M:%S"` mysqlbinlog停止[sc_xtmgame:${REMOTE_HOST}],返回代码:$?" >> ${BACKUP_LOG}
echo "${SLEEP_SECONDS}秒后再次连接并继续备份" >> ${BACKUP_LOG}
sleep ${SLEEP_SECONDS}
done