@@ -404,17 +404,33 @@ func (d *DumpJob) getPassword() string {
404404}
405405
406406func (d * DumpJob ) cleanupDumpLocation (ctx context.Context , dumpContID string ) error {
407- if d .DumpOptions .DumpLocation != "" && d .DumpOptions .Restore .Enabled {
408- cleanupCmd := []string {"rm" , "-rf" , path .Join (d .DumpOptions .DumpLocation , "*" )}
407+ if d .DumpOptions .DumpLocation == "" || d .DumpOptions .Restore .Enabled {
408+ return nil
409+ }
409410
410- log .Msg ("Running cleanup command: " , cleanupCmd )
411+ ls , err := tools .LsContainerDirectory (ctx , d .dockerClient , dumpContID , d .DumpOptions .DumpLocation )
412+ if err != nil {
413+ return errors .Wrap (err , "failed to clean up dump location" )
414+ }
411415
412- if out , err := tools .ExecCommandWithOutput (ctx , d .dockerClient , dumpContID , types.ExecConfig {
413- Cmd : cleanupCmd ,
414- }); err != nil {
415- log .Dbg (out )
416- return errors .Wrap (err , "failed to clean up dump location" )
417- }
416+ if len (ls ) == 0 {
417+ return nil
418+ }
419+
420+ cleanupCmd := []string {"rm" , "-rf" }
421+
422+ for _ , dbName := range ls {
423+ cleanupCmd = append (cleanupCmd , path .Join (d .DumpOptions .DumpLocation , dbName ))
424+ }
425+
426+ log .Msg ("Running cleanup command: " , cleanupCmd )
427+
428+ if out , err := tools .ExecCommandWithOutput (ctx , d .dockerClient , dumpContID , types.ExecConfig {
429+ Tty : true ,
430+ Cmd : cleanupCmd ,
431+ }); err != nil {
432+ log .Dbg (out )
433+ return errors .Wrap (err , "failed to clean up dump location" )
418434 }
419435
420436 return nil
@@ -443,12 +459,13 @@ func (d *DumpJob) dumpDatabase(ctx context.Context, dumpContID, dbName string, d
443459}
444460
445461func setupPGData (ctx context.Context , dockerClient * client.Client , dataDir string , dumpContID string ) error {
446- isEmpty , err := tools .IsEmptyContainerDirectory (ctx , dockerClient , dumpContID , dataDir )
462+ entryList , err := tools .LsContainerDirectory (ctx , dockerClient , dumpContID , dataDir )
447463 if err != nil {
448464 return errors .Wrap (err , "failed to explore the data directory" )
449465 }
450466
451- if ! isEmpty {
467+ // Already initialized.
468+ if len (entryList ) != 0 {
452469 return nil
453470 }
454471
0 commit comments