Skip to content

Commit

Permalink
Merge pull request #1364 from wal-g/fix-bin-backup-mongo42
Browse files Browse the repository at this point in the history
Fix binary backup of mongodb 4.2
  • Loading branch information
KhurtinDN committed Oct 27, 2022
2 parents 5041e57 + 29ca0ed commit 018b4c9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 14 deletions.
22 changes: 13 additions & 9 deletions internal/databases/mongo/binary/mongod.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (mongodService *MongodService) GetBackupCursorExtended(backupCursorMeta *Ba
})
}

func (mongodService *MongodService) FixSystemDataAfterRestore(LastWriteTS primitive.Timestamp) error {
func (mongodService *MongodService) FixSystemDataAfterRestore(LastWriteTS primitive.Timestamp, fixOplog bool) error {
ctx := mongodService.Context
localDatabase := mongodService.MongoClient.Database("local")

Expand All @@ -145,14 +145,18 @@ func (mongodService *MongodService) FixSystemDataAfterRestore(LastWriteTS primit
return err
}

tracelog.DebugLogger.Printf("oplogTruncateAfterPoint: %v", LastWriteTS)
err = replaceData(ctx, localDatabase.Collection("replset.oplogTruncateAfterPoint"), true,
bson.M{
"_id": "oplogTruncateAfterPoint",
"oplogTruncateAfterPoint": LastWriteTS,
})
if err != nil {
return err
if fixOplog {
tracelog.DebugLogger.Printf("oplogTruncateAfterPoint: %v", LastWriteTS)
err = replaceData(ctx, localDatabase.Collection("replset.oplogTruncateAfterPoint"), true,
bson.M{
"_id": "oplogTruncateAfterPoint",
"oplogTruncateAfterPoint": LastWriteTS,
})
if err != nil {
return err
}
} else {
tracelog.InfoLogger.Printf("We are skipping fix replset.oplogTruncateAfterPoint because it is disabled")
}

err = replaceData(ctx, localDatabase.Collection("system.replset"), false, nil)
Expand Down
19 changes: 14 additions & 5 deletions internal/databases/mongo/binary/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,29 @@ func (restoreService *RestoreService) DoRestore(backupName, restoreMongodVersion
return err
}

err = restoreService.fixSystemData(sentinel)
if err != nil {
needFixOplog := NeedFixOplog(restoreMongodVersion)

if err = restoreService.fixSystemData(sentinel, needFixOplog); err != nil {
return err
}

return restoreService.recoverFromOplogAsStandalone()
if needFixOplog {
if err = restoreService.recoverFromOplogAsStandalone(); err != nil {
return err
}
} else {
tracelog.InfoLogger.Printf("We are skipping recoverFromOplogAsStandalone because it is disabled")
}

return nil
}

func (restoreService *RestoreService) downloadFromTarArchives(backupName string) error {
downloader := CreateConcurrentDownloader(restoreService.Uploader)
return downloader.Download(backupName, restoreService.LocalStorage.MongodDBPath)
}

func (restoreService *RestoreService) fixSystemData(sentinel *models.Backup) error {
func (restoreService *RestoreService) fixSystemData(sentinel *models.Backup, needFixOplog bool) error {
mongodProcess, err := StartMongodWithDisableLogicalSessionCacheRefresh(restoreService.minimalConfigPath)
if err != nil {
return errors.Wrap(err, "unable to start mongod in special mode")
Expand All @@ -80,7 +89,7 @@ func (restoreService *RestoreService) fixSystemData(sentinel *models.Backup) err
}

lastWriteTS := sentinel.MongoMeta.BackupLastTS
err = mongodService.FixSystemDataAfterRestore(lastWriteTS)
err = mongodService.FixSystemDataAfterRestore(lastWriteTS, needFixOplog)
if err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions internal/databases/mongo/binary/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"golang.org/x/mod/semver"
)

const minOplogRecoverSupportedVersion = "4.2"

// MajorMinorVersion return version in format '<MajorVersion>.<MinorVersion>' (without patch, prerelease, build, ...)
func MajorMinorVersion(version string) string {
if len(version) == 0 {
Expand All @@ -29,3 +31,7 @@ func EnsureCompatibilityToRestoreMongodVersions(backupMongodVersion, restoreMong
}
return nil
}

func NeedFixOplog(mongodVersion string) bool {
return semver.Compare(MajorMinorVersion(mongodVersion), MajorMinorVersion(minOplogRecoverSupportedVersion)) > 0
}
15 changes: 15 additions & 0 deletions internal/databases/mongo/binary/utilities_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package binary

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNeedFixOplog(t *testing.T) {
assert.False(t, NeedFixOplog("4.0"))
assert.False(t, NeedFixOplog("4.2"))
assert.True(t, NeedFixOplog("4.4"))
assert.True(t, NeedFixOplog("5.0"))
assert.True(t, NeedFixOplog("6.0"))
}

0 comments on commit 018b4c9

Please sign in to comment.