Remote-triggered file synchronization with Sersync

Source: Internet
Author: User
Tags inotify rsync webfile

When doing a Web site or a file services cluster, it is common to use Rsync + inotify to synchronize files between servers, while using scripts to achieve synchronization is inefficient, so here I will use Sersync to implement this function.


1.Inotify Introduction

Inotify is an inode-level file system monitoring technology that is a powerful, granular, asynchronous mechanism that meets a wide range of file monitoring needs, not only for security and performance, The kernel requires more than 2.6.13, Inotify can monitor very many file system events, monitor these events to monitor whether a file has changed, and then update the changed files via rsync, Inotify the file system events that can be monitored include:

In_access, that is, the file is accessed

In_modify, File is write

In_attrib, file attributes are modified, such as chmod, chown, touch, etc.

In_close_write, writable file is CLOSE

In_close_nowrite, non-writable files are CLOSE

In_open, File is OPEN

In_moved_from, files are removed, such as MV

In_moved_to, files are moved, such as MV, CP

In_create, creating a new file

In_delete, files are deleted, such as RM

In_delete_self, self-deletion, that is, an executable file deletes itself when executed

In_move_self, self-moving, that is, an executable file moves itself at execution time

In_unmount, the host file system is Umount

In_close, file is closed, equivalent to (In_close_write | In_close_nowrite)

In_move, file is moved, equivalent to (In_moved_from | IN_MOVED_TO)

Note: The above file also includes the table of contents.


2.rsync Introduction

Rsync,remote Synchronize as implies meaning that it is a remote synchronization software, it synchronizes files at the same time, you can keep the original file permissions, time, soft and hard links and other additional information. Rsync uses the rsync algorithm to provide a quick way for file synchronization between a client and a remote file server, and it can be transferred via SSH, so it's very confidential and it's free software.

Rsync includes some of the following features:

Can update entire directory and tree and file system;

Selective retention of symbolic chains, hard links, file belonging, permissions, equipment, and time;

For the installation, there is no special permission requirements;

For multiple files, the internal pipeline reduces the delay of file waiting;

can use rsh, SSH or direct port as the transmission port;

Support the anonymous rsync synchronization files, is the ideal image tool;


3.sersync Introduction

Sersync uses inotify and rsync to synchronize the server in real time, where inotify is used to monitor file system events, and rsync is a widely used synchronization algorithm, which has the advantage of operating only the different parts of the file. So its advantage is much more than the way to use the hook file system for mirroring synchronization. The development of the Zhou Ocean by Jinshan is one of the most widely used file synchronization tools. This tool has the following advantages over other tools:

Sersync is written in C + +, which saves time and bandwidth more than other synchronization tools because it synchronizes only the files that changed.

Easy to install, easy to configure;

Using multithreading to synchronize, can ensure that multiple servers in real-time synchronization state;

Self-error handling mechanism, the failure of the queue on the wrong file re-error, if it still fails, the synchronization failed to synchronize every 10 hours of files;

With the crontab function, just open in the XML configuration file, you can according to your requirements, the overall synchronization at a time;

With the socket and HTTP protocol extension, you can easily develop two times;


4. Use Sersync to implement a triggered file synchronization instance:

Lab Environment:

Primary server: 192.168.1.60

From server: 192.168.1.66

Operating system version: Redhat5.8-32bit

(1) from the server configuration:

(a) installation of the Rsync service

Yum Install Rsync-y

(b) Edit the/etc/rsyncd.conf file, (configuration files usually need to be created manually)

# Rsync Configuration Fileuid = Rootgid = Rootport = 873max Connections = 20000use chroot = yestimeout = 200log File =/va R/log/rsyncd.logpid File =/var/run/rsyncd.pidlock File =/var/run/rsyncd.locklog format =%t%a%m%f%bauth users = root Secrets file =/etc/rsyncd.secrets[webfile] #设置模板path =/web/test/#同步文件的目录comment = "Test directory file" list = yes #是否允许 List files Read Only = Noignore errors = yeshosts allow = 192.168.1.60/24 #访问权限hosts deny = *

(c) rsync password file creation

echo "Root:redhat" >/etc/rsync.secretchmod 600/etc/rsync.secret #更改密码文件的权限

(d) Activation of the Rsync service

Rsync--daemon--config=/etc/rsyncd.conf

(e) Verify that the service is started

[Email protected] test]# Netstat-lntup |        grep rsynctcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 7541/rsync TCP 0 0::: 873:::* LISTEN 7541/rsync

(2) Primary server configuration

(a) Installing rsync

Yum Install-y rsync

(b) Installation of Sersync

Download Sersync http://www.jb51.net/softs/40308.html

Unzip and move to the appropriate directory

Tar XF sersync_32bit_binary_stable_final.tar.gz mv gnu-linux-x86//usr/local/sersync/

(c) Configuration Sersync

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

(d) Create a password file

echo "Redhat" >/etc/rsync.secret

Just need a password, or you will get an error

(e) Launch of Sersync

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

File updates on the primary server to detect if the server is able to synchronize successfully.

At this point, the experimental completion of trigger file synchronization based on Sersync is implemented.


Report:

To prevent Sersync from accidentally shutting down, use the following script

#!/bin/bash#purpose:check Sersync Whether it is alivesersync= "/usr/local/sersync/sersync2" conf_file= "/usr/local/ Sersync/confxml.xml "status=$ (PS aux |grep ' sersync2 ' |grep-v ' grep ' |wc-l) if [$STATUS-eq 0]; Then $SERSYNC-D-r-o $CONF _file &fi

Add to Task plan

*/5 * * * */root/script/check_sersync.sh &>/dev/null


Remote-triggered file synchronization with Sersync

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.