-
Notifications
You must be signed in to change notification settings - Fork 8
/
push2s3
68 lines (59 loc) · 1.78 KB
/
push2s3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/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='/some/dir'
PID_FILE='/var/run/push2s3.pid'
BUCKET='amazon_bucket_name'
len=${#WATCH_DIR}
# verify inotify installed
if [ ! -x "/usr/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}
aws s3 cp --recursive $path s3://$BUCKET$target
}
function delete() {
local path=$1
local file=$2
local target=${path:$len}
aws s3 rm s3://$BUCKET$target$file
}
# 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