Skip to content

Commit 54cebd3

Browse files
committed
ghe-backup-config shellcheck fixes
1 parent 6d17c01 commit 54cebd3

File tree

1 file changed

+63
-45
lines changed

1 file changed

+63
-45
lines changed

share/github-backup-utils/ghe-backup-config

Lines changed: 63 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ log_level() {
4747
else
4848
display="-"
4949
fi
50-
else
50+
else
5151
if [ "$level" = "info" ]; then
5252
display="${GREEN}INFO${NC}"
5353
elif [ "$level" = "warn" ]; then
@@ -89,7 +89,7 @@ log_rsync(){
8989

9090
log_ssh(){
9191
log_level "ssh" "$1"
92-
}
92+
}
9393

9494
# Assume this script lives in share/github-backup-utils/ when setting the root
9595
GHE_BACKUP_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
@@ -150,15 +150,15 @@ export GHE_RESTORE_IN_PROGRESS
150150
export GHE_BACKUP_IN_PROGRESS
151151

152152
ghe_restore_check() {
153-
if [ -h $GHE_RESTORE_IN_PROGRESS ]; then
153+
if [ -h "$GHE_RESTORE_IN_PROGRESS" ]; then
154154
echo " Error: detected a restore already in progress from a previous version of ghe-restore." 1>&2
155155
echo " If there is no restore in progress anymore, please remove" 1>&2
156156
echo " the $GHE_RESTORE_IN_PROGRESS file and try again." 1>&2
157157
exit 1
158158
fi
159159

160-
if [ -f $GHE_RESTORE_IN_PROGRESS ]; then
161-
progress=$(cat $GHE_RESTORE_IN_PROGRESS)
160+
if [ -f "$GHE_RESTORE_IN_PROGRESS" ]; then
161+
progress=$(cat "$GHE_RESTORE_IN_PROGRESS")
162162
pid=$(echo "$progress" | cut -d ' ' -f 2)
163163
echo " Error: A restore of $GHE_HOSTNAME may still be running on PID $pid." 1>&2
164164
echo " If PID $pid is not a process related to the restore utilities, please remove" 1>&2
@@ -168,15 +168,15 @@ ghe_restore_check() {
168168
}
169169

170170
ghe_backup_check() {
171-
if [ -h $GHE_BACKUP_IN_PROGRESS ]; then
171+
if [ -h "$GHE_BACKUP_IN_PROGRESS" ]; then
172172
echo " Error: detected a backup already in progress from a previous version of ghe-backup." 1>&2
173173
echo " If there is no backup in progress anymore, please remove" 1>&2
174174
echo " the $GHE_DATA_DIR/$GHE_BACKUP_IN_PROGRESS file and try again." 1>&2
175175
exit 1
176176
fi
177177

178-
if [ -f $GHE_BACKUP_IN_PROGRESS ]; then
179-
progress=$(cat $GHE_BACKUP_IN_PROGRESS)
178+
if [ -f "$GHE_BACKUP_IN_PROGRESS" ]; then
179+
progress=$(cat "$GHE_BACKUP_IN_PROGRESS")
180180
pid=$(echo "$progress" | cut -d ' ' -f 2)
181181
echo " Error: A backup of $GHE_HOSTNAME may still be running on PID $pid." 1>&2
182182
echo " If PID $pid is not a process related to the backup utilities, please remove" 1>&2
@@ -186,14 +186,14 @@ ghe_backup_check() {
186186
}
187187

188188
ghe_restore_finished() {
189-
if [ -f $GHE_RESTORE_IN_PROGRESS ]; then
190-
rm -f $GHE_RESTORE_IN_PROGRESS
189+
if [ -f "$GHE_RESTORE_IN_PROGRESS" ]; then
190+
rm -f "$GHE_RESTORE_IN_PROGRESS"
191191
fi
192192
}
193193

194194
ghe_backup_finished() {
195-
if [ -f $GHE_BACKUP_IN_PROGRESS ]; then
196-
rm -f $GHE_BACKUP_IN_PROGRESS
195+
if [ -f "$GHE_BACKUP_IN_PROGRESS" ]; then
196+
rm -f "$GHE_BACKUP_IN_PROGRESS"
197197
fi
198198
}
199199

@@ -265,7 +265,7 @@ if [ -n "$GHE_VERBOSE" ]; then
265265
exec 3>>"$GHE_VERBOSE_LOG"
266266
log_info "$calling_script_name $*" 1>&3
267267
else
268-
# colorize the input if supported.
268+
# colorize the input if supported.
269269
display_caller="${BLUE}$calling_script_name${NC}"
270270
exec 3>>"$GHE_VERBOSE_LOG"
271271
log_info "$display_caller $*" 1>&3
@@ -327,11 +327,11 @@ if [ ! -d "$GHE_DATA_DIR" ]; then
327327
fi
328328

329329
# Set some defaults if needed.
330-
: ${GHE_NUM_SNAPSHOTS:=10}
330+
: "${GHE_NUM_SNAPSHOTS:=10}"
331331

332332
# Generate a backup timestamp if one has not already been generated.
333333
# We export the variable so the process group shares the same value.
334-
: ${GHE_SNAPSHOT_TIMESTAMP:=$(date +"%Y%m%dT%H%M%S")}
334+
: "${GHE_SNAPSHOT_TIMESTAMP:=$(date +"%Y%m%dT%H%M%S")}"
335335
export GHE_SNAPSHOT_TIMESTAMP
336336

337337
# Set the current snapshot directory to <data-dir>/<timestamp>. This is where
@@ -341,50 +341,50 @@ export GHE_SNAPSHOT_DIR
341341

342342
# The root filesystem location. This must be used so that tests can override
343343
# the root as a local directory location.
344-
: ${GHE_REMOTE_ROOT_DIR:=""}
344+
: "${GHE_REMOTE_ROOT_DIR:=""}"
345345

346346
# The root location of persistent data and applications on the remote side. This
347347
# is always "/data" for GitHub instances. Use of this variable allows
348348
# the location to be overridden in tests.
349-
: ${GHE_REMOTE_DATA_DIR:="/data"}
349+
: "${GHE_REMOTE_DATA_DIR:="/data"}"
350350

351351
# The root location of user data stores such as git repositories, pages sites,
352352
# elasticsearch indices, etc. This is "/data" under 1.x filesystem layouts and
353353
# "/data/user" under the 2.x filesystem layout. The location is adjusted
354354
# dynamically in ghe_remote_version_config() immediately after obtaining the
355355
# remote version. Utilities that transfer data in and out of the appliance
356356
# should use this variable to ensure proper behavior under different versions.
357-
: ${GHE_REMOTE_DATA_USER_DIR:="$GHE_REMOTE_DATA_DIR"}
357+
: "${GHE_REMOTE_DATA_USER_DIR:="$GHE_REMOTE_DATA_DIR"}"
358358

359359
# The location of the license file on the remote side. This is always
360360
# "/data/enterprise/enterprise.ghl" for GitHub instances. Use of this variable
361361
# allows the location to be overridden in tests.
362-
: ${GHE_REMOTE_LICENSE_FILE:="$GHE_REMOTE_DATA_DIR/enterprise/enterprise.ghl"}
362+
: "${GHE_REMOTE_LICENSE_FILE:="$GHE_REMOTE_DATA_DIR/enterprise/enterprise.ghl"}"
363363

364364
# The number of seconds to wait for in progress git-gc processes to complete
365365
# before starting the sync of git data. See share/github-backup-utils/ghe-backup-repositories-rsync
366366
# for more information. Default: 10 minutes.
367-
: ${GHE_GIT_COOLDOWN_PERIOD:=600}
367+
: "${GHE_GIT_COOLDOWN_PERIOD:=600}"
368368

369369
# Set "true" to get verbose logging of all ssh commands on stderr
370-
: ${GHE_VERBOSE_SSH:=false}
370+
: "${GHE_VERBOSE_SSH:=false}"
371371

372372
# The location of the cluster configuration file on the remote side.
373373
# This is always "/data/user/common/cluster.conf" for GitHub Cluster instances.
374374
# Use of this variable allows the location to be overridden in tests.
375-
: ${GHE_REMOTE_CLUSTER_CONF_FILE:="$GHE_REMOTE_DATA_DIR/user/common/cluster.conf"}
375+
: "${GHE_REMOTE_CLUSTER_CONF_FILE:="$GHE_REMOTE_DATA_DIR/user/common/cluster.conf"}"
376376

377377
# The location of the file used to disable GC operations on the remote side.
378-
: ${SYNC_IN_PROGRESS_FILE:="$GHE_REMOTE_DATA_USER_DIR/repositories/.sync_in_progress"}
378+
: "${SYNC_IN_PROGRESS_FILE:="$GHE_REMOTE_DATA_USER_DIR/repositories/.sync_in_progress"}"
379379

380380
# Base path for temporary directories and files.
381-
: ${TMPDIR:="/tmp"}
381+
: "${TMPDIR:="/tmp"}"
382382

383383
# Backup cadence for MS SQL. Determines the kind of backup taken, either full, differential,
384384
# or transaction log, based on when the last backup of that kind was taken. This defaults to
385385
# taking a full backup once a week, a differential backup once a day, and transaction logs every
386386
# 15 minutes.
387-
: ${GHE_MSSQL_BACKUP_CADENCE:=10080,1440,15}
387+
: "${GHE_MSSQL_BACKUP_CADENCE:=10080,1440,15}"
388388

389389
###############################################################################
390390
### Dynamic remote version config
@@ -404,11 +404,22 @@ ghe_remote_version_config() {
404404
# If we don't have a readlink command, parse ls -l output.
405405
if ! type readlink 1>/dev/null 2>&1; then
406406
readlink() {
407-
if [ -x "$1" ]; then
408-
ls -ld "$1" | sed 's/.*-> //'
409-
else
410-
return 1
411-
fi
407+
local ret=0 f=''
408+
while [ $# -gt 0 ]; do
409+
f="$1"
410+
shift 1
411+
412+
[ ! -e "$f" ] && [ ! -h "$f" ] && {
413+
ret=1
414+
continue
415+
}
416+
417+
# shellcheck disable=SC2012 # In this specific scenario, this method is OK
418+
f="$(ls -ld "$f")"
419+
echo "${f//*-> /}"
420+
done
421+
422+
return $ret
412423
}
413424
fi
414425

@@ -426,10 +437,12 @@ ghe_remote_version_required() {
426437

427438
# override hostname w/ ghe-host-check output because the port could have
428439
# been autodetected to 122.
429-
GHE_HOSTNAME=$(echo "$_out" | sed 's/Connect \(.*:[0-9]*\) OK.*/\1/')
440+
GHE_HOSTNAME="${_out/Connect /}"
441+
GHE_HOSTNAME="${GHE_HOSTNAME/ OK*/}"
430442
export GHE_HOSTNAME
431443

432-
GHE_REMOTE_VERSION=$(echo "$_out" | sed 's/.*(\(.*\))/\1/')
444+
GHE_REMOTE_VERSION="${GHE_HOSTNAME#*\(}"
445+
GHE_REMOTE_VERSION="${GHE_HOSTNAME%%\)*}"
433446
export GHE_REMOTE_VERSION
434447

435448
ghe_parse_remote_version "$GHE_REMOTE_VERSION"
@@ -441,9 +454,11 @@ ghe_remote_version_required() {
441454
# Parse a version string into major, minor and patch parts and echo.
442455
ghe_parse_version() {
443456
local version_major version_minor version_patch
444-
version_major=$(echo "${1#v}" | cut -f 1 -d .)
445-
version_minor=$(echo "$1" | cut -f 2 -d .)
446-
version_patch=$(echo "$1" | cut -f 3 -d .)
457+
version_patch="${1#v}"
458+
version_major="${version_patch%%.*}"
459+
version_patch="${version_patch#*.}"
460+
version_minor="${version_patch%%.*}"
461+
version_patch="${version_patch#*.}"
447462
version_patch=${version_patch%%[a-zA-Z]*}
448463

449464
echo "$version_major $version_minor $version_patch"
@@ -457,7 +472,7 @@ ghe_parse_version() {
457472
# appliance version.
458473
ghe_parse_remote_version() {
459474
# shellcheck disable=SC2046 # Word splitting is required to populate the variables
460-
read -r GHE_VERSION_MAJOR GHE_VERSION_MINOR GHE_VERSION_PATCH <<<$(ghe_parse_version $1)
475+
read -r GHE_VERSION_MAJOR GHE_VERSION_MINOR GHE_VERSION_PATCH < <(ghe_parse_version "$1")
461476
export GHE_VERSION_MAJOR GHE_VERSION_MINOR GHE_VERSION_PATCH
462477
}
463478

@@ -504,15 +519,18 @@ ghe_debug() {
504519
if [ $# -ne 0 ]; then
505520
echo -e "Debug: $*" 1>&3
506521
elif [ -p /dev/stdin ]; then
507-
echo "\n" 1>&3
508-
while read line; do
522+
echo -e "\n" 1>&3
523+
while read -r line; do
509524
echo -e "Debug: $line" 1>&3
510525
done </dev/stdin
511526
fi
512527
}
513528

514529
version() {
515-
echo "${@#v}" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'
530+
local v="${*#v}"
531+
532+
# shellcheck disable=SC2183,SC2086 # We want to glob (SC2086) and expect 4 (fuzzy) params (SC2183)
533+
printf "%d%03d%03d%03d\n" ${v//./ }
516534
}
517535

518536
# The list of gists returned by the source changed in 2.16.23, 2.17.14,
@@ -521,11 +539,11 @@ version() {
521539
# In newer versions, gist paths are unmodified, and only other repo types
522540
# are truncated with `dirname`.
523541
fix_paths_for_ghe_version() {
524-
if [[ "$GHE_REMOTE_VERSION" =~ 2.16. && "$(version $GHE_REMOTE_VERSION)" -ge "$(version 2.16.23)" ]] ||
525-
[[ "$GHE_REMOTE_VERSION" =~ 2.17. && "$(version $GHE_REMOTE_VERSION)" -ge "$(version 2.17.14)" ]] ||
526-
[[ "$GHE_REMOTE_VERSION" =~ 2.18. && "$(version $GHE_REMOTE_VERSION)" -ge "$(version 2.18.8)" ]] ||
527-
[[ "$(version $GHE_REMOTE_VERSION)" -ge "$(version 2.19.3)" ]]; then
528-
GIST_FILTER="-e /gist/b"
542+
if [[ "$GHE_REMOTE_VERSION" =~ 2.16. && "$(version "$GHE_REMOTE_VERSION")" -ge "$(version 2.16.23)" ]] ||
543+
[[ "$GHE_REMOTE_VERSION" =~ 2.17. && "$(version "$GHE_REMOTE_VERSION")" -ge "$(version 2.17.14)" ]] ||
544+
[[ "$GHE_REMOTE_VERSION" =~ 2.18. && "$(version "$GHE_REMOTE_VERSION")" -ge "$(version 2.18.8)" ]] ||
545+
[[ "$(version "$GHE_REMOTE_VERSION")" -ge "$(version 2.19.3)" ]]; then
546+
GIST_FILTER=(-e /gist/b)
529547
else
530548
unset GIST_FILTER
531549
fi
@@ -538,7 +556,7 @@ fix_paths_for_ghe_version() {
538556
# 3. truncate from the final slash (if any) to the end
539557
# If the GIST_FILTER was set above (because we're on a modern version of
540558
# GHES), then don't modify lines with "gist" in them.
541-
sed $GIST_FILTER -e 's/\/$//; s/^[^\/]*$/./; s/\/[^\/]*$//'
559+
sed "${GIST_FILTER[@]}" -e 's/\/$//; s/^[^\/]*$/./; s/\/[^\/]*$//'
542560
}
543561

544562
is_binary_backup_feature_on() {

0 commit comments

Comments
 (0)