55# / the GHE_RESTORE_HOST config variable is set in backup.config. When a <host>
66# / argument is provided, it always overrides the configured restore host.
77# /
8+ # / Options:
9+ # / -c Clean restore. Restores appliance settings and license
10+ # / in addition to datastores. This is the default for
11+ # / appliances that have not yet been configured but can be
12+ # / forced by passing the argument explicitly.
13+ # / -s <snapshot-id> Restore from the snapshot with the given id. Available
14+ # / snapshots may be listed under the data directory.
15+ # / -v Enable verbose output.
16+ # /
817# / Note that the host must be reachable and your SSH key must be setup as
918# / described in the following help article:
1019# /
1120# / <https://enterprise.github.com/help/articles/adding-an-ssh-key-for-shell-access>
12- # /
13- # / When the -s argument is given, restore from the snapshot with the given id.
14- # / Available snapshots can be listed under the data directory.
15- # /
16- # / When the -v argument is given, enable verbose output and show more information
17- # / about what's being transferred.
1821set -e
1922
2023# Bring in the backup configuration.
@@ -27,11 +30,27 @@ cd $(dirname "$0")/..
2730GHE_RESTORE_SNAPSHOT=" current"
2831export GHE_RESTORE_SNAPSHOT
2932
30- # Parse snapshot argument if provided
31- if [ " $1 " = " -s" ]; then
32- GHE_RESTORE_SNAPSHOT=" $( basename " $2 " ) "
33- shift 2
34- fi
33+ # Parse arguments
34+ clean_restore=false
35+ while true ; do
36+ case " $1 " in
37+ -s)
38+ GHE_RESTORE_SNAPSHOT=" $( basename " $2 " ) "
39+ shift 2
40+ ;;
41+ -c)
42+ clean_restore=true
43+ shift
44+ ;;
45+ -* )
46+ echo " Error: invalid argument: '$1 '" 1>&2
47+ exit 1
48+ ;;
49+ * )
50+ break
51+ ;;
52+ esac
53+ done
3554
3655# Grab the host arg
3756host=" ${1:- $GHE_RESTORE_HOST } "
@@ -53,19 +72,34 @@ echo "Starting $GHE_BACKUP_STRATEGY restore of $host from snapshot $GHE_RESTORE_
5372# Perform a host-check and establish the remote version in GHE_REMOTE_VERSION.
5473ghe_remote_version_required " $host "
5574
56- # Restore license file under versions >= 2.x. This is for automated restores of
57- # newly provisioned VMs in internal GitHub test environments only currently.
58- if [ " $GHE_VERSION_MAJOR " -gt 1 ] && ! ghe-ssh " $host " " test -f '$GHE_REMOTE_LICENSE_FILE '" ; then
59- echo " Restoring license ..."
60- ghe-ssh " $host " -- ' ghe-import-license' < " $GHE_RESTORE_SNAPSHOT_PATH /enterprise.ghl" 1>&3
75+ # Figure out if host has ever been configured based on the presence of the
76+ # dna.json file. If not, enable restoring settings and license in addition
77+ # to primary datastores.
78+ if ! $clean_restore &&
79+ ghe-ssh " ! test -f '$GHE_REMOTE_DATA_DIR /enterprise/dna.json -o \
80+ -f '$GHE_REMOTE_DATA_USER_DIR /common/dna.json'" ; then
81+ clean_restore=true
6182fi
6283
6384# Make sure the GitHub appliance is in maintenance mode and all writing
6485# processes have bled out.
6586ghe-maintenance-mode-enable " $host "
6687
67- echo " Restoring settings ..."
68- ghe-ssh " $host " -- ' ghe-import-settings' < " $GHE_RESTORE_SNAPSHOT_PATH /settings.json" 1>&3
88+ # Restore settings and license if performing a clean restore.
89+ if $clean_restore ; then
90+ echo " Restoring settings ..."
91+ ghe-ssh " $host " -- ' ghe-import-settings' < " $GHE_RESTORE_SNAPSHOT_PATH /settings.json" 1>&3
92+
93+ if [ " $GHE_VERSION_MAJOR " -ge 2 ]; then
94+ echo " Restoring license ..."
95+ ghe-ssh " $host " -- ' ghe-import-license' < " $GHE_RESTORE_SNAPSHOT_PATH /enterprise.ghl" 1>&3
96+
97+ echo "
98+ sudo ghe-service-ensure-mysql &&
99+ sudo ghe-service-ensure-elasticsearch
100+ " | ghe-ssh " $host " -- /bin/sh 1>&3
101+ fi
102+ fi
69103
70104echo " Restoring Git repositories ..."
71105ghe-restore-repositories-${GHE_BACKUP_STRATEGY} " $host " 1>&3
@@ -74,9 +108,6 @@ echo "Restoring GitHub Pages ..."
74108ghe-restore-pages-${GHE_BACKUP_STRATEGY} " $host " 1>&3
75109
76110echo " Restoring MySQL database ..."
77- if [ " $GHE_VERSION_MAJOR " -gt 1 ]; then
78- ghe-ssh " $host " -- ' sudo ghe-service-ensure-mysql' 1>&3
79- fi
80111gzip -dc " $GHE_RESTORE_SNAPSHOT_PATH /mysql.sql.gz" | ghe-ssh " $host " -- ' ghe-import-mysql' 1>&3
81112
82113echo " Restoring Redis database ..."
@@ -86,9 +117,6 @@ echo "Restoring SSH authorized keys ..."
86117ghe-ssh " $host " -- ' ghe-import-authorized-keys' < " $GHE_RESTORE_SNAPSHOT_PATH /authorized-keys.json" 1>&3
87118
88119echo " Restoring Elasticsearch indices ..."
89- if [ " $GHE_VERSION_MAJOR " -gt 1 ]; then
90- ghe-ssh " $host " -- ' sudo ghe-service-ensure-elasticsearch' 1>&3
91- fi
92120ghe-restore-es-${GHE_BACKUP_STRATEGY} " $host " 1>&3
93121
94122echo " Restoring SSH host keys ..."
0 commit comments