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 thecobra
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 theRootCmd
. - Defines the
backup-file
flag (short namef
) to specify the backup file path, defaulting to "badger.bak". - Defines the
num-versions
flag (short namen
) to specify the number of versions to keep, defaulting to 0 (keep all versions).
- Adds the
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<<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 thecobra.Command
object.args
: A slice of strings representing the command-line arguments.
- Returns: An
error
object, ornil
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 usingbo.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.
- Parameters:
Code Examples
None.