Skip to content

Commit

Permalink
fix(Backup/Restore): Keep all versions (#1462) (#1509)
Browse files Browse the repository at this point in the history
Do not drop any versions while doing a backup and restore.

(cherry picked from commit 5d8aee6)

Co-authored-by: Ibrahim Jarif <[email protected]>
  • Loading branch information
NamanJain8 and Ibrahim Jarif authored Sep 10, 2020
1 parent 058a414 commit e3c326e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
17 changes: 14 additions & 3 deletions badger/cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cmd

import (
"bufio"
"math"
"os"

"github.com/dgraph-io/badger"
Expand All @@ -27,6 +28,7 @@ import (

var backupFile string
var truncate bool
var numVersions int

// backupCmd represents the backup command
var backupCmd = &cobra.Command{
Expand All @@ -47,13 +49,22 @@ func init() {
"badger.bak", "File to backup to")
backupCmd.Flags().BoolVarP(&truncate, "truncate", "t",
false, "Allow value log truncation if required.")
backupCmd.Flags().IntVarP(&numVersions, "num-versions", "n",
0, "Number of versions to keep. A value <= 0 means keep all versions.")
}

func doBackup(cmd *cobra.Command, args []string) error {
// Open DB
db, err := badger.Open(badger.DefaultOptions(sstDir).
opt := badger.DefaultOptions(sstDir).
WithValueDir(vlogDir).
WithTruncate(truncate))
WithTruncate(truncate).
WithNumVersionsToKeep(math.MaxUint32)

if numVersions > 0 {
opt.NumVersionsToKeep = numVersions
}

// Open DB
db, err := badger.Open(opt)
if err != nil {
return err
}
Expand Down
5 changes: 4 additions & 1 deletion badger/cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cmd

import (
"errors"
"math"
"os"
"path"

Expand Down Expand Up @@ -65,7 +66,9 @@ func doRestore(cmd *cobra.Command, args []string) error {
}

// Open DB
db, err := badger.Open(badger.DefaultOptions(sstDir).WithValueDir(vlogDir))
db, err := badger.Open(badger.DefaultOptions(sstDir).
WithValueDir(vlogDir).
WithNumVersionsToKeep(math.MaxUint32))
if err != nil {
return err
}
Expand Down

0 comments on commit e3c326e

Please sign in to comment.