@@ -47,7 +47,7 @@ import (
47
47
const (
48
48
defaultRSdbpath = "/data/db"
49
49
defaultCSRSdbpath = "/data/configdb"
50
- fallbackDir = ".fallbackDbPath "
50
+ fallbackDir = ".fallbacksync "
51
51
52
52
mongofslock = "mongod.lock"
53
53
@@ -302,6 +302,57 @@ func (r *PhysRestore) flush(ctx context.Context) error {
302
302
return nil
303
303
}
304
304
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
+
305
356
func nodeShutdown (ctx context.Context , m * mongo.Client ) error {
306
357
err := m .Database ("admin" ).RunCommand (ctx , bson.D {{"shutdown" , 1 }}).Err ()
307
358
if err == nil || strings .Contains (err .Error (), "socket was unexpectedly closed" ) {
0 commit comments