Skip to content

Commit acf263c

Browse files
committed
Add logic for migration to fallback dir
... for dbpath dir content
1 parent 930f489 commit acf263c

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

pbm/restore/physical.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import (
4747
const (
4848
defaultRSdbpath = "/data/db"
4949
defaultCSRSdbpath = "/data/configdb"
50-
fallbackDir = ".fallbackDbPath"
50+
fallbackDir = ".fallbacksync"
5151

5252
mongofslock = "mongod.lock"
5353

@@ -302,6 +302,57 @@ func (r *PhysRestore) flush(ctx context.Context) error {
302302
return nil
303303
}
304304

305+
// migrateDbDirToFallbackDir moves content of dbPath dir into fallback dir.
306+
// It also removes old fallback dir, and creates new with the same perms.
307+
func (r *PhysRestore) migrateDbDirToFallbackDir() error {
308+
dbpath := filepath.Clean(r.dbpath)
309+
fallbackPath := filepath.Join(dbpath, fallbackDir)
310+
r.log.Debug("dbpath: %s, fallbackPath: %s", dbpath, fallbackPath)
311+
312+
r.log.Debug("remove old %s", fallbackPath)
313+
err := os.RemoveAll(fallbackPath)
314+
if err != nil {
315+
return errors.Wrap(err, "remove fallback db path")
316+
}
317+
318+
r.log.Debug("create new %s", fallbackPath)
319+
info, err := os.Stat(dbpath)
320+
if err != nil {
321+
return errors.Wrap(err, "stat")
322+
}
323+
err = os.MkdirAll(fallbackPath, info.Mode())
324+
if err != nil {
325+
return errors.Wrapf(err, "creating dir %s", fallbackPath)
326+
}
327+
328+
err = r.moveToFallback()
329+
if err != nil {
330+
return errors.Wrapf(err, "fail to move to %s", fallbackPath)
331+
}
332+
333+
return nil
334+
}
335+
336+
// moveFromFallback moves all files/dirs from fallback dir to dbpath dir.
337+
func (r *PhysRestore) moveFromFallback() error {
338+
return moveAll(
339+
path.Join(r.dbpath, fallbackDir),
340+
r.dbpath,
341+
nil,
342+
r.log,
343+
)
344+
}
345+
346+
// moveToFallback moves all files/dirs except fallback dir from dbpath to fallback dir
347+
func (r *PhysRestore) moveToFallback() error {
348+
return moveAll(
349+
r.dbpath,
350+
path.Join(r.dbpath, fallbackDir),
351+
[]string{fallbackDir},
352+
r.log,
353+
)
354+
}
355+
305356
func nodeShutdown(ctx context.Context, m *mongo.Client) error {
306357
err := m.Database("admin").RunCommand(ctx, bson.D{{"shutdown", 1}}).Err()
307358
if err == nil || strings.Contains(err.Error(), "socket was unexpectedly closed") {

0 commit comments

Comments
 (0)