Skip to content

scp (secure copy) a file to the same path on a remote system

IT

I've been copying files from one server to the symmetrical cluster partner a lot last week.

It's always

 scp /path/to/directory/file remote:/path/to/directory/

That gets boring after a while but does not really warrant setting up a full blown config management solution like salt, puppet or ansible.

So here is scpover[1.5kB].

It reduces the effort to

 scpover /path/to/directory/file

This will copy "file" from "/path/to/directory/" into exactly the same directory on the remote cluster partner. Which system to target is detected from the hostname of the local system and hard-coded into the script.

Scpover can also take multiple filepaths on one command line so you can beam over a few related config files from multiple locations in one go.
Not an atomic change but at least all within a reasonably short timespan.

  1. #!/bin/bash
  2. # scpover v0.1
  3. # Copyright (c) 2014 Daniel Lange, http://daniel-lange.com.
  4. # Released into the public domain. NO LIABILITY ACCEPTED WHATSOEVER. USE AT YOUR OWN RISK.
  5.  
  6. READLINK="/bin/readlink"
  7. TARGET=""
  8.  
  9.  
  10. if [[ -v HOSTNAME ]] ; then
  11.         case "${HOSTNAME,,}" in
  12.                 ashley)
  13.                         TARGET="marykate"
  14.                         ;;
  15.                 marykate)
  16.                         TARGET="ashley"
  17.                         ;;
  18.                 *)
  19.                         echo "Error: I have no cluster partner for ${HOSTNAME}. Exiting."
  20.                         exit 2
  21.                         ;;
  22.         esac
  23. else
  24.         echo "Error: I need \$HOSTNAME set to know where to copy."
  25.         exit 1
  26. fi
  27.  
  28. if [[ "$1" == "-h" || "$1" == "--help" ]] ; then
  29.         echo "$0: <file1> [file2] ..."
  30.         echo "  Copy files to the cluster partner via scp into the same directory as where is resides on the source host."
  31.         echo "  Links (symbolic or hard) will be resolved and copied as a regular file. That's a feature/bug of scp."
  32.         echo "  The target is determined from a pre-defined list built into the program. So don't specify one."
  33.         echo "  HOSTNAME: ${HOSTNAME:-not set!}"
  34.         exit 0
  35. fi
  36.  
  37. if [[ ! -x $READLINK ]] ; then
  38.         echo "ERROR: I need readlink to work. What strange system is this?"
  39.         exit 3
  40. fi
  41.  
  42. if [[ $# -lt 1 ]] ; then
  43.         echo "I need at least one file to copy over to the cluster partner."
  44.         exit 1
  45. fi
  46.  
  47. echo "Copying from $HOSTNAME to $TARGET..."
  48.  
  49. for f in "$@"; do
  50.         FULLNAME=""
  51.         FULLNAME=$($READLINK -n -f "$f")
  52.         if [[ -f "$f" ]] ; then
  53.                 echo "${FULLNAME} -> ${TARGET}:${FULLNAME%/*}/"
  54.                 scp -p -C "$f" "${TARGET}:${FULLNAME%/*}/"
  55.         else
  56.                 echo "Warning: $f ($FULLNAME) is not a regular file. Skipping."
  57.         fi
  58. done

You need to define the cluster partner for each of your systems from line 12. If you have larger clusters than pairs, think of deploying a config management solution.
And then make TARGET a list and add a loop around the echo and scp commands in lines 53f. :-).

Oh, and of course scpover can self-deploy:

root@marykate:/usr/local/bin# scpover scpover 
Copying from marykate to ashley...
/usr/local/bin/scpover -> ashley:/usr/local/bin/
scpover                                                                               100% 1509     1.5KB/s   00:00

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

Ben on :

A cheeky alternative way to do it is scp {,remote:}/path/to/file. That way you also still have control over the local/remote part, at the cost of readability ;-)

Charles on :

Thanks to Ben for the detailed explanation.

Add Comment

Markdown format allowed
Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Form options

Submitted comments will be subject to moderation before being displayed.