服務 - Sersync數據同步詳細教程


Sersync數據同步


.rsync介紹

rsync是類unix系統下的數據鏡像備份工具——remote sync。一款快速增量備份工具 Remote Sync,遠程同步支持本地複製,或者與其他SSHrsync主機同步。

它的特性如下:

可以鏡像保存整個目錄樹和文件系統。

可以很容易做到保持原來文件的權限、時間、軟硬鏈接等等。

無須特殊權限即可安裝。

快速:第一次同步時 rsync 會複製全部內容,但在下一次只傳輸修改過的文件。rsync 在傳輸數據的過程中可以實行壓縮及解壓縮操作,因此可以使用更少的帶寬。

安全:可以使用scpssh等方式來傳輸文件,當然也可以通過直接的socket連接。

支持匿名傳輸,以方便進行網站鏡象。

IP

角色

安裝軟件

目錄

目的

192.168.91.166

Rsync daemon 服務端

rsync

/data

192.168.91.156 rsync 客戶端上的/data下目錄實時推送到192.168.91.166 rsync服務器端/data

192.168.91.156

Rsync 客戶端

serync

/data

 

.rsync的安裝

yum install -y rsync

.rsync通過ssh認證傳輸

3.1將數據發送到遠端

rsync -avz -P 本地目錄 -e 'ssh -p 端口號' 遠端用戶名@遠端IP:遠端路徑

root@template /tmp 16:33:07 # cat rsync.txt

我是rsync測試

root@template /tmp 16:34:02 # rsync -avzP /tmp/rsync.txt -e 'ssh -p 22' [email protected]:/tmp

The authenticity of host '192.168.44.100(192.168.44.100)' can't be established.

RSA key fingerprint is57:22:33:d6:43:40:31:84:88:19:5d:f5:fc:7d:4e:ce.

Are you sure you want to continue connecting(yes/no)? yes

Warning: Permanently added '192.168.44.100' (RSA) tothe list of known hosts.

reverse mapping checking getaddrinfo for bogon[192.168.44.100] failed - POSSIBLE BREAK-IN ATTEMPT!

[email protected]'s password:

sending incremental file list

rsync.txt

          18100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)

 

sent 93 bytes received 31 bytes  13.05 bytes/sec

total size is 18 speedup is 0.15

詳細參數:

-v, --verbose 詳細模式輸出

-q, --quiet 精簡輸出模式

-c, --checksum 打開校驗開關,強制對文件傳輸進行校驗

-a, --archive 歸檔模式,表示以遞歸方式傳輸文件,並保持所有文件屬性,等於-rlptgoD

-r, --recursive 對子目錄以遞歸模式處理

-R, --relative 使用相對路徑信息

-b, --backup 創建備份,也就是對於目的已經存在有同樣的文件名時,將老的文件重新命名爲~filename。可以使用--suffix選項來指定不同的備份文件前綴。

--backup-dir 將備份文件(~filename)存放在在目錄下。

-suffix=SUFFIX 定義備份文件前綴

-u, --update 僅僅進行更新,也就是跳過所有已經存在於DST,並且文件時間晚於要備份的文件。(不覆蓋更新的文件)

-l, --links 保留軟鏈結

-L, --copy-links 想對待常規文件一樣處理軟鏈結

--copy-unsafe-links 僅僅拷貝指向SRC路徑目錄樹以外的鏈結

--safe-links 忽略指向SRC路徑目錄樹以外的鏈結

-H, --hard-links 保留硬鏈結

-p, --perms 保持文件權限

-o, --owner 保持文件屬主信息

-g, --group 保持文件屬組信息

-D, --devices 保持設備文件信息

-t, --times 保持文件時間信息

-S, --sparse 對稀疏文件進行特殊處理以節省DST的空間

-n, --dry-run現實哪些文件將被傳輸

-W, --whole-file 拷貝文件,不進行增量檢測

-x, --one-file-system 不要跨越文件系統邊界

-B, --block-size=SIZE 檢驗算法使用的塊尺寸,默認是700字節

-e, --rsh=COMMAND 指定使用rshssh方式進行數據同步

--rsync-path=PATH 指定遠程服務器上的rsync命令所在路徑信息

-C, --cvs-exclude 使用和CVS一樣的方法自動忽略文件,用來排除那些不希望傳輸的文件

--existing 僅僅更新那些已經存在於DST的文件,而不備份那些新創建的文件

--delete 刪除那些DSTSRC沒有的文件

--delete-excluded 同樣刪除接收端那些被該選項指定排除的文件

--delete-after 傳輸結束以後再刪除

--ignore-errors 及時出現IO錯誤也進行刪除

--max-delete=NUM 最多刪除NUM個文件

--partial 保留那些因故沒有完全傳輸的文件,以是加快隨後的再次傳輸

--force 強制刪除目錄,即使不爲空

