-
Notifications
You must be signed in to change notification settings - Fork 3
/
rescp
executable file
·66 lines (53 loc) · 1.59 KB
/
rescp
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
#!/usr/bin/env bash
################################################################################
# rescp - retries failed scp operations. This script is useful in situations
# where the remote host may stop responding temporarily or over flaky
# connections.
#
# Version 1
# Matthew Malensek <[email protected]>
################################################################################
retries=32
sleep_time=1
print_usage() {
progname=$(basename ${0})
cat <<EOM
Usage: ${progname} [-n num_retries] [-s sleep_time] <SCP_ARGS>
-n num_retries: Specifies the number of times ${progname} should retry the
scp operation.
-s sleep_time: Sets the delay between retries.
NOTE: The arguments for ${progname} **MUST** come before scp arguments for
correct parsing.
Examples:
rescp -n 1024 -s10 -r ~/somedir somehost:storagedir/
(retry 1024 times, wait 10 seconds between retries, recursive copy)
rescp -n 3 -s0 -C myfile.txt somewhere.net:
(retry 3 times, don't wait between retries, use compression)
EOM
}
ignored=0
OPTERR=0
while getopts "n:s:" flag; do
case ${flag} in
n) retries="${OPTARG}" ;;
s) sleep_time="${OPTARG}" ;;
*) (( ignored++ ));;
esac
done
argidx=$(( $OPTIND - 1 - $ignored ))
if [[ ${#} -lt 2 || ${argidx} -lt 0 ]]; then
print_usage
exit 1
fi
shift "${argidx}"
for (( i = 0; i < "${retries}"; ++i )); do
scp ${@}
return="${?}"
if [[ ${return} -eq 0 ]]; then
exit ${return}
else
sleep "${sleep_time}"
fi
done
echo "Maximum number of retries exhausted. Exiting."
exit ${return}