Skip to content

Commit 3d74bc0

Browse files
committed
Remove internal mongod logs before phys restore
1 parent 1379885 commit 3d74bc0

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

pbm/restore/physical.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,10 @@ func (r *PhysRestore) migrateDBDirToFallbackDir() error {
374374
if err != nil {
375375
return errors.Wrap(err, "remove fallback db path")
376376
}
377+
err = removeInternalMongoLogs(dbpath, r.log)
378+
if err != nil {
379+
return errors.Wrap(err, "remove internal mongod log(s)")
380+
}
377381

378382
r.log.Debug("create %s", fallbackPath)
379383
info, err := os.Stat(dbpath)
@@ -2588,6 +2592,8 @@ func moveAll(fromDir, toDir string, toIgnore []string, l log.LogEvent) error {
25882592
return nil
25892593
}
25902594

2595+
// removeAll removes all files and directories from specified dir.
2596+
// It ignores files selected with filesSkipRules parameter.
25912597
func removeAll(dir string, l log.LogEvent, fileSkipRules ...fileSkipRule) error {
25922598
d, err := os.Open(dir)
25932599
if err != nil {
@@ -2612,6 +2618,33 @@ func removeAll(dir string, l log.LogEvent, fileSkipRules ...fileSkipRule) error
26122618
return nil
26132619
}
26142620

2621+
// removeInternalMongoLogs removes internal mongod logs from directory.
2622+
// It'll remove everything that starts with 'pbm.restore.log'
2623+
func removeInternalMongoLogs(dir string, l log.LogEvent) error {
2624+
d, err := os.Open(dir)
2625+
if err != nil {
2626+
return errors.Wrap(err, "open dir")
2627+
}
2628+
defer d.Close()
2629+
2630+
names, err := d.Readdirnames(-1)
2631+
if err != nil {
2632+
return errors.Wrap(err, "read file names")
2633+
}
2634+
for _, n := range names {
2635+
if isInternalMongoLog(n) {
2636+
err = os.RemoveAll(filepath.Join(dir, n))
2637+
if err != nil {
2638+
return errors.Wrapf(err, "remove '%s", n)
2639+
}
2640+
2641+
l.Debug("remove %s", filepath.Join(dir, n))
2642+
}
2643+
}
2644+
2645+
return nil
2646+
}
2647+
26152648
// isInternalMongoLog checks whether the file with the name f
26162649
// is internal mongo log file
26172650
func isInternalMongoLog(f string) bool {

pbm/restore/physical_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,25 @@ func TestRemoveAll(t *testing.T) {
246246
})
247247
}
248248

249+
func TestRemoveInternalMongoLogs(t *testing.T) {
250+
tmpDir := setupTestFiles(t)
251+
252+
err := removeInternalMongoLogs(tmpDir, log.DiscardEvent)
253+
if err != nil {
254+
t.Fatalf("got error when removing internal mongod logs, err=%v", err)
255+
}
256+
257+
files := readDir(t, tmpDir)
258+
if len(files) != 6 {
259+
t.Fatalf("only mongod log files should be removed, expected 6 files, got=%d files", len(files))
260+
}
261+
for _, f := range files {
262+
if strings.HasPrefix(f, internalMongodLog) {
263+
t.Fatalf("internal mongod log file is not deleted: %s", f)
264+
}
265+
}
266+
}
267+
249268
func readDir(t *testing.T, dir string) []string {
250269
t.Helper()
251270

0 commit comments

Comments
 (0)