Created
October 28, 2017 03:06
-
-
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
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
#!/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