Centos6.5下的rsync+inotify遠程實時文件同步服務搭建

以下爲本人的安裝過程


一、服務器端的安裝

1.系統環境

    服務端:centos6.5出廠環境(系統安裝時爲默認設置),IP:192.168.10.185

    客戶端:遠程登錄終端xshell5或securecrt(具體安裝和使用方法,請自行查詢),

            IP:192.168.10.186


2.安裝Rsync

    通過終端(此處是xshell)登錄系統192.168.10.185

    rpm -qa|grep rsync*  //查看當前是否已安裝此服務,若有並感覺版本低的話,可以執行

    rpm -e  rsync* //卸載該軟件

    

    安裝方式一:

    執行yum -y install rsync

    rpm -qa|grep rsync*   //執行此語句查看已安裝軟件的信息

    rsync.x86_64 0:3.0.6-12.el6

    安裝方式二:

    到rsync官網下載源碼包: http://rsync.samba.org/ 包名:rsync-3.1.1.tar.gz

    yum -y install lrzsz    //在命令行輸入rz:上傳剛剛下載的源碼包到服務端

    tar zxf rsync-3.1.1.tar.gz

    cd rsync-3.1.1

    ./configure --prefix=/app

    make

    make install

    啓動文件位置:/app/bin/rsync


3.創建rsync 配置文件(默認是沒有的)

在/etc/下分別創建 

rsyncd.conf(服務配置文件)

rsyncd.passwd(存放客戶端登錄rsync服務的賬號和密碼)

rsyncd.motd(登錄服務時的歡迎或說明信息,自由指定,可選)

##########################################################################################

vim /etc/rsyncd.conf  //創建服務配置文件

uid = root

gid = root

port = 873        //指定訪問的端口,默認是873,也可自己指定

hosts allow = 客戶端IP地址    //允許訪問的客戶端IP

#hosts deny = 

user chroot = yes

#max connections = 

#timeout =

[backup]

path = /bak  //注意,此目錄如果沒有,不要忘記創建哦!

comment = rsync files

ignore erros

read only = no

list = yes

auth usres = rsync  //同步驗證時用的賬號,若沒有則是匿名同步,client同步時沒有用戶名也能同步。

secrets file = /etc/rsync.passwd //認證文件存放的地方

#########################################################################

vim /etc/rsync.passwd    //創建認證文件

rsync:rsync2015  //必須是這種格式,rsync賬號爲服務配置文件中的auth users,它倆一致就行,密碼自定。

chmod 600 /etc/rsyncd.passwd   //更改文件權限爲所有者只讀

chown root.root /etc/rsync.passwd  //修改文件屬性


4.防火牆設置(此部分根據實際情況而定)

(1)centos 6下的防火牆端口開放

iptables -A INPUT  -p tcp --dport 873  -j ACCEPT  //服務器本地可以訪問,可用來先測試

iptables -I INPUT  -p tcp --dport 873  -j ACCEPT  //客戶端可以訪問

(2)centos7下的防火牆端口開放

添加

firewall-cmd --zone=public --add-port=80/tcp --permanent    (--permanent永久生效,沒有此參數重啓後失效)

重新載入

firewall-cmd --reload

查看

firewall-cmd --zone= public --query-port=80/tcp

刪除

firewall-cmd --zone= public --remove-port=80/tcp --permanent




5.啓動rsync服務

   /usr/bin/rsync --daemon 

注意將服務添加到啓動服務文件中,使其隨系統啓動而啓動,方法:

echo '/usr/bin/rsync --daemon' >>/etc/rc.local


6.查看服務是否啓動

[root@localhost ~]# lsof -i :873

COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

rsync   28055 root    3u  IPv4  81353      0t0  TCP *:rsync (LISTEN)

rsync   28055 root    5u  IPv6  81354      0t0  TCP *:rsync (LISTEN)

此情況爲服務已開啓


二、客戶端的安裝

    和服務端安裝一樣,執行yum -y install rsync

    創建認證密碼文件:vim /etc/secret.passwd(此項可選,路徑隨便,用法請見第三大部分2中的藍色部分)

      安裝inotify-tools-3.14.tar監控文件變化工具

      tar zxf inotify-tools-3.14.tar

      cd inotify-tools-3.14

      ./configure --prefix=/app/inotify/bin/inotifywait

      make  &&   make install

wKiom1l6zCzCxA9hAACRqZttIbw690.png

三、實例(本實驗服務端IP:192.168.10.185,客戶端IP:192.168.10.186)

    1.登錄客戶端,把服務器上的/bak文件夾中的內容備份到客戶端的/imagefile:

      /app/bin/rsync -rvlHpogDtS --delete  --progress \  [email protected]::bak  /imagefile  

    2.登錄客戶端,把客戶端/imagefile文件夾中的內容備份到服務器/bak中:

      /app/bin/rsync -rvlHpogDtS --delete  --progress  /imagefile  [email protected]::backup  --password-file=/etc/secret.passwd

(可以自動提供密碼驗證)

chmod 600 /etc/rsyncd.passwd   //更改文件權限爲所有者只讀

chown root.root /etc/rsyncd.passwd  //修改文件屬性

* 客戶端的password-file文件secret.passwd裏只寫對應服務端的“密碼”


vim  rsync+inotify.sh

#!/bin/bash

#首次運行此腳本,先執行上述命令語句相當於手動把現有的文件同步

/usr/bin/rsync   -rvlHpogDtS --delete  --progress  /imagefile [email protected]::backup  --password-file=/etc/rsyncd.passwd



#下述命令語句爲監視到文件或目錄有變化時執行同步操作

src=/imagefile

/app/inotify/bin/inotifywait -rmq -e create,modify,move,delete,attrib $src|while read event

do

/usr/bin/rsync   -rvlHpogDtS --delete  --progress  /imagefile  [email protected]::backup  --password-file=/etc/rsyncd.passwd

done


   *    --delete 視情況謹慎使用!加上--delete選項則保持兩邊文件一致,

    不加--delete則目標文件目錄只"接收"源文件目錄的文件而不隨着源文件目錄文件刪除而刪除

  * 客戶端的password-file文件secret.passwd裏只寫對應服務端的“密碼”

  *執行inotify+rsync.sh腳本,nohup /bin/bash /app/inotify+rsync.sh >/tmp/rsyncd.log &

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章