1倦挂、環(huán)境準備
操作系統(tǒng):CentOS 7
源服務器地址:192.168.47.129
目標服務器地址:192.168.47.131
目的:將源服務器上的/test目錄實時同步到目標服務器上的/test目錄下
PS:需要配置好端口和安全規(guī)則威始,源服務器去同步目標服務器上的內(nèi)容罗珍。
配置源服務器:192.168.47.129
1、安裝和啟動rsync玄柏,創(chuàng)建用于測試目錄/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 #遇到錯誤可以查看?志,很詳細
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.pass #?戶認證配置?件藏雏,??保存? 戶名稱和密碼,后?會創(chuàng)建這個?件
motd file = /etc/rsyncd.Motd #rsync啟動時歡迎信息頁面文件位置
[test] #同步模塊名稱作煌,自定義(不要太短了)
path = /test #rsync服務端數(shù)據(jù)目錄路徑
comment = test #模塊名稱掘殴,和[test]?樣
uid = root # 需要兩服務器目錄和文件的屬主屬組
gid = root # 需要兩服務器目錄和文件的屬主屬組
port=873 #rsync 默認端?
use chroot = no
read only = no
list = no #不顯示rsync服務端資源列表
max connections = 200
timeout = 600
auth users = user1 #執(zhí)行數(shù)據(jù)同步的?戶名,可以設置多個,?英?狀態(tài)下逗號隔開
#hosts allow = 192.168.47.130 #允許進?數(shù)據(jù)同步的客戶端IP地 址,可以設置多個抖部,?英?狀態(tài)下逗號隔開
#hosts deny = 192.168.47.132 #不允許同步的ip地址,?逗號隔 開
3病瞳、創(chuàng)建用戶認證文件
[root@rsync-server ~]# vim /etc/rsync.pass
user1:123456 #格式揽咕,?戶名:密碼,可以設置多個套菜,每??個 ?戶名:密碼
4亲善、設置?件權限 , 重啟rsync
[root@rsync-server ~]# chmod 600 /etc/rsyncd.conf #設置?件所有者讀取、寫?權限
[root@rsync-server ~]# chmod 600 /etc/rsync.pass #設置?件所有者讀取逗柴、寫?權限
[root@rsync-server ~]# systemctl restart rsyncd
配置目標服務器: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 #設置?件權限嚎于,只設置?件所有者具 有讀取掘而、寫?權限
注意:這?的密碼和客戶端的密碼是?樣的
配置完成,測試同步
[root@rsync-agent ~]# rsync -avH --port=873 --progress --delete /test user1@192.168.47.129::test --password-file=/etc/passwd.txt
安裝Inotify-tools?具于购,實時觸發(fā)rsync進?同步
1袍睡、查看服務器內(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肋僧、設置系統(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默認參數(shù)(inotify默認內(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)建腳本止潘,實時觸發(fā)rsync進?同步
[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、修改權限
[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 不需要同步的?錄辫诅,如果有多個凭戴,每??寫?個?錄,使?相 對于同步模塊的路徑
7炕矮、設置腳本開機自動執(zhí)行
[root@rsync-server inotify-tools-3.14]# vim /etc/rc.d/rc.local #編輯么夫,在最后添加?行
sh /usr/local/inotify/rsync.sh & #設置開機?動在后臺運行腳本
[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
至此,所有的配置就完成了肤视,這個項目還是很實用的档痪。