rsync+sersync實現數據文件實時同步

使用rsync+sersync,實現nfs與backup服務器間實時數據同步

1、數據同步工具介紹

rsync是一款開源的、快速的、多功能的、可實現全量及增量的本地或遠程數據同步備份的優秀工具。rsync軟件適用於unix/linux/windows等多種操做系統平臺。php

rsync和ssh帶的scp命令比較類似,但又優於scp命令的功能,scp每次都是全量拷貝,而rsync能夠進行增量拷貝。固然,rsync還能夠在本地主機的不一樣分區或目錄之間全量及增量的複製數據,這又相似cp命令,但一樣也優於cp命令,cp每次都是全量拷貝,而rsync能夠增量拷貝.利用rsync還能夠實現刪除文件和目錄功能,這又至關於rm命令。html

sersync是基於Inotify開發的,相似於Inotify-tools的工具;mysql

sersync能夠記錄下被監聽目錄中發生變化的(包括增長、刪除、修改)具體某一個文件或某一個目錄的名字;linux

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

:當同步的目錄數據量不大時,建議使用Rsync+Inotify-tools;當數據量很大(幾百G甚至1T以上)、文件不少時,建議使用Rsync+sersync。github

2、實操部分

一、rsync以守護進程(socket)的方式傳輸數據

這個是rsync自身的重要功能,咱們實驗的機器以下:sql

服務端IP:172.16.1.41mongodb

客戶端IP:172.16.1.31express

服務端配置

先新建配置文件,請注意rsyncd.conf配置文件只用在服務端新建就好。vim

vim /etc/rsyncd.conf

如下只是配置文件中最經常使用的部份,更多的請使用命令man rsyncd.conf查看。

uid = rsync       # 運行rsync的用戶和組id

gid = rsync

use chroot = no       # bug信息的處理,一種安全方式

max connections = 200    # 最大的鏈接數

timeout = 300           # 超時時間

pid file = /var/run/rsyncd.pid            # pid進程號文件

lock file = /var/run/rsync.lock           # 鎖文件

log file = /var/run/rsyncd.log            # 日誌文件

[backup]           # 須要同步的模塊,這是其中一個,能夠有多個,對應不一樣的目錄,客戶端能夠

                                     # 選擇要同步的目錄

path = /backup                   # 同步的根目錄

ignore errors             # 忽略錯誤

read only = false                # 只讀falsh 表示可讀可寫

list = false                            # 不可列表

hosts allow = 172.16.1.0/24             # 容許訪問的網段

#hosts deny = 0.0.0.0/32                            # 拒絕訪問的網段,容許與拒毫不能同時設置

auth users = rsync_backup               # 用戶名

secrets file = /etc/rsync.password           # 密碼文件路徑,密碼文件權限要設置成600

以守護進程的方式啓動rsync

[root@backup ~]# rsync --deamon

查看是否啓動成功,rsyncd的默認端口是873

[root@backup ~]# netstat -lntup | grep 873

[linguang@backup tmp]$ ps -ef|grep rsync|grep -v grep

[linguang@backup tmp]$ lsof -i :873

-l顯示監控中的服務器的Socket;-n不經過域名服務器;p顯示正在使用Socket的PID和程序名

若是啓動出錯,咱們就須要查看一下系統日誌,咱們這裏日誌顯示正常啓動

[root@backup /]# cat /var/run/rsyncd.log

2018/12/20 15:02:04 [18822] rsyncd version 3.0.6 starting, listening on port 873

建立一個不須要登陸的系統用戶

[root@backup ~]# useradd rsync -s /bin/nologin -M        

[root@backup ~]# id rsync

uid=501(rsync) gid=501(rsync) 組=501(rsync)

下面咱們建立須要同步的目錄,並給予相應的權限

[root@backup ~]# mkdir -p /backup

[root@backup /]# chown -R rsync.rsync backup

將用戶名和密碼重定義輸出到咱們的密碼存放文件

# rsync_backup是用戶名,linguang是密碼

[root@backup /]# cat /etc/rsync.password

rsync_backup:linguang

[root@backup /]# chmod 600 /etc/rsync.password

重啓rsync服務:

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

COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

rsync   19553 root    4u  IPv4  62838      0t0  TCP *:rsync (LISTEN)

rsync   19553 root    5u  IPv6  62839      0t0  TCP *:rsync (LISTEN)

[root@backup ~]# pkill rsync

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

[root@backup ~]# rsync --daemon

