From fa41ecc8c42ebbf107c90ed2668f48a78f1777ec Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Mon, 10 Jun 2002 13:52:20 +0000 Subject: [PATCH] upload.sh: Make permissions work for uploading of single files with rsync --- scripts/upload.sh | 91 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 73 insertions(+), 18 deletions(-) diff --git a/scripts/upload.sh b/scripts/upload.sh index 37dc3b3d..b4d05bab 100644 --- a/scripts/upload.sh +++ b/scripts/upload.sh @@ -54,7 +54,8 @@ cat << EOT Version '$Revision$' - '$Date$' (c) 2002 Jannet IT Services http://www.jannet.de - usage: $MYNAME source [method://][[password:]login@]host[:target[:mode[:dirmode[:owner[.group]]]]] ... + usage: $MYNAME -h + $MYNAME [-v] [-c variable] source [method://][[password:]login@]host[:target[:mode[:dirmode[:owner[.group]]]]] ... where "method" is one of "ftp", "scp", "rsync", "rsync_ssh". @@ -86,16 +87,29 @@ cat << EOT file_owner="$file_owner" file_group="$file_group" + Options: + + -h : show this help screen + -c variable : show contents of variable + valid variables are login, method, passwd, file_owner, file_group, file_mode, dir_mode, + source, target_file + EOT [ -n "$1" ] && exit $1 } +is_dir() +{ + isdir="`echo $1 | sed 's/.*\/$/yes/'`" + test "$isdir" = yes +} + parse_target() { target=$1 - USER=`echo $target | sed -e 's/^.*:\/\///; /@/ !d; s/@.*//; s/.*://'` - test -n "$USER" && echo login="\"$USER\";" - METHOD=`echo $target | sed -e 's/:\/\/.*//'` + LOGIN=`echo $target | sed -e 's/^.*:\/\///; /@/ !d; s/@.*//; s/.*://'` + test -n "$LOGIN" && echo login="\"$LOGIN\";" + METHOD=`echo $target | sed -e '/:\/\// !d; s/:\/\/.*//'` test -n "$METHOD" && echo method="\"$METHOD\";" PASSWD=`echo $target | sed -e '/@/ !d; /@/ !d; s/^.*:\/\///; s/@.*//' | rev | cut -d: -f2 | rev` if [ -n "$PASSWD" ]; then @@ -131,6 +145,12 @@ parse_target() FILE_GROUP=`echo $target | sed -e 's/^.*:\/\///; s/.*@//; /:/ !d' | cut -d: -f5 | cut -d. -f2` test -n "$FILE_GROUP" && echo file_group="\"$FILE_GROUP\";" + if is_dir "$target_file" ; then + echo target_path=\"$TARGET_FILE\" + else + echo target_path=\"$TARGET_FILE/`basename $source`\" + fi + echo "PARSE_TARGET=\"OK\";" } @@ -143,7 +163,7 @@ ssh_mkdir() { set -e dir=`echo $1 | sed -e 's/\/[^\/]*$//; s/\/*$//'` - parts="`echo $dir | sed -e 's/\// /g'`" + parts="/ `echo $dir | sed -e 's/\// /g'`" test -n "$dir_mode" && MODE="-m $dir_mode" cat << EOT | for part in $parts; do @@ -168,9 +188,10 @@ ssh_chown() set -e dir=`echo $1 | sed -e 's/\/[^\/]*$//; s/\/*$//'` cat << EOT | - cd $1 - if [ -n "$2" ]; then chown -R $2 . ; fi - if [ -n "$3" ]; then chown -R $3 . ; fi + if [ -f "$1" -a -n "$file_owner" ]; then chown $file_owner $1; chgrp $file_group $1; fi + #cd $1 + #if [ -n "$2" ]; then chown -R $2 . ; fi + #if [ -n "$3" ]; then chown -R $3 . ; fi EOT ssh_exec_stdin } @@ -180,16 +201,33 @@ ssh_chmod() set -e dir=`echo $1 | sed -e 's/\/[^\/]*$//; s/\/*$//'` cat << EOT | - if [ -f "$1" ]; then - chmod $ - if [ -n "$2" ]; then find . -type f | xargs --no-run-if-empty chmod $2 ; fi - if [ -n "$3" ]; then find . -type d | xargs --no-run-if-empty chmod $3 ; fi + if [ -f "$1" -a -n "$file_mode" ]; then chmod $file_mode $1; fi + #if [ -n "$2" ]; then find . -type f | xargs --no-run-if-empty chmod $2 ; fi + #if [ -n "$3" ]; then find . -type d | xargs --no-run-if-empty chmod $3 ; fi EOT ssh_exec_stdin } # -- here we go -test $# -lt 2 && usage 1 +# -- command line arguments +set -- `getopt 'hvc:' $*` +while [ "$1" != -- ]; do +case $1 in + h) + usage 0;; + v) + VERBOSE=1;; + c) + VARIABLE=$2 + shift;; + *) + usage 1;; +esac +shift +done +shift + +test -z "$VARIABLE" && test $# -lt 2 && usage 1 # -- get parameters from command line source=$1 @@ -197,14 +235,13 @@ shift # -- check run for t in $*; do - parse_target $t + # parse_target $t eval `parse_target $t` if [ "$PARSE_TARGET" != OK ]; then echo "Failed to parse target \"$t\"; exiting." parse_target $t exit 2 fi - test "$METHOD" && method=$METHOD case $method in rsync_ssh) if [ "$login" != root ]; then @@ -224,15 +261,33 @@ for t in $*; do done # -- real run +if [ -n "$VARIABLE" ]; then + eval `parse_target` + case "$VARABLE" in + login) echo $login;; + method) echo $method;; + passwd) echo $passwd;; + file_owner) echo $file_owner;; + file_group) echo $file_group;; + file_mode) echo $file_mode;; + dir_mode) echo $dir_mode;; + source) echo $source;; + target_file) echo $target_file;; + *) + echo Unknown variable \"$VARIABLE\". Exiting. >&2 + exit 1; + esac + exit 0 +fi + for t in $*; do eval `parse_target $t` - test "$METHOD" && method=$METHOD case $method in rsync_ssh) ssh_mkdir $target_file $file_owner $file_group $dir_mode || break /usr/bin/rsync -az --links -e "/usr/bin/ssh -l $login $IDENTITY" $source $login@$host:$target_file - ssh_chown $target_file $file_owner $file_group - ssh_chmod $target_file $file_mode $dir_mode + ssh_chown $target_path $file_owner $file_group + ssh_chmod $target_path $file_mode $dir_mode ;; *) echo "Internal error: \"$method\" is not implemented." >&2