rsync+inotify檔案即時同步

來源:互聯網
上載者:User

標籤:rsync

安裝環境:

server:192.168.2.183

 client:192.168.2.222

兩台伺服器上分別安裝:

yum install rsync -y

sever端配置rsync檔案vi /etc/rsyncd.conf 預設設定檔不存在自己建立,以下為配置

 uid = nobodygid = nobodyport = 873host all = 192.168.2.222use chroot = onmax connections = 4timeout = yes[web]path = /var/www/html/comment = rsync filesignore errorsread only = nolist = yesauth users = rsyncsecrets file = /etc/rsync_server.passwd~

建立密碼通訊檔案vi /etc/rsync_server.passwd 並給與600許可權

[[email protected] ~]# cat  /etc/rsync_server.passwd   rsync:rsync chmod 600 /etc/rsync_server.passwd

啟動rsync

rsync --daemon

查看監聽連接埠

[[email protected] ~]# netstat -ntulp | grep rsynctcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      20404/rsync         tcp        0      0 :::873                      :::*                        LISTEN      20404/rsync

rsync配置完畢

 

client端配置

實現即時同步需要安裝inotify檔案檢查工具

 

下載安裝

wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz  ./configure --prefix=/usr/local/inotify     make && make install

編寫指令碼inotify.sh然後後端執行,指令碼內容如下:

#!/bin/bashhost=192.168.2.183 src=/var/www/html/dst=webuser=rsyncinotifywait=/usr/local/inotify/bin/inotifywaitrsync=/usr/bin/rsync$inotifywait -mrq --timefmt ‘%d/%m/%y %H:%M‘ --format ‘%T %w%f‘ -e modify,delete,create,attrib $src | while read filesdo$rsync -vzrtopg --delete ./  --timeout=100  --progress --password-file=/etc/rsync_client.passwd  [email protected]$host::$dst $srcecho "${files} was rsynced" >>/tmp/rsync.log 2>&1done~

 用戶端建立rsync同步密碼檔案rsync_client.passwd  並給與600許可權

[[email protected] ~]# cat /etc/rsync_client.passwd  rsync chmod 600  /etc/rsync_client.passwd  rsync

然後

後台運行inotify.sh指令碼

nohup sh inotify.sh &[[email protected] ~]# jobs[1]+  Running                 nohup sh inotify.sh &fg是調用後台啟動並執行程式的,

 然後測試:

在server端cp檔案到/var/www/html/目錄下,然後在用戶端client的/var/www/html/目錄下查看,檔案同步過來了,在server執行for a in `seq 200`;do touch $a;done建立檔案,可以在用戶端查看

 

以上實驗是服務端同步到用戶端,那怎麼將用戶端的檔案推送到服務端了,其實只需要改一下指令碼裡面的同步命令就可以了

 

 

本文出自 “毛毛鴨” 部落格,請務必保留此出處http://woshitieren.blog.51cto.com/2466034/1690555

rsync+inotify檔案即時同步

聯繫我們

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