同步軟件sersync2

sersync2 實現同步配置

在同步服務器上開啓sersync, 將監控路徑中的文件同步到目標服務器,因此需要在同步服務器配置sersync, 在同步目標服務器配置 rsync


 

一 被控端

在每個被控端安裝 rsync , linux系統默認自帶的有 rsync ,建議用 3.0以上版本

[root@redis1 ~]# rpm -qa|grep rsync
rsync-3.1.2-4.el7.x86_64

在從機上創建目錄  /usr/local/nginx

mkdir /usr/local/nginx

 

二 創建 /etc/rsync.pass 文件

touch /etc/rsync.pass

編輯  vim /etc/rsync.pass

更改 rsync.pass 用戶權限爲600

chmod 600 /etc/rsync.pass

eg:

[root@redis1 ~]# chmod 600 /etc/rsync.pass 
[root@redis1 ~]# 
[root@redis1 ~]# ll -a /etc/rsync.pass 
-rw------- 1 root root 10 Feb 14 11:33 /etc/rsync.pass
[root@redis1 ~]#

vim /etc/rsyncd.conf  , 添加以下代碼到 /etc/rsyncd.conf 文件中:

(默認  /etc/rsyncd.conf 文件中的內容是全部註釋了的)

log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.pass
max connections = 0

[nginx]
path = /usr/local/nginx
comment = nginx
uid = root
gid = root
port = 873
use chroot = no
read only = no
list = no
timeout = 600
auth users = xx
hosts allow = 192.168.158.130
hosts deny = 0.0.0.0/32

上面配置文件,表明允許主服務器 (假設ip爲192.168.158.130)訪問,rsync同步模塊名爲[nginx],將同步過來的文件放入 path指定的目錄 /usr/local/nginx 。如果有多臺從服務器,則每一臺都需要進行類似的 rsync 配置,上面的 uid gid 更換成您服務器的相應用戶,注意 rsync 要有對被同步目錄的操作權限。配置好之後,使用如下命令,開啓 rsync 守護進程:

rsync --daemon

如圖,

開啓 rsync 守護進程後,查看 進程 和端口信息:

 

 

 

二 主控端

在主控端服務器上安裝 sersync

1. 在主控端下載 sersync2

wget http://down.whsir.com/downloads/sersync2.5.4_64bit_binary_stable_final.tar.gz

解壓到 /usr/local/ 目錄下

tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/

或者

下載我分享好的 sersync.zip

sersync.zip 下載鏈接:https://pan.baidu.com/s/1iQS2lepRYhfHdPXsA91tfA 
提取碼:8c16 

解壓到 /usr/local 目錄下

unzip -o sersync.zip -d /usr/local/

壓縮包裏面有兩個文件 confxml.xml  sersync2

sersync2 這個是sersync主程序
confxml.xml 這個是sersync配置文件

 

[root@nginx ~]# whereis sersync
sersync: /usr/local/sersync
[root@nginx ~]# which sersync
/usr/bin/which: no sersync in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/java/jdk1.8.0_161/bin:/root/bin)
[root@nginx ~]# 

 

創建  /etc/passwd.txt , 文件內寫入 認證密碼, 注意:要和 從控端的 /etc/rsync.pass 裏設置的相對應

touch /etc/passwd.txt && echo 123asd > /etc/passwd.txt

eg:

[root@nginx ~]# touch /etc/passwd.txt && echo 123asd > /etc/passwd.txt
[root@nginx ~]# cat /etc/passwd.txt 
123asd

修改 /etc/passwd.txt 文件權限爲 600

chmod 600 /etc/passwd.txt

 

[root@balance2 sersync]# cd /usr/local/nginx && rsync -artuz -R "./proxy_temp/8/46/0008512468" xx@{{::nginx --password-file=/etc/passwd.txt

 

配置sersync2:

編輯 confxml.xml 文件

[root@nginx ~]# cd /usr/local/sersync/
[root@nginx sersync]# vim confxml.xml

<?xml version="1.0" encoding="ISO-8859-1"?>    ##版本和字符集
<head version="2.5">
    <host hostip="localhost" port="8008"></host>
    <debug start="false"/>                    ##是否開啓調試模式,默認false即可
    <fileSystem xfs="true"/>
    <filter start="true">                    ##這塊實現過濾,相當於rsync的--exclude排除
        <exclude expression="(.*)\.out"></exclude>
        <exclude expression="(.*)\.gz"></exclude>
        <exclude expression="^logs/*"></exclude>
        <exclude expression="^static/*"></exclude>
    </filter>
    <inotify>        ##配置inotify的監控的事件,實時監控目錄變量,也就是告訴我們sersync什麼時候用rsync推送
        <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>   ##配置sersync
        <localpath watch="/usr/local/nginx/">    ##指定要監控的本地目錄
            <remote ip="192.168.158.201"  name="nginx"/>    #指定要同步的目標服務器的IP地址,及目標服務器rsync的[模塊]
            <remote ip="192.168.158.202"  name="nginx"/>
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>        #rsync參數
            <auth start="true" users="xx" passwordfile="/etc/passwd.txt"/>    ##是否開啓rsync的認證模式,需要配置認證用戶users及認證密碼文件passwordfile,根據情況開啓(如果開啓,注意密碼文件權限一定要是600),經測試認證用戶 users可以寫個並不存在的,但要和從端一致,比如用戶xx,在主控端和被控端可以都不存在這個xx用戶
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
            <timeout start="false" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>
        <failLog path="/tmp/rsync_fail_log.log" timeToExecute="60"/><!--default every 60mins execute once-->
        <crontab start="true" schedule="600"><!--600mins-->
            <crontabfilter start="true">
                <exclude expression="(.*)\.out"></exclude>
                <exclude expression="^logs/*"></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>
           

 

