Example of real-time synchronization of data in Linux under Rsync+sersync

Source: Internet
Author: User
Tags auth chmod file copy file size inotify file permissions iptables rsync

Objective:

Why should I use the Rsync+sersync framework?

1, Sersync is based on inotify development, similar to the Inotify-tools tool

2. Sersync can record the changes in the listening directory (including adding, deleting, modifying) a specific file or a directory name, and then use rsync synchronization, only the change of this file or the directory.

Ii. What is the difference between rsync+inotify-tools and Rsync+sersync in these two architectures?

1, Rsync+inotify-tools

(1): Inotify-tools can only record the monitoring of the directory has changed (including add, delete, modify), and did not specify which file or which directory has changed records;

(2): Rsync in sync, do not know exactly which file or which directory has changed, each time the entire directory is synchronized, when the volume of data is very large, the entire directory synchronization is very time-consuming (rsync to the entire directory traversal to find the comparison file), therefore, the efficiency is very low.

2, Rsync+sersync

(1): Sersync can record the changes in the listening directory (including add, delete, modify) a specific file or a directory name;

(2): Rsync in sync, only the change of the file or this directory (each change in the data relative to the entire synchronization directory data is very small, rsync in the traversal lookup than the file, fast), therefore, high efficiency.

Summary: When the volume of synchronized directory data is not large, it is recommended to use Rsync+inotify-tools, when the amount of data (hundreds of G or even more than 1T), a lot of files, recommend the use of Rsync+sersync.

Description

Operating system: CentOS 5.X

Source server: 192.168.21.129

Target server: 192.168.21.127,192.168.21.128

Objective: To synchronize the/home/www.111cn.net directory on the source server to the/home/www.111cn.net of the target server in real time

System Yun-wei Www.111cn.net warm reminder: qihang01 original content copyright, reproduced please indicate the source and the original link

Specific actions:

Part one: Operate on two target servers 192.168.21.127,192.168.21.128 respectively

One, in two on the target server installation rsync service side

1. Close SELinux

Vi/etc/selinux/config #编辑防火墙配置文件

#SELINUX =enforcing #注释掉

#SELINUXTYPE =targeted #注释掉

Selinux=disabled #增加

: wq! #保存, exit

Setenforce 0 #立即生效

2, open Firewall TCP 873 port (rsync default port)

Vi/etc/sysconfig/iptables #编辑防火墙配置文件

-A rh-firewall-1-input-m state--state new-m tcp-p TCP--dport 873-j ACCEPT

: wq! #保存退出

/etc/init.d/iptables Restart #最后重启防火墙使配置生效

3, install Rsync Server Software

Yum Install rsync xinetd #安装

Vi/etc/xinetd.d/rsync #编辑配置文件, set power-on start rsync

Disable = no #修改为no

: wq! #保存退出

/etc/init.d/xinetd start #启动 (CentOS is xinetd to manage rsync services)

4. Create rsyncd.conf configuration file

Vi/etc/rsyncd.conf #创建配置文件, add the following code

Log file =/var/log/rsyncd.log #日志文件位置, which automatically generates this file when you start rsync, without having to create it in advance

Pidfile =/var/run/rsyncd.pid #pid文件的存放位置

Lock file =/var/run/rsync.lock #支持max connections parameter

Secrets file =/etc/rsync.pass #用户认证配置文件, which saves the user name and password, which is later created

MOTD file =/etc/rsyncd. MOTD #rsync启动时欢迎信息页面文件位置 (File content customization)

[Home_www.111cn.net] #自定义名称

Path =/home/www.111cn.net/#rsync服务端数据目录路径

Comment = Home_www.111cn.net #模块名称与 [home_www.111cn.net] Custom name is the same

UID = root #设置rsync运行权限为root

GID = root #设置rsync运行权限为root

port=873 #默认端口

Use chroot = no #默认为true, modified to No to increase backup of soft connections to directory files

Read Only = no #设置rsync服务端文件为读写权限

List = no #不显示rsync服务端资源列表

Max connections = #最大连接数

Timeout = #设置超时时间

Auth users = Home_www.111cn.net_user #执行数据同步的用户名, you can set multiple, separated by commas in English state

