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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值