在前面一篇文章讲了怎么在ubuntu下使用rsync和inotify-tools来进行俩台ubuntu服务器之间的文件同步。现在更进
一步,假设有A1,A2,A3,A4四台服务器,它们的同步文件夹设置为/data,当其中任意一台机器上文件变化时,能够自动
同步到其它机器上去。这个就是本文要讨论的主题。
首先需要设置每一台机器都成为rsync服务器,并且每一台机器都将作为rync从机并运行inotify-tools。所以,每台机
器上面都要有一个rsync俩个秘钥文件,一个作为主机设置了 用户名:密码 格式的秘钥文件,另外一个作为从机只有密
码的秘钥文件。设四台机器的ip分别为:
A1:192.168.1.10 A2:192.168.1.20 A3:192.168.1.30 A4:192.168.1.40
A1的/etc/rsyncd.conf文件:
# sample rsyncd.conf configuration file # GLOBAL OPTIONS #motd file=/etc/motd log file=/var/log/rsyncd # for pid file, do not use /var/run/rsync.pid if # you are going to run rsync out of the init.d script. # The init.d script does its own pid file handling, # so omit the "pid file" line completely in that case. pid file=/var/run/rsyncd.pid syslog facility=daemon #socket options= # MODULE OPTIONS [ftp] comment = public archive path = /data use chroot = no # max connections=10 lock file = /var/lock/rsyncd # the default for read only is yes... read only = no list = yes uid = root gid = root # exclude = # exclude from = # include = # include from = auth users = lzj secrets file = /etc/rsyncd.secrets strict modes = yes hosts allow = 192.168.1.20 192.168.1.30 192.168.1.40 # hosts deny = ignore errors = yes ignore nonreadable = yes transfer logging = no # log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes. timeout = 600 refuse options = checksum dry-run dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz
A1的 /etc/rsyncd.secrets文件:
lzj:123
A1的/etc/rsync.pass文件:
123
其它3台机器的配置文件跟A1不同的地方就是在配置文件的host allow修改成对应的其它三台机器的ip。
A1的rsyn.sh文件:
src=/data dst[0][email protected]::ftp dst[1][email protected]::ftp dst[2][email protected]::ftp /usr/bin/inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src | while read files do for i in ${dst[@]} do rsync -vzrtopg --delete --progress --password-file=/etc/rsync.pass $src $i echo $i done done exit 0
让rsyn.sh脚本在后台运行:
chmod +x rsync.sh nohup bash rsyn.sh &