@@ -65,7 +65,7 @@ type DumpJob struct {
65
65
66
66
// DumpOptions defines a logical dump options.
67
67
type DumpOptions struct {
68
- DumpFile string `yaml:"dumpLocation"`
68
+ DumpLocation string `yaml:"dumpLocation"`
69
69
DockerImage string `yaml:"dockerImage"`
70
70
Connection Connection `yaml:"connection"`
71
71
Source Source `yaml:"source"`
@@ -273,36 +273,19 @@ func (d *DumpJob) Run(ctx context.Context) (err error) {
273
273
log .Msg ("Partial dump will be run. Tables for dumping: " , strings .Join (d .Partial .Tables , ", " ))
274
274
}
275
275
276
- var output io.Writer = os .Stdout
277
-
278
- if d .DumpOptions .DumpFile != "" {
279
- dumpFile , err := os .Create (d .getDumpContainerPath ())
280
- if err != nil {
281
- return errors .Wrap (err , "failed to create file" )
282
- }
283
-
284
- defer func () {
285
- if err := dumpFile .Close (); err != nil {
286
- log .Err ("failed to close dump file" , err )
287
- }
288
- }()
289
-
290
- output = dumpFile
291
- }
292
-
293
- if err := d .performDumpCommand (ctx , output , cont .ID , execCommand .ID ); err != nil {
276
+ if err := d .performDumpCommand (ctx , os .Stdout , cont .ID , execCommand .ID ); err != nil {
294
277
return errors .Wrap (err , "failed to dump a database" )
295
278
}
296
279
297
- if err := d .markDatabaseData (); err != nil {
298
- return errors .Wrap (err , "failed to mark the created dump" )
299
- }
300
-
301
280
if d .DumpOptions .Restore != nil {
281
+ if err := d .markDatabaseData (); err != nil {
282
+ return errors .Wrap (err , "failed to mark the created dump" )
283
+ }
284
+
302
285
if err := recalculateStats (ctx , d .dockerClient , cont .ID , buildAnalyzeCommand (Connection {
303
286
DBName : d .config .db .DBName ,
304
287
Username : defaults .Username ,
305
- })); err != nil {
288
+ }, d . DumpOptions . ParallelJobs )); err != nil {
306
289
return errors .Wrap (err , "failed to recalculate statistics after restore" )
307
290
}
308
291
}
@@ -345,16 +328,16 @@ func (d *DumpJob) performDumpCommand(ctx context.Context, cmdOutput io.Writer, c
345
328
return nil
346
329
}
347
330
348
- func (d * DumpJob ) getDumpContainerPath () string {
349
- return d .DumpFile
350
- }
351
-
352
331
func (d * DumpJob ) getEnvironmentVariables () []string {
353
332
envs := []string {
354
- "PGDATA=" + d .globalCfg .DataDir ,
355
333
"POSTGRES_HOST_AUTH_METHOD=trust" ,
356
334
}
357
335
336
+ // Avoid initialization of PostgreSQL directory in case of preparing of a dump.
337
+ if d .DumpOptions .Restore != nil {
338
+ envs = append (envs , "PGDATA=" + d .globalCfg .DataDir )
339
+ }
340
+
358
341
if d .DumpOptions .Source .Type == sourceTypeLocal && d .DumpOptions .Source .Connection .Port == defaults .Port {
359
342
log .Msg (fmt .Sprintf ("The default PostgreSQL port is busy, trying to use an alternative one: %d" , reservePort ))
360
343
envs = append (envs , "PGPORT=" + strconv .Itoa (reservePort ))
@@ -365,6 +348,7 @@ func (d *DumpJob) getEnvironmentVariables() []string {
365
348
366
349
func (d * DumpJob ) buildContainerConfig () * container.Config {
367
350
return & container.Config {
351
+ Labels : map [string ]string {"label" : tools .DBLabControlLabel },
368
352
Env : d .getEnvironmentVariables (),
369
353
Image : d .DockerImage ,
370
354
Healthcheck : health .GetConfig (),
@@ -388,11 +372,11 @@ func (d *DumpJob) buildHostConfig() (*container.HostConfig, error) {
388
372
func (d * DumpJob ) getMountVolumes () []mount.Mount {
389
373
mounts := d .dumper .GetMounts ()
390
374
391
- if d .DumpOptions .DumpFile != "" {
375
+ if d .DumpOptions .DumpLocation != "" {
392
376
mounts = append (mounts , mount.Mount {
393
377
Type : mount .TypeBind ,
394
- Source : filepath .Dir (d .DumpOptions .DumpFile ),
395
- Target : filepath .Dir (d .DumpOptions .DumpFile ),
378
+ Source : filepath .Dir (d .DumpOptions .DumpLocation ),
379
+ Target : filepath .Dir (d .DumpOptions .DumpLocation ),
396
380
})
397
381
}
398
382
@@ -426,21 +410,27 @@ func (d *DumpJob) getExecEnvironmentVariables() []string {
426
410
}
427
411
428
412
func (d * DumpJob ) buildLogicalDumpCommand () []string {
429
- dumpCmd := []string {"pg_dump" , "-C" , "-Fc" }
413
+ format := "custom"
414
+
415
+ if d .DumpOptions .ParallelJobs > defaultParallelJobs {
416
+ format = "directory"
417
+ }
430
418
431
419
optionalArgs := map [string ]string {
432
- "-h" : d .config .db .Host ,
433
- "-p" : strconv .Itoa (d .config .db .Port ),
434
- "-U " : d .config .db .Username ,
435
- "-d" : d .config .db .DBName ,
436
- "-j" : strconv .Itoa (d .DumpOptions .ParallelJobs ),
420
+ "--host" : d .config .db .Host ,
421
+ "--port" : strconv .Itoa (d .config .db .Port ),
422
+ "--username " : d .config .db .Username ,
423
+ "--dbname" : d .config .db .DBName ,
424
+ "--jobs" : strconv .Itoa (d .DumpOptions .ParallelJobs ),
437
425
}
438
- dumpCmd = append (dumpCmd , prepareCmdOptions (optionalArgs )... )
426
+
427
+ dumpCmd := append ([]string {"pg_dump" , "--create" , "--format" , format }, prepareCmdOptions (optionalArgs )... )
439
428
440
429
for _ , table := range d .Partial .Tables {
441
- dumpCmd = append (dumpCmd , "-t " , table )
430
+ dumpCmd = append (dumpCmd , "--table " , table )
442
431
}
443
432
433
+ // Define if restore directly or export to dump location.
444
434
if d .DumpOptions .Restore != nil {
445
435
dumpCmd = append (dumpCmd , d .buildLogicalRestoreCommand ()... )
446
436
cmd := strings .Join (dumpCmd , " " )
@@ -450,11 +440,13 @@ func (d *DumpJob) buildLogicalDumpCommand() []string {
450
440
return []string {"sh" , "-c" , cmd }
451
441
}
452
442
443
+ dumpCmd = append (dumpCmd , "--file" , d .DumpOptions .DumpLocation )
444
+
453
445
return dumpCmd
454
446
}
455
447
456
448
func (d * DumpJob ) buildLogicalRestoreCommand () []string {
457
- restoreCmd := []string {"|" , "pg_restore" , "-U " , defaults .Username , "-C " , "-d " , defaults .DBName , "--no-privileges" }
449
+ restoreCmd := []string {"|" , "pg_restore" , "--username " , defaults .Username , "--create " , "--dbname " , defaults .DBName , "--no-privileges" }
458
450
459
451
if d .Restore .ForceInit {
460
452
restoreCmd = append (restoreCmd , "--clean" , "--if-exists" )
0 commit comments