12
12
# so you can manually rerun commands or peruse logs and data
13
13
# under SCRATCH_DIR.
14
14
#
15
+ # Usage:
16
+ # s3_backup_unified.sh <source_dir> <build_dir> [scratch_dir] [--encrypt]
17
+ #
15
18
# See https://apple.github.io/foundationdb/backups.html
16
19
17
20
# Install signal traps. Depends on globals being set.
@@ -30,6 +33,9 @@ function cleanup {
30
33
if type shutdown_aws & > /dev/null; then
31
34
shutdown_aws " ${TEST_SCRATCH_DIR} "
32
35
fi
36
+ if [[ -n " ${ENCRYPTION_KEY_FILE:- } " ]] && [[ -f " ${ENCRYPTION_KEY_FILE} " ]]; then
37
+ rm -f " ${ENCRYPTION_KEY_FILE} "
38
+ fi
33
39
}
34
40
35
41
# Resolve passed in reference to an absolute path.
@@ -45,28 +51,47 @@ function resolve_to_absolute_path {
45
51
realpath " ${p} "
46
52
}
47
53
54
+ function create_encryption_key_file {
55
+ local key_file=" ${1} "
56
+ log " Creating encryption key file at ${key_file} "
57
+ dd if=/dev/urandom bs=32 count=1 of=" ${key_file} " 2> /dev/null
58
+ chmod 600 " ${key_file} "
59
+ }
60
+
48
61
# Run the fdbbackup command.
49
62
# $1 The build directory
50
63
# $2 The scratch directory
51
- # $3 The S3 url.
64
+ # $3 The S3 url
52
65
# $4 credentials file
66
+ # $5 encryption key file (optional)
53
67
function backup {
54
68
local local_build_dir=" ${1} "
55
69
local local_scratch_dir=" ${2} "
56
70
local local_url=" ${3} "
57
71
local local_credentials=" ${4} "
72
+ local local_encryption_key_file=" ${5:- } "
73
+
58
74
# Backup to s3. Without the -k argument in the below, the backup gets
59
75
# 'No restore target version given, will use maximum restorable version from backup description.'
60
76
# TODO: Why is -k needed?
61
- if ! " ${local_build_dir} " /bin/fdbbackup start \
62
- -C " ${local_scratch_dir} /loopback_cluster/fdb.cluster" \
63
- -t " ${TAG} " -w \
64
- -d " ${local_url} " \
65
- -k ' "" \xff' \
66
- --log --logdir=" ${local_scratch_dir} " \
67
- --blob-credentials " ${local_credentials} " \
68
- " ${KNOBS[@]} "
69
- then
77
+ local cmd_args=(
78
+ " -C" " ${local_scratch_dir} /loopback_cluster/fdb.cluster"
79
+ " -t" " ${TAG} " " -w"
80
+ " -d" " ${local_url} "
81
+ " -k" ' "" \xff'
82
+ " --log" " --logdir=${local_scratch_dir} "
83
+ " --blob-credentials" " ${local_credentials} "
84
+ )
85
+
86
+ if [[ -n " ${local_encryption_key_file} " ]]; then
87
+ cmd_args+=(" --encryption-key-file" " ${local_encryption_key_file} " )
88
+ fi
89
+
90
+ for knob in " ${KNOBS[@]} " ; do
91
+ cmd_args+=(" ${knob} " )
92
+ done
93
+
94
+ if ! " ${local_build_dir} " /bin/fdbbackup start " ${cmd_args[@]} " ; then
70
95
err " Start fdbbackup failed"
71
96
return 1
72
97
fi
@@ -77,19 +102,31 @@ function backup {
77
102
# $2 The scratch directory
78
103
# $3 The S3 url
79
104
# $4 credentials file
105
+ # $5 encryption key file (optional)
80
106
function restore {
81
107
local local_build_dir=" ${1} "
82
108
local local_scratch_dir=" ${2} "
83
109
local local_url=" ${3} "
84
110
local local_credentials=" ${4} "
85
- if ! " ${local_build_dir} " /bin/fdbrestore start \
86
- --dest-cluster-file " ${local_scratch_dir} /loopback_cluster/fdb.cluster" \
87
- -t " ${TAG} " -w \
88
- -r " ${url} " \
89
- --log --logdir=" ${local_scratch_dir} " \
90
- --blob-credentials " ${local_credentials} " \
91
- " ${KNOBS[@]} "
92
- then
111
+ local local_encryption_key_file=" ${5:- } "
112
+
113
+ local cmd_args=(
114
+ " --dest-cluster-file" " ${local_scratch_dir} /loopback_cluster/fdb.cluster"
115
+ " -t" " ${TAG} " " -w"
116
+ " -r" " ${url} "
117
+ " --log" " --logdir=${local_scratch_dir} "
118
+ " --blob-credentials" " ${local_credentials} "
119
+ )
120
+
121
+ if [[ -n " ${local_encryption_key_file} " ]]; then
122
+ cmd_args+=(" --encryption-key-file" " ${local_encryption_key_file} " )
123
+ fi
124
+
125
+ for knob in " ${KNOBS[@]} " ; do
126
+ cmd_args+=(" ${knob} " )
127
+ done
128
+
129
+ if ! " ${local_build_dir} " /bin/fdbrestore start " ${cmd_args[@]} " ; then
93
130
err " Start fdbrestore failed"
94
131
return 1
95
132
fi
@@ -100,11 +137,14 @@ function restore {
100
137
# $2 the scratch directory
101
138
# $3 The credentials file.
102
139
# $4 build directory
140
+ # $5 encryption key file (optional)
103
141
function test_s3_backup_and_restore {
104
142
local local_url=" ${1} "
105
143
local local_scratch_dir=" ${2} "
106
144
local credentials=" ${3} "
107
145
local local_build_dir=" ${4} "
146
+ local local_encryption_key_file=" ${5:- } "
147
+
108
148
log " Load data"
109
149
if ! load_data " ${local_build_dir} " " ${local_scratch_dir} " ; then
110
150
err " Failed loading data into fdb"
@@ -130,7 +170,7 @@ function test_s3_backup_and_restore {
130
170
fi
131
171
fi
132
172
log " Run s3 backup"
133
- if ! backup " ${local_build_dir} " " ${local_scratch_dir} " " ${local_url} " " ${credentials} " ; then
173
+ if ! backup " ${local_build_dir} " " ${local_scratch_dir} " " ${local_url} " " ${credentials} " " ${local_encryption_key_file} " ; then
134
174
err " Failed backup"
135
175
return 1
136
176
fi
@@ -140,7 +180,7 @@ function test_s3_backup_and_restore {
140
180
return 1
141
181
fi
142
182
log " Restore from s3"
143
- if ! restore " ${local_build_dir} " " ${local_scratch_dir} " " ${local_url} " " ${credentials} " ; then
183
+ if ! restore " ${local_build_dir} " " ${local_scratch_dir} " " ${local_url} " " ${credentials} " " ${local_encryption_key_file} " ; then
144
184
err " Failed restore"
145
185
return 1
146
186
fi
@@ -179,6 +219,34 @@ set -o nounset # a.k.a. set -u
179
219
set -o pipefail
180
220
set -o noclobber
181
221
222
+ # Parse command line arguments
223
+ USE_ENCRYPTION=false
224
+ PARAMS=()
225
+
226
+ while (( "$# " )) ; do
227
+ case " $1 " in
228
+ --encrypt)
229
+ USE_ENCRYPTION=true
230
+ shift
231
+ ;;
232
+ --encrypt-at-random)
233
+ USE_ENCRYPTION=$(( (RANDOM % 2 )) && echo true || echo false )
234
+ shift
235
+ ;;
236
+ -* |--* =) # unsupported flags
237
+ err " Error: Unsupported flag $1 " >&2
238
+ exit 1
239
+ ;;
240
+ * ) # preserve positional arguments
241
+ PARAMS+=(" $1 " )
242
+ shift
243
+ ;;
244
+ esac
245
+ done
246
+
247
+ # Set positional arguments in their proper place
248
+ set -- " ${PARAMS[@]} "
249
+
182
250
# Globals
183
251
# TEST_SCRATCH_DIR gets set below. Tests should be their data in here.
184
252
# It gets cleaned up on the way out of the test.
@@ -248,7 +316,7 @@ if (( $# < 2 )) || (( $# > 3 )); then
248
316
echo " leave the download of seaweed this directory for other"
249
317
echo " tests to find if they need it. Otherwise, we clean everything"
250
318
echo " else up on our way out."
251
- echo " Example: ${0} ./foundationdb ./build_output ./scratch_dir"
319
+ echo " Example: ${0} ./foundationdb ./build_output ./scratch_dir [--encrypt] "
252
320
exit 1
253
321
fi
254
322
if ! source_dir=$( is_fdb_source_dir " ${1} " ) ; then
@@ -267,6 +335,18 @@ if (( $# == 3 )); then
267
335
fi
268
336
readonly scratch_dir
269
337
338
+ # Create encryption key file if needed
339
+ ENCRYPTION_KEY_FILE=" "
340
+ if [[ " ${USE_ENCRYPTION} " == " true" ]]; then
341
+ log " Enabling encryption for backups"
342
+ ENCRYPTION_KEY_FILE=" ${scratch_dir} /test_encryption_key_file"
343
+ create_encryption_key_file " ${ENCRYPTION_KEY_FILE} "
344
+ log " Created encryption key file at ${ENCRYPTION_KEY_FILE} "
345
+ else
346
+ log " Using plaintext for backups"
347
+ fi
348
+ readonly ENCRYPTION_KEY_FILE
349
+
270
350
# Set host, bucket, and blob_credentials_file whether seaweed or s3.
271
351
readonly path_prefix=" ctests"
272
352
host=
@@ -346,5 +426,5 @@ log "Backup_agent is up"
346
426
# Run tests.
347
427
test=" test_s3_backup_and_restore"
348
428
url=" blobstore://${host} /${path_prefix} /${test} ?${query_str} "
349
- test_s3_backup_and_restore " ${url} " " ${TEST_SCRATCH_DIR} " " ${blob_credentials_file} " " ${build_dir} "
429
+ test_s3_backup_and_restore " ${url} " " ${TEST_SCRATCH_DIR} " " ${blob_credentials_file} " " ${build_dir} " " ${ENCRYPTION_KEY_FILE} "
350
430
log_test_result $? " test_s3_backup_and_restore"
0 commit comments