sersync+rsync deployment and use

Experimental environment: server: 192.168.91.155, 192.168.91.158
client: 192.168.91.152
Objective: to synchronize the files in the specified directory () of clinet to the specified directory () on the server side
Host operating system environment: centos 1.10 (rsync is installed by default)

1. Train of thought

rsync + inotify tools calls rsync by writing its own script, while sersync is written on the basis of inotify tools. When sersync detects file changes and other operations, it will call rsync for synchronization.
Therefore, rsync should be installed first to realize the synchronization operation between servers, and then sersync should be installed to detect the trend of folders and automatically call rsync to realize file synchronization

2. Install rsync

172.16.1.25, 172.16.1.25 operation
1) Modify profile

log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.pass
motd file = /etc/rsyncd.Motd
[slave1]
path = /root/jiaofan/slave1/
comment = slave1
uid = root
gid = root
port=873
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = jiaofan
hosts allow = 192.168.91.152
hosts deny = 192.168.91.254

2) Create password file

cat /etc/rsync.pass
jiaofan:123456

3) Modify permission (must be 600)

chmod 644 /etc/rsyncd.conf
chmod 600 /etc/rsync.pass

4) Start process

systemctl start rsyncd

172.16.1.28 client operation
1) Create a password file for authentication

#cat /etc/rsync.password 
123456

#chmod 600 /etc/rsync.password 

2) Start process

systemctl start rsyncd

3) Test synchronization

rsync -avzP /root/jiaofan/  jiaofan@192.168.91.155::slave1/ --password-file=/etc/rsync.password
rsync -avzP /root/jiaofan/  jiaofan@192.168.91.158::slave1/ --password-file=/etc/rsync.password

3. Installing sersync

1. Why use rsync+sersync

  1. sersync is developed based on inotify, a tool similar to inotify tools
  2. sersync can record the changes in the listener directory (including adding, deleting, modifying) the name of a specific file or directory, then calling rsync to synchronize, only synchronizing the changed files or directories.

2. What is the difference between Rsync + inotify tools and rsync+sersync?

1)rsync+inotify-tools

  1. inotify can only record the changes (additions, deletions and changes) of the monitored directory, but does not record the specific file or directory changes;
  2. When rsync synchronizes, it does not know which file or directory has changed. It synchronizes the entire directory every time. When the amount of data is large, the entire directory synchronization is very time-consuming (rsync needs to traverse the entire directory to find the reference file), so the efficiency is very low

2)rsync+sersync

  1. sersync can record the name of a specific file or directory that changes (add, delete, change) in the monitored directory;
  2. When rsync synchronizes, it only synchronizes the changed files or directories (the data changed each time is very small compared with the whole synchronized directory data, and rsync is very fast when traversing to find the reference files), so it is very efficient.

Summary:
When the amount of synchronized directory data is small, it is recommended to use rsync+inotify
rsync+sersync is recommended when the amount of synchronized directory data is large (hundreds of G or even more than 1T) and there are many files

3. Download sersync

wget https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz

4. Modify profile

a) Modify lines 24-28

        <localpath watch="/root/jiaofan/slave1">
            <remote ip="192.168.91.155" name="slave1"/>
            <remote ip="192.168.91.158" name="slave1"/>
        </localpath>

b) Modify lines 31 – 34, certification section

        <rsync>
            <commonParams params="-artuz"/>
            <auth start="true" users="jiaofan" passwordfile="/etc/passwd.txt"/>
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
            <timeout start="true" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>
# ***The password file modified with Rsync and the account used for synchronization are similar: rsync -artuz /data/www/ jiaofan@192.168.91.155::slave1/ --password-file=/etc/rsync.password

c) Modify line 37

  <failLog path="/usr/local/sersync/logs/rsync_fail_log.sh"timeToExecute="60"/><!--default every 60mins execute once-->
# When synchronization fails, log to / usr/local/sersync/logs/rsync_fail_log.sh file, and resynchronize the failed log every 60 minutes

Modified configuration file

<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <host hostip="localhost" port="8008"></host>
    <debug start="false"/>
         <exclude expression="(.*)\.log\.tar\.gz"></exclude>
    </filter>
    <inotify>
        <delete start="false"/>
        <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="/root/jiaofan/slave1">
            <remote ip="192.168.91.155" name="slave1"/>
            <remote ip="192.168.91.158" name="slave1"/>
            <!--<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="jiaofan" passwordfile="/etc/passwd.txt"/>
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
            <timeout start="true" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>
        <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>

5. sersync parameter description

Sersync parameter	explain
./sersync -r	
-r The parameter is used to synchronize the directory of the main server with the directory of the remote target machine before starting the real-time monitoring; If necessary sersync Before running, all existing files or directories in the main server directory are synchronized to the remote end -r Parameter operation sersync,Synchronize the local and remote as a whole once;

Prompt Description: if the filter is set, the xml In the document, filter by true,It cannot be used temporarily-r Overall synchronization of parameters;
./sersync -o xx.xml	
Not specified -o Parameters: sersync use sersync Default configuration file in executable directory confxml.xml

appoint -o Parameters: you can specify multiple different configuration files to achieve sersync Data synchronization of multi process and multi instance
./sersync -n num	
-n Parameters: specify the total number of threads in the default thread pool;

For example:./sersync -n 5 The total number of specified threads is 5. If not specified, the default number of start thread pools is 10 cpu If the usage is too high, you can lower the parameter. If the machine configuration is high, you can increase the default total number of threads to improve the synchronization efficiency;
./sersync -d	-d Parameter: background service, usually used -r Parameter. After the local to remote overall synchronization, run this parameter in the background to start the real-time synchronization of the daemon; At the first overall synchronization,-d and -r Parameters are often used together;
./sersync -m

pluginName	
-m Parameter: do not synchronize, only run the plug-in ./sersync -m pluginName

For example:./sersync -m command,After the event is monitored, the remote target server will not be synchronized, but run directly command plug-in unit
 Instructions for combined commands:
-n 8 -o liubl.xml -r -d	Multiple parameters can be used together, for example:./sersync -n 16 -o config.xml -r -d Indicates that the number of worker threads in the thread pool is set to 16, which is specified liubl.xml As a configuration file, perform an overall synchronization before real-time monitoring and run in the background in the form of daemon;
./sersync --help	Unfortunately, it does not check the help (if necessary, there are two ways, either look at the source code or self-test for verification)

Profile module description

filter file filtering function

For the files monitored by sersync, the temporary files of the system will be filtered by default (with "." It starts with "~") and ends with "~". In addition to these files, you can customize other files to be filtered.

<filterstart="true">
     <excludeexpression="(.*)\.gz"></exclude>
     <excludeexpression="^info/*"></exclude>
</filter>

Set start to true and fill in the regular expression in the exclude tab. By default, two examples are given: filtering files ending in ". gz" and filtering the info path under the monitoring directory (monitoring path / Info / *). You can add it as needed, but when you turn it on, test it yourself. If there is an error in the regular expression, the console will prompt you. Compared with the exclude function of rsync, the filtered paths will not be monitored, which greatly reduces the traffic of rsync.

inotify monitoring parameter setting (optimization)

inotify monitoring parameters can be set to optimize srsync according to the characteristics of your project.

<inotify>
   <deletestart="true"/>
   <createFolderstart="true"/>
   <createFilestart="true"/>
</inotify>

For most applications, you can try to set createFile (monitor file event option) to false to improve performance and reduce rsync communication. Because copying files to the monitoring directory will generate create events and close events_ Write event, so if the create event is closed, only the close event at the end of the file copy is monitored_ Write, you can also achieve complete file synchronization.
Note: the createFolder is forced to be true. If the createFolder is set to false, the generated directory will not be monitored, and the sub files and subdirectories under the directory will not be monitored. So please turn it on unless special needs.
By default, both file (directory) creation events and file (directory) deletion events are monitored. If the file (directory) of the remote target server does not need to be deleted in the project, the delete parameter can be set to false, and the deletion event will not be monitored.

Debug on

12 <debugstart="false"/>
Set to true and enable the debug mode. inotify events and rsync synchronization commands will be printed on the console where sersync is running.

XFS file system

For users of xfs file system, you need to turn this option on to make sersync work normally

File monitoring and remote synchronization settings