rsync服務添加到開機啓動腳本中:

[root@backup ~]# echo 'rsync --daemon'>>/etc/rc.local

[root@backup ~]# tail -1 /etc/rc.local

rsync --daemon

同時咱們須要關閉防火檣

[root@backup date]# /etc/init.d/iptables stop    /status查看狀態

客戶端配置

將密碼保存在密碼配置文件,同是爲了與服務端統一,咱們使用至關的文件名,注意這裏咱們只須要放入密碼便可

[root@vagrant-centos65 ~]# echo "linguang" >/etc/rsync.password

[root@vagrant-centos65 ~]# chmod 600 /etc/rsync.password

[root@vagrant-centos65 ~]# cat /etc/rsync.password

linguang

rsync經過daemon方式遠程傳輸的語法爲:

Access via rsync daemon:

Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]

rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]

Push: rsync [OPTION...] SRC... [USER@]HOST::DEST

rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST

在客戶端進行拉取實例:

先看看server端的目錄結構

[root@backup backup]# pwd

/backup

[root@backup backup]# mkdir date/{run,log}/linguang -p

[root@backup backup]# tree

.

`-- date

    |-- log

    |   `-- linguang

    `-- run

        `-- linguang

 

5 directories, 0 files

開始提取數據

##backup(模塊名) 即服務端/backup

 [root@nfs01 tmp]# rsync -avzP rsync_backup@172.16.1.41::backup /tmp --password-file=/etc/rsync.password     ##自動讀取密碼,參數--password-file=/etc/rsync.password

receiving incremental file list

./

date/

date/log/

date/log/linguang/

date/run/

date/run/linguang/

sent 87 bytes  received 221 bytes  616.00 bytes/sec

total size is 0  speedup is 0.00

[root@nfs01 tmp]# tree

.

└── date

    ├── log

    │   └── linguang

    └── run

        └── linguang

 

5 directories, 0 files

在客戶端進行推送實例

[linguang@nfs01 tmp]$ sudo touch teach{1..20}

[linguang@nfs01 tmp]$ rsync -avzP /tmp/ rsync_backup@172.16.1.41::backup/nfs01back/tmp --password-file=/etc/rsync.password

rsync: could not open password file "/etc/rsync.password": Permission denied (13)

Password:

@ERROR: auth failed on module backup

rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]

[linguang@nfs01 tmp]$ sudo rsync -avzP /tmp/ rsync_backup@172.16.1.41::backup/nfs01back/tmp --password-file=/etc/rsync.password

sending incremental file list

created directory nfs01back/tmp

./

teach1

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

。。。。。。。。。

date/run/

date/run/linguang/

sent 1068 bytes  received 415 bytes  2966.00 bytes/sec

total size is 0  speedup is 0.00

 其它語法

排除連續的文件,推送文件到服務端

[root@nfs01 ~]# rsync -avz /root/ rsync://rsync_backup@172.16.1.41:873/backup/nfs01 --exclude=student{10..15} --password-file=/etc/rsync.password  

sending incremental file list

created directory nfs01

./

.bash_history

.bash_logout

.bash_profile

.bashrc

提取服務端文件到本地目錄下

[root@nfs01 ~]# rsync -avzP rsync://rsync_backup@172.16.1.41:873/backup/test/ /tmp --password-file=/etc/rsync.password  

receiving incremental file list

./

a/

a/girld/

b/

b/girld/

sent 83 bytes  received 169 bytes  504.00 bytes/sec

total size is 0  speedup is 0.00

限速傳輸:

[root@nfs01 ~]# dd if=/dev/zero of=logfile.tar bs=1M count=128

記錄了128+0 的讀入

記錄了128+0 的寫出

134217728字節(134 MB)已複製,5.06455 秒,26.5 MB/秒

[root@nfs01 ~]# rsync -avzP /root/logfile.tar rsync_backup@172.16.1.41::backup/logfile --bwlimit=100 --password-file=/etc/rsync.password 

sending incremental file list

logfile.tar

   134217728 100%   93.20MB/s    0:00:01 (xfer#1, to-check=0/1)

sent 130594 bytes  received 27 bytes  29026.89 bytes/sec

total size is 134217728  speedup is 1027.54

[root@nfs01 ~]# rsync -avzP /root/logfile.tar rsync_backup@172.16.1.41::backup/logfile --bwlimit=10 --password-file=/etc/rsync.password

sending incremental file list

logfile.tar

   134217728 100%    9.12MB/s    0:00:14 (xfer#1, to-check=0/1)

sent 130594 bytes  received 27 bytes  8427.16 bytes/sec

total size is 134217728  speedup is 1027.54

二、下載sersync壓縮包

[root@nfs01 /]# mkdir application

[root@nfs01 /]# cd application

[root@nfs01 application]# wget https://github.com/wsgzao/sersync/raw/master/sersync2.5.4_64bit_binary_stable_final.tar.gz

[root@nfs01 sersync]# tree

.

├── bin

│   └── sersync

├── conf

│   └── confxml.xml

└── logs

 

1.1.1.2  配置文件介紹

[root@nfs01 data]# cat /application/sersync/conf/confxml.xml

#如下貼上配置文件,使用的時候把帶#好註釋的去掉便可

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

<head version="2.5">

#本機參數設置

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

   <debug start="false"/>

   <fileSystem xfs="false"/>

   <filter start="false">

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

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

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

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

   </filter>

#inotify監控事件狀況設置

   <inotify>

<delete start="true"/>

<createFolder start="true"/>

<createFile start="false"/>

<closeWrite start="true"/>

<moveFrom start="true"/>

<moveTo start="true"/>

<attrib start="false"/>

<modify start="false"/>

   </inotify>

        #修改須要監控的目錄

   <sersync>

<localpath watch="/data">

    #修改爲備份服務器ip、模塊名稱,而不是本機ip地址,能夠有多臺備份服務器

    <remote ip="192.168.90.41" name="nfsbackup"/>

    <!--<remote ip="192.168.8.39" name="tongbu"/>-->

    <!--<remote ip="192.168.8.40" name="tongbu"/>-->

</localpath>

<rsync>

    #下面的是rsync使用的參數,可自定義,也能夠根擴展--delete等,來實現想要的備份方式

    <commonParams params="-avz"/>

    #下面是是否要啓動,改爲true便可,users爲用於rsync鑑權的用戶名,在rsync服務器上設置

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

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

    #下面這個能夠設置超時時間

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

    <ssh start="false"/>

</rsync>

    #下面這個也須要注意,根據實際狀況去修改

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

<crontab start="false" schedule="600"><!--600mins-->

    <crontabfilter start="false">

    <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>

 測試sersync命令:

[root@nfs01 data]# /application/sersync/bin/sersync -h

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

_______________________________________________________

參數-d:啓用守護進程模式

參數-r:在監控前,將監控目錄與遠程主機用rsync命令推送一遍

參數-n: 指定開啓守護線程的數量,默認爲10個

參數-o:指定配置文件,默認使用confxml.xml文件

參數-m:單獨啓用其餘模塊,使用 -m refreshCDN 開啓刷新CDN模塊

參數-m:單獨啓用其餘模塊,使用 -m socket 開啓socket模塊

參數-m:單獨啓用其餘模塊,使用 -m http 開啓http模塊

不加-m參數,則默認執行同步程序

[root@nfs01 data]# /application/sersync/bin/sersync -d -r -n 8 -o /application/sersync/conf/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: -n      thread num is:  8

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

parse xml config file

host ip : localhost     host port: 8008

daemon start,sersync run behind the console

use rsync password-file :

user is rsync_backup

passwordfile is         /etc/rsync.password

config xml parse success

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

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

Max threads numbers is: 18 = 10(Thread pool nums) + 8(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 -avz -R --delete ./ rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password >/dev/null 2>&1

run the sersync:

watch path is: /data

[root@nfs01 data]# ps -ef |grep sersync

[root@nfs01 data]# pkill sersync

添加到開機啓動項:

[root@nfs01 data]# tail -1 /etc/rc.local

#/application/sersync/bin/sersync -d -r -n 8 -o /application/sersync/conf/confxml.xml

 

三、高併發數據實時同步方案小結及拓展

1)inotify(sersync)+rsync,是文件級別的。

2)drbd文件系統級別,文件系統級別,基於block塊同步,缺點:備節點數據不可用。

3)第三方軟件的同步功能:mysql同步,oracle,mongodb。

4)程序雙寫,直接寫兩臺服務器。

5)利用產品業務邏輯解決(讀寫分離,備讀不到,讀主)

6)NFS集羣:1,4,5方案整合,雙寫主存儲,備存儲用inotify(sersync)+rsync,備沒有找主解決延遲問題)

相關文章
相關標籤/搜索