CentOS下rsync+inotify-tools實現資料即時同步更新

來源:互聯網
上載者:User

rsync優點:安全性高、備份速度快、支援增量備份等。inotify是細密度的、非同步檔案系統事件監控機制,Linux核心從2.6.13起,加入了對inotify的支援,通過第三方軟體inotify-tools可以監控檔案系統下的檔案的各種變化情況。

rsync和inotify-tools的配合使用可以實現資料的即時同步更新。以下是配置過程。

環境說明

Server A ip address :192.168.2.102
Server B ip address :192.168.2.103

Server A的rsync服務配置

安裝rsync

這裡直接使用yum安裝rsync。

yum -y install xinetd rsync
sed -i '/disable/s/yes/no/g' /etc/xinetd.d/rsync
/etc/init.d/xinetd restart > /dev/null

建立rsync主配檔案:rsyncd.conf

uid = root
gid = root
use chroot = no
max connections = 200
port = 873
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log

[wwwroot]
path = /data/wsdata/wwwroot/
comment = This is test
auth users = backupadmin
ignore errors
read only = no
list = no
hosts allow = 192.168.2.0/255.255.255.0
secrets file = /etc/rsync.pas

建立rsync認證檔案:sync.pas

【格式】 使用者:密碼檔案

vim /etc/rsync.pas

backadmin:password

chmod 600 rsync.pas
啟動服務rsync服務

rsync --daemon
Server B的rsync服務配置

安裝rsync,跟Server A一樣的方法。

yum -y install xinetd rsync
sed -i '/disable/s/yes/no/g' /etc/xinetd.d/rsync
/etc/init.d/xinetd restart > /dev/null

建立密碼檔案,密碼檔案許可權:600

vim /etc/rsync.pas

password

chmod 600 /etc/rsync.pas

rsync同步檔案方法

從Server A伺服器上下載

rsync -vzrtopg --delete --progress --password-file=/etc/rsync.pas backupadmin@192.168.2.102::wwwroot /data/wsdata/wwwroot/

上傳到Server A伺服器

rsync -vzrtopg --delete --progress --password-file=/etc/rsync.pas /data/wsdata/wwwroot backupadmin@192.168.2.102::wwwroot
inotify-tools實現即時同步更新

以下配置全部在Server B伺服器上操作。

下載安裝inotify-tools

wget https://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar zxvf inotify-tools-3.14.tar.gz
cd inotify-tools
./configure && make && make install
Server B的inotify監控指令碼

vim rsync_inotify.sh
#!/bin/bash
src=/data/wsdata/wwwroot/

/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y/%H:%M' --format '%T %w %f' -e modify,delete,create,attrib $src | while read file
do
rsync -vzrtopg --delete --progress --password-file=/etc/rsync.pas $src backupadmin@192.168.2.102::wwwroot
echo "$src was rsynced"
done

注意:目錄/data/wsdata/wwwroot/末尾一定加斜杠 “/”

-m 是保持一直監聽
-r 是遞迴查看目錄
-q 是列印出事件
--timefmt 是指定時間的輸出格式
--format 指定檔案變化的詳細資料
-e create,move,delete,modify,attrib 是指 “監聽 建立 移動 刪除 寫入 許可權” 事件
執行指令碼

nohup ./rsync_inotify.sh &

nohup 命令運行由 Command 參數和任何相關的 Arg 參數指定的命令,忽略所有掛斷(SIGHUP)訊號。在登出後使用 nohup 命令運行後台中的程式。要運行後台中的 nohup 命令,添加 & ( 表示“and”的符號)到命令的尾部。

執行後的結果是Server B上更新了檔案,會自動同步到Server A上。

如果出現這個錯誤“/usr/local/bin/inotifywait: error while loading shared libraries:
libinotifytools.so.0”可以採用以下辦法解決:

ln -sv /usr/local/lib/libinotify* /usr/lib/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.