--numeric-ids 不將數字的用戶和組ID匹配爲用戶名和組名

--timeout=TIME IP超時時間,單位爲秒

-I, --ignore-times 不跳過那些有同樣的時間和長度的文件

--size-only 當決定是否要備份文件時,僅僅察看文件大小而不考慮文件時間

--modify-window=NUM 決定文件是否時間相同時使用的時間戳窗口,默認爲0

-T --temp-dir=DIR DIR中創建臨時文件

--compare-dest=DIR 同樣比較DIR中的文件來決定是否需要備份

-P 等同於 --partial

--progress 顯示備份過程

-z, --compress 對備份的文件在傳輸時進行壓縮處理

--exclude=PATTERN 指定排除不需要傳輸的文件模式

--include=PATTERN 指定不排除而需要傳輸的文件模式

--exclude-from=FILE 排除FILE中指定模式的文件

--include-from=FILE 不排除FILE指定模式匹配的文件

--version 打印版本信息

--address 綁定到特定的地址

--config=FILE 指定其他的配置文件,不使用默認的rsyncd.conf文件

--port=PORT 指定其他的rsync服務端口

--blocking-io 對遠程shell使用阻塞IO

-stats 給出某些文件的傳輸狀態

--progress 在傳輸時現實傳輸過程

--log-format=formAT 指定日誌文件格式

--password-file=FILE FILE中得到密碼

--bwlimit=KBPS 限制I/O帶寬,KBytes per second

-h, --help 顯示幫助信息


3.2從遠端拉取數據

rsync -avz -P -e 'ssh -p 端口號' 遠端用戶名@遠端IP:遠端路徑 本地目錄

root@template /tmp 16:34:19 # rsync -avzP  -e 'ssh -p 22'[email protected]:/tmp/rsync.txt /tmp

reverse mapping checking getaddrinfo for bogon[192.168.44.100] failed - POSSIBLE BREAK-IN ATTEMPT!

[email protected]'s password:

receiving incremental file list

rsync.txt

          19100%   18.55kB/s    0:00:00 (xfer#1, to-check=0/1)

 

sent 36 bytes received 99 bytes  30.00 bytes/sec

total size is 19 speedup is 0.14

root@template /tmp 16:39:26 # cat rsync.txt

我是rsync測試2


.通過rsync認證傳輸

4.1備份服務器配置也就是服務器端192.168.91.166

4.1.1創建rsync同步用戶    

[root@salt-master data]# useradd -M -s /sbin/nologin rsync

4.1.2修改配置文件(vim /etc/rsyncd.conf

[root@salt-master data]# cat /etc/rsyncd.conf

#config_______________start

##rsyncd.conf start##

uid = root  #此時用戶可以是別的,但是只要有管理服務端下/data的權限即可,不一定非得要用root

gid = root

use chroot = no              #內網設置爲no

max connections = 200          #最大連接數默認爲0無限大,負數爲關閉

timeout = 300              #超時默認爲0 notimeout無超時時間 建議200-6005-10分鐘)

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock

log file = /var/log/rsyncd.log

[data]                      #新建的模塊名

comment = datafile by caimengzhi at 2016-11-10  #註釋部分

path = /data/                  #默認上傳和下載的路徑

ignore errors                  #忽略錯誤

read only = false                #禁止只讀可寫

list = false                     #禁止列表

hosts allow = 192.168.91.0/24            #指定可以連接的客戶端ip或地址段

hosts deny = 0.0.0.0/32             #指定不可以連接的ip或地址段

auth users = rsync_body             #客戶端同步的虛擬用戶

secrets file = /etc/rsync.password        #客戶端同步的虛擬機用戶的密碼


4.1.3添加密碼文件

[root@salt-master data]# echo 'rsync_body:admin' >> /etc/rsync.password   #添加賬戶和密碼,前面是同步時候的虛擬賬號和密碼

[root@salt-master data]# cat /etc/rsync.password

rsync_body:admin

[root@salt-master data]# chmod 600  /etc/rsync.password


4.1.4啓動服務

[root@salt-master data]# rsync --daemon   #rsync服務端以守護進程方式啓動,且端口是tcp協議的873

[root@salt-master data]#  ps -ef | grep rsync |grep -v grep 

root       7247      1  0 17:21 ?        00:00:00 rsync --daemon

[root@salt-master data]# netstat -lnp| grep  873

tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      7247/rsync

tcp        0      0 :::873                      :::*                        LISTEN      7247/rsync            

[root@salt-master data]# lsof -i:873

COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

rsync   7247 root    4u  IPv4  67065      0t0  TCP *:rsync (LISTEN)

rsync   7247 root    5u  IPv6  67066      0t0  TCP *:rsync (LISTEN)             

4.2源數據服務器配置也就是客戶端192.168.91.156

4.2.1配置密碼文件

[root@salt-client01 sersync]# echo "admin">/etc/rsync.password   #客戶端只需要配置密碼文件即可,因爲在同步的時候會指定虛擬同步賬號

[root@salt-client01 sersync]# cat /etc/rsync.password

admin

[root@salt-client01 sersync]# chmod 600 /etc/rsync.password

[root@salt-client01 sersync]# ll /etc/rsync.password

-rw------- 1 root root 6 Nov 10 17:26 /etc/rsync.password

4.2.2向備份服務器發送數據

rsync -avz -P 本地目錄或文件 rsync_backup@服務端地址::模塊名--password-file=/etc/rsync.pwd

[root@salt-client01 sersync]# rsync -avz /data [email protected]::data --password-file=/etc/rsync.password

sending incremental file list

data/

data/3

 

sent 88 bytes  received 31 bytes  238.00 bytes/sec

total size is 0  speedup is 0.00

#說明rsync 服務端和客戶端是OK


4.2.3從備份服務器拉取

rsync -avz -P rsync_backup@服務端地址::模塊名 本地目錄或文件--password-file=/etc/rsync.pwd


.sersync的簡單原理

採用inotify來對文件進行監控,當監控到文件有文件發生改變的時候,就會調用rsync實現觸發式實時同步!


5.1安裝sersync(注意sersync是工作在rsync的源服務器上,也就是客戶端上

[root@salt-client01 ~]# cd /usr/local/src/

[root@salt-client01 src]# tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz  -C /usr/local/

GNU-Linux-x86/

GNU-Linux-x86/sersync2

GNU-Linux-x86/confxml.xml

[root@salt-client01 src]# cd /usr/local/

[root@salt-client01 local]# mv GNU-Linux-x86 sersync

[root@salt-client01 local]# cd sersync/

[root@salt-client01 sersync]# mkdir conf bin log

[root@salt-client01sersync]# mv confxml.xml conf

[root@salt-client01sersync]# mv sersync2 bin/sersync


5.2修改配置文件

       ***********************************30行開始******************************

   <commonParams params="-artuz"/>  #-artuzrsync同步時的參數

    <authstart="true" users="rsync的虛擬用戶名(rsync_backup)" passwordfile="rsync的密碼文件"/>

   <userDefinedPort start="true"port="873"/><!-- port=874 -->

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

    <sshstart="false"/>

       ************************************36***********************************

       <failLogpath="自己定義的log文件夾(/usr/local/sersync/log)rsync_fail_log.sh"

       timeToExecute="60"/><!--defaultevery 60mins execute once-->

       *******************************************************************************

       *注:若有多個目錄備份可以穿件多個配置文件在啓動時的-o參數中添加即可

 

[root@salt-client01 conf]# diff confxml.xml confxml.xml.bak

24,25c24,25

< <localpath watch="/data/">  #data就是本地需要同步的文件夾到服務器端的目錄

<     <remote ip="192.168.91.166" name="data"/> 

#data (server的模塊名)rsync 服務端的文件夾,也就是推送到服務器端的目標文件夾,可以配置多個,

---

> <localpath watch="/opt/tongbu">

>     <remote ip="127.0.0.1" name="tongbu1"/>

31c31

<     <auth start="true" users="rsync_body" passwordfile="/etc/rsync.password"/>   

#true 才能生效,rsync_body同步時候虛擬賬號,後面是密碼文件

---

>     <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>

33c33

<     <timeout start="true" time="100"/><!-- timeout=100 -->                    #true 才能生效

---

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

36c36

< <failLog path="/usr/local/sersync/log/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->  #檢測rsync進程判斷,沒有自動啓

---

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


5.3啓動sersync

[root@salt-client01 src]# echo 'export PATH=$PATH:/usr/local/sersync/bin'>>/etc/profile 

#聲明環境變量

[root@salt-client01 src]# source /etc/profile

[root@salt-client01 src]# sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml  #啓動

set the system param

executeecho 50000000 > /proc/sys/fs/inotify/max_user_watches

executeecho 327679 > /proc/sys/fs/inotify/max_queued_events

parse the command param

option: -r rsync all the local files to the remote servers before the sersync work

option: -d run as a daemon

option: -o config xml name:  /usr/local/sersync/conf/confxml.xml

daemon thread num: 10

parse xml config file

host ip : localhosthost port: 8008

daemon startsersync run behind the console

use rsync password-file :

user isrsync_body

passwordfile is /etc/rsync.password

config xml parse success

please set /etc/rsyncd.conf max connections=0 Manually

sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)

Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)

please according your cpu use -n param to adjust the cpu rate

------------------------------------------

rsync the directory recursivly to the remote servers once

working please wait...

execute command: cd /data && rsync -artuz -R --delete ./ --timeout=100 [email protected]::data --password-file=/etc/rsync.password >/dev/null 2>&1

run the sersync:

watch path is: /data  #此時可以看出sersync已經啓動成功了

 

#檢測腳本

[root@salt-client01 log]# pwd

/usr/local/sersync/log

[root@salt-client01 log]# vim rsync_fail_log.sh

[root@salt-client01 log]# chmod +x rsync_fail_log.sh

[root@salt-client01 ~]# cat /usr/local/sersync/log/rsync_fail_log.sh

#!/bin/bash

#Purpose: Check sersync whether it is alive

#Author: cai meng zhi

SERSYNC="/usr/local/sersync/bin/sersync2"

CONF_FILE="/usr/local/sersync/conf/confxml.xml"

STATUS=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l)

