Detailed steps for real-time data synchronization using rsync and sersync in linux

Source: Internet
Author: User
Tags inotify custom name
This article describes how to install rsync and sersync in linux to implement real-time data synchronization.

I. Why should I use the Rsync + sersync architecture?

1. sersync is developed based on Inotify and is similar to Inotify-tools.

2. sersync can record changes (including adding, deleting, and modifying) in the monitored directory to the name of a specific file or directory. when rsync is used, only the changed file or directory will be synchronized.

II. What are the differences between Rsync + Inotify-tools and Rsync + sersync?

1. Rsync + Inotify-tools

(1) Inotify-tools can only record changes (including adding, deleting, and modifying) to the monitored directories ), no specific file or directory changes have been recorded;

(2): During rsync, you do not know which file or directory has changed. The entire directory is synchronized each time. when the data volume is large, the entire directory synchronization is very time-consuming (rsync needs to traverse the entire directory to find the comparison file), so the efficiency is very low.

2. Rsync + sersync

(1) sersync can record the name of a specific file or directory that has changed (including adding, deleting, and modifying) in the monitored directory;

(2): During rsync, only the file or directory that has changed will be synchronized (the data changed each time is relatively small compared to the data in the entire synchronization Directory, rsync is fast when searching and comparing files. Therefore, it is highly efficient.

Summary: when the amount of data in the synchronized directory is small, we recommend that you use Rsync + Inotify-tools. when the data volume is large (several hundred GB or even 1 TB) and there are many files, we recommend that you use Rsync + sersync.

Note:

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.bitsCN.com directory on the source server to/home/www.bitsCN.com on the target server in real time


Specific operations:

Part 1: operate on the two target servers 192.168.21.127 and 192.168.21.128 respectively

1. install the Rsync server on the target server

1. disable SELINUX

Vi/etc/selinux/config # Edit the firewall configuration file

Copy codeThe code is as follows:
# SELINUX = enforcing # Comment out
# SELINUXTYPE = targeted # Comment out
SELINUX = disabled # Add

: Wq! # Save and exit

Setenforce 0 # effective immediately

2. enable tcp port 873 of the firewall (default Rsync port)

Vi/etc/sysconfig/iptables # Edit the firewall configuration file

Copy codeThe code is as follows:
-A RH-Firewall-1-INPUT-m state -- state NEW-m tcp-p tcp -- dport 873-j ACCEPT

: Wq! # Save and exit

/Etc/init. d/iptables restart # restart the firewall to make the configuration take effect.

3. install Rsync server software

Yum install rsync xinetd # installation
Vi/etc/xinetd. d/rsync # Edit the configuration file and set rsync to start at startup

Copy codeThe code is as follows:
Disable = no # Change to no

: Wq! # Save and exit

/Etc/init. d/xinetd start # start (in CentOS, xinetd is used to manage Rsync services)

4. create the rsyncd. conf configuration file.

Vi/etc/rsyncd. conf # Create a configuration file and add the following code

Copy codeThe code is as follows:
Log file =/var/log/rsyncd. log # log file location. This file is automatically generated after rsync is started. you do not need to create it in advance.
Pidfile =/var/run/rsyncd. pid # storage location of the pid file
Lock file =/var/run/rsync. lock # lock files supporting the max connections parameter
Secrets file =/etc/rsync. pass # The user authentication configuration file, which stores the user name and password. This file will be created later.
Motd file =/etc/rsyncd. Motd # Welcome to the file location on the page when rsync is started (custom file content)
[Home_www.bitsCN.com] # Custom name
Path =/home/www.bitsCN.com/# rsync server data directory path
Comment = home_www.bitsCN.com # The module name is the same as the custom name of [home_www.bitsCN.com ].
Uid = root # set rsync operation permission to root
Gid = root # set rsync operation permission to root
Port = 873 # default port
Use chroot = no # The default value is true. change it to no to add a backup for the soft connection to the directory file.
Read only = no # Set The rsync server file to read and write permissions
List = no # The rsync server resource list is not displayed.
Max connections = 200 # maximum number of connections
Timeout = 600 # set the timeout value
Auth users = home_www.bitsCN.com_user # User name for data synchronization. you can set multiple user names separated by commas (,).
Hosts allow = 192.168.21.129 # IP address of the client that allows data synchronization. you can set multiple IP addresses separated by commas (,).
Hosts deny = 192.168.21.254 # IP addresses of clients that disable data synchronization. you can set multiple IP addresses separated by commas (,).

: Wq! # Save and exit

5. create a user authentication file

Vi/etc/rsync. pass # add the following content to the configuration file:

Copy codeThe code is as follows:
Home_www.bitsCN.com_user: 123456 # Format, user name: password, you can set multiple, one user name per line: password

: Wq! # Save and exit

6. set file permissions

Copy codeThe code is as follows:
Chmod 600/etc/rsyncd. conf # set the read and write permissions of the file owner

Chmod 600/etc/rsync. pass # set the read and write permissions of the file owner

7. start rsync

Copy codeThe code is as follows:
/Etc/init. d/xinetd start # start
Service xinetd stop # stop
Service xinetd restart # restart

Part 2: operate on the source server 192.168.21.129

1. install the Rsync client

1. disable SELINUX

Vi/etc/selinux/config # Edit the firewall configuration file

Copy codeThe code is as follows:
# SELINUX = enforcing # Comment out
# SELINUXTYPE = targeted # Comment out
SELINUX = disabled # Add

: Wq! Save and exit

Setenforce 0 takes effect immediately

2. enable tcp port 873 of the firewall (Rsync default port, which can be used as the Rsync of the client without enabling port 873)

Vi/etc/sysconfig/iptables # Edit the firewall configuration file

Copy codeThe code is as follows:
-A RH-Firewall-1-INPUT-m state -- state NEW-m tcp-p tcp -- dport 873-j ACCEPT

: Wq! Save and exit

/Etc/init. d/iptables restart # restart the firewall to make the configuration take effect.

3. install the Rsync client software

Copy codeThe code is as follows:
Whereis rsync # check whether rsync has been installed in the system. the following prompt is displayed, indicating that rsync has been installed.
Rsync:/usr/bin/rsync/usr/share/man/man1/rsync.1.gz
Yum install xinetd # Only install xinetd. in CentOS, xinetd is used to manage the rsync service.
Yum install rsync xinetd # if no rsync exists by default, run this command to install rsync and xinetd
Vi/etc/xinetd. d/rsync # Edit the configuration file and set rsync to start at startup
Disable = no # Change to no
/Etc/init. d/xinetd start # start (in CentOS, xinetd is used to manage rsync services)

4. create an authentication password file

Vi/etc/passwd.txt # edit the file and add the following content

Copy codeThe code is as follows:
123456 # Password

: Wq! Save and exit

Chmod 600/etc/passwd.txt # set the file permission, only set the file owner to have the read and write permissions.

5. test data synchronization between the source server 192.168.21.129 and the two target servers 192.168.21.127 and 192.168.21.128.

Copy codeThe code is as follows:
Mkdir/home/www.bitsCN.com/ceshi # Create a test folder on the source server and run the following two lines on the source server:
Rsync-avH -- port = 873 -- progress -- delete/home/www.bitsCN.com/rsync-avH -- port = 873 -- progress -- delete/home/www.bitsCN.com/
After the operation is complete, check the two target servers 192.168.21.127 and 192.168.21.128. the ceshi folder is in the/home/www.bitsCN.com Directory, indicating that the data synchronization is successful.

System O & M www.bitsCN.com reminder: qihang01 original content is copyrighted. For more information, see the source and original link.

II. install sersync to trigger rsync in real time for synchronization

1. check whether the server kernel supports inotify.

Ll/proc/sys/fs/inotify # list the file directories. the following content appears, indicating that the server kernel supports inotify.

Copy codeThe code is as follows:
-Rw-r -- 1 root 0 Mar 7 0:17 max_queued_events
-Rw-r -- 1 root 0 Mar 7 max_user_instances
-Rw-r -- 1 root 0 Mar 7 max_user_watches

Note: In Linux, the minimum kernel that supports inotify is 2.6.13. you can run the following command: uname-a to view the kernel:

CentOS 5.X kernel 2.6.18. inotify is supported by default.

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

View the default system parameter values:

Sysctl-a | grep max_queued_events

The result is: fs. inotify. max_queued_events = 16384.

Sysctl-a | grep max_user_watches

The result is: fs. inotify. max_user_watches = 8192.

Sysctl-a | grep max_user_instances

Result: fs. inotify. max_user_instances = 128

Modify parameters:


Copy codeThe code is as follows:
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:

The maximum length of the inotify Queue. if the value is too small, the error "** Event Queue Overflow **" will occur, resulting in inaccurate monitoring files.

Max_user_watches:

Directory of the file to be synchronized, you can use: find/home/www.bitsCN.com-type d | wc-l statistics, make sure that the max_user_watches value is greater than the statistical result (here/home/www.bitsCN.com is the synchronization file directory)

Max_user_instances:

Maximum value of inotify instance created by each user

3. install sersync

Sersync: 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 the/usr/local/src directory.

Copy codeThe code is as follows:
Cd/usr/local/src
Tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz # decompress
Mv GNU-Linux-x86/usr/local/sersync # Move the directory to/usr/local/sersync

4. configure sersync

Copy codeThe code is as follows:
Cd/usr/local/sersync # enter the sersync installation directory
Cp confxml. xml confxml. xml-bak # back up the original file

Edit vi confxml. xml and modify the following code.

Copy codeThe code is as follows:































































: Wq! # Save and exit

Parameter description:

Copy codeThe code is as follows:
Localpath watch = "/home/www.bitsCN.com": # Source Server synchronization Directory
192.168.21.127, 192.168.21.128: # target server IP address
Name = "home_www.bitsCN.com": # name of The rsync directory module on the target server
Users = "home_www.bitsCN.com_user": # target server rsync user name
Passwordfile = "/etc/passwd.txt": # the target server rsync synchronizes the user's password to the storage path of the source server
Remote ip = "192.168.21.127": # The ip address of the target server.
Remote ip = "192.168.21.128": # ip address of the target server, with one ip address per line
FailLog path = "/tmp/rsync_fail_log.sh" # Log of script running failure
Start = "true" # set to true to execute full synchronization every 600 minutes

5. set sersync to monitor automatic execution upon startup

Vi/etc/rc. d/rc. local # edit and add the last line


Copy codeThe code is as follows:
/Usr/local/sersync/sersync2-d-r-o/usr/local/sersync/confxml. xml # set the script to run automatically upon startup

: Wq! # Save and exit

6. add a script to monitor whether sersync runs properly

Edit vi/home/crontab/check_sersync.sh and add the following code:

Copy codeThe code is as follows:
#! /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


Copy codeThe code is as follows:
: Wq! # Save and exit
Chmod + x/home/crontab/check_sersync.sh # add the script execution permission
Vi/etc/crontab # edit and add the following line at the end
*/5 * root/home/crontab/check_sersync.sh>/dev/null 2> & 1 # execute the script every 5 minutes
Service crond reload # reload the service


6. test whether the rsync synchronization script is normal when sersync is triggered in real time.

Create the inotify_rsync_ceshi file on the source server 192.168.21.129.

Copy codeThe code is as follows:
Mkdir/home/www.bitsCN.com/inotify_rsync_ceshi

Restart the source server: 192.168.21.129

After the system is started, check whether the inotify_rsync_ceshi folder exists in/home/www.bitsCN.com of the two target servers 192.168.21.127 and 192.168.21.128.

Create the inotify_rsync_ceshi_new folder on the source server 192.168.21.129.

Copy codeThe code is as follows:
Mkdir/home/www.bitsCN.com/inotify_rsync_ceshi_new

Check whether the inotify_rsync_ceshi_new folder exists in/home/www.bitsCN.com of the two target servers 192.168.21.127 and 192.168.21.128.

If all the tests are successful, the inotify trigger rsync synchronization script in real time runs normally.

Now, Rsync + sersync in Linux completes real-time data synchronization.

Additional reading:

Rsync parameters

-V, -- verbose detailed mode output

-Q, -- quiet simplified output mode

-C, -- checksum: enable the verification switch to force file transfer verification

-A, -- archive mode, indicating that the file is transmitted recursively and all file attributes are kept, equal to-rlptgoD.

-R, -- recursive processes subdirectories in recursive mode

-R, -- relative uses relative path information

-B, -- backup creates a backup, that is, if the object already has the same file name, rename the old file ~ Filename. You can use the -- suffix option to specify different backup file prefixes.

-- Backup-dir: backs up files (for example ~ Filename) is stored in the directory.

-Suffix = SUFFIX defines the backup file prefix.

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

-L, -- links retains soft links

-L, -- copy-links: Process soft links like regular files

-- Copy-unsafe-links: only copies links other than the SRC path directory tree.

-- Safe-links ignores links other than the SRC path directory tree

-H, -- hard-links

-P, -- perms to keep file permissions

-O, -- owner keeps file owner information

-G, -- group: Keep file group information

-D, -- devices: Keep device file information

-T, -- times preserve the file time information

-S, -- sparse performs special processing on 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 span the boundaries of the file system

-B, -- block-size = SIZE indicates the block size used by the algorithm. the default value is 700 bytes.

-E, -- rsh = COMMAND specifies that rsh and ssh are used for data synchronization.

-- Rsync-path = PATH specifies the path of the rsync command on the remote server

-C, -- cvs-exclude automatically ignores files in the same way as CVS to exclude files that do not want to be transmitted

-- Existing only updates the files that already exist in DST, instead of backing up the new files.

-- Delete: delete the files that are not in the SRC file in DST.

-- Delete-excluded: delete files specified by this option at the receiving end.

-- Delete-after: delete after transmission

-- Ignore-errors is deleted when an IO error occurs in a timely manner.

-- Max-delete = NUM: a maximum of NUM files can be deleted.

-- Partial retains the files that are not completely transferred for any reason, so as to speed up subsequent re-transmission.

-- Force directory deletion, even if not empty

-- Numeric-ids does not match the number user and group ID with the user name and group name.

-- Timeout = time ip timeout, in seconds

-I, -- ignore-times do not skip files with the same time and length

-- Size-only: when determining whether to back up a file, only check the file size, regardless of the file Time

-- Modify-window = NUM determines whether the timestamp window of the file is used at the same time. the default value is 0.

-T -- temp-dir = DIR create a temporary file in DIR

-- Compare-dest = DIR: compare the files in DIR to determine whether to back up data.

-P is equivalent to -- partial

-- Progress displays the backup process

-Z, -- compress compresses backup files during transmission

-- Exclude = PATTERN specifies to exclude file modes that do not need to be transmitted

-- Include = PATTERN specifies the file mode to be transmitted without exclusion

-- Exclude-from = FILE: exclude files in the specified mode in the FILE.

-- Include-from = FILE: files with the specified FILE pattern matching are not excluded.

-- Version: prints version information.

-- Address: bind to a specific address

-- Config = FILE: specify other configuration files. the default rsyncd. conf FILE is not used.

-- Port = PORT specify other rsync service ports

-- Blocking-io: Block IO for remote shell

-Stats indicates the transmission status of some files.

-- SS actual transmission process during transmission

-- Log-format = formAT specifies the log file format

-- Password-file = FILE get password from FILE

-- Bwlimit = KBPS limits I/O bandwidth, KBytes per second

-H, -- help: displays help information

Related Article

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.