* rsync ラッパー [#d119bd19]
階層を持ったファイル群をローカルとリモートで同期を取るときに使う。
-app_root/bin/
-app_root/config/
-app_root/lib/
とあるとして、ローカルのlib以下のファイルに修正を加えて、そのlib以下のみをリモートへコピーしたいときなどに使う。rsync app_root/lib/ remote:/foo/dir/app_root/lib/とタイプするのが面倒なので。
#!/bin/sh
# ==============================================================================
# RSYNC Wrapper
# ------------------------------------------------------------------------------
#
# ex. WORKING DIR /home/foo/work/app/ (@localhost)
# TARGET DIR /var/www/htdocs/ (@remotehost)
#
# cd /home/foo/work/app/lib
# sh THIS_COMMAND -> rsync /home/foo/work/app/lib remotehost:/var/www/htdocs/lib
# cd /home/foo/work/app/bin
# sh THIS_COMMAND -> rsync /home/foo/work/app/lib remotehost:/var/www/htdocs/bin
# ==============================================================================
# ------------------------------------------------------------------------------
# Configuration
# ------------------------------------------------------------------------------
FROM1=/usr/home/taro/work/shopping/class
TO1=dessert:/export/home/taro/class
FROM2=/usr/home/ryuichi/work/shopping/lib
TO2=dessert:/export/home/taro/lib
EXCLUDES="
*global.conf
*.swp
"
"
# ------------------------------------------------------------------------------
# Parse arguments
# ------------------------------------------------------------------------------
while [ "$#" -gt 0 ]; do
case "$1" in
-help|-h)
echo "Usage: "
echo "$0 [-index|-i INDEX_NUMBER] [-debug]"
echo
echo 'Now configured: [INDEX NUMBER] "From" -> "To"'
idx=1
while : ; do
f=`eval echo -n '$FROM'$idx`
if [ "$f" = '' ]; then
break
fi
echo -n "[$idx] "
eval echo -n '$FROM'$idx
echo -n ' -> '
eval echo '$TO'$idx
idx=`expr $idx + 1`
done
exit
;;
-index|-i)
if [ ! "$2" ]; then
echo -n "Error: Index number is not found. "
echo "$0 -index INDEX_NUMBER"
exit 1
fi
specified_to=$2
shift
;;
-debug)
debug=1
;;
*)
if [ -e "$1" ]; then
target="$1"
else
opt="$opt $1"
fi
break
;;
esac
shift
done
# ------------------------------------------------------------------------------
# Check which directory you are in, set FROM/TO directories
# ------------------------------------------------------------------------------
pwd=`pwd`
idx=1
while [ -n "FROM$idx" ]; do
tmp=`eval echo '$FROM'$idx`
if echo $pwd/ | grep "$tmp/" 2>&1 1>/dev/null; then
FROM=$tmp
TO=`eval echo '$TO'$idx`
break
fi
idx=`expr $idx + 1`
done
if [ -z "$FROM" ];then
echo "Error: This directory is not configured for. Try $0 -help."
exit 1
fi
if [ -n "$specified_to" ]; then
TO=`eval echo '$TO'$specified_to`
fi
# ------------------------------------------------------------------------------
# Do rsync
# ------------------------------------------------------------------------------
FROM=`echo $FROM | sed -e 's/\//\\\\\//g'`
sed="sed -e 's/$FROM//'"
dir=`echo $pwd | eval $sed`
for e in $EXCLUDES; do
excs="$excs --exclude '$e'"
done
if [ -n "$debug" ]; then
echo rsync -rlDvzcC $excs -e ssh $opt $pwd/$target $TO$dir/
else
eval "rsync -rlDvzcC $excs -e ssh $opt $pwd/$target $TO$dir/"
fi
# vim: set sw=2: