標籤: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檔案即時同步