rsync+inotify-tools+ssh實現mysql-bin記錄檔即時備份

來源:互聯網
上載者:User

標籤:rsync+inotify-tools+ssh實現mysql-bin記錄檔即時備份

1、環境:兩台Centos電腦直連,其伺服器A的IP地址為192.168.1.124,伺服器B的IP地址為192.168.1.121。


2、需求軟體安裝(ssh系統預設已經安裝):


伺服器A:

yum install rsync inotify-tools


伺服器B:

yum install rsync


3、建立使用者(備份執行使用者)

建立使用者:useradd rsync

設定密碼:略

注意:兩台伺服器都需要建立使用者(此處兩天伺服器建立了相同的使用者,也可建立不同的使用者)


4、建立ssh-key實現ssh自動連接

伺服器A


建立公開金鑰和私密金鑰

[[email protected] ~]# su - rsync

[[email protected] ~]$ ssh-keygen -t rsa(全部使用預設值,一直按斷行符號即可)

完成後,此時會在/home/rsync/.ssh下,產生一對“公開金鑰和私密金鑰”,如下所示

[[email protected] .ssh]# ll

total 12

-rw-------. 1 rsync rsync 1675 Oct 20 01:47 id_rsa

-rw-r--r--. 1 rsync rsync  393 Oct 20 01:47 id_rsa.pub


然後,如下命令,將id_rsa.pub負責到伺服器B上。

scp /home/rsync/.ssh/id.rsa.pub [email protected]:/home/rsync/


伺服器B:

建立ssh儲存“公開金鑰”的目錄:

mkdir /home/rsync/.ssh

chmod 700 /home/rsync/.ssh

將伺服器A上傳遞的公開金鑰,mv到/home/rsync/.ssh。如下命令

mv /home/rsync/id_rsa.pub /home/rsync/.ssh/authorized_keys

chmod 600 /home/rsync/.ssh/authorized_keys


重啟SSH服務
[[email protected] ~]# /etc/init.d/sshd restart
[[email protected] ~]# /etc/init.d/sshd restart


5、inotify_tools的使用

1、確定inotifywait的位置

2、inotifywait的常見使用方法


/usr/bin/inotifywait -mrq -e modify,delete,create,attrib

命令解釋:

-m 是保持一直監聽
-r 是遞迴查看目錄
-q 是列印出事件
-e create,move,delete,modify,attrib 是指 “監聽 建立 移動 刪除 寫入 許可權” 事件


6、伺服器A上建立即時監控備份指令碼inotify.ssh


#!/bin/sh

SRC=/usr/local/mysql/binlog/

[email protected]:/home/rsync1/test

/usr/bin/inotifywait -mrq -e modify,delete,create,attrib ${SRC} | while read D E F

do

      /usr/bin/rsync -ahqzt --delete $SRC $DST

done


命令解釋:

SRC:監控的目錄()

DST:備份的目錄

while read D E F:inotifywait 命令產生三個傳回值,分別是“日期,時間,檔案” 這3個傳回值會做為參數傳給read,因此指令碼中的“while read D E F” 寫法細化了傳回值。


rsync命令解釋:

/usr/bin/rsync -ahqzt --delete $SRC $DST
-a 存檔模式
-h 儲存硬串連
-q 制止非錯誤資訊
-z 壓縮檔資料在傳輸
-t 維護修改時間
-delete 刪除於多餘檔案


7、指令碼的運行


伺服器A:


修改執行許可權:chmod +x inotify.sh

在rsync使用者模式下運行該指令碼(ssh-key是rsync使用者建立的):

su - rsync

[[email protected] sbin]$ ./inotify_rsync.sh &   #在後台運行


此次在伺服器A的/usr/local/mysql/binlog/#在該目錄下,建立檔案和目錄,可以即時備份到目標位置


注意:

1、備份使用者rsync有沒有讀取/usr/local/mysql/binlog/目錄的許可權,不然備份失敗

2、建議修改mysql-bin的儲存目錄,如下所示

log-bin=/usr/local/mysql/binlog/mysql-bin           

#開啟日誌(主機需要開啟),這個mysql-bin也可以自訂,這裡也可以加上路徑,如:/usr/local/mysql/binlog/mysql-bin  











本文出自 “linux” 部落格,請務必保留此出處http://469952080.blog.51cto.com/8311493/1704715

rsync+inotify-tools+ssh實現mysql-bin記錄檔即時備份

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.