Skip to content

Commit 21632b2

Browse files
committed
Merge pull request #10 from github/restore-confirm
Require host confirmation on restore
2 parents 35f0239 + 5e2aff3 commit 21632b2

File tree

2 files changed

+87
-9
lines changed

2 files changed

+87
-9
lines changed

bin/ghe-restore

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#/ argument is provided, it always overrides the configured restore host.
77
#/
88
#/ Options:
9+
#/ -f Don't prompt for confirmation before restoring.
910
#/ -c Restore appliance settings and license in addition to
1011
#/ datastores. Settings are not restored by default to
1112
#/ prevent overwriting different configuration on the
@@ -32,8 +33,13 @@ export GHE_RESTORE_SNAPSHOT
3233

3334
# Parse arguments
3435
restore_settings=false
36+
force=false
3537
while true; do
3638
case "$1" in
39+
-f|--force)
40+
force=true
41+
shift
42+
;;
3743
-s)
3844
GHE_RESTORE_SNAPSHOT="$(basename "$2")"
3945
shift 2
@@ -70,11 +76,39 @@ GHE_RESTORE_SNAPSHOT=$(basename "$GHE_RESTORE_SNAPSHOT_PATH")
7076
# strategy file written in the snapshot directory.
7177
GHE_BACKUP_STRATEGY=$(cat "$GHE_RESTORE_SNAPSHOT_PATH/strategy")
7278

73-
echo "Starting $GHE_BACKUP_STRATEGY restore of $host from snapshot $GHE_RESTORE_SNAPSHOT"
74-
7579
# Perform a host-check and establish the remote version in GHE_REMOTE_VERSION.
7680
ghe_remote_version_required "$host"
7781

82+
# Prompt to verify the restore host given is correct. Restoring overwrites
83+
# important data on the destination appliance that cannot be recovered. This is
84+
# mostly to prevent accidents where the backup host is given to restore instead
85+
# of a separate restore host since they're used in such close proximity.
86+
if ! $force; then
87+
echo
88+
echo "WARNING: All data on GitHub Enterprise appliance $hostname ($GHE_REMOTE_VERSION)"
89+
echo " will be overwritten with data from snapshot ${GHE_RESTORE_SNAPSHOT}."
90+
echo "Please verify that this is the correct restore host before continuing."
91+
printf "Type 'yes' to continue: "
92+
93+
while read -r response; do
94+
case $response in
95+
yes|Yes|YES)
96+
break
97+
;;
98+
'')
99+
printf "Type 'yes' to continue: "
100+
;;
101+
*)
102+
echo "Restore aborted." 1>&2
103+
exit 1
104+
;;
105+
esac
106+
done
107+
echo
108+
fi
109+
110+
echo "Starting $GHE_BACKUP_STRATEGY restore of $host from snapshot $GHE_RESTORE_SNAPSHOT"
111+
78112
# Verify the host has been fully configured at least once unless the -c
79113
# argument was provided.
80114
if ! $restore_settings &&

test/test-ghe-restore.sh

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ begin_test "ghe-restore into configured vm"
7777
export GHE_RESTORE_HOST
7878

7979
# run ghe-restore and write output to file for asserting against
80-
ghe-restore -v > "$TRASHDIR/restore-out" 2>&1
80+
ghe-restore -v -f > "$TRASHDIR/restore-out" 2>&1
8181
cat "$TRASHDIR/restore-out"
8282

8383
# verify connect to right host
@@ -110,6 +110,50 @@ begin_test "ghe-restore into configured vm"
110110
)
111111
end_test
112112

