关于减少rsync的遍历,未完:考虑到被监测的目录每次有一下时间时都会触发rsync,modify, delete,create,move每次rsync都会遍历源目录,当被监测目录内文件特别多时,会造成系统资源的严重消耗,所以,让rsync每次只同步修改的文件。因为,如果从监控目录mv走一个目录,那么rsync只会报告找不到你移走的目录而无法删除备份机的应该删除的目录。所以,对于删除这个事件,没有办法了,只能同步被删除文件或目录的上级目录了。将事件分为两部分,modify, create, move事件,触发rsync,只同步修改了的文件。delete事件,同步被删除文件或目录的上级目录(不能越过要同步的根目录)。
关于脚本内容的一些说明:
rsync.conf里的目录格式一定要注意,没有最后的“/"
boot.sh
对于rsync命令的目标地址,是由两部分组成的:
1、rsync.conf里的dest
des=`grep '^dest' ${basedir}/rsync.conf| cut -d '=' -f 2 `
2、被修改了的文件的完全路径,去掉源目录部分,去掉被修改的文件的文件名。
mb=`echo $file|awk -F "/" '{NF=NF-1;OFS="/";print $0}'|sed "s#$src##g"`
boot.sh
############################################################
先做个记录,脚本在delete部分还有问题
#!/bin/sh # basedir=/home/jason/Rsync destNum=`grep -c '^dest' ${basedir}/rsync.conf` src=`grep 'local directory=' ${basedir}/rsync.conf|cut -d '=' -f 2` des=`grep '^dest' ${basedir}/rsync.conf| cut -d '=' -f 2 ` # inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' \ --event modify,create,move,delete ${src} | while read date time file event do echo $event for i in $des do case $event in MODIFY|CREATE|MOVE|MODIFY,ISDIR|CREATE,ISDIR|MODIFY,ISDIR) #echo $src no_src_root_file_name=`echo $file|sed "s#$src##g"` final_target_dest=$i$no_src_root_file_name
echo rsync -avz --delete --progress $file $final_target_dest rsync -avz --delete --progress $file $final_target_dest ;; DELETE|DELETE,ISDIR) src_file_up_dir=`echo $file|awk -F"/" '{NF=NF-1;OFS="/";print $0}'` no_root_src_file_up_dir=`echo $file|awk -F"/" '{NF=NF-2;OFS="/";print $0}'|sed "s#$src##g"` final_target_dest_up_dir=$i$no_root_src_file_up_dir echo rsync -avz --delete --progress $src_file_up_dir $final_target_dest_up_dir rsync -avz --delete --progress $src_file_up_dir $final_target_dest_up_dir ;; esac done done ############################################################################################ rsync.conf local directory=/EBS/www/projects #dest_here dest=root@174.129.219.40:/EBS/www 20090723: --format '%T %w%f' |
其中的%f参数: %f When an event occurs within a directory, this will be replaced with the name of the File which caused he event to occur. Otherwise, this will be replaced with an empty string.
如果不加%f参数,当一个文件夹里的文件发生变化时,不会输出文件名(需要自己echo脚本里read的变量),而时输出发生变化的文件的文件夹名,可以将这一点用于减少rsync遍历中的delete事件。
另外,%w是用于输出发生变化的文件的。
%w This will be replaced with the name of the Watched file on which an event occurred.