Skip to content

Instantly share code, notes, and snippets.

@braxtone
Forked from woodb/gzsync.sh
Last active August 29, 2015 14:17
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 braxtone/43405ecdfc86f4ed4e6c to your computer and use it in GitHub Desktop.
Save braxtone/43405ecdfc86f4ed4e6c to your computer and use it in GitHub Desktop.
Bash script to compress and upload the current directory to an S3 bucket with the ability to exclude files with certain extensions.
#!/bin/bash
BUCKET=$1
TMP_DIR=`mktemp -d -t gzsync`
# Get a list of comma-delimited excluded filetypes
OIFS=$IFS;
IFS=",";
EXCLUDED_FILETYPES=($2)
IFS=$OIFS;
# check that we have a trailing slash
[[ $BUCKET != */ ]] && BUCKET="$BUCKET"/
printf "Copying files to temporary directory... "
cp -R . $TMP_DIR && cd $TMP_DIR
printf "Done\n"
PWD=`pwd`
if [ "$PWD" = "$TMP_DIR" ]
then
for filename in $(find . -type f | sed "s/\.\///g" | grep -v git | grep -v .DS_Store)
do
printf "${filename}..."
# Skip compression for files that we're told to exclude
extension="${filename##*.}"
# Yeah, this is a horrible way to check if an array contains a string
# but it works so... ship it!
excluded=$(echo ${EXCLUDED_FILETYPES[@]} | grep -o "$extension" -c )
if [ $excluded -ne 0 ]
then
printf "not compressing...uploading..."
s3cmd put ${filename} ${BUCKET}${filename} --acl-public > /dev/null 2>&1
else
printf "compressing..."
gzip -t ${filename} > /dev/null 2>&1 || { gzip -9 ${filename}; mv ${filename}.gz ${filename}; }
printf "uploading..."
s3cmd put ${filename} ${BUCKET}${filename} --add-header "Content-Encoding: gzip" --acl-public > /dev/null 2>&1
fi
printf "done\n"
done
printf "Cleaning up... "
cd && rm -rf ${TMP_DIR}
printf "Done\n"
else
echo "ERROR: Failed to change to current directory, exiting" 1>&2
fi
#### End of Script ####
# Run it like:
$ gzsync.sh s3://s3-bucket/ 'mp3'
Copying files to temporary directory... Done
404.html...compressing...uploading...done
about/index.html...compressing...uploading...done
audio/awesome-episode-e001.mp3...not compressing...uploading...done
css/main.css...compressing...uploading...done
episodes/2015/03/01/ep-one/index.html...compressing...uploading...done
feed.xml...compressing...uploading...done
images/itunes.png...compressing...uploading...done
index.html...compressing...uploading...done
podcast.xml...compressing...uploading...done
Cleaning up... Done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment