CentOS 7 Sersync+Rsync 實現數據文件實時同步

rsync+inotify-tools與rsync+sersync架構的區別?html

1、rsync+inotify-toolsgit

inotify只能記錄下被監聽的目錄發生了變化(增,刪,改)並無把具體是哪一個文件或者哪一個目錄發生了變化記錄下來;github

rsync在同步的時候,並不知道具體是哪一個文件或目錄發生了變化,每次都是對整個目錄進行同步,當數據量很大時,整個目錄同步很是耗時(rsync要對整個目錄遍歷查找對比文件),所以效率很低vim

    

2、rsync+sersync服務器

sersync能夠記錄被監聽目錄中發生變化的(增,刪,改)具體某個文件或目錄的名字;架構

rsync在同步時,只同步發生變化的文件或目錄(每次發生變化的數據相對整個同步目錄數據來講很小,rsync在遍歷查找對比文件時,速度很快),所以效率很高測試

 

同步過程:ui

1.  在源數據服務器上開啓sersync服務,sersync負責監控配置路徑中的文件系統事件變化;google

2.  調用rsync命令把更新的文件同步到目標服務器;spa

3.  須要在源數據服務器配置sersync,在同步目標服務器配置rsync server

同步原理:

1.  用戶實時的往sersync服務器上寫入更新文件數據;

2.  此時須要在源數據服務器上配置sersync服務;

3.  在另外一臺服務器開啓rsync守護進程服務,以同步拉取來自sersync服務器上的數據;

經過rsync的守護進程服務後能夠發現,實際上sersync就是監控本地的數據寫入或更新事件;而後,在調用rsync客戶端的命令,將寫入或更新事件對應的文件經過rsync推送到目標服務器

CentOS 7 192.168.94.33(Sersync server)

CentOS 7 192.168.94.44(Rsync server)

先安裝Rsync

[root@Rsync ~]# yum -y install rsync
[root@Rsync ~]# vim /etc/rsyncd.conf 
#Rsync server
uid = root                         #運行進程的身份
gid = root                         #運行進程的組
use chroot = yes                   #是否鎖定家目錄
max connections = 100           #最大鏈接數
timeout = 600                    #超時時間
log file = /var/log/rsyncd.log     #日誌文件
ignore errors                     #忽略錯誤
read only = false                  #設置服務端文件讀寫權限
list = false                        #不顯示服務端資源列表
hosts allow = 192.168.94.0/24    #*表明全部
hosts deny = 0.0.0.0/32
auth users = backup
secrets file = /etc/rsync.password

[www]
comment = www 
path = /var/www/html

建立備份目錄和密碼文件

[root@Rsync ~]# mkdir -p /var/www/html
[root@Rsync ~]# ls /var/www/html/

[root@Rsync ~]# echo "backup:damowang" > /etc/rsync.password
[root@Rsync ~]# chmod 600 /etc/rsync.password

 

在數據源端建立密碼文件 , 而後在rsync命令中使用rsync --password-file 指定該文件

[root@Sersync ~]# echo "damowang" > /etc/rsync.password
[root@Sersync ~]# chmod 600 /etc/rsync.password

測試

[root@Sersync ~]# cp /etc/passwd .
[root@Sersync ~]# rsync -azvp /root/passwd backup@192.168.94.44::www/ --password-file=/etc/rsync.password 
sending incremental file list
passwd

sent 641 bytes  received 43 bytes  1,368.00 bytes/sec
total size is 1,337  speedup is 1.95

[root@Rsync ~]# ls /var/www/html/
passwd

測試這步必定要成功,否則進行不了下一步

部署Sersync服務

下載sersync :

goodle code地址 : https://code.google.com/archive/p/sersync/downloads

 

 

Git Hub 鏡像地址 : https://github.com/orangle/sersync

 

[root@Sersync ~]# unzip sersync-master.zip 
[root@Sersync ~]# tar xf sersync-master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/
[root@Sersync ~]# cd /usr/local/
[root@Sersync local]# mv GNU-Linux-x86/ sersync
[root@Sersync local]# cd sersync/
[root@Sersync sersync]# cp confxml.xml confxml.xml.$(date +%F)
[root@Sersync sersync]# vim confxml.xml
# 修改2四、25行
    <sersync>
        <localpath watch="/var/www/html">    # 本地同步目錄
        <remote ip="192.168.94.44" name="www"/>     # rsync模塊名

# 修改31行
<rsync>
        <commonParams params="-artuz"/>
        <auth start="true" users="backup" passwordfile="/etc/rsync.password"/> 

開啓sersync守護進程 同步數據

[root@Sersync sersync]# sersync2 -d -r -o /usr/local/sersync/confxml.xml
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/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    backup
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 /var/www/html && rsync -artuz -R --delete ./ backup@192.168.94.44::www --password-file=/etc/rsync.password >/dev/null 2>&1 
run the sersync: 
watch path is: /var/www/html

測試

添加數據

 [root@Sersync sersync]# cd /var/www/html
 [root@Sersync html]# ls
 [root@Sersync html]# mkdir damowang.cn
 [root@Sersync html]# cd damowang.cn/
 [root@Sersync damowang.cn]# touch {1..5}.txt
 [root@Sersync damowang.cn]# ll
 總用量 0
 -rw-r--r-- 1 root root 0 8月 28 01:08 1.txt
 -rw-r--r-- 1 root root 0 8月 28 01:08 2.txt
 -rw-r--r-- 1 root root 0 8月 28 01:08 3.txt
 -rw-r--r-- 1 root root 0 8月 28 01:08 4.txt
 -rw-r--r-- 1 root root 0 8月 28 01:08 5.txt

驗證

[root@Rsync ~]# ll /var/www/html/
總用量 0
drwxr-xr-x 2 root root 71 8月  28 01:08 damowang.cn
[root@Rsync ~]# ll /var/www/html/damowang.cn/
總用量 0
-rw-r--r-- 1 root root 0 8月  28 01:08 1.txt
-rw-r--r-- 1 root root 0 8月  28 01:08 2.txt
-rw-r--r-- 1 root root 0 8月  28 01:08 3.txt
-rw-r--r-- 1 root root 0 8月  28 01:08 4.txt
-rw-r--r-- 1 root root 0 8月  28 01:08 5.txt
相關文章
相關標籤/搜索