Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the real-time synchronization of local and remote remote data by Linux through Rsync+Inotify

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you how Linux through Rsync+Inotify to achieve this, remote data real-time synchronization function, the content is simple and easy to understand, I hope you can learn, after learning will definitely have a harvest, let Xiaobian take you to see it.

0x0 Test environment

Headquarters production server and branch backup server require remote data backup, the environment is as follows

**centos 6.5**

The requested URL/home/zytest/files was not found on this server.

Backup server directory: /home/zytest/files

User/Password: zytest / zytest

0x1 Production server environment setup

0x1.1 Install gcc compiler and rsync

yum install gcc rsync -y

0x1.2 Copy inotify to server and extract

cd /root

tar xfvz inotify-tools-3.13.tar.gz

0x1.3 Enter inotify directory installation

cd inotify-tools-3.13 ./ configure make make install /usr/local/bin/inotifywait ##Check if installation succeeded

0x2 Backup server environment setup

0x2.1 Install xinetd and rsync

yum install xinetd rsync -y

0x3 The following contents are operated synchronously by two servers

useradd -u 600 zytest passwd zytest zytest su - zytest -c 'mkdir /home/zytest/files' ##Create synchronization directory

0x4 Configure rsyncd on backup server

0x4.1 Edit/etc/xinetd.d/rsync Modify as follows

disable = yes ==> disable = noflags = IPv6 ==> flags = IPv4server_args = --daemon ==> server_args = --daemon --config=/etc/rsyncd.conf

0x4.2 Edit/etc/rsyncd.conf and add the following script information

uid = rootgid = rootuse chroot = nomax connections = 1000strict mode = yesport = 873pid 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/zytestignore errorsauth users =zytestsecrets file = /home/rsync-dst.psread only = nolist = false

Ps: rsyncd configuration file is on xinetd, so backup server installs xinetd

0x4.3 Write the password to the invoked password file and grant permissions

echo zytest:zytest >> /home/rsync-dst.pschmod 600 /home/rsync-dst.ps

0x4.4 Start rsync via xinetd

/etc/rc.d/init.d/xinetd restart

0x5 Configure inosync script file on primary server

0x5.1 ** ##Edit/root/inosync Add script code **

#!/ bin/sh#chkconfig: 3 78 10#This file exist from compileif [ ! -f /usr/local/bin/inotifywait ]then echo "cannot start. file inotifywait NOT exist! " exitfi#This file is runnable shell scriptif [ ! -f /usr/local/bin/inosync.so.1 ]then echo "contact administrator. inosync.so.1 NOT exist! " exitficase "$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 Grant script permission, set boot boot

chmod a+x /root/inosync cp /root/inosync /etc/rc.d/init.d

0x5.3 Main script file for configuration calls/root/inosync.so.1

rhost=** Backup Server IP**user=zytestsrc=/home/zytest/filesdst=zytest#dst corresponding to [zytest] in file /etc/rsyncd.conf on dst serverlog=/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: %T followed by spaces %f and %e followed by spaces

0x5.4 Give inosync.so.1 script permissions and copy to/usr/local/bin

chmod a+x /root/inosync.so.1cp /root/inosync.so.1 /usr/local/bin

0x5.5 Write the password to the invoked password file and grant permissions

echo zytest >> /home/rsync-src.ps chmod 600 /home/rsync-src.ps

0x6 Target Server Settings inosync Autostart and enable inosync service

chkconfig --level 3 inosync on/etc/rc.d/init.d/inosync start

0x7 TEST END

Create files and folders in the production server/home/zytest/files directory and see if the backup storage synchronizes the files and folders as well. Synchronization is successful.

The process can be viewed through logs

tail -f/root/inosync.log The above is about how Linux realizes real-time synchronization of local and remote data through Rsync+Inotify. If you have learned knowledge or skills, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report