Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lazyjerry/81c3a9e9ffc01e3a35d0ecd4a6f31e97 to your computer and use it in GitHub Desktop.
Save lazyjerry/81c3a9e9ffc01e3a35d0ecd4a6f31e97 to your computer and use it in GitHub Desktop.
# 可能會有圖片 不能上傳的行為,參考: https://stackoverflow.com/questions/31610175/permission-on-files-created-by-nginx
# 複製行為,使用 --recursive 複製資料夾
#aws s3 cp /var/www/syi.news/upload/data/attachment/forum/threadcover/ s3://media.powpost.com/forum/threadcover/ --recursive
------------ 正文開始 ------
#!/bin/bash
#
# Push2 S3
# Copyright (c) 2013-2014 Alexey Baikov <sysboss [at] mail.ru>
#
# Watches specified directory for changes and copy newly created
# or modified files to Amazon S3 Cloud.
WATCH_DIR='/監控路徑'
PID_FILE='/var/run/push2s3.pid'
BUCKET=' AWS 的 BUCKET '
len=${#WATCH_DIR}
# verify inotify installed
if [ ! -x "/usr/local/bin/inotifywait" ] ; then
echo "ERROR: This uses the inotifywait program, which on a Debian-based system is"
echo "part of the 'inotify-tools' package. Please install that and try again."
exit 1
fi
# create PID file
echo $$ > $PID_FILE
# file upload
function upload() {
local path=$1
local file=$2
local target=${path:$len}
echo "PATH: $path"
if [ "$path" = "/排除的路徑/" ]
then
echo "is temp"
else
aws s3 cp $path$file s3://$BUCKET$target
fi
}
function delete() {
local path=$1
local file=$2
local target=${path:$len}
echo "ignore delete s3"
# aws s3 rm s3://$BUCKET$target$file
echo "PATH: $path"
if [ "$path" = "/排除的路徑/" ]
then
echo "is temp"
fi
}
# init
inotifywait --recursive --monitor --exclude '.*\.sw[px]*$|4913|~$' $WATCH_DIR |
while read action_dir event_list action_file; do
if [[ $file =~ .*/\..* ]]; then
echo "Hidden file ignored"
else
case "${event_list}" in
DELETE* )
echo "DELETE from s3 $action_dir$action_file"
delete "$action_dir" "$action_file"
;;
esac
if [ -f "$action_dir$action_file" ]; then # only process a file, not a directory
case "${event_list}" in
CLOSE_WRITE* )
echo "UPLOAD to s3 $action_dir$action_file"
upload "$action_dir" "$action_file"
;;
MOVED_TO* )
echo "UPLOAD to s3 $action_dir$action_file"
upload "$action_dir" "$action_file"
;;
esac
fi
#echo "Got an event $action_dir$action_file $event_list"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment