Skip to content

Commit d1304ea

Browse files
committed
ghe-backup and ghe-restore shellcheck fixes
1 parent 6e2184a commit d1304ea

File tree

3 files changed

+39
-37
lines changed

3 files changed

+39
-37
lines changed

bin/ghe-backup

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ if ! output=$(rsync -a src/ dest1 2>&1 && rsync -av src/ --link-dest=../dest1 de
8080
exit 1
8181
fi
8282

83-
if [ "$(ls -il dest1/testfile | awk '{ print $1 }')" != "$(ls -il dest2/testfile | awk '{ print $1 }')" ]; then
83+
if [ "$(stat -c %i dest1/testfile)" != "$(stat -c %i dest2/testfile)" ]; then
8484
echo "Error: the filesystem containing $GHE_DATA_DIR does not support hard links." 1>&2
8585
echo "Backup Utilities use hard links to store backup data efficiently." 1>&2
8686
exit 1
@@ -101,13 +101,13 @@ cleanup () {
101101
progress=$(cat ../in-progress)
102102
snapshot=$(echo "$progress" | cut -d ' ' -f 1)
103103
pid=$(echo "$progress" | cut -d ' ' -f 2)
104-
if [ "$snapshot" = "$GHE_SNAPSHOT_TIMESTAMP" ] && [ "$$" = $pid ]; then
104+
if [ "$snapshot" = "$GHE_SNAPSHOT_TIMESTAMP" ] && [ "$$" = "$pid" ]; then
105105
unlink ../in-progress
106106
fi
107107
fi
108108

109109
rm -rf "$failures_file"
110-
rm -f ${GHE_DATA_DIR}/in-progress-backup
110+
rm -f "${GHE_DATA_DIR}/in-progress-backup"
111111

112112
# Cleanup SSH multiplexing
113113
ghe-ssh --clean
@@ -147,10 +147,10 @@ if [ -f ../in-progress ]; then
147147
fi
148148

149149
echo "$GHE_SNAPSHOT_TIMESTAMP $$" > ../in-progress
150-
echo "$GHE_SNAPSHOT_TIMESTAMP $$" > ${GHE_DATA_DIR}/in-progress-backup
150+
echo "$GHE_SNAPSHOT_TIMESTAMP $$" > "${GHE_DATA_DIR}/in-progress-backup"
151151

152152
START_TIME=$(date +%s)
153-
echo 'Start time:' $START_TIME
153+
echo "Start time: $START_TIME"
154154
echo "Starting backup of $GHE_HOSTNAME with backup-utils v$BACKUP_UTILS_VERSION in snapshot $GHE_SNAPSHOT_TIMESTAMP"
155155

156156
# Perform a host connection check and establish the remote appliance version.
@@ -238,7 +238,7 @@ if [ "$GHE_BACKUP_STRATEGY" = "rsync" ]; then
238238
fi
239239

240240
if [ "$GHE_PARALLEL_ENABLED" = "yes" ]; then
241-
$GHE_PARALLEL_COMMAND $GHE_PARALLEL_COMMAND_OPTIONS -- "${commands[@]}"
241+
"$GHE_PARALLEL_COMMAND" "${GHE_PARALLEL_COMMAND_OPTIONS[@]}" -- "${commands[@]}"
242242
else
243243
for c in "${commands[@]}"; do
244244
eval "$c"
@@ -251,7 +251,7 @@ fi
251251

252252
# git fsck repositories after the backup
253253
if [ "$GHE_BACKUP_FSCK" = "yes" ]; then
254-
ghe-backup-fsck $GHE_SNAPSHOT_DIR || failures="$failures fsck"
254+
ghe-backup-fsck "$GHE_SNAPSHOT_DIR" || failures="$failures fsck"
255255
fi
256256

257257
# If everything was successful, mark the snapshot as complete, update the
@@ -267,16 +267,16 @@ if [ -z "$failures" ]; then
267267
fi
268268

269269
END_TIME=$(date +%s)
270-
echo 'End time:' $END_TIME
271-
echo 'Runtime:' $(($END_TIME - $START_TIME)) 'seconds'
270+
echo "End time: $END_TIME"
271+
echo "Runtime: $((END_TIME - START_TIME)) seconds"
272272

273273
echo "Completed backup of $GHE_HOSTNAME in snapshot $GHE_SNAPSHOT_TIMESTAMP at $(date +"%H:%M:%S")"
274274

275275
# Exit non-zero and list the steps that failed.
276276
if [ -z "$failures" ]; then
277277
ghe_remote_logger "Completed backup from $(hostname) / snapshot $GHE_SNAPSHOT_TIMESTAMP successfully."
278278
else
279-
steps="$(echo $failures | sed 's/ /, /g')"
279+
steps="${failures// /, }"
280280
ghe_remote_logger "Completed backup from $(hostname) / snapshot $GHE_SNAPSHOT_TIMESTAMP with failures: ${steps}."
281281
echo "Error: Snapshot incomplete. Some steps failed: ${steps}. "
282282
exit 1

bin/ghe-restore

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232
set -e
3333

3434
# Parse arguments
35-
: ${RESTORE_SETTINGS:=false}
35+
: "${RESTORE_SETTINGS:=false}"
3636
export RESTORE_SETTINGS
3737

38-
: ${FORCE:=false}
38+
: "${FORCE:=false}"
3939
export FORCE
4040

41-
: ${SKIP_MYSQL:=false}
41+
: "${SKIP_MYSQL:=false}"
4242
export SKIP_MYSQL
4343

4444
while true; do
@@ -123,7 +123,7 @@ cleanup () {
123123
# Cleanup SSH multiplexing
124124
ghe-ssh --clean
125125
# Remove in-progress file
126-
rm -f ${GHE_DATA_DIR}/in-progress-restore
126+
rm -f "${GHE_DATA_DIR}/in-progress-restore"
127127
}
128128

129129
# This function's type definition is being passed to a remote host via `ghe-ssh` but is not used locally.
@@ -135,16 +135,16 @@ cleanup_cluster_nodes() {
135135
exit 2
136136
fi
137137

138-
ghe-spokes server evacuate git-server-$uuid 'Removing replica'
139-
ghe-spokes server destroy git-server-$uuid
138+
ghe-spokes server evacuate "git-server-$uuid" 'Removing replica'
139+
ghe-spokes server destroy "git-server-$uuid"
140140

141-
ghe-storage destroy-host storage-server-$uuid --force
141+
ghe-storage destroy-host "storage-server-$uuid" --force
142142

143-
ghe-dpages offline pages-server-$uuid
144-
ghe-dpages remove pages-server-$uuid
143+
ghe-dpages offline "pages-server-$uuid"
144+
ghe-dpages remove "pages-server-$uuid"
145145

146-
ghe-redis-cli del resque:queue:maint_git-server-$uuid
147-
ghe-redis-cli srem resque:queues maint_git-server-$uuid
146+
ghe-redis-cli del "resque:queue:maint_git-server-$uuid"
147+
ghe-redis-cli srem resque:queues "maint_git-server-$uuid"
148148
}
149149

150150
# Bring in the backup configuration
@@ -257,11 +257,11 @@ fi
257257

258258
# Log restore start message locally and in /var/log/syslog on remote instance
259259
START_TIME=$(date +%s)
260-
echo 'Start time:' $START_TIME
260+
echo "Start time: $START_TIME"
261261
echo "Starting restore of $GHE_HOSTNAME with backup-utils v$BACKUP_UTILS_VERSION from snapshot $GHE_RESTORE_SNAPSHOT"
262262
ghe_remote_logger "Starting restore from $(hostname) with backup-utils v$BACKUP_UTILS_VERSION / snapshot $GHE_RESTORE_SNAPSHOT ..."
263263
# Create an in-progress-restore file to prevent simultaneous backup or restore runs
264-
echo "${START_TIME} $$" > ${GHE_DATA_DIR}/in-progress-restore
264+
echo "${START_TIME} $$" > "${GHE_DATA_DIR}/in-progress-restore"
265265

266266
# Keep other processes on the VM or cluster in the loop about the restore status.
267267
#
@@ -297,7 +297,7 @@ RELEASE_VERSION=$(ghe-ssh "$GHE_HOSTNAME" -- 'ghe-config --get core.package-vers
297297

298298
# If the backup being restored is from an appliance with Actions disabled, restoring it onto an appliance with Actions enabled will cause
299299
# mismatches in the secrets needed for Actions which ultimately results in Actions not working properly. Note: xargs is to remove whitespace
300-
ACTIONS_ENABLED_IN_BACKUP=$(git config -f $GHE_RESTORE_SNAPSHOT_PATH/settings.json --bool app.actions.enabled | xargs)
300+
ACTIONS_ENABLED_IN_BACKUP=$(git config -f "$GHE_RESTORE_SNAPSHOT_PATH/settings.json" --bool app.actions.enabled | xargs)
301301
if [[ $ACTIONS_ENABLED_IN_BACKUP != true ]] && ghe-ssh "$GHE_HOSTNAME" -- 'ghe-config --true app.actions.enabled'; then
302302
echo "Error: Restoring a backup with Actions disabled onto an appliance with Actions enabled is not supported." >&2
303303
exit 1
@@ -388,11 +388,10 @@ fi
388388
# Restore UUID if present and not restoring to cluster.
389389
if [ -s "$GHE_RESTORE_SNAPSHOT_PATH/uuid" ] && ! $CLUSTER; then
390390
echo "Restoring UUID ..."
391-
cat "$GHE_RESTORE_SNAPSHOT_PATH/uuid" |
392-
ghe-ssh "$GHE_HOSTNAME" -- "sudo sponge '$GHE_REMOTE_DATA_USER_DIR/common/uuid' 2>/dev/null"
391+
ghe-ssh "$GHE_HOSTNAME" -- "sudo sponge '$GHE_REMOTE_DATA_USER_DIR/common/uuid' 2>/dev/null" <"$GHE_RESTORE_SNAPSHOT_PATH/uuid"
393392
ghe-ssh "$GHE_HOSTNAME" -- "sudo systemctl stop consul" || true
394393
ghe-ssh "$GHE_HOSTNAME" -- "sudo rm -rf /data/user/consul/raft"
395-
fi
394+
fi
396395

397396
if is_external_database_snapshot; then
398397
appliance_strategy="external"
@@ -474,13 +473,13 @@ if ! $CLUSTER && [ -d "$GHE_RESTORE_SNAPSHOT_PATH/elasticsearch" ]; then
474473
fi
475474

476475
# Restore the audit log migration sentinel file, if it exists in the snapshot
477-
if test -f $GHE_RESTORE_SNAPSHOT_PATH/es-scan-complete; then
476+
if test -f "$GHE_RESTORE_SNAPSHOT_PATH/es-scan-complete"; then
478477
ghe-ssh "$GHE_HOSTNAME" -- "sudo touch $GHE_REMOTE_DATA_USER_DIR/common/es-scan-complete"
479478
fi
480479

481480
# Restore exported audit logs to 2.12.9 and newer single nodes and
482481
# all releases of cluster
483-
if $CLUSTER || [ "$(version $GHE_REMOTE_VERSION)" -ge "$(version 2.12.9)" ]; then
482+
if $CLUSTER || [ "$(version "$GHE_REMOTE_VERSION")" -ge "$(version 2.12.9)" ]; then
484483
if [[ "$GHE_RESTORE_SKIP_AUDIT_LOGS" = "yes" ]]; then
485484
echo "Skipping restore of audit logs."
486485
else
@@ -492,7 +491,7 @@ if $CLUSTER || [ "$(version $GHE_REMOTE_VERSION)" -ge "$(version 2.12.9)" ]; the
492491
fi
493492

494493
if [ "$GHE_PARALLEL_ENABLED" = "yes" ]; then
495-
$GHE_PARALLEL_COMMAND $GHE_PARALLEL_COMMAND_OPTIONS -- "${commands[@]}"
494+
"$GHE_PARALLEL_COMMAND" "${GHE_PARALLEL_COMMAND_OPTIONS[@]}" -- "${commands[@]}"
496495
else
497496
for c in "${commands[@]}"; do
498497
eval "$c"
@@ -545,7 +544,7 @@ CRON_RUNNING=true
545544

546545
# Clean up all stale replicas on configured instances.
547546
if ! $CLUSTER && $instance_configured; then
548-
restored_uuid=$(cat $GHE_RESTORE_SNAPSHOT_PATH/uuid)
547+
restored_uuid=$(cat "$GHE_RESTORE_SNAPSHOT_PATH/uuid")
549548
other_nodes=$(echo "
550549
set -o pipefail; \
551550
ghe-spokes server show --json \
@@ -585,8 +584,8 @@ else
585584
fi
586585

587586
END_TIME=$(date +%s)
588-
echo 'End time:' $END_TIME
589-
echo 'Runtime:' $(($END_TIME - $START_TIME)) 'seconds'
587+
echo "End time: $END_TIME"
588+
echo "Runtime: $((END_TIME - START_TIME)) seconds"
590589

591590
echo "Restore of $GHE_HOSTNAME from snapshot $GHE_RESTORE_SNAPSHOT finished."
592591
ghe_restore_finished

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ if [ -n "$GHE_SHOW_VERSION" ]; then
2727
fi
2828

2929
# Check for "--help|-h" in args or GHE_SHOW_HELP=true and show usage
30-
# shellcheck disable=SC2120 # the script name is always referenced
30+
# shellcheck disable=SC2120 # Our arguments are optional and not meant to be the owning script's
3131
print_usage() {
3232
grep '^#/' <"$0" | cut -c 4-
3333
exit "${1:-1}"
@@ -121,6 +121,8 @@ ghe_backup_finished() {
121121
}
122122

123123
ghe_parallel_check() {
124+
GHE_PARALLEL_COMMAND_OPTIONS=()
125+
124126
if [ "$GHE_PARALLEL_ENABLED" != "yes" ]; then
125127
return 0
126128
fi
@@ -142,13 +144,13 @@ ghe_parallel_check() {
142144
done
143145

144146
# Check that the GHE_PARALLEL_COMMAND is pointing to moreutils parallel
145-
if ! $GHE_PARALLEL_COMMAND -h | grep -q "parallel \[OPTIONS\] command -- arguments"; then
147+
if ! "$GHE_PARALLEL_COMMAND" -h | grep -q "parallel \[OPTIONS\] command -- arguments"; then
146148
echo "Error: moreutils not found. Please install https://joeyh.name/code/moreutils" 1>&2
147149
exit 1
148150
fi
149151

150152
if [ -n "$GHE_PARALLEL_MAX_JOBS" ]; then
151-
GHE_PARALLEL_COMMAND_OPTIONS="-j $GHE_PARALLEL_MAX_JOBS"
153+
GHE_PARALLEL_COMMAND_OPTIONS+=(-j "$GHE_PARALLEL_MAX_JOBS")
152154
# Default to the number of max rsync jobs to the same as GHE_PARALLEL_MAX_JOBS, if not set.
153155
# This is only applicable to ghe-restore-repositories currently.
154156
: "${GHE_PARALLEL_RSYNC_MAX_JOBS:="$GHE_PARALLEL_MAX_JOBS"}"
@@ -159,7 +161,7 @@ ghe_parallel_check() {
159161
fi
160162

161163
if [ -n "$GHE_PARALLEL_MAX_LOAD" ]; then
162-
GHE_PARALLEL_COMMAND_OPTIONS+=" -l $GHE_PARALLEL_MAX_LOAD"
164+
GHE_PARALLEL_COMMAND_OPTIONS+=(-l "$GHE_PARALLEL_MAX_LOAD")
163165
GHE_PARALLEL_RSYNC_COMMAND_OPTIONS+=" -l $GHE_PARALLEL_MAX_LOAD"
164166
fi
165167
}
@@ -335,6 +337,7 @@ fi
335337
# that need the remote version should use this function instead of calling
336338
# ghe-host-check directly to reduce ssh roundtrips. The top-level ghe-backup and
337339
# ghe-restore commands establish the version for all subcommands.
340+
# shellcheck disable=SC2120 # Our arguments are optional and not meant to be the owning script's
338341
ghe_remote_version_required() {
339342
if [ -z "$GHE_REMOTE_VERSION" ]; then
340343
_out=$(ghe-host-check "$@")

0 commit comments

Comments
 (0)