centos7 inotify deploy real-time data synchronization with rsync

Experimental environment: CentOS Linux release 7.6.1810

node1: 192.168.216.130 client (initiates data synchronization to the server)

node2: 192.168.216.132 server (receive data from the client)

The experiment is unidirectional synchronization

1, two nodes mounting rsync

yum -y install rsync

2, the node node2 modifications rsyncd.conf profile, note that allows data synchronization hosts allow IP, path synchronize directories, Comment remark information, auth users authenticated user, secrets file key file, which is consistent with the actual parameter values ​​required

cat /etc/rsyncd.conf 
# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

uid = 0
gid = 0
use chroot = no
max connections = 200
timeout = 1000
pid file = /var/run/rsyncd.pid
# exclude = lost+found/
transfer logging = yes
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
log format = %t %a %m %f %b
reverse lookup = no
hosts allow = 192.168.216.0/24
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# [ftp]
#        path = /home/ftp
#        comment = ftp export area

[data]
path = /data/
comment = data
ignore errors
read only = no
#write only = yes
list = false
auth users = postgres
secrets file = /etc/rsync/rsync.passwd

3, started on node2 rsync service, and create a user authentication file, note that users must authenticate documents actually exist, create their own user

Start Service 
systemctl start rsyncd 
create a username / password authentication file 
echo "postgres: 123456"> /etc/rsync/rsync.passwd 
modify the file permissions for the authentication 600 
chmod 600 /etc/rsync/rsync.passwd 
view the certification document 
cat / etc / rsync / rsync.passwd 
Postgres: 123456

 4, create a user authentication file on node1

node1 node only need to store the password to content 
echo "123456"> /etc/rsync/rsync.passwd 
CAT /etc/rsync/rsync.passwd 
chmod 600 /etc/rsync/rsync.passwd

5, the following command on node1, manual testing whether normal synchronization data, synchronization data directory on node1 data files to the directory on node2  

touch /data/a{1..100}
rsync -avz --password-file=/etc/rsync/rsync.passwd /data/  [email protected]::data

At this point you can be seen in the data directory data and node1 node2 consistent

 

 6, is mounted on node1 inotify-tools, compiler installation used here, may also be performedyum install inotify-tools安装

cd /tmp/
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar zxf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/usr/local/include/
make && make install

7, created on node1 shell scripts directory for real-time monitoring data changes, and automate data synchronization

vi inotify_rsync.sh
#!/bin/bash
SRC='/data/'
DEST='[email protected]::data'
inotify_home=/usr/local/include/
${inotify_home}/bin/inotifywait -mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %w %f' -e create,delete,moved_to,close_write,attrib ${SRC} |
while read DATE TIME DIR FILE;do
        FILEPATH=${DIR}${FILE}
        rsync -az --delete --password-file=/etc/rsync/rsync.passwd $SRC $DEST && echo "At ${TIME} on ${DATE}, file $FILEPATH was backuped up via rsync" >> /var/log/changelist.log
done

Note that the script variables SRC (directory synchronization), DEST (server information), inotify_home (inotify-tools installation directory) consistent with the needs and the actual directory

8, running in the background script sh inotify_rsync.sh &

Reference: https: //blog.51cto.com/14234525/2396910

 

Guess you like

Origin www.cnblogs.com/caidingyu/p/11708851.html
Recommended