Linux realizes real time synchronization of local and remote data through Rsync+Inotify
- 2021-07-26 09:13:05
- OfStack
0x0 test environment
Headquarters production server and branch backup server require remote data backup, and the environment is as follows
**centos 6.5**
Production Server Directory:/home/zytest/files
Backup Server Directory:/home/zytest/files
User/Password: zytest/zytest
Establishment of 0x1 production server environment
0x 1.1 Install gcc compiler and rsync
yum install gcc rsync -y
0x 1. 2 Copy inotify to server and unzip
cd /root
tar xfvz inotify-tools-3.13.tar.gz
0x1. 3 Install in inotify Directory
cd inotify-tools-3.13
./configure
make
make install
/usr/local/bin/inotifywait ## Check whether the installation is successful
0x2 Backup Server Environment Construction
0x 2.1 Install xinetd and rsync
yum install xinetd rsync -y
0x3 The following two servers operate synchronously
useradd -u 600 zytest
passwd zytest
zytest
su - zytest -c 'mkdir /home/zytest/files' ## Create Synchronization Directory
Configuring rsyncd on 0x4 Backup Server
0x4.1 Edit/etc/xinetd. d/rsync is modified as follows
disable = yes ==> disable = no
flags = IPv6 ==> flags = IPv4
server_args = --daemon ==> server_args = --daemon --config=/etc/rsyncd.conf
0x4.2 Edit/etc/rsyncd. conf and add the following script information
uid = root
gid = root
use chroot = no
max connections = 1000
strict mode = yes
port = 873
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
# following for user "zytest", change for other users
[zytest]
path = /home/zytest
ignore errors
auth users =zytest
secrets file = /home/rsync-dst.ps
read only = no
list = false
Ps: The rsyncd configuration file is on xinetd, so the backup server installs xinetd
0x4.3 Writes the password to the invoked password file and gives permission
echo zytest:zytest >> /home/rsync-dst.ps
chmod 600 /home/rsync-dst.ps
0x4.4 Start rsync through xinetd
/etc/rc.d/init.d/xinetd restart
0x5 Configuring the inosync script file on the primary server
0x5.1 ** # # Edit/root/inosync Add Script Code**
#!/bin/sh
#chkconfig: 3 78 10
#This file exist from compile
if [ ! -f /usr/local/bin/inotifywait ]
then
echo "cannot start. file inotifywait NOT exist!"
exit
fi
#This file is runnable shell script
if [ ! -f /usr/local/bin/inosync.so.1 ]
then
echo "contact administrator. inosync.so.1 NOT exist!"
exit
fi
case "$1" in
'start')
/usr/local/bin/inosync.so.1 &
;;
'stop')
pid=`ps -ef | grep -v grep | grep "inotifywait" | awk '{print $2}'`
kill -9 $pid 2>&1
;;
'restart')
$0 stop
$0 start
;;
esac
0x5.2 Give script permissions and set boot
chmod a+x /root/inosync
cp /root/inosync /etc/rc.d/init.d
0x5.3 Main script file for configuration invocation/root/inosync. so.1
rhost=** Backup server IP**
user=zytest
src=/home/zytest/files
dst=zytest
#dst corresponding to [zytest] in file /etc/rsyncd.conf on dst server
log=/root/inosync.log
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M:%S' --format '%T %w%f %e' -e close_write,create,move,delete,attrib $src | while read files
do
echo == rsync begin == >> $log
rsync -avP --password-file=/home/rsync-src.ps --delete $src $user@$rhost::$dst >> $log 2>&1
echo -- rsyncd -- >> $log
date >> $log
echo "${files} was rsynced " >> $log 2>&1
done
PS: Space after% T and space between% f and% e
0x5.4 Grant inosync. so.1 script permissions and copy the/usr/local/bin
chmod a+x /root/inosync.so.1
cp /root/inosync.so.1 /usr/local/bin
0x5. 5 Writes the password to the invoked password file and gives permission
useradd -u 600 zytest
passwd zytest
zytest
su - zytest -c 'mkdir /home/zytest/files' ## Create Synchronization Directory
0
0x6 Target server sets inosync to start automatically and starts inosync service
useradd -u 600 zytest
passwd zytest
zytest
su - zytest -c 'mkdir /home/zytest/files' ## Create Synchronization Directory
1
0x7 Test END
Create files and folders in the production server/home/zytest/files directory and see if the backup store also synchronizes the files and folders. Synchronization is successful.
Procedures can be viewed through logs
useradd -u 600 zytest
passwd zytest
zytest
su - zytest -c 'mkdir /home/zytest/files' ## Create Synchronization Directory
2
Summarize