Rsync+sersync實現數據實時同步

RSYNC數據備份

RSYNC=Remote Sync 遠程同步 高效,一定要結合shell
官方網站:https://rsync.samba.org/
Author: Andrew Tridgell, Wayne Davison, and others
Andrew Tridgell是Samba項目的領導者和主要開發人員,同時還在參與開發rsync、Linux Kernel。

#rsync --version                          #查看rsync版本,可以看到相關作者相信
rsync  version 3.0.6  protocol version 30
Copyright (C)1996-2009 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/

與SCP的比較:scp=無法備份大量數據,類似windows的複製
rsync=邊複製 ,邊統計,邊比較

scp 的命令格式
1.從當前複製到遠程
scp   文件         遠程用戶@遠程Ip:/路徑
如: scp  /etc/passd/*    [email protected]:/tmp
2.從遠程複製到本地
scp    遠程用戶@遠程IP:/文件路徑     當前路徑
scp   [email protected]:/etc/passwd  ./

Rysnc特性和優點
可以鏡像保存整個目錄樹和文件系統。
可以很容易做到保持原來文件的權限、時間、軟硬鏈接等等。
無須特殊權限即可安裝。
快速:第一次同步時 rsync 會複製全部內容,但在下一次只傳輸修改過的文件。
壓縮傳輸:rsync 在傳輸數據的過程中可以實行壓縮及解壓縮操作,因此可以使用更少的帶寬。
安全:可以使用scp、ssh等方式來傳輸文件,當然也可以通過直接的socket連接。
支持匿名傳輸,以方便進行網站鏡象。
選擇性保持:符號連接,硬鏈接,文件屬性,權限,時間等

常見備份分類
完整備份,差異備份,增量備份
完整備份:每次備份都是從備份源將所有的文件或目錄備份到目的地
差量備份:備份上次完全備份以後有變化的數據(他針對的上次的完全備份,他備份過程中不清除存檔屬性)
增量備份:備份上次備份以後有變化的數據.(他纔不管是那種類型的備份,有變化的數據就備份,他會清除存檔屬性)

運行模式和端口
採用C/S模式(客戶端/服務器模式)[ 就是一個點到點的傳輸,直接使用rsync命令 ]
端口873

發起端和備份源
四個名詞的解釋:
發起端:負責發起rsync同步操作的客戶機叫做發起端,通知服務器我要備份你的數據
備份源:負責相應來自客戶機rsync同步操作的服務器腳在備份源,需要備份的服務器
服務端:運行rsyncd服務,一般來說,需要備份的服務器
客戶端:存放備份數據

數據同步方式
推push:一臺主機負責把數據傳送給其他主機,服務器開銷很大,比較適合後端服務器少的情況
拉pull:所有主機定時去找一主機拉數據,可能就會導致數據緩慢
推:目的主機配置爲rsync服務器,源主機週期性的使用rsync命令把要同步的目錄推過去(需要備份的機器是客戶端,存儲備份的機器是服務端)
拉:源主機配置爲rsync服務器,目的主機週期性的使用rsync命令把要同步的目錄拉過來(需要備份的機器是服務端,存儲備份的機器是客戶端)
兩種方案,rsync都有對應的命令來實現

在這裏插入圖片描述
Xinetd管理Rsync工作原理
在這裏插入圖片描述

使用rsync來同步是先通過xinetd監聽873號端口,如果rsync進來的是873號端口,那麼xinetd就會通知它所管轄的rsync服務來做迴應,接下來就是rsync倆服務於之間的通訊

Rsync服務安裝

源主機10.10.100.175------------------->備份服務器10.10.100.164
主要是在源主機將需要備份的文件推送到備份服務器,進行保存,所以兩臺服務器都需要安裝rsync
Rsync服務依賴Xinetd,是使用超級服務來管理的
兩邊服務器上安裝rsync服務端

[root@test164 ~]# yum -y install xinetd rsync
[root@test164 ~]# rsync --daemon             #啓動服務
[root@test164 ~]# netstat -auntp |grep 873
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      6354/rsync          
tcp6       0      0 :::873                  :::*                    LISTEN      6354/rsync

在這裏插入圖片描述

Rsync命令
rsync命令和scp命令很相似
-a, --archive archive mode 權限保存模式,相當於 -rlptgoD 參數,存檔,遞歸,保持屬性等
-r, --recursive 複製所有下面的資料,遞歸處理
-p, --perms 保留檔案權限 ,文件原有屬性
-t, --times 保留時間點,文件原有時間
-g, --group 保留原有屬組
-o, --owner 保留檔案所有者(root only)
-D, --devices 保留device資訊(root only)
-l, --links 複製所有的連接 ,拷貝連接文件
-z, --compress 壓縮模式, 當資料在傳送到目的端進行檔案壓縮.
-H, --hard-links 保留硬鏈接文件
-A, --acls 保留ACL屬性文件,需要配合–perms
-P,-P參數和 --partial --progress 相同.只是爲了把參數簡單化,表示傳進度
–version, 輸出rsync版本
-v , --verbose 複雜的輸出信息
-u, --update 僅僅進行更新,也就是跳過已經存在的目標位置,並且文件時間要晚於要備份的文件,不覆蓋新的文件
–port=PORT, 定義rsyncd(daemon)要運行的port(預設爲tcp 873)
–delete, 刪除那些目標位置有的文件而備份源沒有的文件
–password-file=FILE ,從 FILE 中得到密碼
–bwlimit=KBPS, 限制 I/O 帶寬
–filter “-filename”,需要過濾的文件
–exclude=filname,需要過濾的文件
–progress,顯示備份過程

常用的 –avz

使用rsync備份數據
對175網站根目錄的/var/www/html目錄備份到164的/web-back

源服務器:175
目標服務器:164

建立測試用戶

[root@test164 ~]# useradd rget1 ;echo rget1:123456 |chpasswd 
[root@test175 ~]# useradd rget1 ;echo rget1:123456 |chpasswd

//測試用戶,rget1用於下載

對目錄賦予ACL權限

[root@test175 ~]# setfacl -R -m user:rget1:rwx /var/www/html/
[root@test175 ~]# mkdir /var/www/html/ -p
[root@test175 ~]# setfacl -R -m default:rget1:rwx /var/www/html/
[root@test175 ~]# getfacl /var/www/html/
getfacl: Removing leading '/' from absolute path names
# file: var/www/html/
# owner: root
# group: root
user::rwx
user:rget1:rwx
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:user:rget1:rwx
default:group::r-x
default:mask::rwx
default:other::r-x

創建測試數據

[root@test175 ~]# cp -r /boot/* /var/www/html/    #boot目錄下的所有數據作爲測試數據
[root@test164 ~]# mkdir /web-back
[root@test164 ~]# chown rget1:rget1 -R /web-back/
[root@test175 ~]# rsync -avz --delete /var/www/html/ [email protected]:/web-back/
正式環境不要用 --delete參數
[root@test164 ~]# cd /web-back/
[root@test164 web-back]# ll
總用量 0
[root@test164 web-back]# ll
總用量 94100
-rw-r--r--. 1 rget1 rget1   151918 10月 15 11:17 config-3.10.0-957.el7.x86_64
drwxr-xr-x. 3 rget1 rget1       17 10月 15 11:17 efi
drwxr-xr-x. 2 rget1 rget1       27 10月 15 11:17 grub
drwx------. 5 rget1 rget1       97 10月 15 11:17 grub2
-rw-rwxr--. 1 rget1 rget1    47728 10月 15 01:42 index.html
-rw-------. 1 rget1 rget1 57146054 10月 15 11:17 initramfs-0-rescue-a8ad76e73be944799750613c7bf1ac95.img
-rw-------. 1 rget1 rget1 21853292 10月 15 11:17 initramfs-3.10.0-957.el7.x86_64.img
-rw-r--r--. 1 rget1 rget1   314036 10月 15 11:17 symvers-3.10.0-957.el7.x86_64.gz
-rw-------. 1 rget1 rget1  3543471 10月 15 11:17 System.map-3.10.0-957.el7.x86_64
-rwxr-xr-x. 1 rget1 rget1  6639904 10月 15 11:17 vmlinuz-0-rescue-a8ad76e73be944799750613c7bf1ac95
-rwxr-xr-x. 1 rget1 rget1  6639904 10月 15 11:17 vmlinuz-3.10.0-957.el7.x86_64

非系統用戶備份數據

使用系統配置文件/etc/rsyncd.conf來備份數據,創建備份賬戶,最後把rsync以deamon方式運行

rsyncd.conf配置文件

配置文件分爲兩部分:全局參數,模塊參數
全局參數:對rsync服務器生效,如果模塊參數和全局參數衝突,衝突的地方模塊參數生效
模塊參數:定義需要通過rsync輸出的目錄定義的參數

常見的全局參數

port          #→指定後臺程序使用的端口號,默認爲873。
uid            #→該選項指定當該模塊傳輸文件時守護進程應該具有的uid,配合gid選項使用可以確定哪些可以訪問怎麼樣的文件權限,默認值是" nobody"。
gid            #→該選項指定當該模塊傳輸文件時守護進程應該具有的gid。默認值爲" nobody"。
max connections        #→指定該模塊的最大併發連接數量以保護服務器,超過限制的連接請求將被告知隨後再試。默認值是0,也就是沒有限制。
lock file                  #→指定支持max connections參數的鎖文件,默認值是/var/run/rsyncd.lock。
motd file         #→" motd file"參數用來指定一個消息文件,當客戶連接服務器時該文件的內容顯示給客戶,默認
是沒有motd文件的。
log file             #→" log file"指定rsync的日誌文件,而不將日誌發送給syslog。
pid file              #→指定rsync的pid文件,通常指定爲“/var/run/rsyncd.pid”,存放進程ID的文件位置。

hosts allow =    #→單個IP地址或網絡地址   //允許訪問的客戶機地址

常見的模塊參數:主要是定義服務器哪個要被同步輸出,其格式必須爲“ [ 共享模塊名 ]” 形式,這個名字就是在 rsync 客戶端看到的名字,其實很像 Samba 服務器提供的共享名。而服務器真正同步的數據是通過 path 來指定的。

Comment         #→給模塊指定一個描述,該描述連同模塊名在客戶連接得到模塊列表時顯示給客戶。默認沒有描述定義。
Path                  #→指定該模塊的供備份的目錄樹路徑,該參數是必須指定的。
read only         #→yes爲只允許下載,no爲可以下載和上傳文件到服務器
exclude             #→用來指定多個由空格隔開的多個文件或目錄(相對路徑),將其添加到exclude列表中。這等同於在客戶端命令中使用―exclude或----filter來指定某些文件或目錄不下載或上傳(既不可訪問)
exclude from   #→指定一個包含exclude模式的定義的文件名,服務器從該文件中讀取exclude列表定義,每個文件或目錄需要佔用一行
include             #→用來指定不排除符合要求的文件或目錄。這等同於在客戶端命令中使用--include來指定模式,結合include和exclude可以定義複雜的exclude/include規則。
include from   #→指定一個包含include模式的定義的文件名,服務器從該文件中讀取include列表定義。
auth users       #→該選項指定由空格或逗號分隔的用戶名列表,只有這些用戶才允許連接該模塊。這裏的用戶和系統用戶沒有任何關係。如果" auth users"被設置,那麼客戶端發出對該模塊的連接請求以後會被rsync請求challenged進行驗證身份這裏使用的challenge/response認證協議。用戶的名和密碼以明文方式存放在" secrets file"選項指定的文件中。默認情況下無需密碼就可以連接模塊(也就是匿名方式)。
secrets file      #→該選項指定一個包含定義用戶名:密碼對的文件。只有在" auth users"被定義時,該文件纔有作用。文件每行包含一個username:passwd對。一般來說密碼最好不要超過8個字符。沒有默認的secures file名,注意:該文件的權限一定要是600,否則客戶端將不能連接服務器。
hosts allow      #→指定哪些IP的客戶允許連接該模塊。定義可以是以下形式:
    單個IP地址,例如:192.167.0.1,多個IP或網段需要用空格隔開,
    整個網段,例如:192.168.0.0/24,也可以是192.168.0.0/255.255.255.0
“*”則表示所有,默認是允許所有主機連接。
hosts deny      #→指定不允許連接rsync服務器的機器,可以使用hosts allow的定義方式來進行定義。默認是沒有hosts deny定義。
list              #→該選項設定當客戶請求可以使用的模塊列表時,該模塊是否應該被列出。如果設置該選項爲false,
可以創建隱藏的模塊。默認值是true。
timeout   #→通過該選項可以覆蓋客戶指定的IP超時時間。通過該選項可以確保rsync服務器不會永遠等待一個崩潰的客戶端。超時單位爲秒鐘,0表示沒有超時定義,這也是默認值。對於匿名rsync服務器來說,一個理想的數字是600。

用配置文件定義目錄輸出
配置是在備份服務端上配置也就是164

# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

# uid = nobody
# gid = nobody
# use chroot = yes
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# [ftp]
#        path = /home/ftp
#        comment = ftp export area
uid = root  #運行進程的身份
gid = root   #運行進程的組
address = 10.10.100.164   #監聽IP
port = 873          #監聽端口
host allow = 10.10.100.0/24  #允許同步客戶端的IP地址,可以是網段,或者用*表示所有
 192.168.1.0/24或192.168.1.0/255.255.255.0
use chroot = yes        #是否囚牢,鎖定家目錄,rsync被黑之後,黑客無法再rsync運行的家目錄之外創建文件,選項設置爲yes
max connections = 5      #最大連接數
pid file = /var/run/rsyncd.pid  #進程PID,自動生成
lock file = /var/run/rsync.lock   #指max connectios參數的鎖文件
log file = /var/log/rsyncd.log    #日誌文件位置
motd file = /etc/rsyncd.motd    #客戶端登陸之後彈出的消息,需要創建

[wwwroot]                 #共享模塊名稱
path = /web-back/          #文件保存路徑
comment = rsync server     #描述
read only = false             #設置服務端文件讀寫權限
list = yes                #是否允許查看模塊信息
auth users = rsyncuser               #備份的用戶,和系統用戶無關
secrets file = /etc/rsyncd.passwd       #存放用戶的密碼文件,格式是  用戶名:密碼

創建提示文件和用戶密碼

[root@test164 ~]# echo "歡迎使用rsync同步服務" > /etc/rsyncd.motd   #這個是個提示信息
[root@test164 ~]# vim /etc/rsync.passwd
[root@test164 ~]# chmod 600 /etc/rsync.passwd 

限必須是700或者600,否則的話身份驗證會失效,設置rsync user的時候

[root@test175 html]# rsync -avz --delete  /var/www/html [email protected]::wwwroot

歡迎使用rsync同步服務

Password: #輸入密碼文件裏面的密碼即可

同步完成,到備份服務器上查看/web-back,是否有同步文件,爲了可以測試的,可以在同步前,將文件夾裏面的文件清空。

腳本實現定時自動備份

先在源服務端創建一個密碼文件,修改文件權限600或者700

[root@test175 html]# touch  /etc/rsyncd.passwd
[root@test175 html]#vim /etc/rsyncd.passwd     #寫入之前配置的密碼
passwd123
[root@test175 html]#chmod 600 /etc/rsyncd.passwd
[root@test175 html]# mkdir a{1,2,3,4,5,6,7,8}    #創建幾個測試的文件夾
[root@test175 html]# rsync -avz --delete  /var/www/html [email protected]::wwwroot --password-file=/etc/rsyncd.passwd 
歡迎使用rsync同步服務
sending incremental file list
html/
html/a1/
html/a2/
html/a3/
html/a4/
html/a5/
html/a6/
html/a7/
html/a8/

sent 228 bytes  received 52 bytes  560.00 bytes/sec
total size is 0  speedup is 0.00

出現上面的信息說明已經同步完成,而且是增量

在這個基礎上可以寫個腳本實現,定時自動備份

[root@test175 ~]# cat autobackup.sh 
#!/bin/bash
rsync -avz --delete  /var/www/html [email protected]::wwwroot --password-file=/etc/rsyncd.passwd
[root@test175 ~]# chmod +x autobackup.sh 
[root@test175 ~]# echo "01 3 * * * bash /root/autobackup.sh &"  >> /var/spool/cron/root
[root@test175 ~]# mkdir /var/www/html/77{k,l,i}
[root@test175 ~]# ./autobackup.sh     #測試腳本
歡迎使用rsync同步服務

sending incremental file list
html/
html/77i/
html/77k/
html/77l/

Rsync+sersync實現數據實時同步

準備兩臺服務器
一臺安裝sersync 一臺安裝rsync
sersync 源機器10.10.100.178
rsync 備份機器10.10.100.181

一、爲什麼要用rsync+sersync架構?
1、sersync是基於inotify開發的,類似於inotify-tools的工具
2、sersync可以記錄下被監聽目錄中發生變化的(包括增加、刪除、修改)具體某一個文件或者某一個目錄的名字,然後使用rsync同步的時候,只同步發生變化的文件或者目錄

二、rsync+inotify-tools與rsync+sersync架構的區別?
1、rsync+inotify-tools
a、inotify只能記錄下被監聽的目錄發生了變化(增,刪,改)並沒有把具體是哪個文件或者哪個目錄發生了變化記錄下來;
b、rsync在同步的時候,並不知道具體是哪個文件或目錄發生了變化,每次都是對整個目錄進行同步,當數據量很大時,整個目錄同步非常耗時(rsync要對整個目錄遍歷查找對比文件),因此效率很低

2、rsync+sersync
a、sersync可以記錄被監聽目錄中發生變化的(增,刪,改)具體某個文件或目錄的名字;
b、rsync在同步時,只同步發生變化的文件或目錄(每次發生變化的數據相對整個同步目錄數據來說很小,rsync在遍歷查找對比文件時,速度很快),因此效率很高。

同步過程:

  1. 在同步服務器上開啓sersync服務,sersync負責監控配置路徑中的文件系統事件變化;
  2. 調用rsync命令把更新的文件同步到目標服務器;
  3. 需要在主服務器配置sersync,在同步目標服務器配置rsync server(注意:是rsync服務)

同步過程和原理:

  1. 用戶實時的往sersync服務器上寫入更新文件數據;
  2. 此時需要在同步主服務器上配置sersync服務;
  3. 在另一臺服務器開啓rsync守護進程服務,以同步拉取來自sersync服務器上的數據;
    通過rsync的守護進程服務後可以發現,實際上sersync就是監控本地的數據寫入或更新事件;然後,在調用rsync客戶端的命令,將寫入或更新事件對應的文件通過rsync推送到目標服務器

下載sersync

在google code下載sersync的可執行文件版本,裏面有配置文件與可執行文件

官網地址 https://code.google.com/archive/p/sersync/downloads
在這裏插入圖片描述

下載解壓

[root@test178 ~]#wget  https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@test178 ~]# tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/src/
GNU-Linux-x86/
GNU-Linux-x86/sersync2
GNU-Linux-x86/confxml.xml
[root@test178 src]# mv GNU-Linux-x86/ sersync      
[root@test178 src]# cd sersync/

配置sersync

[root@test178 sersync]#cp confxml.xml confxml.xml.bak    #修改配置前先備份
[root@test178 sersync]# ll
總用量 1772
-rwxr-xr-x. 1 root root    2214 10月 26 2011 confxml.xml
-rwxr-xr-x. 1 root root 1810128 10月 26 2011 sersync2
[root@test178 sersync]# vim confxml.xml 
 <sersync>
        <localpath watch="/var/www/html/">     #本地源目錄
            <remote ip="10.10.100.181" name="wwwroot"/>     #遠程IP,rsync模塊名稱
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <auth start="true" users="rsyncuser" passwordfile="/etc/rsyncd.passwd"/>
          atart修改成true   用戶寫非系統同步用戶        密碼文件填寫絕對路徑

開啓sersync守護進程同步數據

[root@test178 sersync]# pwd
/usr/local/src/sersync
[root@test178 sersync]# /usr/local/src/sersync/sersync2 -d -r -o /usr/local/src/sersync/confxml.xml &
[1] 17863
[root@test178 sersync]# set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d 	run as a daemon
option: -r 	rsync all the local files to the remote servers before the sersync work
option: -o 	config xml name:  /usr/local/src/sersync/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost	host port: 8008
daemon start,sersync run behind the console 
use rsync password-file :
user is	rsyncuser
passwordfile is 	/etc/rsyncd.passwd
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 /var/www/html && rsync -artuz -R --delete ./ [email protected]::wwwroot --password-file=/etc/rsyncd.passwd >/dev/null 2>&1 

[1]+  完成                  /usr/local/src/sersync/sersync2 -d -r -o /usr/local/src/sersync/confxml.xml

測試

在/var/www/html/目錄下,增刪改查文件

[root@test181 web-back]# watch ls -l /web-back/   #監控目錄的實時變化
[root@test178 html]# touch test

在源服務器上做改動,備份服務器也會及時變化,說明已經成功。

設置sersync監控開機自動執行

vim  /etc/rc.d/rc.local  #編輯,在最後添加一行
/usr/local/src/sersync/sersync2 -d -r -o /usr/local/src/sersync/confxml.xml &
#設置開機自動運行腳本

添加腳本監控sersync是否正常運行

[root@test178 ~]# vim check_sersync.sh
#!/bin/sh
sersync="/usr/local/src/sersync/sersync2"
confxml="/usr/local/src/sersync/confxml.xml"
status=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l)
if [ $status -eq 0 ];
then
$sersync -d -r -o $confxml &
else
exit 0;
fi
[root@test178 ~]# chmod +x check_sersync.sh   #添加腳本執行權限

把這個腳本加到任務計劃,定期執行檢測

實時多個文件目錄的配置,可以配置多個配置文件,起多個進程,一個進程監控一個目錄

1、配置多個confxml.xml文件(比如:www、bbs、blog....等等)
 2、根據不同的需求同步對應的實例文件
/usr/local/sersync/sersync2  -d -o /usr/local/sersync/www_confxml.xml
/usr/local/sersync/sersync2  -d -o /usr/local/sersync/bbs_confxml.xml
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章