Skip to content

Instantly share code, notes, and snippets.

@jbryanscott
Created October 28, 2017 03:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbryanscott/cc71cc13712bb41dc2c276898bc84226 to your computer and use it in GitHub Desktop.
Save jbryanscott/cc71cc13712bb41dc2c276898bc84226 to your computer and use it in GitHub Desktop.
Copy files via SSH command text when you don't have direct access to the host, e.g. in a bastion or virtual setup like Convox
#!/usr/bin/env bash
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
echo "Fatal: Sourcing detected. Cannot source ${BASH_SOURCE[0]}."
return
exit 1
fi
set -e
set -o pipefail
script_root="$(cd $(dirname ${BASH_SOURCE[0]}) && pwd)"
cd "$script_root"
# Create the tarball
tar -cvzh -f payload.tar.gz payload
# Read the tarball as a base64 string
tar_base64=$(\
cat payload.tar.gz \
| openssl base64 -e\
| tr '\n' ' '\
)
# Send the base64 to host as bash command and decode it
instances=$(convox instances | grep '^i-' | cut -f1 -d ' ')
instance_list=($instances)
index=$1 # Defaults to 0, the first instance
convox instances ssh "${instance_list[$index]}" ":;
echo '$tar_base64' | tr ' ' '\n' | openssl base64 -d > payload.tar.gz;
tar -xvz -f payload.tar.gz;
sudo yum -y install rsync;
rsync -avh payload/ .;
bash;
:;"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment