Database backup

There are two public API methods DB.Backup() and DB.Load() which can be used to do online backups and restores. Badger v0.9 provides a CLI tool badger, which can do offline backup/restore. Make sure you have $GOPATH/bin in your PATH to use this tool.

The command below will create a version-agnostic backup of the database, to a file badger.bak in the current working directory

  1. badger backup --dir <path/to/badgerdb>

To restore badger.bak in the current working directory to a new database:

  1. badger restore --dir <path/to/badgerdb>

See badger --help for more details.

If you have a Badger database that was created using v0.8 (or below), you can use the badger_backup tool provided in v0.8.1, and then restore it using the command above to upgrade your database to work with the latest version.

  1. badger_backup --dir <path/to/badgerdb> --backup-file badger.bak

We recommend all users to use the Backup and Restore APIs and tools. However, Badger is also rsync-friendly because all files are immutable, barring the latest value log which is append-only. So, rsync can be used as rudimentary way to perform a backup. In the following script, we repeat rsync to ensure that the LSM tree remains consistent with the MANIFEST file while doing a full backup.

  1. #!/bin/bash
  2. set -o history
  3. set -o histexpand
  4. # Makes a complete copy of a Badger database directory.
  5. # Repeat rsync if the MANIFEST and SSTables are updated.
  6. rsync -avz --delete db/ dst
  7. while !! | grep -q "(MANIFEST\|\.sst)$"; do :; done