Created
January 18, 2022 15:50
-
-
Save iconifyit/861bb2de933dd5abed8a13c7cddf9e2e to your computer and use it in GitHub Desktop.
Script to download files from AWS S3 to a specified location.
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 | |
# Replace the text PATH_TO_YOUR_DROPBOX_FOLDER with the path to your Dropbox folder (e.g. /home/user/Dropbox) | |
DROPBOX_FOLDER="PATH_TO_YOUR_DROPBOX_FOLDER" | |
# DO NOT CHANGE ANYTHING BELOW THIS LINE | |
# ====================================== | |
PDF_MERGE_WORK_FOLDER="$HOME/pdfmerge" | |
# Let me know you are starting. | |
osascript -e 'display notification "Starting PDF Merge cron" with title "PDF Merge"' | |
# Set date prefix | |
DATE_STR=$(date +%y-%d-%m) | |
LOG_FILE=logs/$(date +%y-%d-%m-%H-%M-%S).log | |
# Delete existing log & suppress error if it doesn't exist. | |
rm $LOG_FILE 2> /dev/null | |
# Redirect stdout ( > ) into a named pipe ( >() ) running "tee" | |
exec > >(tee -i $LOG_FILE) | |
# Redirect stderr to stdout so we capture both. | |
exec 2>&1 | |
# Switch to the work folder | |
cd $PDF_MERGE_WORK_FOLDER | |
# Create required subfolders | |
mkdir -p logs | |
# Set variables for resources | |
S3_PATH="https://pdf-merge-prod.s3.amazonaws.com/merged" | |
HR_FILE_NAME=${DATE_STR}-HR.pdf | |
LR_FILE_NAME=${DATE_STR}-LR.pdf | |
REMOTE_FILES=( | |
$HR_FILE_NAME | |
$LR_FILE_NAME | |
) | |
# Loop through files & download if needed. | |
for FILE_NAME in ${REMOTE_FILES[@]}; do | |
# Start downloading HR file | |
echo "Downloading $FILE_NAME" | |
LOCAL_FILE="$DROP_BOX_FOLDER/Merged PDFs/$FILE_NAME" | |
if [ -f "$LOCAL_FILE" ]; then | |
echo "$LOCAL_FILE exists. Will not download." | |
osascript -e "display notification \"$LOCAL_FILE exists. Will not download.\" with title \"PDF Merge\"" | |
else | |
echo "$LOCAL_FILE does not exist. Downloading." | |
osascript -e "display notification \"$LOCAL_FILE does not exist. Downloading.\" with title \"PDF Merge\"" | |
/usr/local/bin/wget $S3_PATH/$FILE_NAME -O $LOCAL_FILE | |
fi | |
# wget writes a file regardless if the source is empty. | |
# Delete HR file if empty. | |
if [ ! -s $LOCAL_FILE ]; then | |
echo "$LOCAL_FILE is empty. Deleting." | |
rm $LOCAL_FILE 2> /dev/null | |
fi | |
done | |
# Let me know you are finished. | |
osascript -e 'display notification "Merged files were downloaded to DropBox" with title "PDF Merge"' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment