詳細版:
https://www.osyunwei.com/archives/7435.html
簡潔版如下:
服務器端(目標服務器):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: [email protected]
# 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 是指 “監聽 創建 移動 刪除 寫入 權限” 事件