Rsync + Inotify

Rsync

# In order to only include files of a certain type, we need to exclude all the other files but not all directories.
rsync -avz --include='*/' --include='*.txt' --exclude='*' src/ dst/
# Copy Directory Structure but Skip Files
rsync -av -f"+ */" -f"- *"  src dst

Reference: https://download.samba.org/pub/rsync/rsync.1

Inotify

dnf -y install autoconf automake libtool
wget -c https://github.com/inotify-tools/inotify-tools/archive/refs/tags/3.22.6.0.tar.gz
tar -zxvf 3.22.6.0.tar.gz  -C /usr/local/src/
cd /usr/local/src/inotify-tools-3.22.6.0
./autogen.sh && \
./configure --prefix=/usr/local/inotify-tools && \
make && \
make install
vim /etc/profile
PATH=$PATH:/usr/local/inotify-tools/bin/
. /etc/profile

Reference: https://docs.rockylinux.org/books/learning_rsync/06_rsync_inotify/

Real-time Sync

#!/bin/bash
# let n=1; while [ $n -le 10 ]; do echo $n; n=$((n+1)); done
# this script works with weekly rsync: 59 23 * * 5 /usr/bin/rsync -avz --delete /home/a /home/b
# this script doesn't sync delete.
a="/usr/local/inotify-tools/bin/inotifywait -mrq -e modify,move,create,delete /home/a"
b="/usr/bin/rsync -avz --files-from=/tmp/rsyncset /home/a /home/b/a" 
n=1
$a | while [ $n -le 100 ]
    do
	if [ $n -lt 100 ]
	  then
	    read -t 0.2 dir e file
		if [ ! -z "$e" ]
		  then 
	   	    echo ${dir/\/home\/a\//}${file}
	   	    echo ${dir/\/home\/a\//}${file} >> /tmp/rsynclist
		    e=""
		fi
	    n=$((n+1))
	else
	   (sort /tmp/rsynclist | uniq )> /tmp/rsyncset
	   if [ -s /tmp/rsyncset ]
	     then
		echo >> /tmp/rsync.log
		date >> /tmp/rsync.log	
		echo "filestosync: " >> /tmp/rsync.log
		cat /tmp/rsyncset >> /tmp/rsync.log
		echo >> /tmp/rsync.log
                $b &>> /tmp/rsync.log
	   fi
	   n=1
	   :>/tmp/rsynclist
	fi
    done

你可能感兴趣的:(Linux,linux,bash,运维)