@@ -65,7 +65,7 @@ type DumpJob struct {
6565
6666// DumpOptions defines a logical dump options.
6767type DumpOptions struct {
68- DumpFile string `yaml:"dumpLocation"`
68+ DumpLocation string `yaml:"dumpLocation"`
6969 DockerImage string `yaml:"dockerImage"`
7070 Connection Connection `yaml:"connection"`
7171 Source Source `yaml:"source"`
@@ -273,36 +273,19 @@ func (d *DumpJob) Run(ctx context.Context) (err error) {
273273 log .Msg ("Partial dump will be run. Tables for dumping: " , strings .Join (d .Partial .Tables , ", " ))
274274 }
275275
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 {
294277 return errors .Wrap (err , "failed to dump a database" )
295278 }
296279
297- if err := d .markDatabaseData (); err != nil {
298- return errors .Wrap (err , "failed to mark the created dump" )
299- }
300-
301280 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+
302285 if err := recalculateStats (ctx , d .dockerClient , cont .ID , buildAnalyzeCommand (Connection {
303286 DBName : d .config .db .DBName ,
304287 Username : defaults .Username ,
305- })); err != nil {
288+ }, d . DumpOptions . ParallelJobs )); err != nil {
306289 return errors .Wrap (err , "failed to recalculate statistics after restore" )
307290 }
308291 }
@@ -345,16 +328,16 @@ func (d *DumpJob) performDumpCommand(ctx context.Context, cmdOutput io.Writer, c
345328 return nil
346329}
347330
348- func (d * DumpJob ) getDumpContainerPath () string {
349- return d .DumpFile
350- }
351-
352331func (d * DumpJob ) getEnvironmentVariables () []string {
353332 envs := []string {
354- "PGDATA=" + d .globalCfg .DataDir ,
355333 "POSTGRES_HOST_AUTH_METHOD=trust" ,
356334 }
357335
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+
358341 if d .DumpOptions .Source .Type == sourceTypeLocal && d .DumpOptions .Source .Connection .Port == defaults .Port {
359342 log .Msg (fmt .Sprintf ("The default PostgreSQL port is busy, trying to use an alternative one: %d" , reservePort ))
360343 envs = append (envs , "PGPORT=" + strconv .Itoa (reservePort ))
@@ -365,6 +348,7 @@ func (d *DumpJob) getEnvironmentVariables() []string {
365348
366349func (d * DumpJob ) buildContainerConfig () * container.Config {
367350 return & container.Config {
351+ Labels : map [string ]string {"label" : tools .DBLabControlLabel },
368352 Env : d .getEnvironmentVariables (),
369353 Image : d .DockerImage ,
370354 Healthcheck : health .GetConfig (),
@@ -388,11 +372,11 @@ func (d *DumpJob) buildHostConfig() (*container.HostConfig, error) {
388372func (d * DumpJob ) getMountVolumes () []mount.Mount {
389373 mounts := d .dumper .GetMounts ()
390374
391- if d .DumpOptions .DumpFile != "" {
375+ if d .DumpOptions .DumpLocation != "" {
392376 mounts = append (mounts , mount.Mount {
393377 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 ),
396380 })
397381 }
398382
@@ -426,21 +410,27 @@ func (d *DumpJob) getExecEnvironmentVariables() []string {
426410}
427411
428412func (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+ }
430418
431419 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 ),
437425 }
438- dumpCmd = append (dumpCmd , prepareCmdOptions (optionalArgs )... )
426+
427+ dumpCmd := append ([]string {"pg_dump" , "--create" , "--format" , format }, prepareCmdOptions (optionalArgs )... )
439428
440429 for _ , table := range d .Partial .Tables {
441- dumpCmd = append (dumpCmd , "-t " , table )
430+ dumpCmd = append (dumpCmd , "--table " , table )
442431 }
443432
433+ // Define if restore directly or export to dump location.
444434 if d .DumpOptions .Restore != nil {
445435 dumpCmd = append (dumpCmd , d .buildLogicalRestoreCommand ()... )
446436 cmd := strings .Join (dumpCmd , " " )
@@ -450,11 +440,13 @@ func (d *DumpJob) buildLogicalDumpCommand() []string {
450440 return []string {"sh" , "-c" , cmd }
451441 }
452442
443+ dumpCmd = append (dumpCmd , "--file" , d .DumpOptions .DumpLocation )
444+
453445 return dumpCmd
454446}
455447
456448func (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" }
458450
459451 if d .Restore .ForceInit {
460452 restoreCmd = append (restoreCmd , "--clean" , "--if-exists" )
0 commit comments