遠程備份binlog服務
阿新 • • 發佈:2018-05-05
user ever 百度搜 cal 目錄 port cte ignore 運行
Ⅰ、bonlog server介紹
對於binlog的備份,之前文章裏說的是有從機,一般不備份,那現在人家就是 要備份嘛,怎麽辦嘛,
寫個腳本每天夜裏去把前一天產生的binlog拷貝出來可以不?
行啊,沒問題,你可以的,但你這個一下子整會不會有點累嘛
從5.6版本開始,我們有更好的辦法了,用mysqlbinlog可以將遠端的binlog時時地拉取到本地來
這可不是簡單的拷貝哦,是通過Replication API去時時拉取產生的event,相當於做了一個主從。
Ⅱ、給我弄,操作起來
MySQL server
準備工作:創建rpl賬號,授予replication權限 看下當前二進制文件是which one? (root@localhost) [(none)]> show master status; +------------+----------+--------------+------------------+---------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------+----------+--------------+------------------+---------------------------------------------+ | bin.000022 | 194 | | | d565cde8-0573-11e8-89b2-525400a4dac1:1-1747 | +------------+----------+--------------+------------------+---------------------------------------------+ 1 row in set (0.00 sec)
binlog server
[root@VM_42_63_centos backup]# pwd /data/backup 在備份目錄下弄 [root@VM_42_63_centos backup]# mysqlbinlog --read-from-remote-server --raw --host=123.207.244.133 --port=3306 --user=rpl --password=123 --stop-never bin.000022 & [1] 17292 [root@VM_42_63_centos backup]# mysqlbinlog: [Warning] Using a password on the command line interface can be insecure. 看下binlog拉過來了沒?no problem!!! [root@VM_42_63_centos backup]# ll total 4 -rw-r----- 1 root root 194 Mar 21 16:38 bin.000022
MySQL server 刷一刷
(root@localhost) [(none)]> flush binary logs; Query OK, 0 rows affected (0.02 sec) (root@localhost) [(none)]> flush binary logs; Query OK, 0 rows affected (0.02 sec) (root@localhost) [(none)]> flush binary logs; Query OK, 0 rows affected (0.02 sec) (root@localhost) [(none)]> flush binary logs; Query OK, 0 rows affected (0.03 sec) (root@localhost) [(none)]> show master status; +------------+----------+--------------+------------------+---------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------+----------+--------------+------------------+---------------------------------------------+ | bin.000026 | 194 | | | d565cde8-0573-11e8-89b2-525400a4dac1:1-1747 | +------------+----------+--------------+------------------+---------------------------------------------+ 1 row in set (0.00 sec)
binlog server
[root@VM_42_63_centos backup]# ll
total 20
-rw-r----- 1 root root 235 Mar 21 16:38 bin.000022
-rw-r----- 1 root root 235 Mar 21 16:38 bin.000023
-rw-r----- 1 root root 235 Mar 21 16:38 bin.000024
-rw-r----- 1 root root 235 Mar 21 16:38 bin.000025
-rw-r----- 1 root root 194 Mar 21 16:38 bin.000026
okay ! 沒毛病,都同步過來了、
Ⅲ、重要參數參數
--read-from-remote-server:從遠端拉binlog,不加會在本地找
--raw:本地以二進制方式保存binlog,不指定則以文本保存
--stop-never:一直同步不斷
bin.000022:從這個binlog文件開始拉
- 指定為raw,數據不會時時落盤,而是先搞到內存裏,然後每4k刷盤一次,一旦連接斷開,內存中數據馬上都刷到磁盤上
- 不指定raw,則需要用--result-file參數指定數據寫入某個文本,不可指定為目錄,此時數據會時時刷盤
Ⅳ、問題與解決
如果mysqlbinlog斷了怎麽辦,並不會像主從一樣去嘗試重連
這裏我抄襲一個陳老師腳本(陳老師博客請百度搜索ivictor)
思路:將mysqlbinlog寫在一個死循環裏,斷了就發起重新執行
#!/bin/sh
BACKUP_BIN=/usr/bin/mysqlbinlog
LOCAL_BACKUP_DIR=/backup/binlog/
BACKUP_LOG=/backup/binlog/backuplog
REMOTE_HOST=192.168.244.145
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 backuplog |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}
echo "`date +"%Y/%m/%d %H:%M:%S"` mysqlbinlog停止,返回代碼:$?" | tee -a ${BACKUP_LOG}
echo "${SLEEP_SECONDS}秒後再次連接並繼續備份" | tee -a ${BACKUP_LOG}
sleep ${SLEEP_SECONDS}
done
遠程備份binlog服務