-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsync.sh
executable file
·160 lines (130 loc) · 3.71 KB
/
rsync.sh
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/sh
# Stubborn rsync - Runs a rsync stubbornly, with auto-retry and auto-kill
# By Roy Curtis, licensed under MIT, 2017
# #########
# CONFIGURATION:
# #########
# Debug this script; set to true with --debug
DEBUG=0
# How long to sleep for between retries; override with --delay x
DELAY=5s
# What port to use for rsync ssh; override with --port x
PORT=22
# How many times to retry a failed rsync; override with --retries x
RETRIES=20
# Maximum seconds rsync will wait on connection or I/O; override with --rsyncTimeout x
RSYNC_TIMEOUT=10
# Maximum time rsync can run before getting killed; override with --totalTimeout x
TOTAL_TIMEOUT=1h
# #########
# CONSTANTS:
# #########
# Standard "make sure we're in script's directory" boilerplate
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $DIR
# #########
# FUNCTIONS:
# #########
function helpAndExit
{
echo "Usage: $0 [options] source destination"
echo
echo "Available options (with their defaults):"
echo " --debug If set, prints out all configured options"
echo " --delay x Sets sleep delay between retries to x (5s)"
echo " --port x Sets rsync's target SSH port to x (22)"
echo " --retries x Sets amount of retries to x (10)"
echo " --rsyncTimeout x Sets rsync's connection and I/O timeout to x seconds (5)"
echo " --totalTimeout x Sets total rsync run time before kill to x (1h)"
exit $1
}
# #########
# ARGUMENTS:
# #########
# https://stackoverflow.com/a/30830291
while true; do
case "$1" in
--help)
echo "*** Stubborn rsync, by Roy Curtis"
helpAndExit 0
;;
--debug)
DEBUG=1
shift 1
;;
--delay)
DELAY="$2"
shift 2
;;
--port)
PORT="$2"
shift 2
;;
--retries)
RETRIES="$2"
shift 2
;;
--rsyncTimeout)
RSYNC_TIMEOUT="$2"
shift 2
;;
--totalTimeout)
TOTAL_TIMEOUT="$2"
shift 2
;;
-*)
echo "!!! Unknown option: $1" >&2
helpAndExit 1
;;
*)
if [ -z "$1" ]; then
echo "!!! Source path is not set"
helpAndExit 1
fi
if [ -z "$2" ]; then
echo "!!! Destination path is not set"
helpAndExit 1
fi
SOURCE_ARG=$1
TARGET_ARG=$2
break
;;
esac
done
# #########
# SCRIPT:
# #########
# Debug
if [ "$DEBUG" -eq "1" ]; then
echo "*** DEBUG ***"
echo "SOURCE_ARG = $SOURCE_ARG"
echo "TARGET_ARG = $TARGET_ARG"
echo "DELAY = $DELAY"
echo "PORT = $PORT"
echo "RETRIES = $RETRIES"
echo "RSYNC_TIMEOUT = $RSYNC_TIMEOUT"
echo "TOTAL_TIMEOUT = $TOTAL_TIMEOUT"
fi
# Begin loop
ATTEMPTS=1
while [ $ATTEMPTS -le $RETRIES ]; do
echo "*** Attempt $ATTEMPTS out of $RETRIES..."
# https://serverfault.com/a/460536
timeout --foreground -s 9 $TOTAL_TIMEOUT \
rsync -varzi --append-verify \
--out-format='[%t] [%i] (Last Modified: %M) (bytes: %-10l) %-100n' \
-e "ssh -p $PORT" \
--timeout=$RSYNC_TIMEOUT \
$SOURCE_ARG $TARGET_ARG
if [ "$?" -eq "0" ]; then
echo "*** rsync to $TARGET_ARG successful!"
exit 0
elif [ "$?" -eq "124" ]; then
echo "!!! rsync appears to have hung, and has been killed!"
else
echo "!!! rsync appears to have crashed or timed out!"
fi
echo "*** Retrying after delay of $DELAY..."
sleep $DELAY
let ATTEMPTS=ATTEMPTS+1
done