rsync+inotify实时同步:
注:
rsync的不足:
1、rsync同步数据时,需要扫描所有文件后进行对比,进行差量传输。如果文件数量达到百万甚至千万量级,扫描所有文件是非常耗时的。 2、rsync不能实时的去检测、同步数据。
inotify可以做到对目录的实时监控,然后告知rsync只对更新部分进行同步
apt-get install inotify-tools
inotifywait -mrq /home/ming -e create,delete,moved_to,close_write
写个实时同步的脚本:
注:
1、该脚本只能在主操作的一端运行 2、inotifywait 排除目录是--fromfile,写绝对路径 3、rsync排除目录是--exclude,写相对路径
vi /usr/local/bin/inotify/inotify.sh
#!/bin/bash
des=/home/ming/ host=10.0.0.2 user=ming log=/usr/local/bin/inotify/logs/inotify.log
/usr/bin/inotifywait -mrq -e create,delete,attrib,moved_to,close_write --fromfile '/usr/local/bin/inotify/inotify_file' --timefmt '%Y-%m-%d %H:%M' --format '%T %w%f %e' | while read date time file event do echo $date $time $file $event >> $log if [ $event = 'CLOSE_WRITE,CLOSE' ];then /usr/bin/rsync -avzcR --progress --exclude-from=/usr/local/bin/inotify/rsync_file $(dirname $file) $user@$host:$des fi if [ $event = 'DELETE' ] || [ $event = 'MOVED_FROM' ];then /usr/bin/rsync -avzR --progress --delete --exclude-from=/usr/local/bin/inotify/rsync_file $(dirname $file) $user@$host:$des fi if [ $event = 'ATTRIB' ];then echo 'ATTRIB' if [ ! -d $file ];then /usr/bin/rsync -avzcR --progress --exclude-from=/usr/local/bin/inotify/rsync_file $(dirname $file) $user@$host:$des fi fi
done
:wq
vi /usr/local/bin/inotify/inotify_file
/home/ming (监控/home/ming目录) @/home/ming/bb (排除/home/ming/bb目录) :wq
vi /usr/local/bin/inotify/rsync_file
/home/ming/bb /home/ming/aa :wq
chmod +x /usr/local/bin/inotify/inotify.sh
sh /usr/local/bin/inotify/inotify.sh >> /usr/loca/bin/inotify/logs/rsync.log &
设置开机自启动:
vi /etc/rc.local (在exit 0前添加)
/bin/sh /usr/local/bin/inotify/inotify.sh >> /usr/local/bin/inotify/logs/rsync.log &
:wq