Skip to content

Instantly share code, notes, and snippets.

@mario21ic
Last active April 6, 2017 16:51
Show Gist options
  • Save mario21ic/eac7c0f8b5f56b5b0497 to your computer and use it in GitHub Desktop.
Save mario21ic/eac7c0f8b5f56b5b0497 to your computer and use it in GitHub Desktop.
Script to automatically compress and sync your bucket s3 hosted static website
#!/bin/bash
DIR=`pwd`
SRC_DIR=$1
BUCKET=$2
TMP_DIR="/tmp/gzsync"
mkdir -p $TMP_DIR
# check that we have a trailing slash
[[ $BUCKET != */ ]] && BUCKET="$BUCKET"/
printf "Copying files to temporary directory... "
cp -rf $SRC_DIR $TMP_DIR && cd $TMP_DIR
echo "Done copy"
PWD=`pwd`
if [ "$PWD" == "$TMP_DIR" ]
then
for filename in $(find . -type f | grep -v git | grep -v .DS_Store)
do
printf "filename: ${filename}... "
gzip -t ${filename} > /dev/null 2>&1 || { gzip -9 ${filename} && mv -f ${filename}.gz ${filename}; }
echo "Ruta: s3cmd put ${filename} ${BUCKET}${filename#"./"}"
s3cmd put ${filename} ${BUCKET}${filename#"./"} --add-header "Content-Encoding: gzip" --delete-removed --guess-mime-type --reduced-redundancy --acl-public > /dev/null 2>&1
printf "Done\n"
done
printf "Compressing.."
cd $DIR && tar -cvf "files_gzip.tar" ${TMP_DIR}
printf "Done"
printf "Cleaning... "
cd && rm -rf ${TMP_DIR}
printf "Done"
else
echo "ERROR: Failed to change to current directory, exiting" 1>&2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment