mac 下 rsync + fswatch 自动同步

mac 下没有inotifywait,替代品是fswatch,brewhome安装。

#!/usr/bin/env bash
dirPrefixPhp='/Users/Ares/Documents/Work/php'
excludeCommon="app/cache/* app/logs/* app/runtime/*"
projectArr[0]=$dirPrefixPhp'/ald/xxx1'
projectArr[1]=$dirPrefixPhp'/ald/xxx2'
projectArr[2]=$dirPrefixPhp'/ald/xxx3'
rsyncArr[0]='-e "ssh -p222" root@ip0:/srv/www/xxx1'
rsyncArr[1]='-e "ssh -p222" root@ip1:/srv/www/xxx2'
rsyncArr[2]='root@ip2:/srv/www/xxx2'
#wild card,seperated by space
exclude[0]='public/_captcha/*'
exclude[2]='system/caches/*'
watchArgs=$*
if [ -z "$watchArgs" ];then
	watchArgs='.'
fi
fswatch --exclude=\\.git --exclude=\\.svn --exclude=\\.settings --exclude=\\.project --exclude=\\.buildpath $watchArgs|while read v;do
	if [ ! -e $v ];then
		continue
	fi
	for k1 in ${!projectArr[*]};do
		dirPrefix=${projectArr[$k1]}
		if [ "${v:0:${#dirPrefix}}" = "$dirPrefix" ];then
			path=${v#$dirPrefix}
			excludeK1=($excludeCommon' '${exclude[$k1]})
			if [ -n "$excludeK1" ];then
				for v2 in ${excludeK1[*]};do
					if [[ "${path:1}" == $v2 ]];then
						break 2
					fi
				done
			fi
			rsync='rsync -avz -R --chown apache:apache'
			rsync="cd ${projectArr[$k1]} && "$rsync' .'$path' '${rsyncArr[$k1]}' --no-p --no-t'
			dst=${rsyncArr[$k1]#*@}
			#dst=${dst%:*}
			#echo -n '['`date +%H:%M:%S`'] '${projectArr[$k1]##*/}$path' => '$dst
			echo -n '['`date +%H:%M:%S`'] '$k1 $dst$path
			time=$( (time eval $rsync > /dev/null) 2>&1|head -n2|tail -n1|cut -f2)
			if [ 0 -eq $? ];then
				echo -ne " \033[32mOK\033[0m"
			else
				echo -ne " \033[31mFAILED\033[0m"
			fi
			echo '('${time:2}')'
		fi
	done
done

 

发表评论

电子邮件地址不会被公开。

*