Hosts allow = 192.168.21.129 #允许进行数据同步的客户端IP地址, you can set multiple, separated by commas in English state

Hosts deny = 192.168.21.254 #禁止数据同步的客户端IP地址, you can set multiple, separated by commas in English state

: wq! #保存, exit

5, the creation of user certification files

Vi/etc/rsync.pass #配置文件, add the following

home_www.111cn.net_user:123456 #格式, Username: password, you can set multiple, one user name per line: password

: wq! #保存退出

6. Set file permissions

chmod 600/etc/rsyncd.conf #设置文件所有者读取, Write permissions

chmod 600/etc/rsync.pass #设置文件所有者读取, Write permissions

7. Start Rsync

/etc/init.d/xinetd Start #启动

Service xinetd Stop #停止

Service xinetd Restart #重新启动

Part Two: Operating on the source server 192.168.21.129

First, install the rsync client

1. Close SELinux

Vi/etc/selinux/config #编辑防火墙配置文件

#SELINUX =enforcing #注释掉

#SELINUXTYPE =targeted #注释掉

Selinux=disabled #增加

: wq! #保存退出

Setenforce 0 #立即生效

2, open Firewall TCP 873 port (rsync default port, as the client's rsync can not open 873 ports)

Vi/etc/sysconfig/iptables #编辑防火墙配置文件

-A rh-firewall-1-input-m state--state new-m tcp-p TCP--dport 873-j ACCEPT

: wq! #保存退出

/etc/init.d/iptables Restart #最后重启防火墙使配置生效

3. Install rsync client side software

Whereis rsync #查看系统是否已安装rsync, the following prompts show that you have installed

Rsync:/usr/bin/rsync/usr/share/man/man1/rsync.1.gz

Yum install xinetd #只安装xinetd即可, CentOS to manage rsync services in xinetd

Yum Install rsync xinetd #如果默认没有rsync, run this command to install Rsync and xinetd

Vi/etc/xinetd.d/rsync #编辑配置文件, set power-on start rsync

Disable = no #修改为no

/etc/init.d/xinetd start #启动 (CentOS is xinetd to manage rsync services)

4. Create the authentication password file

Vi/etc/passwd.txt #编辑文件, add the following

123456 #密码

: wq! #保存退出

chmod 600/etc/passwd.txt #设置文件权限, only set the file owner to have read, write permission

5, test the source server 192.168.21.129 to the two target server 192.168.21.127,192.168.21.128 data synchronization between

Mkdir/home/www.111cn.net/ceshi #在源服务器上创建测试文件夹, and then run the following 2-line command on the source server

RSYNC-AVH--port=873--progress--delete/home/www.111cn.net/home_www.111cn.net_user@192.168.21.127::home_ Www.111cn.net--password-file=/etc/passwd.txt

RSYNC-AVH--port=873--progress--delete/home/www.111cn.net/home_www.111cn.net_user@192.168.21.128::home_ Www.111cn.net--password-file=/etc/passwd.txt

After the operation is completed, the two target servers are 192.168.21.127,192.168.21.128 on each other, and the Ceshi folder under the/home/www.111cn.net directory indicates that the data has been synchronized successfully.

System Yun-wei Www.111cn.net warm reminder: qihang01 original content copyright, reproduced please indicate the source and the original link

Second, install Sersync tools, real-time trigger rsync to synchronize

1. See if the server kernel supports inotify

Ll/proc/sys/fs/inotify #列出文件目录, the following contents show that the server kernel supports inotify

-rw-r--r--1 Root 0 Mar 7 02:17 max_queued_events

-rw-r--r--1 Root 0 Mar 7 02:17 max_user_instances

-rw-r--r--1 Root 0 Mar 7 02:17 max_user_watches

Note: Linux support inotify kernel minimum 2.6.13, you can enter the command: UNAME-A view the kernel

The CentOS 5.X kernel is 2.6.18 and is supported by default INotify

2, modify inotify default parameter (inotify default kernel parameter value is too small)

To view system default parameter values:

sysctl-a | grep max_queued_events

The result: Fs.inotify.max_queued_events = 16384

sysctl-a | grep max_user_watches

The result: Fs.inotify.max_user_watches = 8192

sysctl-a | grep max_user_instances

The result: fs.inotify.max_user_instances = 128

To modify a parameter:

Sysctl-w fs.inotify.max_queued_events= "99999999"

Sysctl-w fs.inotify.max_user_watches= "99999999"

Sysctl-w fs.inotify.max_user_instances= "65535"

Parameter description:

Max_queued_events:

INotify Queue Maximum Length, if the value is too small, there will be "* Event queue Overflow * *" error, resulting in inaccurate monitoring files

Max_user_watches:

How many directories are included in the file to sync, available by: Find/home/www.111cn.net-type D | Wc-l statistics, the max_user_watches value must be guaranteed to be greater than the statistical result (here/home/www.111cn.net is the synchronized file directory)

Max_user_instances:

Create INotify instance maximum per user

3. Installation Sersync

Sersync Download Address: ps://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz ">https:// Sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz

Upload sersync2.5.4_64bit_binary_stable_final.tar.gz to/usr/local/src directory

Cd/usr/local/src

Tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz #解压

MV Gnu-linux-x86/usr/local/sersync #移动目录到/usr/local/sersync

4. Configure Sersync

Cd/usr/local/sersync #进入sersync安装目录

CP Confxml.xml Confxml.xml-bak #备份原文件

VI confxml.xml #编辑, modify the following code

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

<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= "false"/>

<closewrite start= "true"/>

<movefrom start= "true"/>

<moveto start= "true"/>

<attrib start= "false"/>

<modify start= "false"/>

</inotify>

<sersync>

<localpath watch= "/home/www.111cn.net" >

<remote ip= "192.168.21.127" name= "Home_www.111cn.net"/>

<remote ip= "192.168.21.128" name= "Home_www.111cn.net"/>

<!--<remote ip= "192.168.8.40" name= "Tongbu"/>-->

</localpath>

<rsync>

<commonparams params= "-artuz"/>

<auth start= "true" users= "Home_www.111cn.net_user" passwordfile= "/etc/rsync.pas"/>

<userdefinedport start= "false" port= "874"/><!--port=874-->

<timeout start= "false" Time= "/><!--timeout=100-->

<ssh start= "false"/>

</rsync>

<faillog path= "/tmp/rsync_fail_log.sh" timetoexecute= "/><"!--default every 60mins execute once-->

<crontab start= "true" schedule= "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= "xxx" 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>

: wq! #保存退出

Parameter description:

LocalPath watch= "/home/www.111cn.net": #源服务器同步目录

192.168.21.127,192.168.21.128: #目标服务器IP地址

Name= "Home_www.111cn.net": #目标服务器rsync同步目录模块名称

Users= "Home_www.111cn.net_user": #目标服务器rsync同步用户名

Passwordfile= "/etc/passwd.txt": #目标服务器rsync同步用户的密码在源服务器的存放路径

Remote ip= "192.168.21.127": #目标服务器ip, one per line

Remote ip= "192.168.21.128": #目标服务器ip, one per line

Faillog path= "/tmp/rsync_fail_log.sh" #脚本运行失败日志记录

Start= "true" #设置为true, perform a full sync every 600 minutes

5, set up sersync monitoring and automatic execution

Vi/etc/rc.d/rc.local #编辑, adding a row at the end

/usr/local/sersync/sersync2-d-r-o/usr/local/sersync/confxml.xml # set boot autorun script

: wq! #保存退出

6, add script to monitor whether the Sersync normal operation

Vi/home/crontab/check_sersync.sh #编辑, add the following code

#!/bin/sh

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

: wq! #保存退出

chmod +x/home/crontab/check_sersync.sh #添加脚本执行权限

Vi/etc/crontab #编辑, add the following line at the end

*/5 * * * * root/home/crontab/check_sersync.sh >/dev/null 2>&1 #每隔5分钟执行一次脚本

Service Crond Reload #重新加载服务

6, test Sersync real-time trigger rsync synchronization script is normal operation

Create a file on the source server 192.168.21.129 Inotify_rsync_ceshi

Mkdir/home/www.111cn.net/inotify_rsync_ceshi

Reboot source server: 192.168.21.129

After the system starts, check to see if there are Inotify_rsync_ceshi folders under the/home/www.111cn.net of the two target servers 192.168.21.127,192.168.21.128

Then create the folder on the source server 192.168.21.129 inotify_rsync_ceshi_new

Mkdir/home/www.111cn.net/inotify_rsync_ceshi_new

Continue to see if there are inotify_rsync_ceshi_new folders under the/home/www.111cn.net of two target servers 192.168.21.127,192.168.21.128

If all the above tests pass, the inotify real-time triggering rsync synchronization script is working properly.

At this point, Linux Rsync+sersync implementation of real-time data synchronization completed.

Extended reading:

Sersync Developer Website: http://blog.johntechinfo.com/sersyncguild

rsync parameters

-V,--verbose verbose mode output

-Q,--quiet thin output mode

-C,--checksum Open the check switch to force the file transfer to verify

-A,--archive archive mode, which represents the transfer of files recursively and maintains all file attributes equal to-rlptgod

-R,--recursive the subdirectory in recursive mode

-R,--relative using relative path information

-B,--backup creates a backup, which means that the old file is renamed to ~filename for the same file name already exists for the purpose. You can use the--suffix option to specify different backup file prefixes.

--backup-dir store backup files (such as ~filename) in the directory.

-suffix=suffix defines the backup file prefix

-U,--update only updates, skipping all files that already exist in DST, and the file time is later than the file you want to back up. (Do not overwrite the updated file)

-L,--links retention soft link knot

-L,--copy-links to treat soft links like regular files

--copy-unsafe-links only copies links that point beyond the src Path directory tree

--safe-links ignores links to the SRC path tree

-H,--hard-links keep hard links

-P,--perms keep file permissions

-O,--owner keep file owner information

-G,--group keep file group information

-D,--devices maintain device file information

-T,--times keep file time information

-S,--sparse special handling of sparse files to save DST space

-N,--dry-run which files will be transmitted

-W,--whole-file copy files without incremental detection

-X,--one-file-system do not cross file system boundaries

-B, the block size used by the--block-size=size test algorithm is 700 bytes by default

-E,--rsh=command specifies the use of rsh, SSH mode for data synchronization

--RSYNC-PATH=PATH Specifies the path information for the rsync command on the remote server

-C,--cvs-exclude use the same method as CVs to automatically ignore files to exclude files that you do not want to transfer

--existing updates only those files that already exist in DST, not the newly created files

--delete Delete files that are not in the DST SRC

--delete-excluded also deletes the receiving end of files that are excluded by the option specified

--delete-after after transmission is over

--ignore-errors in time IO errors are also deleted

--max-delete=num Delete NUM files up to

--partial retains files that are not fully transmitted for some reason, to expedite subsequent transmissions

--force forcibly deletes a directory, even if it is not empty

--numeric-ids does not match the user and group IDs of numbers to user and group names

--timeout=time IP timeout time in seconds

-I,--ignore-times not skip those files that have the same time and length

--size-only when deciding whether to back up a file, just look at the file size without considering the file time

--modify-window=num the time stamp window that determines whether a file is in the same time, default is 0

-t--temp-dir=dir create temporary files in dir

--compare-dest=dir also compares files in DIR to determine whether a backup is required

-P equals to--partial

--progress Display backup process

-Z,--compress compression of backed-up files on transfer

--EXCLUDE=PATTERN Specifies to exclude file modes that do not require transfer

--INCLUDE=PATTERN Specifies the file mode that is not excluded and needs to be transferred

--exclude-from=file exclude files in the specified mode in file

--include-from=file does not exclude files that match the file-specified pattern

--version Print version Information

--address bound to a specific address

--CONFIG=FILE specifies a different profile and does not use the default rsyncd.conf file

--PORT=PORT Specifies the other rsync service ports

--blocking-io blocking IO for a remote shell

-stats gives the transfer status of some files

--progress transmission process in real time

--log-format=format Specify log file format

--password-file=file gets the password from file

--bwlimit=kbps limit I/O bandwidth, Kbytes per second

-H,--help display Help information

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.