Created
May 17, 2022 06:19
-
-
Save ronanmccoy/e4fb962ad8ea519cf94fd1a7735a3aa1 to your computer and use it in GitHub Desktop.
Bash script to download a list of files from an AWS EC2 server.
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 | |
############################################################################### | |
## Script to download a list of files from a remote server on AWS EC2. | |
## Requirements are the pem file to connect to the server, the user, and | |
## the servers AWS domain (i.e. EC2 domain name). The goal is to build the | |
## SSH connection string and pass it to SCP. | |
## | |
## The SSH connection string for the EC2 instance should be in the format: | |
## "ssh -i /path/to/pem user@ec2-xx-xx-xxx-xxx.compute-1.amazonaws.com" | |
## | |
## Note that this will download the file to the current directory (note the | |
## value of DESTINATION when set to ".") | |
############################################################################### | |
# configure these | |
USER="ubuntu" | |
HOST="ec2-xx-xx-xxx-xxx.compute-1.amazonaws.com" | |
PEM="/PATH/TO/THE/PROPER/PEM/FILE" | |
SOURCE="/HOME/DIRECTORY/LOCATION" | |
DESTINATION="." | |
# update to proper file names | |
FILES_ARRAY=( | |
"FILE_TO_DOWNLOAD_1.sql" | |
"FILE_TO_DOWNLOAD_2.sql" | |
"FILE_TO_DOWNLOAD_3.sql" | |
) | |
# general notification when something fails | |
function die() { | |
echo "$1" | |
exit 1 | |
} | |
# build the main connection strings | |
COMMAND_STR="${PEM} ${USER}@${HOST}:${SOURCE}" | |
counter=0 | |
# loop through array of files and download each | |
for str in ${FILES_ARRAY[@]}; do | |
echo "- downloading '$str'" | |
scp -i ${COMMAND_STR}/${str} ${DESTINATION} || die "*** failed to download ${str}***" | |
counter=$((counter + 1)) | |
done | |
echo "\nFinished. Downloaded ${counter} files.\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment