Rsync + inotify for One-Way Real-time Data Synchronization
The master server synchronizes data to the slave server in real time.
Master Ip address: 192.168.1.39 (node1)
Slave IP Address: 192.168.1.40 (node2)
1. Configure slave server
Install rsync on the slave server and create and configure the rsync file.
[Root @ node2 ~] # Yum install-y rsync
Hosts allow = 192.168.1.39
Hosts deny = *
List = true
Uid = root
Gid = root
Pid file =/var/run/rsyncd. pid
Lock file =/var/run/rsync. lock
Log file =/var/log/rsyncd. log
[Node2]
Path =/data/node2
Read only = no
2. Create a file directory to be synchronized on the slave server, start rsync, and view the process and listening port.
[Root @ node2 ~] # Mkdir-p/data/node2
[Root @ node2 ~] # Rsync -- daemon
[Root @ node2 ~] # Ps-ef | grep rsync | grep-v "grep"
Root 1723 1 0? 00:00:00 rsync -- daemon
[Root @ node2 ~] # Netstat-anpt | grep rsync
Tcp 0 0 0.0.0.0: 873 0.0.0.0: * LISTEN 1723/rsync
Tcp 0 0: 873: * LISTEN 1723/rsync
3. Manually test the rsync synchronization function.
[Root @ node2 ~] # Cd/data/node2/
[Root @ node2 node2] # echo "123456"> a.txt
[Root @ node2 node2] # echo "abcd"> B .txt
[Root @ node2 node2] # cat a.txt B .txt
123456
Abcd
[Root @ node1 ~] # Yum install-y rsync (install rsync on the master)
[Root @ node1 ~] # Mkdir-p/data/node1
[Root @ node1 ~] # Rsync-avzP 192.168.1.40: node2/data/node1/
Refreshing incremental file list
./
A.txt
7 100% 6.84kB/s 0:00:00 (xfer #1, to-check = 1/3)
B .txt
5 100% 0.12kB/s 0:00:00 (xfer #2, to-check = 0/3)
Sent 68 bytes encoded ed 180 bytes 23.62 bytes/sec
Total size is 12 speedup is 0.05
[Root @ node1 ~] # Cd/data/node1/
[Root @ node1 node1] # ls
A.txt B .txt
[Root @ node1 node1] # cat a.txt B .txt
123456
Abcd
Synchronization is manually tested.
Ii. Configure the master server
Compile and install inotify.
[Root @ node1 ~] # Mkdir-p/taokey/tools
[Root @ node1 ~] # Cd/taokey/tools/
[Root @ node1 tools] # tar-zxf inotify-tools-3.14.tar.gz
[Root @ node1 tools] # cd inotify-tools-3.14
[Root @ node1 inotify-tools-3.14] #./configure
[Root @ node1 inotify-tools-3.14] # make
[Root @ node1 inotify-tools-3.14] # make install
2. Configure the inotify script.
#! /Bin/bash
Host = 192.168.1.40
Data_dir =/data/node1/
Dst = node2
/Usr/local/bin/inotifywait-mrq -- timefmt '% d/% m/% y % H: % m' -- format' % T % w % f % e'-e modify, delete, create, attrib $ data_dir | while read files
Do
# Rsync-avzP $ host: $ dst $ data_dir
Rsync-avzP -- delete -- progress $ data_dir $ host: $ dst
Echo "$ {files} was rsynced">/tmp/rsync. log 2> & 1
Done
[Root @ node1 inotify-tools-3.14] # chmod u + x inotify_rsync.sh
[Root @ node1 inotify-tools-3.14] # bash inotify_rsync.sh &
[1] 4533
[Root @ node1 inotify-tools-3.14] # ps-ef | grep inoti | grep-v "grep"
Root 4533 1880 0 00:00:00 pts/1 bash inotify_rsync.sh
Root 4534 4533 0 00:00:00 pts/1/usr/local/bin/inotifywait-mrq -- timefmt % d/% m/% y % H: % M -- format % T % w % f % e-e modify, delete, create, attrib/data/node1/
Root 4535 4533 0 00:00:00 pts/1 bash inotify_rsync.sh
3. synchronize data on the master server to the slave server for testing.
[Root @ node1 ~] # Cd/data/node1/
[Root @ node1 node1] # ls
A.txt B .txt
[Root @ node1 node1] # touch c.txt
[Root @ node1 node1] # sending incremental file list
./
C.txt
0 100% 0.00kB/s 0:00:00 (xfer #1, to-check = 0/4)
Sent 111 bytes encoded ed 30 bytes 13.43 bytes/sec
Total size is 12 speedup is 0.09
Sending incremental file list
Sent 72 bytes encoded ed 8 bytes 7.62 bytes/sec
Total size is 12 speedup is 0.15
[Root @ node1 node1] #
[Root @ node2 node2] # ls
A.txt B .txt c.txt
[Root @ node1 node1] # rm-rf a.txt
[Root @ node1 node1] # sending incremental file list
./
Deleting a.txt
Sent 61 bytes encoded ed 11 bytes 6.86 bytes/sec
Total size is 5 speedup is 0.07
[Root @ node1 node1] #
[Root @ node2 node2] # ls
B .txt c.txt
Test successful!
-------------------------------------- Split line --------------------------------------
Rsync + inotify implement real-time synchronization and backup of Git data
Rsync for file backup Synchronization
Rsync synchronizes two servers
Remote synchronization of Rsync in CentOS 6.5
Use Rsync in Ubuntu Linux for data backup and Synchronization
Linux uses the Rsync client to synchronize directories with the server for backup
Rsync details: click here
Rsync: click here
This article permanently updates the link address: