sersync+rsync實時備份

簡介:
inotify的優點:
(1) 監控文件系統時間的變化,通過同步工具實現實時同步數據
inotify的缺點:
(1) 併發如果大於200個文件(10-100K),同步就會有延遲
(2)我們前面寫的腳本,每次都是全部推送一次,但確實是增量備份額
也可以只同步變化的文件 不變化的文件不理
(3) 監控到事件後,調用rsync同步是單線程的(加&併發)。sersync是多線程同步的
既然有了inotify-tools,爲什麼還要開發sersync
sersync的功能多
(1)配置文件
(2) 真正的守護進程socket
(3)可以對失敗的文件定時重傳
(4)第三方的http接口
(5) 默認是多線程的同步
sersync原理圖解

本次實戰的圖解
實驗配置

1.Rsync服務(S1和S2的配置,都一樣),這個配置文件默認是不存在的(得新建)
[root@S2-SERVER ~]# vim /etc/rsyncd.conf
#QQ 31333741 blog:http://oldboy.blog.51cto.com
##rsyncd.conf start##
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 10.0.0.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
[www]
comment = "write by sanlang"
path = /data0/www/www
[bbs]
comment = "write by sanlang"
path = /data0/www/bbs
[blog]
comment = "write by sanlang"
path = /data0/www/blog
"/etc/rsyncd.conf" 27L, 604C 已寫入
[root@S2-SERVER ~]#

  1. Rsync服務(S1和S2的配置,都一樣)添加用戶
    [root@S2-SERVER ~]# useradd rsync -s /sbin/nologin -M
    3.修改/或者密碼文件(默認是不存的)
    [root@S2-SERVER ~]# vim /etc/rsync.password
    rsync_backup:oldboy
    [root@S2-SERVER ~]# cat /etc/rsync.password
    rsync_backup:oldboy
    [root@S2-SERVER ~]# chmod 600 /etc/rsync.password
    [root@S2-SERVER ~]#
  2. Rsync服務(S1和S2的配置,都一樣)創建目錄,並把目錄賦予相應的權限
    [root@S2-SERVER ~]# mkdir /data0/www/www -p
    [root@S2-SERVER ~]# mkdir -p /data0/www/bbs
    [root@S2-SERVER ~]# mkdir -p /data0/www/blog
    [root@S2-SERVER ~]# chown -R rsync.rsync /data0
    5.以守護進程的方式啓動rsync
    [root@S1-SERVER ~]# rsync --daemon
    [root@S1-SERVER ~]# echo "/usr/bin/rsync --daemon">>/etc/rc.local
    可能會出錯錯誤:
    [root@S1-SERVER ~]# rsync --daemon
    [root@S1-SERVER ~]# failed to create pid file /var/run/rsyncd.pid: File exists
    解決方法:
    [root@S1-SERVER ~]# rm -f /var/run/rsyncd.pid
    6.Sersync服務(M)端的配置(默認文件不存在自己指定)
    [root@M-SERVER ~]# cat /etc/rsync.password
    oldboy
    [root@M-SERVER ~]# chmod 600 /etc/rsync.password
    [root@M-SERVER ~]# ll /etc/rsync.password
    -rw-------. 1 root root 7 4月 22 16:18 /etc/rsync.password
    [root@M-SERVER ~]#
    7.測試(M-SERVER 上執行OK)
    [root@M-SERVER ~]# cd /backup
    [root@M-SERVER backup]# ls
    sanlang01.txt sanlang03.txt sanlang05.txt sanlang07.txt sanlang09.txt
    sanlang02.txt sanlang04.txt sanlang06.txt sanlang08.txt sanlang10.txt
    [root@M-SERVER backup]# cd
    [root@M-SERVER ~]# rsync -avz /backup/ [email protected]::www/ --password-file=/etc/rsync.password
    sending incremental file list
    ./
    sanlang01.txt
    sanlang02.txt
    sanlang03.txt
    sanlang04.txt
    sanlang05.txt
    sanlang06.txt
    sanlang07.txt
    sanlang08.txt
    sanlang09.txt
    sanlang10.txt
    sent 508 bytes received 201 bytes 1418.00 bytes/sec
    total size is 0 speedup is 0.00
    [root@M-SERVER ~]#
    6.Sersync服務(M)端的配置(可以不加,主要是把安裝包放在自己的家目錄下)
    [root@M-SERVER ~]# useradd oldboy
    [root@M-SERVER ~]# passwd oldboy
    更改用戶 oldboy 的密碼 。
    新的 密碼:
    無效的密碼: 過於簡單化/系統化
    無效的密碼: 過於簡單
    重新輸入新的 密碼:
    passwd: 所有的身份驗證令牌已經成功更新。
    [root@M-SERVER ~]#
    新建一個目錄用於sersync的安裝
    [root@M-SERVER ~]# mkdir -p /home/oldboy/tools
    [root@M-SERVER ~]#
    7.Sersync服務(M)端開始安裝sersync
    [root@M-SERVER tools]# rz
    rz waiting to receive.
    zmodem trl+C
    [root@M-SERVER tools]# rz
    rz waiting to receive.
    zmodem trl+C
    100% 710 KB 710 KB/s 00:00:01 0 Errorsary_stable_final.tar.gz...
    [root@M-SERVER tools]# ls
    sersync2.5.4_64bit_binary_stable_final.tar.gz
    [root@M-SERVER tools]#
    8.Sersync服務(M)端.解壓文件(解壓到/usr/local/ 下面)
    [root@M-SERVER tools]# 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@M-SERVER tools]# cd /usr/local/
    [root@M-SERVER local]# ls
    bin etc games GNU-Linux-x86 include lib lib64 libexec sbin share src
    [root@M-SERVER local]#
    9.Sersync服務(M)端.改名解壓文件的名稱
    [root@M-SERVER local]# mv GNU-Linux-x86 sersync
    [root@M-SERVER local]#
    [root@M-SERVER local]# tree sersync/
    sersync/
    ├── confxml.xml
    └── sersync2
    0 directories, 2 files
    [root@M-SERVER local]#
    10.規範安裝包(在 /usr/local/sersync 下操作)
    [root@M-SERVER sersync]# mkdir conf bin logs
    [root@M-SERVER sersync]# mv confxml.xml conf/
    [root@M-SERVER sersync]# mv sersync2 bin/sersync
    [root@M-SERVER local]# tree sersync/
    sersync/
    ├── bin
    │ └── sersync
    ├── conf
    │ └── confxml.xml
    └── logs
    3 directories, 2 files
    [root@M-SERVER local]#
    11.備份配置文件並且配置
    [root@M-SERVER sersync]# cd /usr/local/sersync/conf/
    [root@M-SERVER conf]# cp confxml.xml confxml.xml.org
    [root@M-SERVER conf]# ls
    confxml.xml confxml.xml.org
    [root@M-SERVER conf]#
    ①編輯confxml.xml
    24 <localpath watch="/opt/tongbu">
    25 <remote ip="127.0.0.1" name="tongbu1"/>
    26 <!--<remote ip="192.168.8.39" name="tongbu"/>-->
    27 <!--<remote ip="192.168.8.40" name="tongbu"/>-->
    28 </localpath>
    改成:
    24 <localpath watch="/data0/www/www"> #data/www/www 代表內地目錄
    25 <remote ip="10.0.0.182" name="www"/> # www 這個是遠程服務器的模塊
    26 <remote ip="10.0.0.183" name="www"/> # 10.0.0.182/183 是遠程的Ip地址
    27 </localpath>
    28
    29 <localpath watch="/data0/www/bbs">
    30 <remote ip="10.0.0.182" name="bbs"/>
    31 <remote ip="10.0.0.183" name="bbs"/>
    32 </localpath>
    33
    34 <localpath watch="/data0/www/blog">
    35 <remote ip="10.0.0.182" name="blog"/>
    36 <remote ip="10.0.0.183" name="blog"/>
    37 </localpath>
    ②修改認證的部分
    <rsync>
    40 <commonParams params="-artuz"/>
    41 <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
    42 <userDefinedPort start="false" port="874"/><!-- port=874 -->
    43 <timeout start="false" time="100"/><!-- timeout=100 -->
    44 <ssh start="false"/>
    45 </rsync>
    改成:
    39 <rsync>
    40 <commonParams params="-artuz"/>
    41 <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
    42 <userDefinedPort start="false" port="874"/><!-- port=874 -->
    43 <timeout start="true" time="100"/><!-- timeout=100 -->
    44 <ssh start="false"/>
    45 </rsync>
    ③修fail隊列日誌
    <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execut
    e once-->
    改成(這個目錄是自己提前創建好的)
    <failLog path="/usr/local/sersync/logs/rsync_fail_log.sh" timeToExecute="60"/><!--default
    every 60mins execute once-->
    最後保存配置文件
    12.配置sersync的命令
    [root@M-SERVER conf]# echo "export PATH=$PATH:/usr/local/sersync/bin">>/etc/profile
    [root@M-SERVER conf]# tail -n 1 /etc/profile
    export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/sersync/bin
    [root@M-SERVER conf]# which rersync
    /usr/bin/which: no rersync in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
    [root@M-SERVER conf]# source /etc/profile
    [root@M-SERVER conf]# which sersync
    /usr/local/sersync/bin/sersync
    [root@M-SERVER conf]#
    13.啓動命令
    [root@M-SERVER ~]# sersync -r -d -o /usr/local/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: -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 : 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 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
    Max threads numbers is: 32 = 12(Thread pool nums) + 20(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 /data0/www/www && rsync -artuz -R --delete ./ --timeout=100 [email protected]::www --password-file=/etc/rsync.password >/dev/null 2>&1
    run the sersync:
    watch path is: /data0/www/www
    [root@M-SERVER ~]#
    -r 客戶端和服務端可能第一次數據不一樣 開啓先做一次同步
    -d 在後臺以守護進程的方式啓動
    -o 指定xml 文件
    14.測試以後發現,在第11步驟寫的模塊(localpath )只識別第一個
    這個時候進行多實例傳送,每個推送數據的目錄給一個配置文件下面三個文件,每個文件都有自己的單獨日誌和<localpath watch="/data0/www/www"> 只有一個模塊,這幾個文件可以通過拷貝生成,修改後保存
    [root@M-SERVER conf]# ls
    bbs_confxml.xml blog_confxml.xml tmp www_confxml.xml
    [root@M-SERVER conf]#
    以blog_confxml.xml 爲例
    <localpath watch="/data0/www/bbs">
    <remote ip="10.0.0.182" name="bbs"/>
    <remote ip="10.0.0.183" name="bbs"/>
    </localpath>
    <failLog path="/usr/local/sersync/logs/bbs_rsync_fail_log.sh" timeToExecute="60"/><!--default
    every 60mins execute once-->
    其餘的密碼文件如果自己想修該的話在自行修改

15啓動三個配置文件
先殺掉原來配置
[root@M-SERVER conf]# ps -ef |grep rsync
root 2147 1 0 17:30 ? 00:00:00 sersync -r -d -o /usr/local/sersync/conf/confxml.xml
root 2289 2232 0 17:53 pts/1 00:00:00 grep rsync
[root@M-SERVER conf]# pkill sersync
[root@M-SERVER conf]# ps -ef |grep rsync
root 2292 2232 0 17:53 pts/1 00:00:00 grep rsync
[root@M-SERVER conf]#
多實例啓動
sersync -r -d -o /usr/local/sersync/conf/www_confxml.xml
sersync -r -d -o /usr/local/sersync/conf/bbs_confxml.xml
sersync -r -d -o /usr/local/sersync/conf/blog_confxml.xml
檢查:可以查出總共起了三個進程,每個進行都對應一個文件
[root@M-SERVER conf]# ps -ef |grep sersync
root 2300 1 0 17:57 ? 00:00:00 sersync -r -d -o /usr/local/sersync/conf/www_confxml.xml
root 2320 1 0 17:57 ? 00:00:00 sersync -r -d -o /usr/local/sersync/conf/bbs_confxml.xml
root 2340 1 0 17:58 ? 00:00:00 sersync -r -d -o /usr/local/sersync/conf/blog_confxml.xml
root 2411 2232 0 18:00 pts/1 00:00:00 grep sersync
[root@M-SERVER conf]#
最後主服務器Sersync服務(M)端可能也會重啓
所以:防止重啓(吧配置文件放在/etc/rc.local中,這個時候可以不加-r文件比較大,沒有必要只要第一次即可)
[root@M-SERVER tmp]# cat >>/etc/rc.local<<EOF

sersync -d -o /usr/local/sersync/conf/www_confxml.xml
sersync -d -o /usr/local/sersync/conf/bbs_confxml.xml
sersync -d -o /usr/local/sersync/conf/blog_confxml.xml
EOF
[root@M-SERVER tmp]#
測試(Sersync服務(M)端)(測試成功)
[root@M-SERVER ~]# cd /data0
[root@M-SERVER data0]# ls
www
[root@M-SERVER data0]# cd www
[root@M-SERVER www]# ls
bbs blog www
[root@M-SERVER www]# cd bbs
[root@M-SERVER bbs]# touch succes.log
[root@M-SERVER bbs]# cd ..
[root@M-SERVER www]# cd blog/
[root@M-SERVER blog]# touch success.log
[root@M-SERVER blog]# cd ..
[root@M-SERVER www]# cd www
[root@M-SERVER www]# touch success.log
[root@M-SERVER www]#
S2-SERVER端查看
[root@S2-SERVER ~]# tree /data0
/data0
└── www
├── bbs
│ ├── bbs.log
│ ├── bbs.txt
│ ├── hello
│ ├── succes.log
│ └── world
├── blog
│ ├── blog.log
│ ├── blog.txt
│ └── success.log
└── www
├── success.log
├── www.log
└── www.txt
4 directories, 11 files
[root@S2-SERVER ~]#
經驗
2710服務器千兆網
每秒可以同步10-100K 能同步 40-50 張
大量文件同步也是受限制的
sersync的參數
-r 開啓第一次進行一次完全的同步,保持一致
-o 指定confxml.xml 的文件
-n 指定默認線程池的線程總數,如果不指定默認啓動線程池的數量是10,CPU使用過高,可以通過這個參數進行調整
-d 參數爲後臺啓動
-m 不同步,只運行插件
./sersync --help

進行壓力測試
監控是否同步不同步報警

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