測試服務器信息
IP | 主機名 | 部署軟件 | 操作系統 | 說明 |
10.33.24.42 | slave-rsync | rsync-3.1.2-6.el7_6.1.x86_64 | Centos 7.6 x64 | 服務端 |
10.33.0.15 | master-rsync | rsync-3.1.2-6.el7_6.1.x86_64 inotify-tools-3.13.tar.gz | Centos 7.6 x64 | 客戶端 |
其他的原理就不說了,只是簡單記錄下操作過程。
實時同步的目錄:/usr/local/sourcecode
腳本和密碼文件存放的目錄:/usr/local/shellfile
我們想測試的目的是:當master-rsync主機的/usr/local/sourcecode/目錄下的文件或者文件夾發生變化的時候,slave-rsync的/usr/local/sourcecode/目錄實時發生變化。
在master-rsync主機安裝inofity-tool工具
[root@master-rsync software]#tar xzvf inotify-tools-3.13.tar.gz
[root@master-rsync software]#cd inofity-tools-3.13
[root@master-rsync software]#./configure --prefix=/usr/local/inofity-tool
[root@master-rsync software]#make && make install
上面四步就把inofity-tool安裝到了/usr/local/inofity-tool目錄下面了。
配置inofity-tool到環境變量
[root@master-rsync software]#vi /etc/profile
在這個文件的最底部輸入以下內容
export INOTIFY_HOME=/usr/local/inofity-tool export PATH=$INOTIFY_HOME/bin:$PATH
保存退出.
[root@master-rsync software]#source /etc/profile
輸入上面命令變量實時生效。
在master-rsync主機安裝rsync
[root@master-rsync software]#yum -y install rsync
在/usr/local/shellfile創建以下腳本
[root@master-rsync shellfile]#vi rsync_do.sh
#!/bin/bash src=/usr/local/sourcecode/ #需要實時同步的文件夾。注意後面必須以"/"結尾。 des1=web # 同步的模塊名稱。這個名稱是要和slave-rsync的主機rsync定義的模塊名稱對應的。特別重要。 #des2=web_log # web2上需要同步的目錄 host1=10.33.24.42 #rsync服務節點,就是slave-rsync的主機IP #host2=192.168.1.104 #rsync服務節點2 user1=www #連接rsync使用的用戶名web1上。就是2臺主機連接的用戶名。 #user2=backuser #連接rsync使用的用戶名web2上 inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -emodify,delete,create,attrib $src | while read file do /bin/rsync -vzrtopg --delete --bwlimit=2000 --progress $src $user1@$host1::$des1 --password-file=/usr/local/shellfile/rsync.passwd ##上面的rsync.passwd文件也放在/usr/local/shellfile目錄下。內容等會看看。 --bwlimit=2000 限速參數 。 #/bin/rsync -vzrtopg --delete --progress $src $user2@$host2::$des2 --password-file=/etc/rsync.password echo "${files} was rsynced" >> /var/log/rsync.log 2>&1 done
看看rsync.passwd的內容
[root@master-rsync shellfile]#cat rsync.passwd
123456
上面的這個123456就是連接到slave-rsync主機的密碼。用戶在上面的rsync_do.sh腳本里面定義了user1=www.
給rsync_do.sh腳本增加可執行權限。
[root@master-rsync shellfile]#chmod +x rsync_do.sh
master-rsync就安裝完成了。
下面我們開始安裝slave-rsync。
安裝非常的簡單。
[root@slave-rsync shellfile]#yum -y install rsync
這樣就好了。
如果slave-rsync有外網,直接上面這樣安裝就好了,yum源使用阿里雲或者163的都可以。
如果沒有外網,可以創建本地的yum源進行安裝或者下載tar.gz的源碼進行編譯安裝。
安裝如果有什麼問題,可以交流下,詳細的就不介紹了。就是上面的一個yum安裝就好了。
關鍵是配置要注意一下。
具體的配置文件如下:
[root@slave-rsync shellfile]#vi /etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode # See rsyncd.conf man page for more options. # configuration example: uid = root gid = root use chroot = no max connections = 20 pid file = /var/run/rsyncd.pid lock file = /var/run/rsyncd.lock log file = /var/log/rsyncd.log # exclude = lost+found/ # transfer logging = yes timeout = 900 ignore nonreadable = yes # dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 [web] ##這個模塊很重要,master-rsync 主機上面的腳本配置的dest1就是這個。 path=/usr/local/sourcecode/ ##本地的同步目錄。注意一定要以"/"結尾。 #ignore error read only = false # writeonly = false list = false hosts allow = * #slave-rsync作爲服務端,允許哪些主機連接,這裏輸入*表示所有的主機。也可以指定一個網絡段:10.33.0.0./24 ,也可以直接指定一個固定的IP。 hosts deny = 0.0.0.0/32 #不允許訪問的地址 auth users = www #定義連接的用戶名 secrets file = /usr/local/shellfile/rsync.passwd #定義用戶名和密碼
看看slave-rsync端的rsync.passwd內容
[root@slave-rsync shellfile]# cat rsync.passwd
www:123456
在 slave-rsync主機啓動rsync
[root@slave-rsync shellfile]# /bin/rsync --daemon
注意,在通過kill殺死rsync進程的時候,還需要處理掉/var/run/rsync.pid文件。
在master-rsync主機上,啓動rsync_do.sh腳本
[root@master-rsync shellfile]# ./rsync_do.sh &
測試一下
在master-rsync的/usr/local/sourcecode/目錄下增加、刪除、更新文件,同時查看slave-rsync的/usr/local/sourcecode/目錄下的文件變化。
[root@master-server sourcecode]# echo "this is my test">>test.txt
[root@master-server sourcecode]# sending incremental file list
./
test.txt
16 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=2/8)
sent 329 bytes received 47 bytes 752.00 bytes/sec
total size is 15,010,629 speedup is 39,921.89
sending incremental file list
sent 267 bytes received 21 bytes 576.00 bytes/sec
total size is 15,010,629 speedup is 52,120.24
在salve-rsync主機上查看文件變化
[root@slave-rsync sourcecode]# ll
總用量 14676
-rw-r--r-- 1 root root 202 7月 10 2019 1.txt
-rw-r--r-- 1 root root 11 7月 10 2019 2.tx
-rw-r--r-- 1 root root 16 7月 10 2019 2.txt
drwxr-xr-x 2 root root 18 7月 10 2019 freeflowtroll
-rw-r--r-- 1 root root 15010375 7月 10 2019 hsserver-3.1.4.49-1.el6.x86_64.rpm
-rw-r--r-- 1 root root 16 7月 10 2019 test.txt
[root@slave-rsync sourcecode]# cat test.txt
this is my test