-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsync.sh
More file actions
80 lines (64 loc) · 1.94 KB
/
rsync.sh
File metadata and controls
80 lines (64 loc) · 1.94 KB
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
#!/bin/bash
# Allow multiple return codes for rsync if specified...
IFS=',' read -r -a allowed_codes <<< "$ALLOWED_RETURN_CODES"
run_rsync() {
local args=()
local paths=()
for arg in "$@"; do
if [[ "$arg" == -* ]]; then
args+=("$arg") # Add options like -avz, --delete, etc.
else
paths+=("$arg") # Add paths where globbing is allowed
fi
done
# echo rsync "$@"
set -x
eval "${IONICE_CONFIG} rsync ${args[*]} ${paths[*]}"
local rsync_return_code=$?
set +x
# Check if the exit code exists in the allowed list
for code in "${allowed_codes[@]}"; do
if [[ $rsync_return_code -eq $code ]]; then
return 0
fi
done
# If the exit code is not in the list, exit with the actual exit code
return $rsync_return_code
}
# if there is no recurring watch set, then we just run normal rsync (with exit code manipulation)
if [ -z "$RECURRING_WATCH" ]; then
run_rsync "$@"
exit $?
fi
# support multiple files/paths for recurring watch
IFS=':' read -r -a recurring_watch_paths <<< "$RECURRING_WATCH"
# keep variable for whether termination was requested
TERM_CALL="0"
if [ -z "$INOTIFY_EVENTS" ]; then
INOTIFY_EVENTS="modify,create,delete,move"
fi
handle_term_call() {
echo "Received SIGINT/SIGTERM Signal"
TERM_CALL="1"
}
reconf_traps() {
# function to configure traps; probably doesn't need to be function but for historical reasons, it is...
trap handle_term_call SIGINT
trap handle_term_call SIGTERM
}
reconf_traps
# while we didn't receive a termination signal...
while [ "$TERM_CALL" != "1" ]; do
if [ "$INOTIFY_EVENTS" != "-" ]; then
# wait for change(s) in the files/directory (create, delete, modify, move)
inotifywait -r -e "$INOTIFY_EVENTS" "${recurring_watch_paths[@]}"
fi
# run rsync on changes
run_rsync "$@"
ret_code=$?
if [ "$TERM_CALL" == "1" ]; then
exit $ret_code
elif [ -n "$SLEEP_DELAY" ]; then
sleep "$SLEEP_DELAY"
fi
done