Use rsync + inotify-tools in CentOS to synchronize and update data in real time

Source: Internet
Author: User
Tags chmod inotify centos rsync

Rsync features high security, fast backup speed, and support for incremental backup. Inotify is a fine-density, asynchronous file system event monitoring mechanism. Since Linux Kernel 2.6.13, it has added support for inotify, inotify-tools, a third-party software, monitors various file changes in the file system.

Rsync and inotify-tools can be used together to synchronize and update data in real time. The configuration process is as follows.

Environment description

Server A ip address: 192.168.2.102
Server B ip address: 192.168.2.103

Server A's rsync service configuration

Install rsync

Use yum to install rsync.

Yum-y install xinetd rsync
Sed-I '/disable/s/yes/no/g'/etc/xinetd. d/rsync
/Etc/init. d/xinetd restart>/dev/null

Create the rsync Master File: rsyncd. conf

Uid = root
Gid = root
Use chroot = no
Max connections = 200
Port = 873
Timeout = 600
Pid file =/var/run/rsyncd. pid
Lock file =/var/run/rsync. lock
Log file =/var/log/rsyncd. log

[Wwwroot]
Path =/data/wsdata/wwwroot/
Comment = This is test
Auth users = backupadmin
Ignore errors
Read only = no
List = no
Hosts allow = 192.168.2.0/255.255.255.0
Secrets file =/etc/rsync. pas

Create an rsync authentication File: sync. pas

[Format] User: password file

Vim/etc/rsync. pas

Backadmin: password

Chmod 600 rsync. pas
Start the rsync service

Rsync -- daemon
Server B rsync service configuration

Install rsync in the same way as Server.

Yum-y install xinetd rsync
Sed-I '/disable/s/yes/no/g'/etc/xinetd. d/rsync
/Etc/init. d/xinetd restart>/dev/null

Create a password file with the following permissions: 600

Vim/etc/rsync. pas

Password

Chmod 600/etc/rsync. pas

Rsync file sync method

Download from Server

Rsync-vzrtopg -- delete -- progress -- password-file =/etc/rsync. pas backupadmin@192.168.2.102: wwwroot/data/wsdata/wwwroot/

Upload to Server

Rsync-vzrtopg -- delete -- progress -- password-file =/etc/rsync. pas/data/wsdata/wwwroot backupadmin@192.168.2.102: wwwroot
Inotify-tools implement real-time synchronization update

The following configurations are all performed on Server B.

Download and install inotify-tools

Wget https://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
Tar zxvf inotify-tools-3.14.tar.gz
Cd inotify-tools
./Configure & make install
Inotify monitoring script of Server B

Vim rsync_inotify.sh
#! /Bin/bash
Src =/data/wsdata/wwwroot/

/Usr/local/bin/inotifywait-mrq -- timefmt '% d/% m/% y/% H: % M' -- format' % T % w % F'-e modify, delete, create, attrib $ src | while read file
Do
Rsync-vzrtopg -- delete -- progress -- password-file =/etc/rsync. pas $ src backupadmin@192.168.2.102: wwwroot
Echo "$ src was rsynced"
Done

Note: The Directory/data/wsdata/wwwroot/must end with a slash "/"

-M: Keep Listening
-R is used to recursively View directories.
-Q indicates the output event.
-- Timefmt is the output format of the specified time
-- Format specifies detailed information about file changes
-E create, move, delete, modify, and attrib refer to the "listening for creating a mobile delete write permission" event.
Execute scripts

Nohup./rsync_inotify.sh &

The nohup Command runs the Command specified by the Command parameter and any relevant Arg parameter, ignoring all SIGHUP signals. After logging out, run the nohup command to run the program in the background. To run the nohup command in the background, add the & (symbol indicating "and") to the end of the command.

After the execution, the file is updated on Server B and will be automatically synchronized to Server.

If the error "/usr/local/bin/inotifywait: error while loading shared libraries:
Libinotifytools. so.0 "can be solved using the following methods:

Ln-sv/usr/local/lib/libinotify */usr/lib/

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.