-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathwatch
executable file
·93 lines (75 loc) · 1.6 KB
/
watch
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
#!/bin/bash
[[ "$TRACE" ]] && set -x
function usage {
cat <<-EOF
Usage: $PROGNAME [OPTIONS] <local-path> <remote-path>
Sync s3 directory locally and backup changed files on exit
--force-restore restore even if local directory is not empty
eg: $PROGNAME /data s3://bucket/dir
EOF
}
function error_exit {
echo "${1:-"Unknown Error"}" 1>&2
exit 1
}
PARSED_OPTIONS=$(getopt -n "$0" -o f --long "force-restore" -- "$@")
if [ $? -ne 0 ]; then
exit 1
fi
eval set -- "$PARSED_OPTIONS"
while true; do
case "$1" in
-f|--force-restore)
FORCE_RESTORE="true"
shift;;
--)
shift
break;;
esac
done
PROGNAME=$0
LOCAL=$1
REMOTE=$2
if [ "$ENDPOINT_URL" ]; then
AWS="aws --endpoint-url $ENDPOINT_URL"
else
AWS=aws
fi
function restore {
if [ "$(ls -A $LOCAL)" ]; then
if [[ ${FORCE_RESTORE:false} == 'true' ]]; then
error_exit "local directory is not empty"
fi
fi
echo "restoring $REMOTE => $LOCAL"
if ! $AWS s3 sync "$REMOTE" "$LOCAL"; then
error_exit "restore failed"
fi
}
function backup {
echo "backup $LOCAL => $REMOTE"
if ! $AWS s3 sync "$LOCAL" "$REMOTE" $S3_SYNC_FLAGS; then
echo "backup failed" 1>&2
return 1
fi
}
function final_backup {
echo "backup $LOCAL => $REMOTE"
while ! $AWS s3 sync "$LOCAL" "$REMOTE" $S3_SYNC_FLAGS; do
echo "backup failed, will retry" 1>&2
sleep 1
done
exit 0
}
function idle {
echo "ready"
while true; do
sleep ${BACKUP_INTERVAL:-42} &
wait $!
[ -n "$BACKUP_INTERVAL" ] && backup
done
}
restore
trap final_backup SIGHUP SIGINT SIGTERM
trap "backup; idle" USR1
idle