Skip to main content

backup.go

backup.go - Overview

This file implements the backup command for the Badger database, allowing users to create a version-agnostic backup of the database to a specified file.

Detailed Documentation

bo struct

var bo = struct {
backupFile string
numVersions int
}{}
  • Purpose: Holds the command-line options for the backup command.
    • backupFile: The file path where the backup will be stored.
    • numVersions: The number of versions of each key to keep in the backup.

backupCmd variable

var backupCmd = &cobra.Command{
Use: "backup",
Short: "Backup Badger database.",
Long: `Backup Badger database to a file in a version-agnostic manner.

Iterates over each key-value pair, encodes it along with its metadata and
version in protocol buffers and writes them to a file. This file can later be
used by the restore command to create an identical copy of the
database.`,
RunE: doBackup,
}
  • Purpose: Defines the backup command using the cobra library.
    • Use: The command's name (backup).
    • Short: A short description of the command.
    • Long: A detailed description of the command.
    • RunE: The function to execute when the command is invoked (doBackup).

init function

func init() {
RootCmd.AddCommand(backupCmd)
backupCmd.Flags().StringVarP(&bo.backupFile, "backup-file", "f",
"badger.bak", "File to backup to")
backupCmd.Flags().IntVarP(&bo.numVersions, "num-versions", "n",
0, "Number of versions to keep. A value <= 0 means keep all versions.")
}
  • Purpose: Initializes the backup command by adding it to the root command and defining its flags.
    • Adds the backupCmd to the RootCmd.
    • Defines the backup-file flag (short name f) to specify the backup file path, defaulting to "badger.bak".
    • Defines the num-versions flag (short name n) to specify the number of versions to keep, defaulting to 0 (keep all versions).

doBackup function

func doBackup(cmd *cobra.Command, args []string) error {
opt := badger.DefaultOptions(sstDir).
WithValueDir(vlogDir).
WithNumVersionsToKeep(math.MaxInt32)

if bo.numVersions > 0 {
opt.NumVersionsToKeep = bo.numVersions
}

// Open DB
db, err := badger.Open(opt)
if err != nil {
return err
}
defer db.Close()

// Create File
f, err := os.Create(bo.backupFile)
if err != nil {
return err
}

bw := bufio.NewWriterSize(f, 64&lt;&lt;20)
if _, err = db.Backup(bw, 0); err != nil {
return err
}

if err = bw.Flush(); err != nil {
return err
}

if err = f.Sync(); err != nil {
return err
}

return f.Close()
}
  • Purpose: Executes the backup process.
    • Parameters:
      • cmd: A pointer to the cobra.Command object.
      • args: A slice of strings representing the command-line arguments.
    • Returns: An error object, or nil if the backup was successful.
    • Sets up Badger options, opens the database, creates the backup file, performs the backup using db.Backup, flushes the buffer, syncs the file to disk, and closes the file.
    • opt: badger options, ValueDir and NumVersionsToKeep are set.
    • The NumVersionsToKeep is set using bo.numVersions value passed in argument.
    • A buffered writer bw is created to write to the backup file.
    • db.Backup writes the backup to the buffered writer.
    • The buffered writer is flushed and synced before closing the file.

Code Examples

None.

Getting Started Relevance