Sersync+Rsync implementation of trigger file synchronization actual combat process

  • 2020-05-30 20:46:43
  • OfStack

background

Usually we use rsync plus crontab on the server to synchronize and backup files at regular intervals. With the continuous expansion of business and application requirements, real-time requirements are getting higher and higher. 1. General rsync synchronizes the difference after verifying all the files. If the amount of files is 10 minutes, then the process of checking rsync is also 10 minutes time-consuming. And what's changing is often a very small part of it, which is a very inefficient way to do it. Secondly, rsync cannot monitor and synchronize the data in real time. Although it can synchronize the touch through crontab, there must be a time difference between the two triggering actions, which may result in different data between the server and the client, so that the data cannot be completely recovered when the application fails. The combination of Sersync+Rsync can solve this problem well.

Sersync introduction

1. sersync is written with c++, and it filters the temporary files and repeated file operations generated by linux system document system (see the appendix for details, this filter script is not implemented). Therefore, when synchronizing with rsync, the runtime consumption and network resources are saved. So it's faster.

2. The configuration of sersync is very simple, among which there are basically statically compiled binary files in the directory bin, which can be directly used in conjunction with the xml configuration file in the directory bin.

3. In addition, compared with other open source scripting projects, this project USES multiple threads for synchronization, especially when synchronizing large files, so as to ensure that multiple servers can keep synchronizing in real time.

4. There is an error handling mechanism in this project. If the failed file is resynchronized through the failure queue, the failed file will be resynchronized every 10 hours.

5. This project comes with crontab function, which can be turned on in the xml configuration file to synchronize the whole once every 1 period of time according to your requirements. No additional configuration of crontab functionality is required.

6. This project extends socket and http plug-ins to meet your needs of twice development.

Actual combat process

1. Server environment

Server: 172.16.57.26 centos6.7 rsync-server receive files

Client: 172.16.57.25 centos6.7 sersync+ rsync-client send files

2. The server installs rsync-server

1. Install rsync

Check to see if rsync has been installed. If not, install yum install directly

2. Launch rsync using xinetd


# vim /etc/xinetd.d/rsync # Modify the disable = no . flags = IPv4

3. Modify the rsync configuration file


# mkdir /etc/rsyncd
# vim /etc/rsyncd/rsyncd.conf # Modify the configuration file as follows 
# GLOBAL OPTIONS
motd file=/etc/motd
port=873
pid file=/var/run/rsyncd.pid
lock file = /var/lock/rsyncd
log file=/var/log/rsyncd
transfer logging = yes
log format = [op]:%o [ip]:%a [module]:%m [path]:%P [file]:%f [size]:%l
syslog facility=daemon
max connections=100
[recv]
comment = "recv data from 57.25"
path = /opt/rsync_data/recv # The host of this directory will be changed apprun , where a normal account is used during the synchronization process apprun
list = yes
use chroot = yes
uid = apprun
gid = apprun
read only = no
write only = no
exclude =
include =
auth users = rsync
secrets file = /etc/rsyncd/rsyncd.secrets
strict modes = yes
hosts allow = 172.16.57.25
hosts deny = *
# ln -s /etc/rsyncd/rsyncd.conf /etc/rsyncd.conf

4. Set up user authentication files


# vim /etc/rsyncd/rsyncd.secrets
rsync:111111   # format    The user name : password 
#chmod 600 /etc/rsyncd/rsyncd.secrets # Permissions set to 600 , otherwise it will report an error 

5. Start rsync


# /etc/init.d/xinetd start
# netstat -tpln | grep 873 # To view 873 Is the port already listening 

3. Install sersync+ rsync-client on the client

1. Install rsync, as shown in server 1. If there is no installation, install yum install

2. Install sersync


# tar xzvf sersync2.5_64bit_binary_stable_final.tar.gz
# mv GNU-Linux-x86 /opt/programs/sersync # Extract and copy to the installation directory 

3. Configure sersync


<?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>
  <delete start="true"/>
  <createFolder start="true"/>
  <createFile start="true"/>
  <closeWrite start="true"/>
  <moveFrom start="true"/>
  <moveTo start="true"/>
  <attrib start="true"/>
  <modify start="true"/>
  </inotify>
  <sersync>
  <localpath watch="/opt/rsync_data/send"> # Monitor the directory, 1 Once a file has changed in the local directory, it will be synchronized to the server 
    <remote ip="172.16.57.26" name="recv"/># The service side ip And synchronization module 
  </localpath>
  <rsync>
    <commonParams params="-artuz"/> #rsync The synchronization 
    <auth start="true" users="rsync" passwordfile="/etc/rsync.pas"/> Server side authentication password 
    <userDefinedPort start="false" port="873"/>
    <timeout start="false" time="100"/><!-- timeout=100 -->
    <ssh start="false"/>
  </rsync>
  <failLog path="/tmp/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>
</head>

4. Server password authentication


# vim /etc/rsync.pas # Configure the authentication file in the appropriate directory, enter the server password, and chmod 600
# chmod 600 /etc/rsync.pas

5. Start sersync


# ./sersync2 -d -o confxml.xml

4. Testing and certification

Monitor the directory /opt/rsync_data/send on the client side to add or delete files, and the server's receiving directory will be updated in real time.

In this example, servers iptables and selinux are both down.

note: when synchronizing files with this method, if the number of synchronizing files is large, some files may be missing during the synchronization process. After consulting relevant information, we found the following solution. Since the rsync service started in xinetd mode is used in this example, in the configuration file of xinetd, the following parameters are modified:

# vim /etc/xinetd.conf

Modify several parameters:


 cps       = 500 30
 instances    = UNLIMITED
 per_source   = UNLIMITED

conclusion


Related articles: