Skip to content

Instantly share code, notes, and snippets.

@andru255
Forked from mario21ic/gzip_s3_sync.sh
Created April 6, 2017 16:51
Show Gist options
  • Save andru255/7378e0ba22f83b1c78e97d0474bfddf8 to your computer and use it in GitHub Desktop.
Save andru255/7378e0ba22f83b1c78e97d0474bfddf8 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