Skip to content

Commit 6d17c01

Browse files
authored
Merge pull request #276 from github/krayon-shellcheck-p1
ghe-backup and ghe-restore shellcheck fixes
2 parents e453b40 + af87247 commit 6d17c01

File tree

3 files changed

+73
-72
lines changed

3 files changed

+73
-72
lines changed

bin/ghe-backup

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

81-
if [ "$(ls -il dest1/testfile | awk '{ print $1 }')" != "$(ls -il dest2/testfile | awk '{ print $1 }')" ]; then
81+
if [ "$(stat -c %i dest1/testfile)" != "$(stat -c %i dest2/testfile)" ]; then
8282
log_error "Error: the filesystem containing $GHE_DATA_DIR does not support hard links.\n Backup Utilities use hard links to store backup data efficiently." 1>&2
8383
exit 1
8484
fi
@@ -98,13 +98,13 @@ cleanup () {
9898
progress=$(cat ../in-progress)
9999
snapshot=$(echo "$progress" | cut -d ' ' -f 1)
100100
pid=$(echo "$progress" | cut -d ' ' -f 2)
101-
if [ "$snapshot" = "$GHE_SNAPSHOT_TIMESTAMP" ] && [ "$$" = $pid ]; then
101+
if [ "$snapshot" = "$GHE_SNAPSHOT_TIMESTAMP" ] && [ "$$" = "$pid" ]; then
102102
unlink ../in-progress
103103
fi
104104
fi
105105

106106
rm -rf "$failures_file"
107-
rm -f ${GHE_DATA_DIR}/in-progress-backup
107+
rm -f "${GHE_DATA_DIR}/in-progress-backup"
108108

109109
# Cleanup SSH multiplexing
110110
ghe-ssh --clean
@@ -140,11 +140,11 @@ if [ -f ../in-progress ]; then
140140
fi
141141

142142
echo "$GHE_SNAPSHOT_TIMESTAMP $$" > ../in-progress
143-
echo "$GHE_SNAPSHOT_TIMESTAMP $$" > ${GHE_DATA_DIR}/in-progress-backup
143+
echo "$GHE_SNAPSHOT_TIMESTAMP $$" > "${GHE_DATA_DIR}/in-progress-backup"
144144

145+
START_TIME=$(date +%s)
145146
log_info "Starting backup of $GHE_HOSTNAME with backup-utils v$BACKUP_UTILS_VERSION in snapshot $GHE_SNAPSHOT_TIMESTAMP"
146147

147-
148148
# Perform a host connection check and establish the remote appliance version.
149149
# The version is available in the GHE_REMOTE_VERSION variable and also written
150150
# to a version file in the snapshot directory itself.
@@ -166,9 +166,6 @@ echo "$GHE_BACKUP_STRATEGY" > strategy
166166
# Create benchmark file
167167
bm_init > /dev/null
168168

169-
START_TIME=$(date +%s)
170-
log_info "Starting backup of $GHE_HOSTNAME with backup-utils v$BACKUP_UTILS_VERSION"
171-
172169
ghe-backup-store-version ||
173170
log_warn "Warning: storing backup-utils version remotely failed."
174171

@@ -240,7 +237,7 @@ if [ "$GHE_BACKUP_STRATEGY" = "rsync" ]; then
240237
fi
241238

242239
if [ "$GHE_PARALLEL_ENABLED" = "yes" ]; then
243-
$GHE_PARALLEL_COMMAND $GHE_PARALLEL_COMMAND_OPTIONS -- "${commands[@]}"
240+
"$GHE_PARALLEL_COMMAND" "${GHE_PARALLEL_COMMAND_OPTIONS[@]}" -- "${commands[@]}"
244241
else
245242
for c in "${commands[@]}"; do
246243
eval "$c"
@@ -254,7 +251,7 @@ fi
254251
# git fsck repositories after the backup
255252
if [ "$GHE_BACKUP_FSCK" = "yes" ]; then
256253
log_info "Running git fsck on repositories ..."
257-
ghe-backup-fsck $GHE_SNAPSHOT_DIR || failures="$failures fsck"
254+
ghe-backup-fsck "$GHE_SNAPSHOT_DIR" || failures="$failures fsck"
258255
fi
259256

260257
# If everything was successful, mark the snapshot as complete, update the
@@ -269,13 +266,15 @@ if [ -z "$failures" ]; then
269266
ghe-prune-snapshots
270267
fi
271268

269+
END_TIME=$(date +%s)
270+
log_info "Runtime: $((END_TIME - START_TIME)) seconds"
272271
log_info "Completed backup of $GHE_HOSTNAME in snapshot $GHE_SNAPSHOT_TIMESTAMP at $(date +"%H:%M:%S")"
273272

274273
# Exit non-zero and list the steps that failed.
275274
if [ -z "$failures" ]; then
276275
ghe_remote_logger "Completed backup from $(hostname) / snapshot $GHE_SNAPSHOT_TIMESTAMP successfully."
277276
else
278-
steps="$(echo $failures | sed 's/ /, /g')"
277+
steps="${failures// /, }"
279278
ghe_remote_logger "Completed backup from $(hostname) / snapshot $GHE_SNAPSHOT_TIMESTAMP with failures: ${steps}."
280279
log_error "Error: Snapshot incomplete. Some steps failed: ${steps}. "
281280
exit 1
@@ -285,11 +284,7 @@ fi
285284
log_info "Checking for leaked ssh keys ..."
286285
ghe-detect-leaked-ssh-keys -s "$GHE_SNAPSHOT_DIR" || true
287286

288-
END_TIME=$(date +%s)
289-
log_info "Runtime: $((${END_TIME} - ${START_TIME})) seconds"
290-
log_info "Backup of $GHE_HOSTNAME finished."
291-
# Make sure we exit zero after the conditional
292-
true
287+
log_info "Restore of $GHE_HOSTNAME finished."
293288

294289
# Remove in-progress file
295290
ghe_backup_finished

bin/ghe-restore

Lines changed: 55 additions & 52 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
@@ -87,30 +87,30 @@ while true; do
8787
done
8888

8989
start_cron () {
90-
log_info "Starting cron ..."
90+
log_info "Starting cron ..."
9191
if $CLUSTER; then
92-
if ! ghe-ssh "$GHE_HOSTNAME" -- "ghe-cluster-each -- sudo timeout 120s service cron start" ; then
93-
log_warn "Failed to start cron on one or more nodes"
92+
if ! ghe-ssh "$GHE_HOSTNAME" -- "ghe-cluster-each -- sudo timeout 120s service cron start"; then
93+
log_warn "Failed to start cron on one or more nodes"
9494
fi
9595
else
96-
if ! ghe-ssh "$GHE_HOSTNAME" -- "sudo timeout 120s service cron start" ; then
97-
log_warn "Failed to start cron"
96+
if ! ghe-ssh "$GHE_HOSTNAME" -- "sudo timeout 120s service cron start"; then
97+
log_warn "Failed to start cron"
9898
fi
9999
fi
100100
}
101101

102102
cleanup () {
103-
log_info " Exiting, cleaning up ..."
103+
log_info " Exiting, cleaning up ..."
104104
if [ -n "$1" ]; then
105105
update_restore_status "$1"
106106
fi
107107

108108
if $ACTIONS_STOPPED && ghe-ssh "$GHE_HOSTNAME" -- 'ghe-config --true app.actions.enabled'; then
109-
log_info "Restarting Actions after restore ..."
109+
log_info "Restarting Actions after restore ..."
110110
# In GHES 3.3+, ghe-actions-start no longer has a -f (force) flag. In GHES 3.2 and below, we must provide the
111111
# force flag to make sure it can start in maintenance mode. Use it conditionally based on whether it exists
112112
# in the --help output
113-
if ghe-ssh "$GHE_HOSTNAME" -- 'ghe-actions-start --help' | grep -q force ; then
113+
if ghe-ssh "$GHE_HOSTNAME" -- 'ghe-actions-start --help' | grep -q force; then
114114
ghe-ssh "$GHE_HOSTNAME" -- 'ghe-actions-start -f' 1>&3
115115
else
116116
ghe-ssh "$GHE_HOSTNAME" -- 'ghe-actions-start' 1>&3
@@ -122,15 +122,15 @@ cleanup () {
122122
fi
123123

124124
# Cleanup SSH multiplexing
125-
log_info "Cleaning up SSH multiplexing ..."
126-
if ! ghe-ssh --clean ; then
127-
log_info "Failed to clean up SSH multiplexing"
125+
log_info "Cleaning up SSH multiplexing ..."
126+
if ! ghe-ssh --clean; then
127+
log_info "Failed to clean up SSH multiplexing"
128128
fi
129-
# Remove in-progress file
130-
log_info "Removing in-progress file ..." 1>&3
131129

132-
if ! rm -f ${GHE_DATA_DIR}/in-progress-restore ; then
133-
log_error "Failed to remove in-progress file" 1>&3
130+
# Remove in-progress file
131+
log_info "Removing in-progress file ..." 1>&3
132+
if ! rm -f "${GHE_DATA_DIR}/in-progress-restore"; then
133+
log_error "Failed to remove in-progress file" 1>&3
134134
fi
135135
}
136136

@@ -139,23 +139,24 @@ cleanup () {
139139
cleanup_cluster_nodes() {
140140
uuid="$1"
141141
if [ -z "$uuid" ]; then
142-
log_error "Node UUID required."
142+
log_error "Node UUID required."
143143
exit 2
144144
fi
145-
log_info "Cleaning up spokes" 1>&3
146-
ghe-spokes server evacuate git-server-$uuid 'Removing replica'
147-
ghe-spokes server destroy git-server-$uuid
148145

149-
log_info "Cleaning up storage" 1>&3
150-
ghe-storage destroy-host storage-server-$uuid --force
146+
log_info "Cleaning up spokes" 1>&3
147+
ghe-spokes server evacuate "git-server-$uuid" 'Removing replica'
148+
ghe-spokes server destroy "git-server-$uuid"
149+
150+
log_info "Cleaning up storage" 1>&3
151+
ghe-storage destroy-host "storage-server-$uuid" --force
151152

152-
log_info "Cleaning up dpages" 1>&3
153-
ghe-dpages offline pages-server-$uuid
154-
ghe-dpages remove pages-server-$uuid
153+
log_info "Cleaning up dpages" 1>&3
154+
ghe-dpages offline "pages-server-$uuid"
155+
ghe-dpages remove "pages-server-$uuid"
155156

156-
log_info "Cleaning up redis" 1>&3
157-
ghe-redis-cli del resque:queue:maint_git-server-$uuid
158-
ghe-redis-cli srem resque:queues maint_git-server-$uuid
157+
log_info "Cleaning up redis" 1>&3
158+
ghe-redis-cli del "resque:queue:maint_git-server-$uuid"
159+
ghe-redis-cli srem resque:queues "maint_git-server-$uuid"
159160
}
160161

161162
# Bring in the backup configuration
@@ -190,7 +191,7 @@ export GHE_RESTORE_SNAPSHOT
190191
ghe_backup_check
191192

192193
# Detect if the backup we are restoring has a leaked ssh key
193-
echo "Checking for leaked keys in the backup snapshot that is being restored ..."
194+
echo "Checking for leaked keys in the backup snapshot that is being restored ..."
194195
ghe-detect-leaked-ssh-keys -s "$GHE_RESTORE_SNAPSHOT_PATH" || true
195196

196197
# Figure out whether to use the tarball or rsync restore strategy based on the
@@ -218,7 +219,7 @@ export CLUSTER
218219

219220
# Restoring a cluster backup to a standalone appliance is not supported
220221
if ! $CLUSTER && [ "$GHE_BACKUP_STRATEGY" = "cluster" ]; then
221-
log_error "Error: Snapshot from a GitHub Enterprise cluster cannot be restored to a standalone appliance. Aborting." >&2
222+
log_error "Error: Snapshot from a GitHub Enterprise cluster cannot be restored to a standalone appliance. Aborting." >&2
222223
exit 1
223224
fi
224225

@@ -230,7 +231,7 @@ fi
230231
# Figure out if this appliance is in a replication pair
231232
if ghe-ssh "$GHE_HOSTNAME" -- \
232233
"[ -f '$GHE_REMOTE_ROOT_DIR/etc/github/repl-state' ]"; then
233-
log_error "Error: Restoring to an appliance with replication enabled is not supported. Please teardown replication before restoring." >&2
234+
log_error "Error: Restoring to an appliance with replication enabled is not supported. Please teardown replication before restoring." >&2
234235
exit 1
235236
fi
236237

@@ -267,9 +268,10 @@ fi
267268
# Log restore start message locally and in /var/log/syslog on remote instance
268269
START_TIME=$(date +%s)
269270
log_info "Starting restore of $GHE_HOSTNAME with backup-utils v$BACKUP_UTILS_VERSION from snapshot $GHE_RESTORE_SNAPSHOT"
271+
270272
ghe_remote_logger "Starting restore from $(hostname) with backup-utils v$BACKUP_UTILS_VERSION / snapshot $GHE_RESTORE_SNAPSHOT ..."
271273
# Create an in-progress-restore file to prevent simultaneous backup or restore runs
272-
echo "${START_TIME} $$" > ${GHE_DATA_DIR}/in-progress-restore
274+
echo "${START_TIME} $$" > "${GHE_DATA_DIR}/in-progress-restore"
273275

274276
# Keep other processes on the VM or cluster in the loop about the restore status.
275277
#
@@ -295,7 +297,7 @@ update_restore_status "restoring"
295297
# Make sure the GitHub appliance is in maintenance mode.
296298
if $instance_configured; then
297299
if ! ghe-maintenance-mode-status "$GHE_HOSTNAME"; then
298-
log_error "Error: $GHE_HOSTNAME must be put in maintenance mode before restoring. Aborting." 1>&2
300+
log_error "Error: $GHE_HOSTNAME must be put in maintenance mode before restoring. Aborting." 1>&2
299301
exit 1
300302
fi
301303
fi
@@ -305,9 +307,9 @@ RELEASE_VERSION=$(ghe-ssh "$GHE_HOSTNAME" -- 'ghe-config --get core.package-vers
305307

306308
# If the backup being restored is from an appliance with Actions disabled, restoring it onto an appliance with Actions enabled will cause
307309
# mismatches in the secrets needed for Actions which ultimately results in Actions not working properly. Note: xargs is to remove whitespace
308-
ACTIONS_ENABLED_IN_BACKUP=$(git config -f $GHE_RESTORE_SNAPSHOT_PATH/settings.json --bool app.actions.enabled | xargs)
310+
ACTIONS_ENABLED_IN_BACKUP=$(git config -f "$GHE_RESTORE_SNAPSHOT_PATH/settings.json" --bool app.actions.enabled | xargs)
309311
if [[ $ACTIONS_ENABLED_IN_BACKUP != true ]] && ghe-ssh "$GHE_HOSTNAME" -- 'ghe-config --true app.actions.enabled'; then
310-
log_error "Restoring a backup with Actions disabled onto an appliance with Actions enabled is not supported." >&2
312+
log_error "Restoring a backup with Actions disabled onto an appliance with Actions enabled is not supported." >&2
311313
exit 1
312314
fi
313315

@@ -391,11 +393,10 @@ fi
391393
# Restore UUID if present and not restoring to cluster.
392394
if [ -s "$GHE_RESTORE_SNAPSHOT_PATH/uuid" ] && ! $CLUSTER; then
393395
log_info "Restoring UUID ..."
394-
cat "$GHE_RESTORE_SNAPSHOT_PATH/uuid" |
395-
ghe-ssh "$GHE_HOSTNAME" -- "sudo sponge '$GHE_REMOTE_DATA_USER_DIR/common/uuid' 2>/dev/null"
396+
ghe-ssh "$GHE_HOSTNAME" -- "sudo sponge '$GHE_REMOTE_DATA_USER_DIR/common/uuid' 2>/dev/null" <"$GHE_RESTORE_SNAPSHOT_PATH/uuid"
396397
ghe-ssh "$GHE_HOSTNAME" -- "sudo systemctl stop consul" || true
397398
ghe-ssh "$GHE_HOSTNAME" -- "sudo rm -rf /data/user/consul/raft"
398-
fi
399+
fi
399400

400401
if is_external_database_snapshot; then
401402
appliance_strategy="external"
@@ -428,12 +429,13 @@ if ghe-ssh "$GHE_HOSTNAME" -- 'ghe-config --true app.actions.enabled'; then
428429
ACTIONS_STOPPED=true
429430
ghe-ssh "$GHE_HOSTNAME" -- 'ghe-actions-stop' 1>&3
430431

431-
log_info "Restoring MSSQL databases ..."
432+
log_info "Restoring MSSQL databases ..."
432433
ghe-restore-mssql "$GHE_HOSTNAME" 1>&3
433434

434435
log_info "Restoring Actions data ..."
435436
ghe-restore-actions "$GHE_HOSTNAME" 1>&3
436-
echo "* WARNING: Every self-hosted Actions runner that communicates with the restored GHES server must be restarted or reconfigured in order to continue functioning. \n See https://docs.github.com/en/actions/hosting-your-own-runners/adding-self-hosted-runners for more details on how to reconfigure self-hosted Actions runners."
437+
echo "* WARNING: Every self-hosted Actions runner that communicates with the restored GHES server must be restarted or reconfigured in order to continue functioning."
438+
echo "See https://docs.github.com/en/actions/hosting-your-own-runners/adding-self-hosted-runners for more details on how to reconfigure self-hosted Actions runners."
437439
fi
438440

439441
if ghe-ssh "$GHE_HOSTNAME" -- 'ghe-config --true app.minio.enabled'; then
@@ -485,16 +487,16 @@ if ! $CLUSTER && [ -d "$GHE_RESTORE_SNAPSHOT_PATH/elasticsearch" ]; then
485487
fi
486488

487489
# Restore the audit log migration sentinel file, if it exists in the snapshot
488-
if test -f "$GHE_RESTORE_SNAPSHOT_PATH"/es-scan-complete; then
490+
if test -f "$GHE_RESTORE_SNAPSHOT_PATH/es-scan-complete"; then
489491
log_info "Restoring Elasticsearch audit log migration sentinel file ..." 1>&3
490-
if ! ghe-ssh "$GHE_HOSTNAME" -- "sudo touch $GHE_REMOTE_DATA_USER_DIR/common/es-scan-complete" ; then
492+
if ! ghe-ssh "$GHE_HOSTNAME" -- "sudo touch $GHE_REMOTE_DATA_USER_DIR/common/es-scan-complete"; then
491493
log_info "Failed to restore Elasticsearch audit log migration sentinel file." 1>&3
492494
fi
493495
fi
494496

495497
# Restore exported audit logs to 2.12.9 and newer single nodes and
496498
# all releases of cluster
497-
if $CLUSTER || [ "$(version $GHE_REMOTE_VERSION)" -ge "$(version 2.12.9)" ]; then
499+
if $CLUSTER || [ "$(version "$GHE_REMOTE_VERSION")" -ge "$(version 2.12.9)" ]; then
498500
if [[ "$GHE_RESTORE_SKIP_AUDIT_LOGS" = "yes" ]]; then
499501
log_info "Skipping restore of audit logs."
500502
else
@@ -508,16 +510,16 @@ fi
508510

509511
if [ "$GHE_PARALLEL_ENABLED" = "yes" ]; then
510512
log_info "Restoring data in parallel ..."
511-
$GHE_PARALLEL_COMMAND $GHE_PARALLEL_COMMAND_OPTIONS -- "${commands[@]}"
513+
"$GHE_PARALLEL_COMMAND" "${GHE_PARALLEL_COMMAND_OPTIONS[@]}" -- "${commands[@]}"
512514
else
513515
log_info "Restoring data serially ..." 1>&3
514516
for c in "${commands[@]}"; do
515-
eval "$c"
517+
eval "$c"
516518
done
517519
fi
518520

519521
# Restart an already running memcached to reset the cache after restore
520-
log_info "Restarting memcached ..." 1>&3
522+
log_info "Restarting memcached ..." 1>&3
521523
echo "sudo restart -q memcached 2>/dev/null || true" |
522524
ghe-ssh "$GHE_HOSTNAME" -- /bin/sh
523525

@@ -564,7 +566,7 @@ CRON_RUNNING=true
564566
# Clean up all stale replicas on configured instances.
565567
if ! $CLUSTER && $instance_configured; then
566568
log_info "Cleaning up replicas..." 1>&3
567-
restored_uuid=$(cat $GHE_RESTORE_SNAPSHOT_PATH/uuid)
569+
restored_uuid=$(cat "$GHE_RESTORE_SNAPSHOT_PATH/uuid")
568570
other_nodes=$(echo "
569571
set -o pipefail; \
570572
ghe-spokes server show --json \
@@ -604,11 +606,12 @@ else
604606
fi
605607

606608
END_TIME=$(date +%s)
607-
log_info "Runtime: $((${END_TIME} - ${START_TIME})) seconds"
608-
log_info "Restore of $GHE_HOSTNAME from snapshot $GHE_RESTORE_SNAPSHOT finished."
609+
log_info "Runtime: $((END_TIME - START_TIME)) seconds"
610+
log_info "Completed restore of $GHE_HOSTNAME from snapshot $GHE_RESTORE_SNAPSHOT at $(date +"%H:%M:%S")"
609611

612+
log_info "Restore of $GHE_HOSTNAME finished."
610613

611614
if ! $instance_configured; then
612-
echo "To complete the restore process, please visit https://$hostname/setup/settings to review and save the appliance configuration."
615+
echo "To complete the restore process, please visit https://$hostname/setup/settings to review and save the appliance configuration."
613616
fi
614617

0 commit comments

Comments
 (0)