简洁版如下: 服务器端(目标服务器):192.168.1.1 客户端(源服务器): 192.168.1.2
【服务器端部署】 iptables -I INPUT -s 192.168.1.2/32 -p tcp --dport 873 -j ACCEPT service iptables save whereis rsync || yum -y install rsync xintd sed -i "s/= yes/= no/" /etc/xinetd.d/rsync vim /etc/rsyncd.conf uid = root gid = root 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 [testYHT] path = /data/yhtlog/ comment = testYHT file ignore errors read only = no write only = no hosts allow = 192.168.1.2 hosts deny = * list = false uid = root gid = root auth users = yhtuser secrets file = /etc/rsyncd.secrets
vim /etc/rsyncd.secrets yhtuser:123456
chmod 600 /etc/rsyncd.conf
chmod 600 /etc/rsyncd.secrets service xinetd restart
【客户端部署】 whereis rsync || yum -y install rsync vim /etc/rsyncd.secrets 123456 chmod 600 /etc/rsyncd.secrets vim /usr/bin/rsync.sh
#########################################################################
# File Name: rsync.sh
# Author: jason lee
# mail: ianc@
# Created Time: Wed Jun 27 17:41:41 2018
#########################################################################
#!/bin/bash
host=192.168.1.1
src=/data/logs/
des=testNet
user=yhtuser
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files
do
/usr/bin/rsync -zrtopg --delete --progress --password-file=/etc/rsyncd.secrets $src $user@$host::$des
echo "${files} was rsynced" > /var/log/rsyncd.log 2>&1
done
测试同步 sh /usr/bin/rsync.sh
注意: 如果服务端防火墙配置了只允许客户端公网IP,则服务器端rsync配置文件也得配置公网IP,而客户端同步脚本也得配置公网IP,否则全是内网inotify参数 -m 是保持一直监听 -r 是递归查看目录 -q 是打印出事件 -e create,move,delete,modify,attrib 是指 “监听 创建 移动 删除 写入 权限” 事件