113+
begin_test "ghe-restore aborts without user verification"
114+
(
115+
set -e
116+
rm -rf "$GHE_REMOTE_DATA_DIR"
117+
setup_remote_metadata
118+
119+
# create settings file -- used to determine if instance has been configured.
120+
touch "$GHE_REMOTE_DATA_DIR/enterprise/dna.json"
121+
122+
# set restore host environ var
123+
GHE_RESTORE_HOST=127.0.0.1
124+
export GHE_RESTORE_HOST
125+
126+
# run ghe-restore and write output to file for asserting against
127+
if echo "no" | ghe-restore -v > "$TRASHDIR/restore-out" 2>&1; then
128+
cat "$TRASHDIR/restore-out"
129+
false # ghe-restore should have exited non-zero
130+
fi
131+
132+
grep -q "Restore aborted" "$TRASHDIR/restore-out"
133+
)
134+
end_test
135+
136+
begin_test "ghe-restore accepts user verification"
137+
(
138+
set -e
139+
rm -rf "$GHE_REMOTE_DATA_DIR"
140+
setup_remote_metadata
141+
142+
# create settings file -- used to determine if instance has been configured.
143+
touch "$GHE_REMOTE_DATA_DIR/enterprise/dna.json"
144+
145+
# set restore host environ var
146+
GHE_RESTORE_HOST=127.0.0.1
147+
export GHE_RESTORE_HOST
148+
149+
# run ghe-restore and write output to file for asserting against
150+
if ! echo "yes" | ghe-restore -v > "$TRASHDIR/restore-out" 2>&1; then
151+
cat "$TRASHDIR/restore-out"
152+
false # ghe-restore should have accepted the input
153+
fi
154+
)
155+
end_test
156+
113157
begin_test "ghe-restore -c into unconfigured vm"
114158
(
115159
set -e
@@ -121,7 +165,7 @@ begin_test "ghe-restore -c into unconfigured vm"
121165
export GHE_RESTORE_HOST
122166

123167
# run ghe-restore and write output to file for asserting against
124-
ghe-restore -v -c > "$TRASHDIR/restore-out" 2>&1
168+
ghe-restore -v -f -c > "$TRASHDIR/restore-out" 2>&1
125169
cat "$TRASHDIR/restore-out"
126170

127171
# verify connect to right host
@@ -165,7 +209,7 @@ begin_test "ghe-restore into unconfigured vm"
165209

166210
# run ghe-restore and write output to file for asserting against
167211
# this should fail due to the appliance being in an unconfigured state
168-
! ghe-restore -v > "$TRASHDIR/restore-out" 2>&1
212+
! ghe-restore -v -f > "$TRASHDIR/restore-out" 2>&1
169213

170214
# verify that ghe-restore failed due to the appliance not being configured
171215
grep -q -e "Error: $GHE_RESTORE_HOST not configured" "$TRASHDIR/restore-out"
@@ -186,7 +230,7 @@ begin_test "ghe-restore with host arg"
186230
export GHE_RESTORE_HOST
187231

188232
# run it
189-
output="$(ghe-restore localhost)" || false
233+
output="$(ghe-restore -f localhost)" || false
190234

191235
# verify host arg overrides configured restore host
192236
echo "$output" | grep -q 'Connect localhost OK'
@@ -220,7 +264,7 @@ begin_test "ghe-restore no host arg or configured restore host"
220264
unset GHE_RESTORE_HOST
221265

222266
# verify running ghe-restore fails
223-
! ghe-restore
267+
! ghe-restore -f
224268
)
225269
end_test
226270

@@ -237,7 +281,7 @@ begin_test "ghe-restore with no pages backup"
237281
rm -rf "$GHE_DATA_DIR/1/pages"
238282

239283
# run it
240-
ghe-restore -v localhost
284+
ghe-restore -v -f localhost
241285
)
242286
end_test
243287

@@ -252,7 +296,7 @@ begin_test "ghe-restore with tarball strategy"
252296

253297
# run it
254298
echo "tarball" > "$GHE_DATA_DIR/current/strategy"
255-
output=$(ghe-restore -v localhost)
299+
output=$(ghe-restore -v -f localhost)
256300

257301
# verify ghe-import-repositories was run on remote side with fake tarball
258302
echo "$output" | grep -q 'fake ghe-export-repositories data'

0 commit comments

Comments
 (0)