Skip to content

Instantly share code, notes, and snippets.

@bluejack
Last active February 23, 2023 12:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save bluejack/5d18998628f920953f6ed1b2b1def142 to your computer and use it in GitHub Desktop.
Save bluejack/5d18998628f920953f6ed1b2b1def142 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