Rsync+inotify for data synchronization

Source: Internet
Author: User
Tags inotify rsync

Rsync principle:

Rsync is based on the difference between the rsync algorithm checksum source (SRC) and the target (DEST) to achieve data synchronization. That is, when using the Rsync tool to synchronize data, only files that have changed from the source are copied to the target (DEST), which is similar to incremental backups. So rsync synchronizes data at a very fast speed. However, Rsync's transmission performance is very poor, when the large amount of small files need to synchronize, performance is very bad. Because Rsync is based on file synchronization (not block level), it is also verified before transmission.

Rsync works in a variety of modes, using Rsync's service model to build: master/Slave Server data synchronization. At this point rsync works for the daemon monitoring in: tcp/873, can receive the client's data synchronization request.


INotify principle:

INotify can monitor files, and can also monitor directories. When monitoring the directory, it can simultaneously monitor the directory and the directory of subdirectories and file changes. Because Intify uses file scanners as an excuse, you can use common file I/O operations such as SELECT, pool, Epoll to see the changes in the file system


Environmental requirements:

Host1:

Centos-7-x86_64

Hostname:ws1

ipaddr:10.0.0.61

Host2:

Centos-6-x86_64

Hostname:ws2

ipaddr:10.0.0.62

Note: two hosts are minimized installation iptables and SELinux are off


Configure Host1


CentOS 7 minimized installation, Rsync pack installed by default

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/82/05/wKioL1dH7c3iLlbFAAAlVMrP6iI834.png "title=" 9971b9ab-4304-4c65-9e88-908ede75b433.png "alt=" Wkiol1dh7c3illbfaaalvmrp6ii834.png "/>


Edit the rsync configuration file

# vim /etc/rsyncd.confuid = root           //rsync synchronizing the identity of the shared user gid = root          // rsync Sync Shared user Group identity use chroot = no         // Do not use chrootport = 873          //to define the listening port, default 873max  connections = 200   //maximum number of concurrent servers timeout = 300           //Session Timeout Length pid file = /var/run/rsyncd.pid     //pid file storage path Lock file = /var/run/rsyncd.lock    //lock file storage path, The log file = /var/log/rsyncd.log    //log file storage path is used when the service starts to stop log format  = %h %o %1 %b      //Log Record Format ignore errors               //Ignore error messages read only = false            //whether to allow users to upload files, default to truelist = false               //whether to allow users to list files, default to truewrite only = false            //whether to allow download, default is TRUEHOSTS ALLOW = 10.0.0.61/8        //which hosts are allowed to access hosts deny = *   s          //which hosts are denied access to auth users = sync             //user secrets file = /etc/required for authentication at the time of synchronization rsyncd.passwd    //Authentication Required password file [data]    //module name, the client needs to specify the module name accurately path =  /data     //module corresponding directory path


Password file/etc/rsyncd.passwd When creating authentication (requires user name and password, clear text storage, only primary readable)

# echo "sync:123456" >/etc/rsynd.passwd# chmod 600/etc/rsynd.passwd


Create the corresponding directory for the module and ensure that the owner and the group are consistent with the UID and GID defined in the configuration file

# mkdir-p/data/# ls-ld/data/drwxr-xr-x 2 root root 21 May 22:47/data/# cp/var/log/message/data/


Start the service and check the process and listening ports

# systemctl start rsyncd.service# ps aux | grep rsyncroot        3121  0.0  0.1 114640  1164 ?         ss   22:47   0:00 /usr/bin/rsync  --daemon --no-detachroot       3123  0.0   0.0 112660   956 pts/0    r+   22:48    0:00 grep --color rsync# ss -tunl | grep 873tcp     LISTEN     0      5          *:873                    *:*                    tcp    listen     0       5        :::873                   :::*


Configure Host2

The password file ~/RSYNCD when the certificate was created. passwd (password only, plaintext storage, primary readable only)

# echo "123456" > rsyncd.passwd# chmod rsyncd.passwd


Test to view files in the data module on the rsync server

]# rsync--password-file=rsyncd.passwd [email protected]::d atadrwxr-xr-x 2016/05/27 22:47:34.-RW------- 242668 2016/05/27 22:47:35 Messages

Note: once again, be sure to check the firewall


Set up local Data Catalog file/data,/data directory with the directory/data defined in the data module on the rsync server as the Dir-sync directory, that is, the data in both directories is always consistent.


Test: Fetch the data from the Host1/data directory.

# RSYNC-AVZ--PASSWORD-FILE=RSYNCD.PASSWD [email protected]::d ata/data/receiving Incremental file list./messagessent Bytes received 26839 bytes 53830.00 bytes/sectotal size is 242668 speedup is 9.02


Then create several files locally using the rsync command to synchronize to the Host1 data directory

# RSYNC-AVZ--password-file=rsyncd.passwd/data/[email protected]::d atasending Incremental file list./ Rsyncd.passwdsent 2567 Bytes Received bytes 5202.00 bytes/sectotal size is 30649078 speedup is 11783.57


Note: The data directory itself, not the files in the directory, will be synchronized with/if less than one/at the back of the directory


# rm-f/data/fstab# rsync-avz--delete--password-file=rsyncd.passwd/data/[email protected]::d atasending incremental F Ile listdeleting fstabsent 2521 Bytes Received bytes 5066.00 bytes/sectotal size is 30649078 speedup is 12099.91

In this case, manually synchronizing the data files on the two nodes directory is no problem, now it is necessary to implement the data on the HOST2 to synchronize the data on the Host1 immediately, which requires inotify.


Inptify


INotify is the kernel function, but need through the system call interface program to use, this interface program is provided by Inotify-tools, so we have to install!!! Here we install on the HOST2

# yum install-y inotify-tools# # RPM-QL inotify-tools/usr/bin/inotifywait/usr/bin/inotifywatch/usr/lib64/ libinotifytools.so.0/usr/lib64/libinotifytools.so.0.4.1


The following are several inotify-related kernel parameters that may be adjusted according to actual needs

# ls-l/proc/sys/fs/inotify/Total dosage 0-rw-r--r--1 root root 0 May 00:23 max_queued_events-rw-r--r--1 root root 0 May 28 0 0:23 max_user_instances-rw-r--r--1 root root 0 May 00:23 max_user_watches


Real-time monitoring of data catalog files, changes immediately call rsync for data synchronization, the script is as follows

# vim auto_rsync.sh#!/bin/bashsync_user= "sync" sync_host= "10.0.0.61" sync_mod= "Data" data_dir= "/data/" rsync_cmd= " RSYNC-VZRTOPG--delete--progress--password-file=./rsyncd.passwd $data _dir [email protected] $sync _host:: $sync _mod " Inotifywait-mrq $data _dir--timefmt '%d/%m/%y%h:%m '--format '%T%w%f%e '-e modify,delete,create,attrib | While read Files;do $rsync _cmddone


Start the script, and then modify the files in the Host2 data directory to observe the changes

Bash auto_rsync.sh sending incremental file list./deleting issuedeleting. auto_rsync.sh.swpauto_rsync.sh 359 100% 0.00kb/s 0:00:00 (xfer#1, to-check=108/110) sent 2800 bytes received bytes 5668.00 bytes/sectotal size is 306493 Speedup is 10814.89







Rsync+inotify for data synchronization

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.