linux文件同步方案:rsync+sersync實現服務器間文件同步

拓撲圖

如圖,目的是將A中的指定文件實時同步到B和C的指定目錄下。在圖中我們將A成爲source或client,將B和C稱爲target或server。

 

我們在source和target上都安裝rsync以實現文件的遠程傳輸;在source中安裝sersync以實現對指定文件/文件夾的變化進行監聽並執行rsync文件同步命令;在target中開啓rsync daemon模式以監聽其他client發起的rsync請求。

 

具體需求如下:

1.source   192.168.188.130  /data/source

2.target    192.168.188.131  /data/target

當source中/data/source的文件發生變化時,實時增量同步到target的/data/target目錄下。

 

一、source和target上都安裝rsync

1.若未安裝rsync,則安裝rsync

[root@target ~]# rsync --version

rsync  version 2.6.8  protocol version 29

Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.

<http://rsync.samba.org/>

Capabilities: 64-bit files, socketpairs, hard links, ACLs, xattrs, symlinks, batchfiles,

              inplace, IPv6, 64-bit system inums, 64-bit internal inums



rsync comes with ABSOLUTELY NO WARRANTY.  This is free software, and you

are welcome to redistribute it under certain conditions.  See the GNU

General Public Licence for details.

可以看到已存在2.6.8版本,版本太低,將其升級爲3.0.9。先卸載當前版本:

[root@target ~]# yum remove rsync

Loaded plugins: rhnplugin, security

This system is not registered with RHN.

RHN support will be disabled.

Setting up Remove Process

Resolving Dependencies

--> Running transaction check

---> Package rsync.i386 0:2.6.8-3.1 set to be erased

--> Finished Dependency Resolution

…

Is this ok [y/N]: y

…

Complete!

rsync-3.0.9.tar.gz上傳到/tmp,然後安裝3.0.9版本:

[root@target ~]# cd /tmp

[root@target tmp]# tar zxf rsync-3.0.9.tar.gz

[root@target tmp]# cd rsync-3.0.9

[root@target rsync-3.0.9]# ./configure

[root@target rsync-3.0.9]# make && make install

2.默認安裝到了/usr/local/bin.rsync,配置環境變量:

[root@ localhost rsync-3.0.9]# vim /etc/profile.d/rsync.sh

#!/bin/sh

export PATH=$PATH:/usr/local/bin/rsync/bin

3.建立軟連接:

[root@target rsync-3.0.9]# ln -s /usr/local/bin/rsync /usr/bin/rsync

 

4.安裝完後查看版本,確定是否安裝成功:

[root@target /]# rsync --version

rsync  version 3.0.9  protocol version 30

Copyright (C) 1996-2011 by Andrew Tridgell, Wayne Davison, and others.

Web site: http://rsync.samba.org/

Capabilities:

    64-bit files, 64-bit inums, 32-bit timestamps, 64-bit long ints,

    socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,

    append, no ACLs, xattrs, iconv, no symtimes



rsync comes with ABSOLUTELY NO WARRANTY.  This is free software, and you

are welcome to redistribute it under certain conditions.  See the GNU

General Public Licence for details.

二、target上開啓daemon模式

rsync daemon是"rsync --daemon"或再加上其他一些選項啓動的,它會讀取配置文件,默認是/etc/rsyncd.conf,並默認監聽在873端口上,當外界有客戶端對此端口發起連接請求,通過這個網絡套接字就可以完成連接,以後與該客戶端通信的所有數據都通過該網絡套接字傳輸。

 

  1. 創建需要的目錄和文件
[root@target /]# mkdir /etc/rsyncd/    #創建配置文佳的目錄

[root@target /]# cd /etc/rsyncd/       

[root@target rsyncd]# touch rsyncd.conf   #創建rsync的配置文件

[root@target rsyncd]# touch rsyncd.pw    #創建rsync的密碼文件

[root@target rsyncd]# chmod -R 600 .     #將文件權限設置爲root

 

2.編輯配置文件(注意在複製時不要保留註釋,此處僅爲解釋說明用)

[root@target rsyncd]# vim rsyncd.conf



uid = root       #rsync服務的運行用戶,默認是nobody,文件傳輸成功後屬主將是這個uid

gid = root       #rsync服務的運行組,默認是nobody,文件傳輸成功後屬組將是這個gid

max connections = 100          #最大連接數

timeout = 300         # 確保rsync服務器不會永遠等待一個崩潰的客戶端,0表示永遠等待

use chroot = no

log file = /var/rsync/rsync.log       #指定同步日誌

pid file = /var/rsync/rsyncd.pid     #指定運行時PID文件

lock file = /var/rsync/rsyncd.lock

dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2  # 指定哪些文件不用進行壓縮傳輸



###########下面指定模塊,並設定模塊配置參數,可以創建多個模塊###########

[hr_syn]        # 模塊ID

path = /data/target/ # 指定該模塊的路徑,該參數必須指定。啓動rsync服務前該目錄必須存在。rsync請求訪問模塊本質就是訪問該路徑。

ignore errors = true      # 忽略某些IO錯誤信息

read only = false  # 指定該模塊是否可讀寫,即能否上傳文件,false表示可讀寫,true表示可讀不可寫。所有模塊默認不可上傳

write only = false # 指定該模式是否支持下載,設置爲true表示客戶端不能下載。所有模塊默認可下載

list = false       # 客戶端請求顯示模塊列表時,該模塊是否顯示出來,設置爲false則該模塊爲隱藏模塊。默認true

hosts allow = 192.168.188.130/24 # 指定允許連接到該模塊的機器,多個ip用空格隔開或者設置區間

hosts deny = 0.0.0.0/32   # 指定不允許連接到該模塊的機器

auth users = rsync_user # 指定連接到該模塊的用戶列表,只有列表裏的用戶才能連接到模塊,用戶名和對應密碼保存在secrts file中,

                          # 這裏使用的不是系統用戶,而是虛擬用戶。不設置時,默認所有用戶都能連接,但使用的是匿名連接

secrets file = /etc/rsyncd/rsyncd.pw # 保存auth users用戶列表的用戶名和密碼,每行包含一個username:passwd。由於"strict modes"

                                  # 默認爲true,所以此文件要求非rsync daemon用戶不可讀寫。只有啓用了auth users該選項纔有效。
  1. 編輯密碼文件
[root@target rsyncd]# vim rsyncd.pw

rsync_user:123456
  1. 啓動rsync_daemon守護進程
[root@target rsyncd]# mkdir /var/rsync/

[root@target rsyncd]# rsync --daemon --config=/etc/rsyncd/rsyncd.conf

 

4.默認監聽873端口,如果有啓用iptables,則放行873端口:

[root@target rsyncd]# /sbin/iptables -I INPUT -p tcp --dport 873 -j ACCEPT

[root@target rsyncd]# /etc/rc.d/init.d/iptables save

[root@target rsyncd]# service iptables restart

 

  1. 查看iptables狀態
[root@target rsyncd]# /etc/init.d/iptables status

 

三、source上安裝sersync並啓動sersync服務

1.安裝sersync

先將sersync2.5.4_64bit_binary_stable_final.tar.gz上傳到source的/tmp文件夾,然後進行安裝:

[root@livedvd ~]# cd /tmp/

[root@livedvd tmp]# tar zxf sersync2.5.4_64bit_binary_stable_final.tar.gz

[root@livedvd tmp]# mv GNU-Linux-x86 /usr/local/bin/sersync

 

2.修改環境變量配置

[root@livedvd sersync]# vim /etc/profile.d/sersync.sh

#!/bin/sh

export PATH=$PATH:/usr/local/bin/sersync/

3.編輯sersync配置文件

