安装 inotify-tools
yum install -y inotify-tools
安装 rsync
yum install -y xinetd
配置 rsync
把 /etc/xinetd.d/rsync 里面的 disable 值由 yes 改为 no,启动 xinetd,“service xinetd start”,再看看 netstat -tunlp | grep xinetd ,发现 rsync 已经启动。
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
# allows crc checksumming etc.
service rsync
{
disable = no
flags = IPv6
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
以上两个工具最好在每台服务器都安装上,至于 rsync 配置则在需要同步的服务器上配置好
vim /etc/rsyncd.conf
# Minimal configuration file for rsync daemon
# See rsync(1) and rsyncd.conf(5) man pages for help
uid = root
gid = root
use chroot = no
read only = yes
#limit access to private LANs
hosts allow=192.168.1.1
hosts deny=*
max connections = 5
pid file = /var/run/rsyncd.pid
secrets file = /etc/rsyncd/rsyncd.secrets
#lock file = /var/run/rsync.lock
motd file = /etc/rsyncd/rsyncd.motd
#This will give you a separate log file
log file = /var/log/rsync.log
#This will log every file transferred - up to 85,000+ per user, per sync
transfer logging = yes
log format = %t %a %m %f %b
syslog facility = local3
timeout = 300
# MODULE OPTIONS
[test]
path = /var/www/html/test/
read only = no
list=yes
ignore errors
exclude = important/
重启 xinetd
service xinetd restart
同步脚本
在分发服务器把需要监听的目录写在一个文件里 (/usr/local/src/notify/notify-list),一行一个目录,如果需要同步的目录里有某个目录不需要同步,则在前面加上 @ 即可
/var/www/html/test/
@/var/www/html/test/cache
同步脚本
#!/bin/bash
inotifywait -mrq -e close_write,delete,create,attrib --fromfile /usr/local/src/notify/notify-list --timefmt '%Y/%m/%d %H:%M' --format '%T %w%f%e' | while read file
do
rsync -arqz --delete /var/www/html/test/ 192.168.1.2::test > /dev/null 2>&1
done
执行此脚本后,在分发服务器上的 test 目录的变动都会同步到对应的服务器上。