backup

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package backup makes, lists, prunes and restores database backups, as in Sonarr's System > Backup: scheduled backups every N days kept for M days, plus backups made on demand.

A backup is a consistent copy of the SQLite database made with VACUUM INTO, stored as a plain .db file in a "backups" folder next to the database. Restoring stages the file next to the database; it is swapped in on the next start (ApplyStagedRestore), before the database opens.

Index

Constants

View Source
const (
	KindScheduled = "scheduled"
	KindManual    = "manual"
)

Kinds of backup.

Variables

View Source
var ErrInvalidName = errors.New("invalid backup name")

ErrInvalidName is returned for a name that isn't a backup file name. Names come from the API, so this also blocks path traversal.

View Source
var ErrNotFound = errors.New("backup not found")

ErrNotFound is returned for a backup name that doesn't exist.

Functions

func ApplyStagedRestore

func ApplyStagedRestore(dbPath string) (bool, error)

ApplyStagedRestore swaps a staged restore into place. Call it before opening the database. It reports whether a restore was applied.

The database runs in WAL mode: the old database's -wal and -shm files are removed first. Left in place, SQLite would replay the old database's write-ahead log onto the restored file after an unclean shutdown, corrupting it.

func StagingPath

func StagingPath(dbPath string) string

StagingPath is where a restore waits for the next start.

Types

type Backup

type Backup struct {
	Name      string
	Kind      string
	Size      int64
	CreatedAt time.Time
}

Backup describes one backup file.

type Service

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

Service manages backups for one SQLite database.

func NewService

func NewService(sqlDB *sql.DB, q db.Querier, dbPath string) *Service

NewService creates a Service. Backups go in a "backups" folder next to dbPath.

func (*Service) Create

func (s *Service) Create(ctx context.Context, kind string) (Backup, error)

Create makes a backup of the given kind.

func (*Service) Delete

func (s *Service) Delete(name string) error

Delete removes a backup.

func (*Service) Dir

func (s *Service) Dir() string

Dir is the folder backups are stored in.

func (*Service) GetSettings

func (s *Service) GetSettings(ctx context.Context) (Settings, error)

GetSettings returns the saved schedule, or DefaultSettings.

func (*Service) List

func (s *Service) List() ([]Backup, error)

List returns every backup, newest first.

func (*Service) Path

func (s *Service) Path(name string) (string, error)

Path returns the file path of a backup, checking the name.

func (*Service) Restore

func (s *Service) Restore(name string) error

Restore stages a backup to replace the database on the next start.

func (*Service) RunScheduled

func (s *Service) RunScheduled(ctx context.Context) (bool, error)

RunScheduled makes a scheduled backup if one is due and prunes old ones. It reports whether a backup was made. Safe to call often: the schedule is read from the newest scheduled backup on disk, so it survives restarts.

func (*Service) SaveSettings

func (s *Service) SaveSettings(ctx context.Context, st Settings) error

SaveSettings validates and saves the schedule.

type Settings

type Settings struct {
	// IntervalDays between scheduled backups; 0 turns them off.
	IntervalDays int `json:"interval_days"`
	// RetentionDays scheduled backups are kept. The newest is always kept.
	RetentionDays int `json:"retention_days"`
}

Settings is the backup schedule.

func DefaultSettings

func DefaultSettings() Settings

DefaultSettings matches Sonarr: weekly, kept four weeks.

func (Settings) Validate

func (s Settings) Validate() error

Validate checks s.

type ValidationError

type ValidationError struct{ Msg string }

ValidationError explains why settings can't be saved.

func (*ValidationError) Error

func (e *ValidationError) Error() string

Jump to

Keyboard shortcuts

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