Skip to content

Commit 17603ca

Browse files
committed
Merge branch '294-cleanup-dump-location' into 'master'
fix: cleanup the dumpLocation directory before taking a dump as it can restore an outdated dump from the previous launches (#294) Closes #294 See merge request postgres-ai/database-lab!358
2 parents a8f9a46 + f644fa8 commit 17603ca

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

pkg/retrieval/engine/postgres/logical/dump.go

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -404,17 +404,33 @@ func (d *DumpJob) getPassword() string {
404404
}
405405

406406
func (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

445461
func 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

pkg/retrieval/engine/postgres/tools/tools.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,9 @@ func MakeDir(ctx context.Context, dockerClient *client.Client, dumpContID, dataD
215215
return nil
216216
}
217217

218-
// IsEmptyContainerDirectory checks whether a directory is empty.
219-
func IsEmptyContainerDirectory(ctx context.Context, dockerClient *client.Client, containerID, dir string) (bool, error) {
220-
lsCommand := []string{"ls", "-A", dir}
218+
// LsContainerDirectory lists content of the directory in a container.
219+
func LsContainerDirectory(ctx context.Context, dockerClient *client.Client, containerID, dir string) ([]string, error) {
220+
lsCommand := []string{"ls", "-A", dir, "--color=never"}
221221

222222
log.Dbg("Check directory: ", lsCommand)
223223

@@ -228,10 +228,10 @@ func IsEmptyContainerDirectory(ctx context.Context, dockerClient *client.Client,
228228

229229
if err != nil {
230230
log.Dbg(out)
231-
return false, errors.Wrap(err, "failed to init Postgres")
231+
return nil, errors.Wrap(err, "failed to init Postgres")
232232
}
233233

234-
return out == "", nil
234+
return strings.Fields(out), nil
235235
}
236236

237237
// StartPostgres stops Postgres inside container.

0 commit comments

Comments
 (0)