1、環(huán)境準(zhǔn)備
操作系統(tǒng):CentOS 7
源服務(wù)器地址:192.168.47.129
目標(biāo)服務(wù)器地址:192.168.47.131
目的:將源服務(wù)器上的/test目錄實(shí)時(shí)同步到目標(biāo)服務(wù)器上的/test目錄下
PS:需要配置好端口和安全規(guī)則,源服務(wù)器去同步目標(biāo)服務(wù)器上的內(nèi)容。
配置源服務(wù)器:192.168.47.129
1、安裝和啟動(dòng)rsync,創(chuàng)建用于測(cè)試目錄/test
[root@rsync-server ~]# yum -y install rsync
[root@rsync-server ~]# systemctl start rsyncd
[root@rsync-server ~]# mkdir /test
2、創(chuàng)建rsync.conf配置文件,添加以下代碼
[root@rsync-server ~]# vim /etc/rsyncd.conf
log file = /var/log/rsyncd.log #遇到錯(cuò)誤可以查看?志,很詳細(xì)
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.pass #?戶認(rèn)證配置?件,??保存? 戶名稱和密碼,后?會(huì)創(chuàng)建這個(gè)?件
motd file = /etc/rsyncd.Motd #rsync啟動(dòng)時(shí)歡迎信息頁(yè)面文件位置
[test] #同步模塊名稱,自定義(不要太短了)
path = /test #rsync服務(wù)端數(shù)據(jù)目錄路徑
comment = test #模塊名稱,和[test]?樣
uid = root # 需要兩服務(wù)器目錄和文件的屬主屬組
gid = root # 需要兩服務(wù)器目錄和文件的屬主屬組
port=873 #rsync 默認(rèn)端?
use chroot = no
read only = no
list = no #不顯示rsync服務(wù)端資源列表
max connections = 200
timeout = 600
auth users = user1 #執(zhí)行數(shù)據(jù)同步的?戶名,可以設(shè)置多個(gè),?英?狀態(tài)下逗號(hào)隔開
#hosts allow = 192.168.47.130 #允許進(jìn)?數(shù)據(jù)同步的客戶端IP地 址,可以設(shè)置多個(gè),?英?狀態(tài)下逗號(hào)隔開
#hosts deny = 192.168.47.132 #不允許同步的ip地址,?逗號(hào)隔 開
3、創(chuàng)建用戶認(rèn)證文件
[root@rsync-server ~]# vim /etc/rsync.pass
user1:123456 #格式,?戶名:密碼,可以設(shè)置多個(gè),每??個(gè) ?戶名:密碼
4、設(shè)置?件權(quán)限 , 重啟rsync
[root@rsync-server ~]# chmod 600 /etc/rsyncd.conf #設(shè)置?件所有者讀取、寫?權(quán)限
[root@rsync-server ~]# chmod 600 /etc/rsync.pass #設(shè)置?件所有者讀取、寫?權(quán)限
[root@rsync-server ~]# systemctl restart rsyncd
配置目標(biāo)服務(wù)器:192.168.47.131
1、安裝rsync
[root@rsync-agent ~]# yum -y install rsync xinetd
[root@rsync-agent ~]# systemctl start rsyncd xinetd
2、配置密碼文件
[root@rsync-agent ~]# vim /etc/passwd.txt
123456 #密碼
[root@rsync-agent ~]# chmod 600 /etc/passwd.txt #設(shè)置?件權(quán)限,只設(shè)置?件所有者具 有讀取、寫?權(quán)限
注意:這?的密碼和客戶端的密碼是?樣的
配置完成,測(cè)試同步
[root@rsync-agent ~]# rsync -avH --port=873 --progress --delete /test user1@192.168.47.129::test --password-file=/etc/passwd.txt
安裝Inotify-tools?具,實(shí)時(shí)觸發(fā)rsync進(jìn)?同步
1、查看服務(wù)器內(nèi)核是否?持inotify (下?輸出說明?持)
[root@rsync-server ~]# ll /proc/sys/fs/inotify
total 88
-rw-r--r-- 1 root root 0 Nov 1 20:47 max_queued_events
-rw-r--r-- 1 root root 0 Nov 1 20:47 max_user_instances
-rw-r--r-- 1 root root 0 Nov 1 20:47 max_user_watches
2、安裝inotify-tools
[root@rsync-server ~]# wget http://github.com/downloads/rvoicilas/inotifytools/inotify-tools-3.14.tar.gz
[root@rsync-server ~]# tar zxvf inotify-tools-3.14.tar.gz
[root@rsync-server ~]# cd inotify-tools-3.14
[root@rsync-server inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify && make && make install
3、設(shè)置系統(tǒng)環(huán)境變量,添加軟連接
[root@rsync-server inotify-tools-3.14]# echo "PATH=/usr/local/inotify/bin:$PATH" >>/etc/profile.d/inotify.sh
[root@rsync-server inotify-tools-3.14]# source /etc/profile.d/inotify.sh
[root@rsync-server inotify-tools-3.14]# echo "/usr/local/inotify/lib" >/etc/ld.so.conf.d/inotify.conf
[root@rsync-server inotify-tools-3.14]# ln -s /usr/local/inotify/include /usr/include/inotify
[root@rsync-server inotify-tools-3.14]# ln -s /usr/local/inotify/lib/libnotifytools.so.0 /usr/lib64
4、修改inotify默認(rèn)參數(shù)(inotify默認(rèn)內(nèi)核參數(shù)值太?)
[root@rsync-server inotify-tools-3.14]# vim /etc/sysctl.conf #添加以下代碼
fs.inotify.max_queued_events=99999999
fs.inotify.max_user_watches=99999999
fs.inotify.max_user_instances=65535
5、創(chuàng)建腳本,實(shí)時(shí)觸發(fā)rsync進(jìn)?同步
[root@rsync-server inotify-tools-3.14]# vim /usr/local/inotify/rsync.sh
#!/bin/bash
srcdir="/test"
dstdir="test"
excludedir="/usr/local/inotify/exclude.list"
rsyncuser="user1"
rsyncpassdir="/etc/passwd.txt"
dstip="192.168.47.131"
for ip in $dstip
do
rsync -avH --port=873 --progress --delete --exclude
from=$excludedir $srcdir $rsyncuser@$ip::$dstdir --passwordfile=$rsyncpassdir
done
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $srcdir | while read file
do
for ip in $dstip
do
rsync -avH --port=873 --progress --delete --exclude
from=$excludedir $srcdir $rsyncuser@$ip::$dstdir --passwordfile=$rsyncpassdir echo "${file} was rsynced" >> /tmp/rsync.log 2>&1
done
done
6、修改權(quán)限
[root@rsync-server inotify-tools-3.14]# mkdir -p /usr/local/inotify/exclude.list
[root@rsync-server inotify-tools-3.14]# chmod +x /usr/local/inotify/rsync.sh
腳本參數(shù)說明:
excludedir=/usr/local/inotify/exclude.list 不需要同步的?錄,如果有多個(gè),每??寫?個(gè)?錄,使?相 對(duì)于同步模塊的路徑
7、設(shè)置腳本開機(jī)自動(dòng)執(zhí)行
[root@rsync-server inotify-tools-3.14]# vim /etc/rc.d/rc.local #編輯,在最后添加?行
sh /usr/local/inotify/rsync.sh & #設(shè)置開機(jī)?動(dòng)在后臺(tái)運(yùn)行腳本
[root@rsync-server inotify-tools-3.14]# chmod +x /etc/rc.d/rc.local
[root@rsync-server inotify-tools-3.14]# systemctl enable rc-local
至此,所有的配置就完成了,這個(gè)項(xiàng)目還是很實(shí)用的。