backup

package
v1.23.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 22, 2026 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Overview

Package backup provides database backup and restore functionality. It supports incremental backups using BadgerDB's native backup mechanism with optional zstd compression.

Index

Constants

View Source
const (
	// DefaultChunkSize is the default size of backup chunks (1MB).
	DefaultChunkSize = 1 << 20

	// MetadataFileName is the name of the backup metadata file.
	MetadataFileName = "backup_metadata.json"
)

Variables

View Source
var (
	// ErrDatabaseClosed is returned when the database is closed.
	ErrDatabaseClosed = errors.New("database is closed")

	// ErrBackupInProgress is returned when a backup is already in progress.
	ErrBackupInProgress = errors.New("backup already in progress")

	// ErrRestoreInProgress is returned when a restore is already in progress.
	ErrRestoreInProgress = errors.New("restore already in progress")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	// DB is the database to backup/restore.
	DB database.Database
	// MetadataDir is the directory to store backup metadata.
	MetadataDir string
	// ChunkSize is the size of backup chunks in bytes.
	ChunkSize int
	// Log is the logger.
	Log log.Logger
}

Config configures the backup service.

type Metadata

type Metadata struct {
	LastVersion    uint64    `json:"lastVersion"`
	LastBackupTime time.Time `json:"lastBackupTime"`
	LastBackupSize uint64    `json:"lastBackupSize"`
	Incremental    bool      `json:"incremental"`
}

Metadata stores information about the last backup.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service provides backup and restore operations.

func New

func New(cfg Config) (*Service, error)

New creates a new backup service.

func (*Service) Backup

func (s *Service) Backup(w io.Writer, since uint64, compress bool) (uint64, error)

Backup performs a database backup, writing to the provided writer. since: 0 for full backup, or the version from the last backup for incremental. compress: whether to compress the output with zstd. Returns the new version number for future incremental backups.

func (*Service) BackupChunked

func (s *Service) BackupChunked(since uint64, compress bool) (<-chan []byte, <-chan error, <-chan uint64)

BackupChunked performs a backup and returns data in chunks. This is useful for streaming backups over gRPC.

func (*Service) BackupToFile

func (s *Service) BackupToFile(path string, since uint64, compress bool) (uint64, error)

BackupToFile performs a backup to a file path.

func (*Service) GetLastVersion

func (s *Service) GetLastVersion() uint64

GetLastVersion returns the version of the last backup. Returns 0 if no backup has been performed.

func (*Service) GetMetadata

func (s *Service) GetMetadata() *Metadata

GetMetadata returns the current backup metadata.

func (*Service) Restore

func (s *Service) Restore(r io.Reader, compressed bool) error

Restore restores the database from the provided reader. The reader should contain backup data (optionally zstd compressed).

func (*Service) RestoreChunked

func (s *Service) RestoreChunked(compressed bool) (chan<- []byte, <-chan error)

RestoreChunked restores from chunks. Send chunks to the returned channel, close when done.

func (*Service) RestoreFromFile

func (s *Service) RestoreFromFile(path string, compressed bool) error

RestoreFromFile restores the database from a file path.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL