@@ -19,7 +19,7 @@ usage() {
19
19
--rsync-option Can be repeated
20
20
21
21
Examples:
22
- $0 --verbse --dry-run rsync://rsync.samba.org/rsyncftp/ $PWD /rsync/
22
+ $0 --verbse --dry-run rsync://rsync.samba.org/rsyncftp/ ${ PWD} /rsync/
23
23
$0 --rsync-option '--bwlimit=5m' rsync://foo.bar/archlinux /srv/mirror/archlinux
24
24
$0 rsync://foo.bar/archlinux /srv/mirror/archlinux --last-update-url=http://foo.bar/archlinux/lastupdate
25
25
USAGE
@@ -38,18 +38,18 @@ rsync_cmd() {
38
38
cmd+=(--delete-delay --delay-updates " --temp-dir=${temp_dir} " )
39
39
cmd+=(" --contimeout=60" " --timeout=600" )
40
40
41
- test -z " $verbose " || cmd+=(--verbose --progress --stats)
41
+ [[ -z " ${ verbose} " ]] || cmd+=(--verbose --progress --stats)
42
42
43
43
cmd=(" ${cmd[@]} " " ${rsync_options[@]} " )
44
- cmd+=(" $source " )
44
+ cmd+=(" ${ source} " )
45
45
46
- test -z " $destination " || cmd+=(" $destination " )
46
+ [[ -z " ${ destination} " ]] || cmd+=(" ${ destination} " )
47
47
48
48
verbose " About to run:" " ${cmd[@]} "
49
49
verbose " "
50
50
51
51
set +e
52
- if [ " $ verbose" ]; then
52
+ if [[ -n " ${ verbose} " ] ]; then
53
53
" ${cmd[@]} "
54
54
exit_status=$?
55
55
else
@@ -59,21 +59,21 @@ rsync_cmd() {
59
59
set -e
60
60
61
61
verbose " "
62
- verbose " rsync exited with $exit_status "
62
+ verbose " rsync exited with ${ exit_status} "
63
63
64
- if [[ -z " $verbose " && $exit_status -ne 0 && $exit_status -ne 23 && $exit_status -ne 24 ]]; then
65
- echo " $output "
64
+ if [[ -z " ${ verbose} " && ${ exit_status} -ne 0 && ${ exit_status} -ne 23 && ${ exit_status} -ne 24 ]]; then
65
+ echo " ${ output} "
66
66
fi
67
67
68
- # Treat partial transfers as non-error
69
- [ $ exit_status -ne 23 ] || exit_status=0
70
- [ $ exit_status -ne 24 ] || exit_status=0
68
+ # Treat partial transfers as non-errors
69
+ [[ " ${ exit_status} " -ne 23 ] ] || exit_status=0
70
+ [[ " ${ exit_status} " -ne 24 ] ] || exit_status=0
71
71
72
- return $ exit_status
72
+ return " ${ exit_status} "
73
73
}
74
74
75
75
verbose () {
76
- test -z " $verbose " || warn " $@ "
76
+ [[ -z " ${ verbose} " ]] || warn " $@ "
77
77
}
78
78
79
79
warn () {
@@ -98,7 +98,7 @@ declare -a rsync_options
98
98
99
99
# PARSE OPTIONS
100
100
101
- [ $# -ge 1 ] || usage
101
+ [[ $# -ge 1 ] ] || usage
102
102
103
103
while true ; do
104
104
case ${1:- } in
@@ -120,19 +120,19 @@ while true; do
120
120
esac
121
121
done
122
122
123
- id=$( echo " $destination_path " | tr ' /' ' -' )
123
+ id=$( echo " ${ destination_path} " | tr ' /' ' -' )
124
124
id=${id# -} # trim prefix
125
125
id=${id% -} # trim suffix
126
- [ " $id " ] || id=" temp"
126
+ [[ -n " ${id} " ] ] || id=" temp"
127
127
128
128
# Directory where files are downloaded to before being moved in place.
129
129
# This should be on the same filesystem as $destination_path, but not a
130
130
# subdirectory of $destination_path.
131
131
temp_dir=" ${temporary_directory} /msync-${id} "
132
132
lock_file=" ${temporary_directory} /msync-${id} .lck"
133
- mkdir -p " $( dirname $ lock_file) "
133
+ mkdir -p " $( dirname " ${ lock_file} " ) "
134
134
135
- if [[ " $ last_update_sync" && -z " $last_update_url " ]]; then
135
+ if [[ -n " ${ last_update_sync} " && -z " ${ last_update_url} " ]]; then
136
136
warn " --last-update-sync requires --last-update-url to be set."
137
137
exit 1
138
138
fi
@@ -144,33 +144,34 @@ verbose "Verbose mode is on"
144
144
exec 9> " ${lock_file} "
145
145
flock -n 9 || { verbose " ${lock_file} present, exiting." ; exit 0; }
146
146
147
+ # shellcheck disable=SC2317
147
148
exit_handler () {
148
- [ $ SECONDS -le " $warning_timeout " ] || warn " msync took $SECONDS s"
149
+ [[ " ${ SECONDS} " -le " ${ warning_timeout} " ]] || warn " msync took ${ SECONDS} s"
149
150
150
- [ ! -d $ temp_dir ] || rmdir $ temp_dir
151
- rm $ lock_file
151
+ [[ ! -d " ${ temp_dir} " ]] || rmdir " ${ temp_dir} "
152
+ rm " ${ lock_file} "
152
153
verbose " msync cleaned up successfully, bye."
153
154
}
154
155
155
156
trap exit_handler EXIT
156
157
157
- [ -z " $destination_path " ] || [ -d " ${destination_path} " ] || mkdir -p " ${destination_path} "
158
- [ -d " ${temp_dir} " ] || mkdir -p " ${temp_dir} "
158
+ [[ -z " ${ destination_path} " ]] || [[ -d " ${destination_path} " ] ] || mkdir -p " ${destination_path} "
159
+ [[ -d " ${temp_dir} " ] ] || mkdir -p " ${temp_dir} "
159
160
160
161
tty -s || {
161
162
delay=$(( RANDOM % random_delay))
162
163
warning_timeout=$(( warning_timeout + delay))
163
- sleep $ delay
164
+ sleep " ${ delay} "
164
165
}
165
166
166
- if [ " ${skip_connection_check} " ]; then
167
+ if [[ -n " ${skip_connection_check} " ] ]; then
167
168
rsync_uri=${rsync_uris[0]}
168
169
verbose " Skipping connection check and using ${rsync_uri} "
169
170
else
170
171
rsync_uri=" "
171
172
for uri in " ${rsync_uris[@]} " ; do
172
- if rsync --no-motd --contimeout=10 --timeout=30 " $uri " > /dev/null; then
173
- rsync_uri=$uri
173
+ if rsync --no-motd --contimeout=10 --timeout=30 " ${ uri} " > /dev/null; then
174
+ rsync_uri=${ uri}
174
175
verbose " ${uri} is up, using it as upstream"
175
176
break
176
177
else
@@ -179,18 +180,18 @@ else
179
180
done
180
181
fi
181
182
182
- if [ -z " $rsync_uri " ]; then
183
+ if [[ -z " ${ rsync_uri} " ] ]; then
183
184
warn " Could not find any valid upstreams, aborting."
184
185
exit 1
185
186
fi
186
187
187
- if [ " $ last_update_url" ]; then
188
- last_update_file=" $destination_path /${last_update_url##*/ } "
188
+ if [[ -n " ${ last_update_url} " ] ]; then
189
+ last_update_file=" ${ destination_path} /${last_update_url##*/ } "
189
190
190
- if [ -f " $last_update_file " ]; then
191
+ if [[ -f " ${ last_update_file} " ] ]; then
191
192
verbose " Checking whether ${last_update_url} is different than ${last_update_file} "
192
193
193
- if diff -b <( curl -Ls " $last_update_url " ) " $last_update_file " > /dev/null; then
194
+ if diff -b <( curl -Ls " ${ last_update_url} " || true ) " ${ last_update_file} " > /dev/null; then
194
195
verbose " Files are identical, doing partial sync"
195
196
verbose " Syncing ${rsync_uri}${last_update_sync} "
196
197
@@ -200,23 +201,23 @@ if [ "$last_update_url" ]; then
200
201
verbose " Files are different, doing full sync"
201
202
fi
202
203
else
203
- verbose " $last_update_file missing, doing full sync"
204
+ verbose " ${ last_update_file} missing, doing full sync"
204
205
fi
205
206
else
206
207
verbose " --last-update-url unset, doing full sync"
207
208
fi
208
209
209
210
set +e
210
- if [ " $ verbose" ]; then
211
+ if [[ -n " ${ verbose} " ] ]; then
211
212
rsync_cmd " ${rsync_uri} " " ${destination_path} "
212
213
else
213
214
output=$( rsync_cmd " ${rsync_uri} " " ${destination_path} " )
214
215
fi
215
216
exit_status=$?
216
217
set -e
217
218
218
- if [[ -z " $verbose " && $exit_status -ne 0 ]]; then
219
- warn " $output "
219
+ if [[ -z " ${ verbose} " && ${ exit_status} -ne 0 ]]; then
220
+ warn " ${ output} "
220
221
fi
221
222
222
- exit $ exit_status
223
+ exit " ${ exit_status} "
0 commit comments