if [ $STATUS -eq 0 ];

then

        $SERSYNC -d -r -o $CONF_FILE &

else

        exit 0;

fi

腳本寫好以後,添加到計劃任務中去

*/1 * * * * /bin/bash /usr/local/sersync/log/rsync_fail_log.sh  > /dev/null 2>&1

 

測試同步:

客戶端新增文件

[root@salt-client01 data]# cp /etc/passwd 192.168.91.156.passwd

[root@salt-client01 data]# ll

total 4

-rw-r--r-- 1 root root 1928 Nov 10 18:15 192.168.91.156.passwd

-rw-r--r-- 1 root root    0 Nov 10 17:27 3

服務端檢測

[root@salt-master data]# cd /data/

[root@salt-master data]# ll

total 8

-rw-r--r-- 1 root root 1928 Nov 10 18:15 192.168.91.156.passwd  #說明已經同步過來了

-rw-r--r-- 1 root root    0 Nov 10 17:27 3

drwxr-xr-x 2 root root 4096 Nov 10 17:27 data

 

客戶端測試刪除

[root@salt-client01 data]# rm rf 192.168.91.156.passwd

rm: cannot remove `rf': No such file or directory

rm: remove regular file `192.168.91.156.passwd'? y

[root@salt-client01 data]# ll

total 0

-rw-r--r-- 1 root root 0 Nov 10 17:27 3

[root@salt-client01 data]#

 

服務器端:

[root@salt-master data]# ll

total 4

-rw-r--r-- 1 root root    0 Nov 10 17:27 3   #說明已經刪除掉了

drwxr-xr-x 2 root root 4096 Nov 10 17:27 data


5.4常見錯誤彙總

錯誤一:

@ERROR: auth failed on module xxxxx

rsync: connection unexpectedly closed(90 bytes read so far)

rsync error: error in rsync protocoldata stream (code 12) at io.c(150)

說明:這是因爲密碼設置錯了,無法登入成功,檢查一下rsync.pwd,看客服是否匹配。還有服務器端沒啓動rsync 服務也會出現這種情況。

 

錯誤二:

password file must not beother-accessible

continuing without password file

Password:

說明:這是因爲rsyncd.pwdrsyncd.sec的權限不對,應該設置爲600。如:chmod600 rsyncd.pwd

 

錯誤三:

@ERROR: chroot failed

rsync: connection unexpectedly closed(75 bytes read so far)

rsync error: error in rsync protocoldata stream (code 12) at io.c(150)

說明:這是因爲你在 rsync.conf中設置的 path 路徑不存在,要新建目錄才能開啓同步

 

錯誤四:

rsync: failed to connect to218.107.243.2: No route to host (113)

rsync error: error in socket IO (code10) at clientserver.c(104) [receiver=2.6.9]

說明:防火牆問題導致,這個最好先徹底關閉防火牆,排錯的基本法就是這樣,無論是S還是C,還有ignore errors選項問題也會導致

 

錯誤五:

@ERROR: access denied to www fromunknown (192.168.1.123)

rsync: connection unexpectedly closed(0 bytes received so far) [receiver]

rsync error: error in rsync protocoldata stream (code 12) at io.c(359)

說明:此問題很明顯,是配置選項hostallow的問題,初學者喜歡一個允許段做成一個配置,然後模塊又是同一個,致使導致

 

錯誤六:

rsync error: received SIGINT,SIGTERM, or SIGHUP (code 20) at rsync.c(244) [generator=2.6.9]

rsync error: received SIGUSR1 (code19) at main.c(1182) [receiver=2.6.9]

說明:導致此問題多半是服務端服務沒有被正常啓動,到服務器上去查查服務是否有啓動,然後查看下 /var/run/rsync.pid 文件是否存在,最乾脆的方法是殺死已經啓動了服務,然後再次啓動服務或者讓腳本加入系統啓動服務級別然後shutdown -r now服務器

 

錯誤七:

rsync: read error: Connection resetby peer (104)

rsync error: error in rsync protocoldata stream (code 12) at io.c(604) [sender=2.6.9]

說明:原數據目錄裏沒有數據存在

 

 

六.資料

.......


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