Rsync+inotify
Requirements:
Synchronize 192.168.1.100 from /data/book/ to 192.168.1.101 from /data/book/
Real-time synchronization is required,
Part 1: Operate on target server 192.168.1.101
Install Rsync server on the target server
1. Close SELinux
Open firewall TCP 873 port (Rsync default port)
3. Install Rsync server software
Yum -y install rsync #
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf#
4. Create rsyncd.conf configuration file, create rsync module, username.
/etc/rsyncd.conf # create a configuration file and add the following code
The log file = / var/log/rsyncd log
Pidfile = / var/run/rsyncd pid
Lock the file = / var/run/rsync. Lock
Secrets file = / etc/rsync. Pass
Uid = root
Gid = root
Use chroot = no
Max connections = 1200
Timeout = 600
# module 1
[book]
Path = / data/book /
Comment = the book
port=873
Read only = no
The list = no
Auth users = bookuser
Hosts allow = 192.168.1.100
Hosts deny = 192.168.21.254
: wq! # Save, exit
Parameter description:
Log file = /var/log/rsyncd.log # This file is automatically generated when rsync is started. No need to create this file in front of you
Pidfile = /var/run/rsyncd.pid = /var/run/rsyncd.pid = /var/run/rsyncd.pid
Lock file = /var/run/rsync.lock # Support Max connections
Secrets File = /etc/rsync.pass # User Authentication Configuration File, which holds the user name and password, will be created later
Motd file = /etc/rsyncd.motd #rsync file = /etc/rsyncd.motd #rsync
[home_www.osyunwei.com] # Customize name
Path = /data/book/ #rsync server data directory path
Comment = book # Module name is the same as [book] custom name
Uid = root # Set rsync to root
Gid = root # Set rsync permissions to root
Port =873 # Default port
Use chroot = no # Default to true, change to no, add backup of directory files soft connection
Read only = No # Set the rsync server file to read and write
List = no # does not display a list of rsync server resources
Max connections = 200 # Max number of connections
Timeout = 600 # Set the timeout time
Auth users = bookuser # The user name to perform data synchronization, which can be set multiple times, separated by commas in the English state
Hosts Allow = 192.168.1.100 # Allow multiple IP addresses for data synchronization, separated by commas in the English state
Hosts Deny = 192.168.21.254 # Disallow data synchronization of client IP addresses. Multiple IP addresses can be set, separated by commas in English
5. Create user authentication file
Vim /etc/rsync.pass # configuration file, add the following
BookUser :123456 # format, username: password, can be set more than one, per line username: password
: wq! # save exit
6. Set file permissions
/etc/rsyncd.conf # Set file owner access to read/write
Chmod 600 /etc/rsync.pass # Set the permissions for the file owner to read and write
Service xinetd restart
Part 2: Operating on the source server 192.168.1.100
Install the Rsync client
1. Close SELinux
Enable port 873 on firewall (default port Rsync, client Rsync can not open port 873)
3. Install Rsync client software
Yum -y install rsync xinetd
The chkconfig rsync on
/etc/init.d/xinetd start # to use xinetd to manage rsync
4. Create an authentication password file
Vi /etc/passwd.txt # Edit the file and add the following
123456 # password
:wq! # save exit
/etc/passwd.txt # Set file permissions to read and write to the owner of the file
5. Test data synchronization between source server 192.168.1.100 and target server 192.168.1.101
The mkdir/data/testfile
Rsync-av /data/testfile bookuser@192.168.1.101::book --password-file=/etc/passwd.txt
Go to 192.168.1.101 to see if there is a testfile in /data/book/.
Part 3: Operating on the source server 192.168.1.100
Download the way
Compile the installation
Wget HTTP: / / http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
Tar xf way - tools - 3.14. Tar. Gz. CD way - tools - 3.14
./configure --prefix=/usr/local/inotify && make && make install
Create a script, join the background run.
vim /usr/local/inotify/inotify.sh
#! /bin/bash
#para
Host01 =192.168.1.101 # Inotify-slave IP address
SRC =/data/book # The directory to monitor locally
DST =book #inotify-slave rsync service module name
User =bookuser #inotify-slave for the virtual user of the rsync service
Rsync_passfile =/etc/passwd.txt # Password file for local invocation of rsync service
Inotify_home =/usr/local/inotify #
#judge
If [! -e "$SRC "]
[! | | -e "${rsync_passfile}"]
[! | | -e "${inotify_home} / bin/inotifywait"]
[! | | -e "/ usr/bin/rsync"].
then
Echo "Check File and Folder"
The exit 9
fi
${inotify_home} / bin/inotifywait - MRQ - timefmt '% % % % d/m/y H: % m' - the format '% T % w % f - e close_write, delete, create, attrib $ The SRC
| while read the file
do
# rsync - avzP -- delete - timeout = 100 - password - file = ${rsync_passfile} $SRC $user @ $host01: : $DST > / dev/null 2 > &1
CD $SRC && rsync aruz - R -- delete. / $user @ $host01: : $DST - password - file = ${rsync_passfile} > / dev/null 2 > &1
done
The exit 0
Chmod + x/usr/local/way/way. The sh # add execute permissions
/ usr/local/way/way. Sh & # background running this script
########### Error while loading shared libraries: libinotifytools.so.0: cannot open shared object fil file
The system could not find the library
Enter the directory
cd /usr/local/inotify/
find . -name libinotifytools.so.0
Add the file path to /usr/lib64/
ln -s /usr/local/inotify/lib/libinotifytools.so.0 /usr/lib64/
The configuration is complete, so check the data synchronization for yourself.
The source directory /data/testfile/ has a very big difference in the back of the /data/testfile/, usually did not notice, today was hit.
Without a slash, /data/testfile synchronizes subdirectories under this directory, including this directory.
After the slash, /data/testfile/, synchronize subdirectories under this directory, not including this directory.
Share on: