-
Notifications
You must be signed in to change notification settings - Fork 0
/
datalayer-s3-sync
106 lines (89 loc) · 2.91 KB
/
datalayer-s3-sync
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash
# (c) Wolfgang Ziegler // fago
# Modified by Zachary Brown, Chia Networks Inc
#
# Original Script: https://gist.github.com/fago/9608238
#
# Inotify script to trigger a command on file changes.
#
# The script triggers the command as soon as a file event occurs. Events
# occurring during command execution are aggregated and trigger a single command
# execution only.
#
# Paths are hard-coded in the "Configuration" section. Edit the "COMMAND" with
# the local path and the S3 bucket name you wish to use. Also update the
# "WATCHDIR" to be the directory to watch to trigger the syncing action.
#
# Copy this file to /usr/local/bin/datalayer-s3-sync for best results with the
# systemd init file provided.
#
# Usage example: Trigger rsync for synchronizing file changes.
# /usr/local/bin/datalayer-s3-sync
######### Configuration #########
EVENTS="CREATE,CLOSE_WRITE,DELETE,MODIFY,MOVED_FROM,MOVED_TO"
COMMAND="/usr/local/bin/aws s3 sync --delete /home/zachary/.chia/mainnet/data_layer/db/server_files_location_testnet10 s3://mydatalayer1"
WATCHDIR="/home/zachary/.chia/mainnet/data_layer/db/server_files_location_testnet10"
## Exclude Git and temporary files from watching.
EXCLUDE='(\.git|\.swp|\.swx)'
## Whether to enable verbosity. If enabled, change events are output.
VERBOSE=1
##################################
#if [ -z "$1" ]; then
# echo "Usage: $0 Command"
# exit 1;
#fi
##
## Setup pipes. For usage with read we need to assign them to file descriptors.
##
RUN=$(mktemp -u)
mkfifo "$RUN"
exec 3<>$RUN
RESULT=$(mktemp -u)
mkfifo "$RESULT"
exec 4<>$RESULT
clean_up () {
## Cleanup pipes.
rm $RUN
rm $RESULT
}
## Execute "clean_up" on exit.
trap "clean_up" EXIT
##
## Run inotifywait in a loop that is not blocked on command execution and ignore
## irrelevant events.
##
inotifywait -m -q -r -e $EVENTS --exclude $EXCLUDE --format '%w%f' $WATCHDIR | \
while read FILE
do
if [ $VERBOSE -ne 0 ]; then
echo [CHANGE] $FILE
fi
## Clear $PID if the last command has finished.
if [ ! -z "$PID" ] && ( ! ps -p $PID > /dev/null ); then
PID=""
fi
## If no command is being executed, execute one.
## Else, wait for the command to finish and then execute again.
if [ -z "$PID" ]; then
## Execute the following as background process.
## It runs the command once and repeats if we tell him so.
($COMMAND; while read -t0.001 -u3 LINE; do
echo running >&4
$COMMAND
done)&
PID=$!
WAITING=0
else
## If a previous waiting command has been executed, reset the variable.
if [ $WAITING -eq 1 ] && read -t0.001 -u4; then
WAITING=0
fi
## Tell the subprocess to execute the command again if it is not waiting
## for repeated execution already.
if [ $WAITING -eq 0 ]; then
echo "run" >&3
WAITING=1
fi
## If we are already waiting, there is nothing todo.
fi
done