主控端不需要配置 /etc/rsyncd.conf

 

 

[root@nginx sersync]# telnet 192.168.158.201 873

 

[root@balance2 sersync]# cat /home/check_sersync.sh

啓動命令

/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml &

 

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

sersync的參數:

參數-d:啓用守護進程模式
參數-r:在監控前,將監控目錄與遠程主機用rsync命令推送一遍
參數-n: 指定開啓守護線程的數量,默認爲10個
參數-o:指定配置文件,默認使用confxml.xml文件
參數-m:單獨啓用其他模塊,使用 -m refreshCDN 開啓刷新CDN模塊
參數-m:單獨啓用其他模塊,使用 -m socket 開啓socket模塊
參數-m:單獨啓用其他模塊,使用 -m http 開啓http模塊,-不加-m參數,則默認執行同步程序
不加-m參數,則默認執行同步程序

 

 

[root@nginx ~]# /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml &
[1] 86652
[root@nginx ~]# 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
now the filter work ,if you set the crontab,you have to set crontab filter 
WARNING XFS FILE SYSTEM WORK
daemon start,sersync run behind the console 
Start the crontab       Every 600 minutes rsync all the files to the remote servers entirely
開啓crontab過濾功能:    作整體同步的時候會對如下文件進行過濾
(.*)\.out
^logs/*
use rsync password-file :
user is xx
passwordfile is         /etc/passwd.txt
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
************************attention***********************************
you set the filter so the "-r " can not work
run the sersync: 
watch path is: /usr/local/nginx

表明,sersync2 已經開啓,可以在本地監控路徑下建立文件,查看遠程是否同步成功。

 

修改confxml.xml文件, 將 filter設置 改爲,然後進行整理同步一次

[root@nginx sersync]# /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml &
[1] 88839
[root@nginx 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/sersync/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost     host port: 8008
WARNING XFS FILE SYSTEM WORK
daemon start,sersync run behind the console 
Start the crontab       Every 600 minutes rsync all the files to the remote servers entirely
開啓crontab過濾功能:    作整體同步的時候會對如下文件進行過濾
(.*)\.out
^logs/*
use rsync password-file :
user is xx
passwordfile is         /etc/passwd.txt
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 /usr/local/nginx && rsync -artuz -R --delete ./ [email protected]::nginx --password-file=/etc/passwd.txt --exclude="(.*)\.out" --exclude="^logs/*" >/dev/null 2>&1 
run the sersync: 
watch path is: /usr/local/nginx         
# 點擊回車即可

[1]+  Done                    /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml
[root@nginx sersync]#

 

在被控端從機上可以看到效果:

 

殺掉進程,修改confxml.xml 文件把filter 過濾設置 恢復 成 “true” ,開啓過濾

kill -9 `pidof sersync2`

殺掉主控端sersync2進程後,被控端上來自主控端同步過來的文件不會丟失

 

filter 過濾設置 恢復 成 “true” ,開啓過濾後,重新啓動 sersync2

/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml &

如下:

[root@nginx sersync]# /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml &
[1] 90769
[root@nginx 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/sersync/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost     host port: 8008
now the filter work ,if you set the crontab,you have to set crontab filter 
WARNING XFS FILE SYSTEM WORK
daemon start,sersync run behind the console 
Start the crontab       Every 600 minutes rsync all the files to the remote servers entirely
開啓crontab過濾功能:    作整體同步的時候會對如下文件進行過濾
(.*)\.out
^logs/*
use rsync password-file :
user is xx
passwordfile is         /etc/passwd.txt
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
************************attention***********************************
you set the filter so the "-r " can not work
run the sersync: 
watch path is: /usr/local/nginx
                                       
# 點擊回車
[1]+  Done                    /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml
[root@nginx sersync]# 

 

這次,重啓後,過濾文件和過濾路徑下的內容不會被同步。

我們更改nginx配置 nginx.conf

worker_processes  的值由1 改成 auto, 則被控端從機會跟着更改。

若在主控端 /usr/local/nginx 路徑下 創建一個  test.txt,  並寫入 內容test,該操作會同步至被控端。從而做到實時同步。

 

啓動後查看進程

ps -ef|grep sersync2

 

可以配個定時任務,檢測 sersync2 進程有沒有跪,跪的話,使用定時任務重新啓動

contab -e 進行配置定時任務

[root@balance2 sersync]# crontab -l
#Ansible: update time
00 00 * * * ntpdate 192.168.222.100

#nginx日誌切割
58 23 * * * /usr/sbin/logrotate -vf /usr/local/nginx/logrotate_nginx
#Ansible: check sersync process
2 * * * * /bin/bash /home/check_sersync.sh > /dev/null 2>&1

其中check_sersyc.sh 腳本內容如下:

#!/bin/bash

sersync="/usr/local/sersync/sersync2"
confxml="/usr/local/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

 

 

 

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