[root@livedvd /]# vim /usr/local/bin/sersync/confxml.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<head version="2.5">

    <host hostip="localhost" port="8999"></host>

    <debug start="false"/>           # 是否開啓調試模式,下面所有出現false和true的地方都分別表示關閉和開啓的開關

    <fileSystem xfs="false"/>        # 監控的是否是xfs文件系統

    <filter start="false">           # 是否啓用監控的篩選功能,篩選的文件將不被監控

        <exclude expression="(.*)\.svn"></exclude>

        <exclude expression="(.*)\.gz"></exclude>

        <exclude expression="^info/*"></exclude>

        <exclude expression="^static/*"></exclude>

    </filter>

    <inotify>                         # 監控的事件,默認監控的是delete/close_write/moved_from/moved_to/create folder

        <delete start="false"/>  #不監控文件或目錄的刪除

        <createFolder start="true"/>

        <createFile start="false"/> #不監控文件的創建

        <closeWrite start="true"/> # 通過監聽文件的關閉,實現監聽文件的增改

        <moveFrom start="true"/>

        <moveTo start="true"/>

        <attrib start="false"/>

        <modify start="false"/>

    </inotify>



    <sersync>                       # rsync命令的配置段

        <localpath watch="/data/source">    # 同步的目錄或文件,同inotify+rsync一樣,建議同步目錄

            <remote ip="192.168.188.131" name="hr_syn"/>  # 目標地址和rsync daemon的模塊名,所以遠端要以daemon模式先運行好rsync

            <!--remote ip="IPADDR" name="module"-->     # 除非下面開啓了ssh start,此時name爲遠程shell方式運行時的目標目錄

        </localpath>

        <rsync>                      # 指定rsync選項

            <commonParams params="-az"/>

            <auth start="true" users="rsync_user" passwordfile="/etc/rsyncd/rsyncd.pw"/> #此處填寫target對應的rsync用戶名,指定密碼文件,密碼文件中的密碼爲target端指定的密碼

            <userDefinedPort start="false" port="874"/><!-- port=874 -->

            <timeout start="false" time="100"/><!-- timeout=100 -->

            <ssh start="false"/>      # 是否使用遠程shell模式而非rsync daemon運行rsync命令

        </rsync>

        <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->  # 錯誤重傳

        <crontab start="false" schedule="600"><!--600mins-->    # 是否開啓crontab功能

            <crontabfilter start="false">       # crontab定時傳輸的篩選功能

                <exclude expression="*.php"></exclude>

                <exclude expression="info/*"></exclude>

            </crontabfilter>

        </crontab>

        <plugin start="false" name="command"/>

    </sersync>



    <plugin name="command">

        <param prefix="/bin/sh" suffix="" ignoreError="true"/>  <!--prefix /opt/tongbu/mmm.sh suffix-->

        <filter start="false">

            <include expression="(.*)\.php"/>

            <include expression="(.*)\.sh"/>

        </filter>

    </plugin>



    <plugin name="socket">

        <localpath watch="/opt/tongbu">

            <deshost ip="192.168.138.20" port="8009"/>

        </localpath>

    </plugin>

    <plugin name="refreshCDN">

        <localpath watch="/data0/htdocs/cms.xoyo.com/site/">

            <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>

            <sendurl base="http://pic.xoyo.com/cms"/>

            <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>

        </localpath>

    </plugin>

</head>

4.編輯密碼文件

[root@livedvd /]# mkdir /etc/rsyncd/

[root@livedvd /]# touch /etc/rsyncd/rsyncd.pw

[root@livedvd /]# vim /etc/rsyncd/rsyncd.pw

123456

[root@livedvd /]#chmod 600 /etc/rsyncd/rsyncd.pw

5.創建日誌文件

[root@livedvd /]# mkdir /var/rsync

[root@livedvd /]# touch /var/rsync/rsync_error.log

6.開啓sersync服務

[root@livedvd sersync]# /usr/local/bin/sersync/sersync2 -r -d -o /usr/local/bin/sersync/confxml.xml

 

四、測試

1. 向source的監聽目錄中新建一個文件

[root@livedvd ~]# cd /data/source/

[root@livedvd source]# touch test1.txt

2.查看target的/data/target,發現test1.txt並沒有同步過來。查看同步日誌:

[root@localhost rsync]# cd /var/rsync/

[root@localhost rsync]# tail -f rsync.log

2019/10/07 17:06:58 [18614] name lookup failed for 192.168.188.130: Name or service not known

2019/10/07 17:06:58 [18614] connect from UNKNOWN (192.168.188.130)

2019/10/07 17:06:58 [18614] rsync: connection unexpectedly closed (21 bytes received so far) [Receiver]

2019/10/07 17:06:58 [18614] rsync error: error in rsync protocol data stream (code 12) at io.c(605) [Receiver=3.0.9]

3.需要將192.168.188.130添加到/etc/hosts中:

[root@localhost rsync]# vim /etc/hosts

192.168.188.130  192.168.188.130

4.再次修改source中的test1.txt文件

[root@livedvd source]# touch test1.txt

 

5.此時再查看target的/data/target路徑,test1.txt成功同步

[root@localhost rsync]# cd /data/target/

[root@localhost target]# ls

test1.txt

附件:

rsync-3.0.9.tar.gz:https://download.csdn.net/download/jc_workspace/11834690

sersync2.5.4_64bit_binary_stable_final.tar.gz:https://download.csdn.net/download/jc_workspace/11834691

特別感謝:

https://www.cnblogs.com/f-ck-need-u/p/7220009.html#auto_id_8

https://www.cnblogs.com/cnsanshao/p/4024126.html

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