Linux-13-Inotify+Rsync real time data synchronization

Keywords: inotify rsync yum EPEL

Check before configuration

1. First, make sure that Rsync has been configured and the client can push files to the server

[test@C64-6-B ~]$ rsync -avzP ./syncdir rsync_backup@2.2.2.5::syner --password-file=/etc/rsync.password
sending incremental file list
syncdir/

sent 52 bytes  received 12 bytes  42.67 bytes/sec
total size is 0  speedup is 0.00

Please refer to the following blogs for specific configuration

https://blog.csdn.net/Paul_George/article/details/82805496

 

2. Check whether the current system supports Inotify

[test@C64-5-S ~]$ ls -l /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Sep 25 13:25 max_queued_events
-rw-r--r-- 1 root root 0 Sep 25 13:25 max_user_instances
-rw-r--r-- 1 root root 0 Sep 25 13:25 max_user_watches

If the above three files appear, the system supports Inotify

3. Download and install Inotify

Execute the following command (due to too much information, it will not be posted here)

yum install -y epel-release && yum update
yum install inotify-tools

 

Configure Inotify

1. Write Inotify real-time monitoring script

[syner@C64-5-S ~]$ mkdir -p /home/syner/r_inotify/
vi /server/scripts/inotify/inotify.sh
#!/bin/bash
#para
host01=2.2.2.6
src=/home/syner/
dst=syner
user=rsync_backup
rsync_passfile=/etc/rsync.password
inotify_home=/usr/src/kernels/2.6.32-754.3.5.el6.x86_64/

#judge
if [ ! -e "$src" ] \
|| [ ! -e "${rsync_passfile}" ] \
|| [ ! -e "/usr/bin/inotifywait" ] \
|| [ ! -e "/usr/bin/rsync" ];
then
  echo "Check File and Folder"
  exit 9
fi

/usr/bin/inotifywait -mrq --timefmt '%d%m%y %H:%M' --format '%T %w%f' -e close_write,delete,cr
eate,attrib $src | while read file
do
  cd $src && rsync -aruzR --delete ./r_inotify --timeout=100 -e 'ssh -p 52113'  $dst@$host01:/home/syner/  >> /home/syner/inotify.log 2>&1
done
exit 0 

2. Operation procedure

[syner@C64-5-S ~]$ sh /server/scripts/inotify/inotify.sh &
[1] 8509
[syner@C64-5-S ~]$ ps -ef | grep inotify
syner     8509  8440  0 19:01 pts/2    00:00:00 sh /server/scripts/inotify/inotify.sh
syner     8510  8509  0 19:01 pts/2    00:00:00 /usr/bin/inotifywait -mrq --timefmt %d%m%y %H:%M --format %T %w%f -e close_write,delete,create,attrib /home/syner/
syner     8511  8509  0 19:01 pts/2    00:00:00 sh /server/scripts/inotify/inotify.sh
syner     8513  8440  0 19:01 pts/2    00:00:00 grep inotify

Test success

[syner@C64-5-S r_inotify]$ echo "test inotify" > test.txt
[syner@C64-6-B r_inotify]$ ll
total 4
-rw-rw-r-- 1 syner syner 13 Sep 25 19:02 test.txt
[syner@C64-6-B r_inotify]$ more test.txt 
test inotify

Successfully synced past

[syner@C64-5-S r_inotify]$ rm test.txt 
[syner@C64-6-B r_inotify]$ ll
total 0

It can also be deleted synchronously

 

 

 

Posted by Ralf Jones on Wed, 25 Dec 2019 12:18:46 -0800