Created
July 13, 2012 14:59
-
-
Save rctay/3105344 to your computer and use it in GitHub Desktop.
[sh] rsync runner (just a fancy macro really)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Usage: name host ['common opts'] ['1st run opts'] ['2nd run opts'] ... | |
if test "$#" -lt 2; then | |
echo "too little arguments!" | |
exit 1 | |
fi | |
RSYNC=rsync | |
NAME="$1"; shift | |
HOST="rsync://$1"; shift | |
if test "$#" -eq 1; then | |
# no stage 1, only common | |
set -- "$1" '' | |
elif test "$#" -eq 0; then | |
# no common | |
set -- '' '' | |
fi | |
COMMON_OPTS="$1"; shift | |
BASEDIR=/var/www/download | |
CONTENTLOG=$BASEDIR/log/$NAME | |
MAINLOG=$BASEDIR/mirror.log | |
_log() { | |
level="$1"; shift | |
printf >>$MAINLOG "`date +'%Y/%m/%d %T'` $NAME [$level] $1\n" | |
} | |
log_info() { _log 'info' "$*"; } | |
log_error() { _log 'error' "$*"; } | |
LOCKFILE=$BASEDIR/$NAME.lock | |
if [ -e $LOCKFILE ]; then | |
log_error 'sync in progress? stopping' | |
exit 1 | |
fi | |
touch $LOCKFILE | |
die() { | |
/bin/rm $LOCKFILE | |
log_info 'sync done' | |
} | |
trap 'die' EXIT | |
DATADIR=$BASEDIR/data/$NAME/ | |
log_info "sync started" | |
stage=1 | |
while test "$#" -ne 0; do | |
opts="$1"; shift | |
args="$COMMON_OPTS $opts -q --log-file=$CONTENTLOG $HOST $DATADIR" | |
log_info "$NAME stage $stage" | |
eval </dev/null >>$MAINLOG 2>&1 "$RSYNC $args" | |
code="$?" | |
if [ "$code" = 0 ]; then | |
log_info "stage $stage done" | |
else | |
log_error "stage $stage failed with '$code'; args were '$args'" | |
exit 1 | |
fi | |
stage=$(($stage + 1)) | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment