Skip to content

Instantly share code, notes, and snippets.

@kklem0
Forked from bluejack/s3-auto-sync.sh
Created March 19, 2020 17:00
Show Gist options
  • Save kklem0/483b6fb5c0a16ba582bf90eb969fc4ed to your computer and use it in GitHub Desktop.
Save kklem0/483b6fb5c0a16ba582bf90eb969fc4ed to your computer and use it in GitHub Desktop.
#!/bin/bash
## Conventions:
##
## Working directory has the same name as the s3 bucket.
## Script is executed from the parent of this directory.
##
BUCKET=$1
if [ ! -d ${BUCKET} ]; then
echo "Directory '${BUCKET}' not found."
exit
fi
inotifywait -m -qr -e modify,create,delete,moved_to,moved_from --format '%e %w%f' ${BUCKET} | while true
do
read -r op file;
case $op in
## Simple case: creating or modifying a file, upload it.
CREATE | MODIFY | MOVED_TO)
aws s3 cp $file s3://$file
;;
MOVED_TO,ISDIR)
aws s3 sync $file s3://$file
;;
MOVED_FROM,ISDIR)
aws s3 rm s3://$file --recursive
;;
DELETE | MOVED_FROM)
aws s3 rm s3://$file
;;
DELETE,ISDIR | CREATE,ISDIR)
echo Ignoring directories - S3 creates and destroys automatically
;;
*)
echo Unexpected op $op on file $file
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment