Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

rsync 伺服器端與客戶端組態範例:模組、認證與 inotify 觸發同步

伺服器端 rsyncd.conf

uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[web1]
path = /usr/local/rubyzhu/test
comment = web1 file
ignore errors = yes
read only = no
write only = no
list = false
hosts allow = 127.0.0.1
hosts deny = *
uid = root
gid = root
auth users = backup
secrets file = /etc/rsyncd.secrets

幾個重點:模組名 web1 就是客戶端要連的 target;auth users 指的是 rsync 層級的認證帳號,與系統帳號無關,密碼另外放在 secrets file 且該檔權限需收緊;hosts allow / deny 用來限制來源。

客戶端 inotify 觸發腳本

#!/bin/sh
src=/usr/local/rubyzhu/test1/
des1=web1
ip1=127.0.0.1
user=backup
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,modify,create,attrib,delete ${src} |
while read file
do
  rsync -avz --delete --progress --password-file=/etc/server.pass ${src} ${user}@${ip1}::${des1} &&
#  rsync -avz --delete --progress --password-file=/etc/server.pass ${src} ${user}@${ip2}::${des2} &&
  echo "${src} was rsynced"
  echo "-----------------------------------------------------"
done

密碼檔 /etc/server.pass 內只放一行密碼,權限建議設為 600。若有多個目標,把第二行 rsync 的註解打開即可同時推送。事件監聽加上 close_write 可以避免檔案還在寫入就急著同步,導致目的端拿到半截檔案。

Copyright © Linux教程網 All Rights Reserved