From 2b47bb65d2aba2175fd9062fc65cefa9b57f581b Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Mon, 21 Oct 2019 16:09:35 +0200 Subject: [PATCH] [ssh_tools] multicopy: don't require pscp Fallback to plain SCP if parallel-scp is not available. --- ssh_tools/multicopy.sh | 61 +++++++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/ssh_tools/multicopy.sh b/ssh_tools/multicopy.sh index fb8860c..081074e 100755 --- a/ssh_tools/multicopy.sh +++ b/ssh_tools/multicopy.sh @@ -53,12 +53,24 @@ bad_usage() err() { - printf 'Error! %s\n' "$@" >&2 + printf 'Error! ' >&2 + printf '%s\n' "$@" >&2 } warn() { - printf 'Warning! %s\n' "$@" >&2 + printf 'Warning! ' >&2 + printf '%s\n' "$@" >&2 +} + +# Copy file $FILE to the remote hosts using plain old SCP +scp_copy() +{ + grep -v '^#' "$HOSTS" | while read -r host ; do + [ -z "$host" ] && continue + echo "Deploying '$FILE' to '$host'..." + scp -r "$FILE" "${SSH_LOGIN}${host}:${DEST_DIR}" + done } # Parse CLI options @@ -84,30 +96,38 @@ shift $((OPTIND - 1)) [ $# -lt 2 ] && bad_usage "Wrong number of arguments." # Check the usage of -r and -R -if [ "$DELETE" = "1" ] ; then - [ "$RSYNC" = "1" ] && bad_usage "Use either -r or -R, but not both." +if [ "$DELETE" = 1 ] ; then + [ "$RSYNC" = 1 ] && bad_usage "Use either -r or -R, but not both." RSYNC=1 DELETE="-X --delete" fi # Ignore -P if rsync is going to be used -if [ "$RSYNC" = "1" ] && [ "$PARALLEL" = "1" ] ; then +if [ "$RSYNC" = 1 ] && [ "$PARALLEL" = 1 ] ; then warn "Cannot transfer in parallel when using rsync: ignoring -P." PARALLEL= fi # Check dependencies -PSCP=$(command -v parallel-scp || command -v pscp.pssh || command -v pscp) -PRSYNC=$(command -v parallel-rsync || command -v prsync) +if [ "$RSYNC" = 1 ] ; then + PRSYNC=$(command -v parallel-rsync || command -v prsync) + if [ -z "$PRSYNC" ] ; then + err "Parallel rsync (prsync) is required for this script to work with -r or -R." + exit $EXIT_DEPENDENCY + fi + +else # $RSYNC != 1 + PSCP=$(command -v parallel-scp || command -v pscp.pssh || command -v pscp) + if [ -z "$PSCP" ] ; then + warn "Parallel SSH (pssh) is not installed. Install it for best results." + if [ "$PARALLEL" = 1 ] ; then + warn "Parallel copy will not work without Parallel SSH. Ignoring -P." + PARALLEL= + fi + fi +fi + MULTIPING=$(command -v multiping) -if [ -z "$RSYNC" ] && [ -z "$PSCP" ] ; then - err "Parallel SSH (pssh) is required for this script to work." - exit $EXIT_DEPENDENCY -fi -if [ -z "$PRSYNC" ] ; then - err "Parallel rsync (prsync) is required for this script to work." - exit $EXIT_DEPENDENCY -fi if [ -z "$MULTIPING" ] ; then err "multiping (which should have been provided along with this" \ "script) is required for this script to work." @@ -150,7 +170,7 @@ fi echo "Destination directory: $DEST_DIR" # Transfer the files in parallel... -if [ "$PARALLEL" = "1" ] ; then +if [ "$PARALLEL" = 1 ] ; then # shellcheck disable=SC2086 exec $PSCP -r $LOGIN -h "$HOSTS" -- "$@" "$DEST_DIR" fi @@ -162,11 +182,16 @@ for FILE in "$@" ; do warn "This file doesn't exist, skipping." continue fi - if [ "$RSYNC" = "1" ] ; then + if [ "$RSYNC" = 1 ] ; then # shellcheck disable=SC2086 $PRSYNC -a $DELETE $LOGIN -h "$HOSTS" -- "$FILE" "$DEST_DIR" - else + continue + fi + if [ -n "$PSCP" ] ; then # shellcheck disable=SC2086 $PSCP -r $LOGIN -h "$HOSTS" -- "$FILE" "$DEST_DIR" + continue fi + # If we're still here, that means we'll have to copy with plain SCP + scp_copy done