-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
464 lines (363 loc) · 16.5 KB
/
flags.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package mongounarchive
import (
"context"
"errors"
"flag"
"os"
"path"
"github.com/egose/database-tools/storage"
"github.com/egose/database-tools/utils"
mlog "github.com/mongodb/mongo-tools/common/log"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
const (
envPrefix = "MONGOUNARCHIVE__"
)
var (
verbose *string
quietPtr *bool
hostPtr *string
portPtr *string
sslPtr *bool
sslCAFilePtr *string
sslPEMKeyFilePtr *string
sslPEMKeyPasswordPtr *string
sslCRLFilePtr *string
sslAllowInvalidCertificatesPtr *bool
sslAllowInvalidHostnamesPtr *bool
sslFIPSModePtr *bool
usernamePtr *string
passwordPtr *string
authenticationDatabasePtr *string
authenticationMechanismPtr *string
gssapiServiceNamePtr *string
gssapiHostNamePtr *string
uriPtr *string
uriPrunePtr *bool
dbPtr *string
collectionPtr *string
nsExcludePtr *string
nsIncludePtr *string
nsFromPtr *string
nsToPtr *string
dropPtr *bool
dryRunPtr *bool
writeConcernPtr *string
noIndexRestorePtr *bool
noOptionsRestorePtr *bool
keepIndexVersionPtr *bool
maintainInsertionOrderPtr *bool
numParallelCollectionsPtr *string
numInsertionWorkersPerCollectionPtr *string
stopOnErrorPtr *bool
bypassDocumentValidationPtr *bool
preserveUUIDPtr *bool
azAccountNamePtr *string
azAccountKeyPtr *string
azContainerNamePtr *string
awsAccessKeyIdPtr *string
awsSecretAccessKeyPtr *string
awsRegionPtr *string
awsBucketPtr *string
gcpBucketPtr *string
gcpCredsFilePtr *string
gcpProjectIDPtr *string
gcpPrivateKeyIdPtr *string
gcpPrivateKeyPtr *string
gcpClientEmailPtr *string
gcpClientIDPtr *string
localPathPtr *string
objectNamePtr *string
dirPtr *string
updatesPtr *string
updatesFilePtr *string
keepPtr *bool
)
func ParseFlags() {
// verbosity options:
verbose = flag.String("verbose", os.Getenv(envPrefix+"VERBOSE"), "more detailed log output (include multiple times for more verbosity, e.g. -vvvvv, or specify a numeric value, e.g. --verbose=N)")
quietPtr = flag.Bool("quiet", os.Getenv(envPrefix+"QUIET") == "true", "hide all log output")
// connection options:
hostPtr = flag.String("host", os.Getenv(envPrefix+"HOST"), "MongoDB host to connect to (setname/host1,host2 for replica sets)")
portPtr = flag.String("port", os.Getenv(envPrefix+"PORT"), "MongoDB port (can also use --host hostname:port)")
// ssl options:
sslPtr = flag.Bool("ssl", os.Getenv(envPrefix+"SSL") == "true", "connect to a mongod or mongos that has ssl enabled")
sslCAFilePtr = flag.String("ssl-ca-file", os.Getenv(envPrefix+"SSL_CA_FILE"), "the .pem file containing the root certificate chain from the certificate authority")
sslPEMKeyFilePtr = flag.String("ssl-pem-key-file", os.Getenv(envPrefix+"SSL_PEM_KEY_FILE"), "the .pem file containing the certificate and key")
sslPEMKeyPasswordPtr = flag.String("ssl-pem-key-password", os.Getenv(envPrefix+"SSL_PEM_KEY_PASSWORD"), "the password to decrypt the sslPEMKeyFile, if necessary")
sslCRLFilePtr = flag.String("ssl-crl-file", os.Getenv(envPrefix+"SSL_CRL_File"), "the .pem file containing the certificate revocation list")
sslAllowInvalidCertificatesPtr = flag.Bool("ssl-allow-invalid-certificates", os.Getenv(envPrefix+"SSL_ALLOW_INVALID_CERTIFICATES") == "true", "bypass the validation for server certificates")
sslAllowInvalidHostnamesPtr = flag.Bool("ssl-allow-invalid-hostnames", os.Getenv(envPrefix+"SSL_ALLOW_INVALID_HOSTNAMES") == "true", "bypass the validation for server name")
sslFIPSModePtr = flag.Bool("ssl-fips-mode", os.Getenv(envPrefix+"SSL_FIPS_MODE") == "true", "use FIPS mode of the installed openssl library")
// authentication options:
usernamePtr = flag.String("username", os.Getenv(envPrefix+"USERNAME"), "username for authentication")
passwordPtr = flag.String("password", os.Getenv(envPrefix+"PASSWORD"), "password for authentication")
authenticationDatabasePtr = flag.String("authentication-database", os.Getenv(envPrefix+"AUTHENTICATION_DATABASE"), "database that holds the user's credentials")
authenticationMechanismPtr = flag.String("authentication-mechanism", os.Getenv(envPrefix+"AUTHENTICATION_MECHANISM"), "authentication mechanism to use")
// kerberos options:
gssapiServiceNamePtr = flag.String("gssapi-service-name", os.Getenv(envPrefix+"GSSAPI_SERVICE_NAME"), "service name to use when authenticating using GSSAPI/Kerberos (default: mongodb)")
gssapiHostNamePtr = flag.String("gssapi-host-name", os.Getenv(envPrefix+"GSSAPI_HOST_NAME"), "hostname to use when authenticating using GSSAPI/Kerberos (default: <remote server's address>)")
// uri options:
uriPtr = flag.String("uri", os.Getenv(envPrefix+"URI"), "MongoDB uri connection string")
uriPrunePtr = flag.Bool("uri-prune", os.Getenv(envPrefix+"URI_PRUNE") == "true", "prune MongoDB uri connection string")
// namespace options:
dbPtr = flag.String("db", os.Getenv(envPrefix+"DB"), "database to use")
collectionPtr = flag.String("collection", os.Getenv(envPrefix+"COLLECTION"), "collection to use")
nsExcludePtr = flag.String("ns-exclude", os.Getenv(envPrefix+"NS_EXCLUDE"), "exclude matching namespaces")
nsIncludePtr = flag.String("ns-include", os.Getenv(envPrefix+"NS_INCLUDE"), "include matching namespaces")
nsFromPtr = flag.String("ns-from", os.Getenv(envPrefix+"NS_FROM"), "rename matching namespaces, must have matching nsTo")
nsToPtr = flag.String("ns-to", os.Getenv(envPrefix+"NS_TO"), "rename matched namespaces, must have matching nsFrom")
// restore options:
dropPtr = flag.Bool("drop", os.Getenv(envPrefix+"DROP") == "true", "drop each collection before import")
dryRunPtr = flag.Bool("dry-run", os.Getenv(envPrefix+"DRY_RUN") == "true", "view summary without importing anything. recommended with verbosity")
writeConcernPtr = flag.String("write-concern", os.Getenv(envPrefix+"WRITE_CONCERN"), "write concern options")
noIndexRestorePtr = flag.Bool("no-index-restore", os.Getenv(envPrefix+"NO_INDEX_RESTORE") == "true", "don't restore indexes")
noOptionsRestorePtr = flag.Bool("no-options-restore", os.Getenv(envPrefix+"NO_OPTIONS_RESTORE") == "true", "don't restore collection options")
keepIndexVersionPtr = flag.Bool("keep-index-version", os.Getenv(envPrefix+"KEEP_INDEX_VERSION") == "true", "don't update index version")
maintainInsertionOrderPtr = flag.Bool("maintain-insertion-order", os.Getenv(envPrefix+"MAINTAIN_INSERTION_ORDER") == "true", "restore the documents in the order of their appearance in the input source. By default the insertions will be performed in an arbitrary order. Setting this flag also enables the behavior of --stopOnError and restricts NumInsertionWorkersPerCollection to 1")
numParallelCollectionsPtr = flag.String("num-parallel-collections", os.Getenv(envPrefix+"NUM_PARALLEL_COLLECTIONS"), "number of collections to restore in parallel (default: 4)")
numInsertionWorkersPerCollectionPtr = flag.String("num-insertion-workers-per-collection", os.Getenv(envPrefix+"NUM_INSERTION_WORKERS_PER_COLLECTION"), "number of insert operations to run concurrently per collection (default: 1)")
stopOnErrorPtr = flag.Bool("stop-on-error", os.Getenv(envPrefix+"STOP_ON_ERROR") == "true", "halt after encountering any error during insertion. By default, mongorestore will attempt to continue through document validation and DuplicateKey errors, but with this option enabled, the tool will stop instead. A small number of documents may be inserted after encountering an error even with this option enabled; use --maintainInsertionOrder to halt immediately after an error")
bypassDocumentValidationPtr = flag.Bool("bypass-document-validation", os.Getenv(envPrefix+"BYPASS_DOCUMENT_VALIDATION") == "true", "bypass document validation")
preserveUUIDPtr = flag.Bool("preserve-uuid", os.Getenv(envPrefix+"PRESERVE_UUID") == "true", "preserve original collection UUIDs (off by default, requires drop)")
azAccountNamePtr = flag.String("az-account-name", os.Getenv(envPrefix+"AZ_ACCOUNT_NAME"), "Azure Blob Storage Account Name")
azAccountKeyPtr = flag.String("az-account-key", os.Getenv(envPrefix+"AZ_ACCOUNT_KEY"), "Azure Blob Storage Account Key")
azContainerNamePtr = flag.String("az-container-name", os.Getenv(envPrefix+"AZ_CONTAINER_NAME"), "Azure Blob Storage Container Name")
awsAccessKeyIdPtr = flag.String("aws-access-key-id", os.Getenv(envPrefix+"AWS_ACCESS_KEY_ID"), "AWS access key associated with an IAM account")
awsSecretAccessKeyPtr = flag.String("aws-secret-access-key", os.Getenv(envPrefix+"AWS_SECRET_ACCESS_KEY"), "AWS secret key associated with the access key")
awsRegionPtr = flag.String("aws-region", os.Getenv(envPrefix+"AWS_REGION"), "AWS Region whose servers you want to send your requests to")
awsBucketPtr = flag.String("aws-bucket", os.Getenv(envPrefix+"AWS_BUCKET"), "AWS S3 bucket name")
gcpBucketPtr = flag.String("gcp-bucket", os.Getenv(envPrefix+"GCP_BUCKET"), "GCP storage bucket name")
gcpCredsFilePtr = flag.String("gcp-creds-file", os.Getenv(envPrefix+"GCP_CREDS_FILE"), "GCP service account's credentials file")
gcpProjectIDPtr = flag.String("gcp-project-id", os.Getenv(envPrefix+"GCP_PROJECT_ID"), "GCP service account's project id")
gcpPrivateKeyIdPtr = flag.String("gcp-private-key-id", os.Getenv(envPrefix+"GCP_PRIVATE_KEY_ID"), "GCP service account's private key id")
gcpPrivateKeyPtr = flag.String("gcp-private-key", os.Getenv(envPrefix+"GCP_PRIVATE_KEY"), "GCP service account's private key")
gcpClientEmailPtr = flag.String("gcp-client-email", os.Getenv(envPrefix+"GCP_CLIENT_EMAIL"), "GCP service account's client email")
gcpClientIDPtr = flag.String("gcp-client-id", os.Getenv(envPrefix+"GCP_CLIENT_ID"), "GCP service account's client id")
localPathPtr = flag.String("local-path", os.Getenv(envPrefix+"LOCAL_PATH"), "Local directory path to store backups")
objectNamePtr = flag.String("object-name", os.Getenv(envPrefix+"OBJECT_NAME"), "Object name of the archived file in the storage (optional)")
dirPtr = flag.String("dir", os.Getenv(envPrefix+"DIR"), "directory name that contains the dumped files")
updatesPtr = flag.String("updates", os.Getenv(envPrefix+"UPDATES"), "array of update specifications in JSON string")
updatesFilePtr = flag.String("updates-file", os.Getenv(envPrefix+"UPDATES_FILE"), "path to a file containing an array of update specifications")
keepPtr = flag.Bool("keep", os.Getenv(envPrefix+"KEEP") == "true", "keep data dump")
flag.Parse()
}
func GetMongounarchiveOptions(destPath string) []string {
options := []string{
"--gzip",
"--drop",
}
tdir := ""
if *dirPtr != "" {
tdir = *dirPtr
} else if *dbPtr != "" {
tdir = *dbPtr
}
options = append(options, "--dir="+path.Join(destPath, tdir))
if *verbose != "" {
options = append(options, "--verbose="+*verbose)
}
if *quietPtr {
options = append(options, "--quiet")
}
if *hostPtr != "" {
options = append(options, "--host="+*hostPtr)
}
if *portPtr != "" {
options = append(options, "--port="+*portPtr)
}
if *sslPtr {
options = append(options, "--ssl")
}
if *sslCAFilePtr != "" {
options = append(options, "--sslCAFile="+*sslCAFilePtr)
}
if *sslPEMKeyFilePtr != "" {
options = append(options, "--sslPEMKeyFile="+*sslPEMKeyFilePtr)
}
if *sslPEMKeyPasswordPtr != "" {
options = append(options, "--sslPEMKeyPassword="+*sslPEMKeyPasswordPtr)
}
if *sslCRLFilePtr != "" {
options = append(options, "--sslCRLFile="+*sslCRLFilePtr)
}
if *sslAllowInvalidCertificatesPtr {
options = append(options, "--sslAllowInvalidCertificates")
}
if *sslAllowInvalidHostnamesPtr {
options = append(options, "--sslAllowInvalidHostnames")
}
if *sslFIPSModePtr {
options = append(options, "--sslFIPSMode")
}
if *usernamePtr != "" {
options = append(options, "--username="+*usernamePtr)
}
if *passwordPtr != "" {
options = append(options, "--password="+*passwordPtr)
}
if *authenticationDatabasePtr != "" {
options = append(options, "--authenticationDatabase="+*authenticationDatabasePtr)
}
if *authenticationMechanismPtr != "" {
options = append(options, "--authenticationMechanism="+*authenticationMechanismPtr)
}
if *gssapiServiceNamePtr != "" {
options = append(options, "--gssapiServiceName="+*gssapiServiceNamePtr)
}
if *gssapiHostNamePtr != "" {
options = append(options, "--gssapiHostName="+*gssapiHostNamePtr)
}
if *uriPtr != "" {
uri := *uriPtr
if *uriPrunePtr {
uri = utils.PruneMongoDBURI(uri)
}
options = append(options, "--uri="+uri)
}
if *dbPtr != "" {
options = append(options, "--db="+*dbPtr)
}
if *collectionPtr != "" {
options = append(options, "--collection="+*collectionPtr)
}
if *nsExcludePtr != "" {
options = append(options, "--nsExclude="+*nsExcludePtr)
}
if *nsIncludePtr != "" {
options = append(options, "--nsInclude="+*nsIncludePtr)
}
if *nsFromPtr != "" {
options = append(options, "--nsFrom="+*nsFromPtr)
}
if *nsToPtr != "" {
options = append(options, "--nsTo="+*nsToPtr)
}
if *dropPtr {
options = append(options, "--drop")
}
if *dryRunPtr {
options = append(options, "--dryRun")
}
if *writeConcernPtr != "" {
options = append(options, "--writeConcern="+*writeConcernPtr)
}
if *noIndexRestorePtr {
options = append(options, "--noIndexRestore")
}
if *noOptionsRestorePtr {
options = append(options, "--noOptionsRestore")
}
if *keepIndexVersionPtr {
options = append(options, "--keepIndexVersion")
}
if *maintainInsertionOrderPtr {
options = append(options, "--maintainInsertionOrder")
}
if *numParallelCollectionsPtr != "" {
options = append(options, "--numParallelCollections="+*numParallelCollectionsPtr)
}
if *numInsertionWorkersPerCollectionPtr != "" {
options = append(options, "--numInsertionWorkersPerCollection="+*numInsertionWorkersPerCollectionPtr)
}
if *stopOnErrorPtr {
options = append(options, "--stopOnError")
}
if *bypassDocumentValidationPtr {
options = append(options, "--bypassDocumentValidation")
}
if *preserveUUIDPtr {
options = append(options, "--preserveUUID")
}
return options
}
func getAzBlob() (*storage.AzBlob, error) {
az := new(storage.AzBlob)
err := az.Init(*azAccountNamePtr, *azAccountKeyPtr, *azContainerNamePtr)
if err != nil {
return nil, err
}
return az, nil
}
func getAwsS3() (*storage.AwsS3, error) {
s3 := new(storage.AwsS3)
err := s3.Init(*awsAccessKeyIdPtr, *awsSecretAccessKeyPtr, *awsRegionPtr, *awsBucketPtr)
if err != nil {
return nil, err
}
return s3, nil
}
func getGCP() (*storage.GcpStorage, error) {
storage := new(storage.GcpStorage)
err := storage.Init(*gcpBucketPtr, *gcpCredsFilePtr, *gcpProjectIDPtr, *gcpPrivateKeyIdPtr, *gcpPrivateKeyPtr, *gcpClientEmailPtr, *gcpClientIDPtr)
if err != nil {
return nil, err
}
return storage, nil
}
func getLocal() (*storage.LocalStorage, error) {
storage := new(storage.LocalStorage)
err := storage.Init(*localPathPtr, 0)
if err != nil {
return nil, err
}
return storage, nil
}
func GetStorage() (storage.Storage, error) {
if useAzure() {
mlog.Logvf(mlog.Always, "Found Storage Option: %v", "Azure")
return getAzBlob()
}
if useAWS() {
mlog.Logvf(mlog.Always, "Found Storage Option: %v", "AWS")
return getAwsS3()
}
if useGCP() {
mlog.Logvf(mlog.Always, "Found Storage Option: %v", "GCP")
return getGCP()
}
if useLocal() {
mlog.Logvf(mlog.Always, "Found Storage Option: %v", "Local")
return getLocal()
}
return nil, errors.New("no storage provider detected")
}
func GetObjectName() string {
return *objectNamePtr
}
func GetMongoClient() (*mongo.Client, *mongo.Database, error) {
client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(*uriPtr))
if err != nil {
return nil, nil, err
}
dbClient := client.Database(*dbPtr)
return client, dbClient, nil
}
func GetUpdates() ([]byte, error) {
if *updatesPtr != "" {
return []byte(*updatesPtr), nil
} else if *updatesFilePtr != "" {
content, err := os.ReadFile(*updatesFilePtr)
return content, err
}
return []byte(""), nil
}
func HasUpdates() bool {
return *updatesPtr != "" || *updatesFilePtr != ""
}
func HasKeep() bool {
return *keepPtr
}
func useAzure() bool {
return *azAccountNamePtr != "" && *azAccountKeyPtr != "" && *azContainerNamePtr != ""
}
func useAWS() bool {
return *awsAccessKeyIdPtr != "" && *awsSecretAccessKeyPtr != "" && *awsRegionPtr != "" && *awsBucketPtr != ""
}
func useGCP() bool {
return *gcpBucketPtr != ""
}
func useLocal() bool {
return *localPathPtr != ""
}