3     <sersync>
14         <localpathwatch="/opt/tongbu1">
15             <remoteip="192.168.1.11"name="tongbu1"/>
16             <!--<remote ip="192.168.1.12" name="tongbu"/>-->
17             <!--<remote ip="192.168.1.13" name="tongbu"/>-->
18         </localpath>

Rsync parameter configuration

19         <rsync>
20            <commonParams params="-artuz"/>
20             <authstart="false"users="root"passwordfile="/etc/rsync.pas"/>
21             <userDefinedPortstart="false"port="874"/><!-- port=874 -->
22             <timeoutstart="false"time="100"/><!-- timeout=100 -->
23             <sshstart="false"/>
24         </rsync>

commonParams allows users to customize the rsync parameter, which is - artuz by default
When authstart = "false" is set to true, the authentication mode of Rsync is used. You need to configure user and passwrodfile(– password file = / etc / Rsync. PAS) to use. userDefinedPort is used when the Rsync port of the remote synchronization target server is not the default port (– port=874). Timeout sets the timeout time of Rsync (– timeout=100). ssh uses rsync-e ssh for transmission.

Failure log configuration

25 <failLogpath="/tmp/rsync_fail_log.sh"timeToExecute="60"/><!–default every 60min
If the transmission fails, it will be retransmitted, and if it fails again, it will be written to rsync_fail_log, then execute the script every other period of time (timeToExecute is set), retransmit it again, and then empty the script. You can set the log path through path.

Crontab regular overall synchronization function

26         <crontabstart="false"schedule="600"><!--600mins-->
27             <crontabfilterstart="false">
28                 <excludeexpression="*.gz"></exclude>
29                 <excludeexpression="info/*"></exclude>
30             </crontabfilter>
31         </crontab>

Crontab can synchronize the monitoring path with the remote target host at regular intervals. For some reasons, both failed retransmissions may fail. At this time, if the crontab function is enabled, it can further ensure that the files of each server are consistent. If the number of files is large, the time interval of crontab should be set larger, Otherwise, communication overhead may be increased. The schedule parameter is used to set the time interval of crontab. The default is 600 minutes
If the filter file filtering function is enabled, the crontab overall synchronization also needs to set filtering. Otherwise, although the files are filtered during real-time synchronization, if the crontab filter is not set separately during crontab overall synchronization, the files to be filtered will also be synchronized to the remote. The filtering regularity of crontab is different from that of filter, Two examples are also given, which correspond to and filter files and directories respectively. In short, if both filter and crontab are enabled, you should enable the crontab filter of crontab and set it to correspond to the filter one by one according to the example.

Plug in settings

<pluginstart="false"name="command"/>
Plug in related xml<sersync>
......     
<pluginstart="false"name="command"/>
</sersync>

<pluginname="command">
<paramprefix="/bin/sh"suffix=""ignoreError="true"/> 
<filterstart="false">
<includeexpression="(.*)\.php"/>
<includeexpression="(.*)\.sh"/>
</filter>
</plugin>
<pluginname="socket">
<localpathwatch="/opt/tongbu">
<deshostip="192.168.138.20"port="8009"/>
</localpath>
</plugin>
<pluginname="refreshCDN">
<localpathwatch="/data0/htdocs/cms.88181.com/site/">
<cdninfodomainname="ccms.chinacache.com"port="80"username="xxxx"passwd="xxxx"/>
<sendurlbase="http://pic.88181.com/cms"/>
<regexurlregex="false"match="cms.88181.com/site([/a-zA-Z0-9]*).88181.com/images"/>
</localpath>
</plugin>

As shown in the xml above, when the plugin tag is set to true, the plug-in is called after the synchronization file or path to the remote. Specify the plug-in to be executed through the name parameter. Currently, four plug-ins, command refreshCDN socket http, are supported. The http plug-in is currently removed for compatibility reasons and will be rejoined in the future.
The command plug-in will call the command plug-in after the file synchronization is completed. For example, the synchronization file is test PHP, then test After changing the PHP file, calling rsync to synchronize to the remote server, call the command plug-in and execute it.

Keywords: Linux Operation & Maintenance network rsync

Added by Shadow Hatake on Sat, 08 Jan 2022 09:53:47 +0200