@@ -22,14 +22,45 @@ tran_expire=
22
22
# Check if the export tool is available in this version
23
23
export_tool_available () {
24
24
if [ -z " $GHE_TEST_REMOTE_VERSION " ]; then
25
- ghe-ssh " $GHE_HOSTNAME " " test -e /usr/local/bin/ghe-export-mssql"
25
+ ghe_ssh_mssql " test -e /usr/local/bin/ghe-export-mssql"
26
26
else
27
27
# Always return available for test
28
28
return 0
29
29
fi
30
30
}
31
31
32
- if ! export_tool_available; then
32
+ ghe_ssh_mssql () {
33
+ ghe-ssh $opts $ssh_config_file_opt " $GHE_MSSQL_PRIMARY_HOST " " $@ "
34
+ }
35
+
36
+ cleanup () {
37
+ rm -rf $tempdir
38
+ }
39
+ trap ' cleanup' EXIT INT
40
+
41
+ # use the mssql primary host if GHES cluster configuration contains a mssql-master or use the ghe server if the mssql-master is not available.
42
+ GHE_MSSQL_PRIMARY_NODE=" $( ghe-ssh " $GHE_HOSTNAME " -- " ghe-config cluster.mssql-master" || true) "
43
+ GHE_MSSQL_PRIMARY_HOST=" $( ghe-ssh " $GHE_HOSTNAME " -- " ghe-config cluster.$GHE_MSSQL_PRIMARY_NODE .hostname" || true) "
44
+
45
+ if [ -z " $GHE_MSSQL_PRIMARY_HOST " ]; then
46
+ GHE_MSSQL_PRIMARY_HOST=" $GHE_HOSTNAME "
47
+ fi
48
+
49
+ tempdir=$( mktemp -d -t backup-utils-backup-XXXXXX)
50
+ ssh_config_file_opt=
51
+ opts=
52
+
53
+ isHA=" $( ghe-ssh " $GHE_HOSTNAME " -- " ghe-config cluster.ha" || true) "
54
+
55
+ # get server hostnames under cluster and HA
56
+ if [ " $GHE_BACKUP_STRATEGY " = " cluster" ] || [ " $isHA " = " true" ] ; then
57
+ ssh_config_file=" $tempdir /ssh_config"
58
+ ssh_config_file_opt=" -F $ssh_config_file "
59
+ opts=" -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PasswordAuthentication=no"
60
+ ghe-ssh-config " $GHE_HOSTNAME " " $GHE_MSSQL_PRIMARY_HOST " > " $ssh_config_file "
61
+ fi
62
+
63
+ if ! export_tool_available ; then
33
64
ghe_verbose " ghe-export-mssql is not available"
34
65
exit
35
66
fi
@@ -49,11 +80,11 @@ find_timestamp() {
49
80
filename=" ${1##*/ } "
50
81
IFS=' @' read -ra parts <<< " $filename"
51
82
datetime_part=${parts[1]: 0: 15}
52
- echo $datetime_part
83
+ echo " $datetime_part "
53
84
}
54
85
55
86
actions_dbs () {
56
- all_dbs=$( echo ' set -o pipefail; ghe-mssql-console -y -n -q "SET NOCOUNT ON; SELECT name FROM sys.databases"' | ghe-ssh " $GHE_HOSTNAME " /bin/bash)
87
+ all_dbs=$( echo ' set -o pipefail; ghe-mssql-console -y -n -q "SET NOCOUNT ON; SELECT name FROM sys.databases"' | ghe_ssh_mssql /bin/bash)
57
88
for db in $all_dbs ; do
58
89
if [[ ! " $db " =~ ^(master| tempdb| model| msdb)$ ]] && [[ " $db " =~ ^[a-zA-Z0-9_-]+$ ]]; then
59
90
echo " $db "
@@ -90,15 +121,15 @@ ensure_same_dbs() {
90
121
}
91
122
92
123
run_query () {
93
- echo " set -o pipefail; ghe-mssql-console -y -n -q \" SET NOCOUNT ON; $1 \" " | ghe-ssh " $GHE_HOSTNAME " /bin/bash | sed ' s/^[[:space:]]*//;s/[[:space:]]*$//'
124
+ echo " set -o pipefail; ghe-mssql-console -y -n -q \" SET NOCOUNT ON; $1 \" " | ghe_ssh_mssql /bin/bash | sed ' s/^[[:space:]]*//;s/[[:space:]]*$//'
94
125
}
95
126
96
127
get_latest_backup_file () {
97
128
backups_dir=$1
98
129
db=$2
99
130
ext=$3
100
131
101
- latest_full_backup=$( find " $backups_dir " -type f -name " $db *.$ext " | egrep ' [0-9]{8}T[0-9]{6}' | sort | tail -n 1)
132
+ latest_full_backup=$( find " $backups_dir " -type f -name " $db *.$ext " | grep -E ' [0-9]{8}T[0-9]{6}' | sort | tail -n 1)
102
133
latest_full_backup_file=" ${latest_full_backup##*/ } "
103
134
echo " $latest_full_backup_file "
104
135
}
@@ -146,8 +177,8 @@ get_next_diff_backup_base_lsn() {
146
177
147
178
last_mssql=$GHE_DATA_DIR /current/mssql
148
179
149
- if [ ! -d $last_mssql ] \
150
- || [ -z " $( find $last_mssql -type f -name ' *.bak' | head -n 1) " ]; then
180
+ if [ ! -d " $last_mssql " ] \
181
+ || [ -z " $( find " $last_mssql " -type f -name ' *.bak' | head -n 1) " ]; then
151
182
ghe_verbose " Taking first full backup"
152
183
backup_type=" full"
153
184
else
@@ -159,34 +190,34 @@ else
159
190
current=$( date -u +%Y%m%d%H%M%S)
160
191
161
192
full=$( find " $last_mssql " -type f -name " *.bak" | head -n 1)
162
- full=$( find_timestamp $full )
163
- full_expire=$( add_minute $full ${cadence[0]} )
193
+ full=$( find_timestamp " $full " )
194
+ full_expire=$( add_minute " $full " " ${cadence[0]} " )
164
195
full_expire=" ${full_expire// T} "
165
196
166
197
diff=$( find " $last_mssql " -type f -name " *.diff" | head -n 1)
167
198
if [ -f " $diff " ]; then
168
- diff=$( find_timestamp $diff )
169
- diff_expire=$( add_minute $diff ${cadence[1]} )
199
+ diff=$( find_timestamp " $diff " )
200
+ diff_expire=$( add_minute " $diff " " ${cadence[1]} " )
170
201
diff_expire=" ${diff_expire// T} "
171
202
else
172
- diff_expire=$( add_minute $full ${cadence[1]} )
203
+ diff_expire=$( add_minute " $full " " ${cadence[1]} " )
173
204
diff_expire=" ${diff_expire// T} "
174
205
fi
175
206
176
- tran=$( find " $last_mssql " -type f -name " *.log" | egrep ' [0-9]{8}T[0-9]{6}' | sort | tail -1)
177
- tran=$( find_timestamp $tran )
178
- tran_expire=$( add_minute $tran ${cadence[2]} )
207
+ tran=$( find " $last_mssql " -type f -name " *.log" | grep -E ' [0-9]{8}T[0-9]{6}' | sort | tail -1)
208
+ tran=$( find_timestamp " $tran " )
209
+ tran_expire=$( add_minute " $tran " " ${cadence[2]} " )
179
210
tran_expire=" ${tran_expire// T} "
180
211
181
212
ghe_verbose " current $current , full expire $full_expire , \
182
213
diff expire $diff_expire , tran expire $tran_expire "
183
214
184
215
# Determine the type of backup to take based on expiry time
185
- if [ $current -gt $full_expire ]; then
216
+ if [ " $current " -gt " $full_expire " ]; then
186
217
backup_type=' full'
187
- elif [ $current -gt $diff_expire ]; then
218
+ elif [ " $current " -gt " $diff_expire " ]; then
188
219
backup_type=' diff'
189
- elif [ $current -gt $tran_expire ]; then
220
+ elif [ " $current " -gt " $tran_expire " ]; then
190
221
backup_type=' transaction'
191
222
fi
192
223
264
295
mkdir -p " $backup_dir "
265
296
266
297
# Use hard links to "copy" over previous applicable backups to the new snapshot folder to save disk space and time
267
- if [ -d $last_mssql ]; then
268
- for p in $last_mssql /*
298
+ if [ -d " $last_mssql " ]; then
299
+ for p in " $last_mssql " /*
269
300
do
270
301
[[ -e " $p " ]] || break
271
302
@@ -274,23 +305,23 @@ if [ -d $last_mssql ]; then
274
305
transfer=
275
306
276
307
# Copy full backups unless we're taking a new full backup
277
- if [ $extension = " bak" ] && [ " $backup_type " != ' full' ]; then
308
+ if [ " $extension " = " bak" ] && [ " $backup_type " != ' full' ]; then
278
309
transfer=1
279
310
fi
280
311
281
312
# Copy diff backups unless we're taking a new full or diff backup
282
- if [ $extension = " diff" ] && [ " $backup_type " != ' full' ] && [ " $backup_type " != ' diff' ]; then
313
+ if [ " $extension " = " diff" ] && [ " $backup_type " != ' full' ] && [ " $backup_type " != ' diff' ]; then
283
314
transfer=1
284
315
fi
285
316
286
317
# Copy transaction log backups unless we're taking a new full or diff backup
287
- if [ $extension = " log" ] && [ " $backup_type " != ' full' ] && [ " $backup_type " != ' diff' ]; then
318
+ if [ " $extension " = " log" ] && [ " $backup_type " != ' full' ] && [ " $backup_type " != ' diff' ]; then
288
319
transfer=1
289
320
fi
290
321
291
322
if [ -n " $transfer " ]; then
292
323
ghe_verbose " Creating hard link to $filename "
293
- ln $last_mssql / $filename $backup_dir / $filename
324
+ ln " $last_mssql " / " $filename " " $backup_dir " / " $filename "
294
325
fi
295
326
done
296
327
fi
@@ -304,21 +335,21 @@ if [ -n "$backup_type" ]; then
304
335
elif [ " $backup_type " = " transaction" ]; then
305
336
backup_command=' ghe-export-mssql -t'
306
337
fi
307
-
308
- bm_start " $( basename $0 ) "
309
- ghe-ssh " $GHE_HOSTNAME " -- " $backup_command " || failures=" $failures mssql"
310
- bm_end " $( basename $0 ) "
338
+
339
+ bm_start " $( basename " $0 " ) "
340
+ ghe_ssh_mssql -- " $backup_command " || failures=" $failures mssql"
341
+ bm_end " $( basename " $0 " ) "
311
342
312
343
# Configure the backup cadence on the appliance, which is used for diagnostics
313
- ghe-ssh " $GHE_HOSTNAME " " ghe-config mssql.backup.cadence $GHE_MSSQL_BACKUP_CADENCE "
344
+ ghe_ssh_mssql " ghe-config mssql.backup.cadence $GHE_MSSQL_BACKUP_CADENCE "
314
345
315
346
# Transfer backup files from appliance to backup host
316
347
appliance_dir=" $GHE_REMOTE_DATA_DIR /user/mssql/backups"
317
348
backups=$( echo " set -o pipefail; if sudo test -d \" $appliance_dir \" ; then \
318
- sudo ls \" $appliance_dir \" ; fi" | ghe-ssh " $GHE_HOSTNAME " /bin/bash)
349
+ sudo ls \" $appliance_dir \" ; fi" | ghe_ssh_mssql /bin/bash)
319
350
for b in $backups
320
351
do
321
352
ghe_verbose " Transferring to backup host $b "
322
- ghe-ssh " $GHE_HOSTNAME " " sudo cat $appliance_dir /$b " > $backup_dir / $b
353
+ ghe_ssh_mssql " sudo cat $appliance_dir /$b " > " $backup_dir " / " $b "
323
354
done
324
355
fi
0 commit comments