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
- Variables
- func ApplyStagedRestore(dbPath string) (bool, error)
- func StagingPath(dbPath string) string
- type Backup
- type Service
- func (s *Service) Create(ctx context.Context, kind string) (Backup, error)
- func (s *Service) Delete(name string) error
- func (s *Service) Dir() string
- func (s *Service) GetSettings(ctx context.Context) (Settings, error)
- func (s *Service) List() ([]Backup, error)
- func (s *Service) Path(name string) (string, error)
- func (s *Service) Restore(name string) error
- func (s *Service) RunScheduled(ctx context.Context) (bool, error)
- func (s *Service) SaveSettings(ctx context.Context, st Settings) error
- type Settings
- type ValidationError
Constants ¶
const ( KindScheduled = "scheduled" KindManual = "manual" )
Kinds of backup.
Variables ¶
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.
var ErrNotFound = errors.New("backup not found")
ErrNotFound is returned for a backup name that doesn't exist.
Functions ¶
func ApplyStagedRestore ¶
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 ¶
StagingPath is where a restore waits for the next start.
Types ¶
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service manages backups for one SQLite database.
func NewService ¶
NewService creates a Service. Backups go in a "backups" folder next to dbPath.
func (*Service) GetSettings ¶
GetSettings returns the saved schedule, or DefaultSettings.
func (*Service) RunScheduled ¶
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.
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.
type ValidationError ¶
type ValidationError struct{ Msg string }
ValidationError explains why settings can't be saved.
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string