Docs Package reference

backup

Replicates SQLite databases to S3-compatible storage with Litestream, embedded in the process.

Import path github.com/zweiundeins/lith/backup, a module of its own: go get github.com/zweiundeins/lith/backup. Generated from the doc comments in backup/.

It is a module of its own so that projects without it never pull in Litestream's dependencies.

A Manager replicates every database attached to it, each under a name: its key prefix in the bucket, such as an account ID. Without a bucket (or Dir) the Manager is disabled and does nothing, so development runs without credentials.

Start each database in this order:

  1. RestoreIfMissing, before the database is opened. Opening creates an empty file, and a restore that finds a file does nothing.
  2. Open the database. While the Manager is enabled its write connection sets PRAGMA wal_autocheckpoint=0 (see WALAutocheckpoint) and leaves checkpoints to Litestream, which makes them at positions it has replicated, and keeps the WAL past its last close (SQLITE_FCNTL_PERSIST_WAL). lith's sqlite.Open does both with sqlite.CheckpointExternal. Without Litestream the WAL would grow without bound, so keep SQLite's default then.
  3. Attach. It returns once the first sync has reached the replica.

Run RunMaintenanceLoop and RunVerifyLoop once per process. An incident at the storage provider fails every replica at once and usually passes by itself, so RunVerifyLoop retries a failed verification before it reports, and reports once per run: alert on its OnFail, not on each failure.

A restore reads the newest snapshot and every WAL segment since, a file per second of writes. It downloads them with 4 connections and merges them 32 at a time, in a directory beside its output, so what it holds does not grow with their number; it needs disk for them and the restored file. A killed restore's directory goes with the next restore beside it once untouched for six hours. A replica with Litestream v0.3 generations restores through Litestream's own restore, which picks the newer format. VerifyRestore logs every outcome, also a restore cut off. To keep a restore out of the server's memory, run VerifyOnce from a command that a timer starts, with a memory limit of its own, in place of RunVerifyLoop.

Both loops cover the attached replicas. An app that attaches its tenants on demand passes its account list as VerifyOptions.Names, so every tenant is verified, and as MaintenanceOptions.Names, so every tenant's history older than the retention window is pruned, attached or not. A tenant not attached gets no snapshot, since nothing changed while it was not; Attach writes one in the background when it is due.

Litestream writes each WAL segment to the directory beside the database (MetaPath) before the replica uploads it. An attached replica removes the uploaded ones from there every 15 s once they are Config.LocalL0Retention old (5 minutes), and keeps the newest, so that directory holds minutes of writes. A segment the replica has not uploaded stays: a replica that cannot reach its storage keeps every write since, on the server's disk. Watch <namespace>_backup_replica_local_l0_bytes.

Stop in this order: drain HTTP and the writer, so every accepted write is in the file, and end the reads; call detach with a live context (the signal's context is already cancelled); then close the database, without a checkpoint of the application's own. detach has Litestream checkpoint the WAL and start it over, which Litestream records, then makes the final sync. The close keeps that WAL of one frame, so the next Attach finds the database where the final sync left it and uploads nothing for a database that did not change, and the next open has no frames to read from the WAL. A WAL deleted at the close, or emptied by PRAGMA wal_checkpoint(TRUNCATE) after detach, leaves Litestream no way to tell, and the next Attach uploads the whole database as a new LTX file. A WAL closed with its frames, as a read transaction open during detach leaves it (detach logs a warning then), costs memory instead: SQLite's recovery at the next open counts every frame since the WAL last started over as not yet checkpointed, and a connection, the writer's too, reads those pages from the WAL into its page cache instead of through the memory map, and keeps them until another connection commits after Litestream's next checkpoint. The process's stop timeout (systemd TimeoutStopSec) must cover the drain, the checkpoint and the final sync: killed mid-sync, the replica trails the file.

Litestream opens the database with modernc.org/sqlite, so the application must use it too: two SQLite copies in one process break each other's locks. Only one process may replicate a database at a time. Pruning relies on it: with one replicating process the positions in a replica only grow, so the pruning of a replica not attached is safe beside an Attach of it. A second writer, or a prefix and name reused for another database, resets the positions, and then any pruning is unsafe, attached or not.

An upload of 5 MiB or more to S3 goes up in parts, and one cut off (a snapshot that detach cancels, a crash) can leave its parts in the bucket, unlisted as objects. The Manager aborts the upload of a snapshot cut off by detach, shutdown or Config.SnapshotTimeout, best effort, before the name can be attached again, and PurgeReplica aborts every one under the name before it deletes, so an erasure leaves nothing. A provider that does not implement ListMultipartUploads gets none aborted, logged once. As a backstop, give the bucket a lifecycle rule that aborts incomplete multipart uploads after a day where the provider supports one.

Constants

Go
const (
	DefaultRetentionWindow     = 30 * 24 * time.Hour
	DefaultSnapshotInterval    = 24 * time.Hour
	DefaultMaintenanceInterval = time.Hour
	DefaultVerifyInterval      = 24 * time.Hour
	DefaultSnapshotTimeout     = 10 * time.Minute

	// DefaultPurgeSettle covers the few seconds Infomaniak's S3 over Swift
	// (s3.pub2) was seen to list late.
	DefaultPurgeSettle = 15 * time.Second

	// DefaultLocalL0Retention is Litestream's own: how long a WAL segment
	// stays in the local directory after it reached the replica.
	DefaultLocalL0Retention = litestream.DefaultL0Retention
)

Policy defaults. RetentionWindow is also the point-in-time recovery floor: nothing older is restorable.

Go
const (
	DefaultMaintenanceConcurrency = 2
	DefaultMaintenancePause       = 100 * time.Millisecond
	DefaultDetachedInterval       = 24 * time.Hour
	DefaultDetachedConcurrency    = 1
	DefaultDetachedPause          = 250 * time.Millisecond
)

Maintenance defaults. A pass over the attached replicas maintains two at a time and waits after each. Names not attached get a pass a day, one at a time with a longer wait, since each costs a client of its own (see MaintenanceOptions.Names). Both spread a long list, so that the storage provider sees no burst of listings and deletes.

Variables

Go
var (
	// ErrNothingToRestore means nothing was ever replicated under the name,
	// or nothing at or before the requested time.
	ErrNothingToRestore = errors.New("backup: nothing to restore")
	// ErrDisabled is returned by the operations that need a replica (Restore,
	// RestoreAt, RestoreWindow, RestoreTimeline, VerifyRestore) when the
	// Manager is disabled.
	ErrDisabled = errors.New("backup: not configured")
)

Functions

func MetaPath

Go
func MetaPath(dbPath string) string

MetaPath is the directory Litestream keeps beside a replicated database (".<file>-litestream"). Remove it with the database, and the database's -wal and -shm files, when erasing one.

Types

type Config

Go
type Config struct {
	Bucket          string
	Endpoint        string // empty for AWS; set for other S3-compatible services
	Region          string
	AccessKeyID     string // empty: the AWS default credential chain
	SecretAccessKey string
	ForcePathStyle  bool // most S3-compatible services other than AWS need it

	// ContentMD5 has Litestream send a Content-MD5 header with every upload
	// it makes in a single PutObject: those smaller than the part size of 5
	// MiB, and those of exactly 5 MiB. A provider that enforces S3 Object
	// Lock rules refuses an upload without Content-MD5 or an x-amz-checksum
	// header. Larger uploads go up in parts through the SDK's upload
	// manager, which never sends Content-MD5 on a part: with an Endpoint
	// the parts carry no checksum at all, and on AWS (no Endpoint) each
	// carries the SDK's CRC32. A provider that wants a checksum on each
	// part too refuses the larger uploads to an Endpoint: ContentMD5 covers
	// snapshots and WAL segments of up to 5 MiB. DeleteObjects carries a
	// Content-MD5 with or without it.
	ContentMD5 bool

	Dir string

	// Prefix goes in front of every replica's name, so that deployments can
	// share a bucket.
	Prefix string

	// Zero means DefaultRetentionWindow and DefaultSnapshotInterval.
	RetentionWindow  time.Duration
	SnapshotInterval time.Duration

	// SnapshotTimeout bounds the snapshot Attach starts in the background
	// when one is due; zero means DefaultSnapshotTimeout.
	SnapshotTimeout time.Duration

	// LocalL0Retention is how long a WAL segment stays in the directory
	// beside the database (MetaPath) once it has reached the replica; zero
	// means DefaultLocalL0Retention. A segment not uploaded yet stays, and so
	// does the newest, which Litestream reads.
	LocalL0Retention time.Duration

	// PurgeSettle is how long PurgeReplica waits after its deletes before
	// it lists an S3 replica again, deleting what the listing shows, and
	// between two more listings while one shows objects. Zero means
	// DefaultPurgeSettle. A provider whose listings lag its writes, such as
	// S3 over OpenStack Swift (Infomaniak's s3.pub2), can leave an object
	// written just before the purge out of the first listing; AWS S3 and
	// Infomaniak's native S3 (s3.gva2) list every object at once, and a
	// millisecond will do.
	PurgeSettle time.Duration

	// Logger defaults to slog.Default().
	Logger *slog.Logger
}

Config says where replicas go: set Bucket for S3, or Dir for a local directory (a mounted volume, or tests). With neither, the Manager is disabled.

func ConfigFromEnv

Go
func ConfigFromEnv(prefix string) (Config, error)

ConfigFromEnv reads a Config from the environment variables prefix plus BUCKET, ENDPOINT, REGION, ACCESS_KEY_ID, SECRET_ACCESS_KEY, FORCE_PATH_STYLE and CONTENT_MD5 (bools), DIR, PREFIX, RETENTION, SNAPSHOT_INTERVAL, SNAPSHOT_TIMEOUT, LOCAL_L0_RETENTION and PURGE_SETTLE (durations such as 720h). With prefix "BACKUP_" the bucket is BACKUP_BUCKET.

type MaintenanceOptions

Go
type MaintenanceOptions struct {
	// Interval is the time between passes over the attached replicas; zero
	// means DefaultMaintenanceInterval. It only has to be shorter than the
	// snapshot interval.
	Interval time.Duration

	// Concurrency is the number of attached replicas maintained at once; zero
	// means DefaultMaintenanceConcurrency.
	Concurrency int

	// Pause is the wait after each attached replica before the same worker
	// takes the next; zero means DefaultMaintenancePause, a negative value
	// none. It sets a floor of 50 ms per replica with the defaults; the round
	// trips to the provider usually take longer.
	Pause time.Duration

	// Names, if set, lists the replicas of a second loop, for the ones not
	// attached: an app that attaches its tenants on demand passes its
	// account list, since a tenant not attached at the time is otherwise
	// never pruned. That loop skips the attached names. A name not attached
	// gets no snapshot, since nothing changed while it was not attached,
	// only the pruning of its history older than the retention window,
	// through the bucket alone: one listing, and one more with a snapshot
	// older than the window. A name with nothing replicated is no failure.
	//
	// Each such name takes a replica client of its own, and with S3 a new
	// HTTP transport and TLS handshake, whose idle connection lingers for
	// 90 s. There is no cache of clients, since thousands of tenants would
	// keep thousands of transports; the cost is held down by the cadence
	// instead: a pass a day, one name at a time.
	Names func(ctx context.Context) ([]string, error)

	// DetachedInterval is the time from the start of one pass over Names to
	// the start of the next; zero means DefaultDetachedInterval. The first
	// starts after at most ten minutes, and a pass that runs longer is
	// followed at once by the next, never overlapped.
	DetachedInterval time.Duration

	// DetachedConcurrency and DetachedPause are Concurrency and Pause for
	// the names not attached; zero means DefaultDetachedConcurrency and
	// DefaultDetachedPause. With the defaults 1,000 names take at least four
	// minutes, and in practice more: the round trips, a handshake and one or
	// two listings per name, dominate.
	DetachedConcurrency int
	DetachedPause       time.Duration
}

MaintenanceOptions configure RunMaintenanceLoop.

type Manager

Go
type Manager struct {
	// contains filtered or unexported fields
}

Manager owns replication for the process. Its methods are safe for concurrent use.

func New

Go
func New(cfg Config) (*Manager, error)

New returns a Manager for cfg.

func (*Manager) Attach

Go
func (m *Manager) Attach(ctx context.Context, name, dbPath string) (detach func(context.Context) error, err error)

Attach starts replicating the database at dbPath under name. It returns once the first sync has reached the replica, since until then a final sync does nothing. dbPath must exist and name must not be attached already.

Call detach once, before the application closes the database and after its writes and reads have ended: it has Litestream checkpoint the WAL and start it over, when it holds more than one frame, and makes the final sync. Close the database after it without a checkpoint of your own (see the package doc). A cancelled context gets a fresh one for it, bounded by Litestream's shutdown sync timeout. On a disabled Manager detach does nothing.

When the newest snapshot is older than the snapshot interval, Attach writes one in the background after the first sync, bounded by Config.SnapshotTimeout, so that a database attached only between maintenance passes still gets snapshots, and its WAL segments a floor to be pruned at. detach cancels it and waits for it to stop before the final sync, on S3 with the abort of the upload it cut off (10 s at most); a database detached before its snapshot is done gets one on its next Attach.

Litestream v0.5.18 keeps the encoder of the last LTX file it wrote for each attached database, about 155 KiB of Go heap, more while that file is a whole large database, until a fix upstream: count it against GOMEMLIMIT where hundreds of databases are attached.

func (*Manager) Collector

Go
func (m *Manager) Collector(namespace string) prometheus.Collector

Collector returns a prometheus.Collector of the Manager's state, read from Status on each scrape. Register it even when the Manager is disabled: <namespace>_backup_enabled 0 is the alert for a process without a backup. Series with a replica label cover the attached replicas; maintenance of the others counts in the detached totals, so that a long MaintenanceOptions.Names adds no series. The pruned counts are close, not exact: a batch delete that fails in part counts nothing, and a key already gone counts as pruned. <namespace>_backup_detached_last_pass_timestamp_seconds older than twice the detached interval is the alert for a loop over the names that does not run, or whose Names keeps failing. It reads 0 until the first pass ends, about ten minutes after the start, so the alert needs "and ... > 0" or a "for:". A pass in which every name failed still sets it: the failures counter and the error log are the signal for that.

func (*Manager) Enabled

Go
func (m *Manager) Enabled() bool

Enabled reports whether the Manager replicates.

func (*Manager) MaintainAll

Go
func (m *Manager) MaintainAll(ctx context.Context, now time.Time) map[string]error

MaintainAll runs one maintenance pass on every attached replica, as of now: a full snapshot when the newest is older than the snapshot interval, then pruning of the history older than the retention window.

func (*Manager) PurgeReplica

Go
func (m *Manager) PurgeReplica(ctx context.Context, name string) error

PurgeReplica deletes everything replicated under name, for an erasure: the objects, and on S3 the parts of every unfinished multipart upload under it, which a listing of objects does not show. It fails without deleting anything when those uploads cannot be listed or aborted, so that a retry finds them. On S3 it lists the replica again Config.PurgeSettle after its deletes, for a provider whose listings lag, and deletes what that listing shows; it lists up to three times while objects show, and fails when the third listing still shows some, so it takes PurgeSettle at least. An object whose listing lags longer is still missed: an erasure that must be certain purges again later, which is harmless. The database must be detached first. Purging a name never replicated, or on a disabled Manager, does nothing.

func (*Manager) ReplicaNames

Go
func (m *Manager) ReplicaNames(ctx context.Context) ([]string, error)

ReplicaNames lists the names of the replicas the bucket (or Dir) holds under Config.Prefix, sorted, without the prefix: every name something was ever replicated under, attached or not, including a name whose PurgeReplica failed and left objects behind. An app that removes tenants compares it to its own account list and purges the names it no longer has. With no Prefix that is every top-level name in the bucket, also those of anything else stored there. Names PurgeReplica would refuse are left out. A disabled Manager returns nil.

func (*Manager) Restore

Go
func (m *Manager) Restore(ctx context.Context, name, out string) error

Restore writes the latest state of the replica under name to out, which must not exist yet.

func (*Manager) RestoreAt

Go
func (m *Manager) RestoreAt(ctx context.Context, name, out string, at time.Time) error

RestoreAt writes the replica under name as of at (zero for the latest) to out, which must not exist yet. Any moment inside the retention window is reachable, not only snapshots. A time before the oldest retained point gives ErrNothingToRestore; check it against RestoreWindow for a clearer message.

func (*Manager) RestoreIfMissing

Go
func (m *Manager) RestoreIfMissing(ctx context.Context, name, dbPath string) error

RestoreIfMissing restores the replica under name to dbPath when no file is there: a first boot on a fresh disk, or recovery after losing one. It does nothing when the Manager is disabled, the file exists, or nothing was ever replicated under name. A restore that fails quick_check is removed again. Like every restore, it first removes a -wal or -shm file left beside the missing database, which SQLite would otherwise replay into the restored file.

func (*Manager) RestoreTimeline

Go
func (m *Manager) RestoreTimeline(ctx context.Context, name string) (Timeline, error)

RestoreTimeline lists the replica under name as a Timeline. Snapshots are listed first, so a truncated timeline still has all of them.

func (*Manager) RestoreWindow

Go
func (m *Manager) RestoreWindow(ctx context.Context, name string) (earliest, latest time.Time, err error)

RestoreWindow reports the times a restore of the replica under name can reach: after earliest, the oldest full image (a snapshot or the first sync), up to latest. It lists the replica, so call it on demand, not per request.

func (*Manager) RetentionWindow

Go
func (m *Manager) RetentionWindow() time.Duration

RetentionWindow is how far back a restore can reach.

func (*Manager) RunMaintenanceLoop

Go
func (m *Manager) RunMaintenanceLoop(ctx context.Context, opt MaintenanceOptions)

RunMaintenanceLoop maintains the attached replicas every opt.Interval and, when opt.Names is set, the names not attached every opt.DetachedInterval, until ctx is done. An attached replica costs a listing of each level per pass, for Status. Its failure is logged as a warning, since the next pass retries it, and as an error from its third failed pass in a row on. The names not attached that fail are logged once per pass, as a count with the first few names, by the same rule, and so is a failure of opt.Names.

func (*Manager) RunVerifyLoop

Go
func (m *Manager) RunVerifyLoop(ctx context.Context, opt VerifyOptions)

RunVerifyLoop verifies every attached replica, or opt.Names, until ctx is done: the first run after opt.FirstDelay, then one every opt.Interval from the start of the one before. The replicas a run finds failing are verified again after each of opt.Retries, and those that never pass go to opt.OnFail, once per run. When ctx ends during a run, the run reports nothing to OnFail; VerifyRestore logs the replica it cut off.

A restore runs in the calling process. To keep its memory and I/O out of the server, run VerifyOnce from a command of its own instead, under a timer with its own memory limit, and leave this loop off.

func (*Manager) Status

Go
func (m *Manager) Status() []ReplicaStatus

Status reports every attached replica, sorted by name. It reads no network, so it is cheap enough for a metrics scrape.

func (*Manager) VerifyAll

Go
func (m *Manager) VerifyAll(ctx context.Context) map[string]error

VerifyAll runs VerifyRestore on every attached replica, and stops when ctx is done.

func (*Manager) VerifyOnce

Go
func (m *Manager) VerifyOnce(ctx context.Context, opt VerifyOptions) error

VerifyOnce is one run of RunVerifyLoop, for a command that a timer starts: it verifies every attached replica, or opt.Names (a command attaches nothing, so set it), retries the failed ones after each of opt.Retries, calls opt.OnFail once with those that never passed, and returns their errors joined, or nil when every replica passed. It returns ctx's error when ctx ends first. Status in the command's process shows the results; a server's Status does not.

func (*Manager) VerifyRestore

Go
func (m *Manager) VerifyRestore(ctx context.Context, name string) error

VerifyRestore proves the replica under name is restorable: it restores the latest state to a temporary file, runs PRAGMA integrity_check and removes it. The result shows in Status while the replica is attached; it is kept for a replica that is not, and shows once it is. A restore cut off by a cancelled ctx keeps no result, one cut off by its deadline fails, and one that overlaps PurgeReplica of the name keeps none. It reads the whole replica, so it belongs in a daily loop, not on a request.

func (*Manager) WALAutocheckpoint

Go
func (m *Manager) WALAutocheckpoint() int

WALAutocheckpoint is the value for PRAGMA wal_autocheckpoint on the application's write connection: 0 while the Manager replicates, since Litestream then checkpoints, and SQLite's default of 1000 pages otherwise.

type ReplicaStatus

Go
type ReplicaStatus struct {
	Name       string
	Path       string
	LastSyncAt time.Time // zero until the first sync
	TXID       string    // replicated position, "" until known

	LastVerifyAt time.Time // zero until VerifyRestore ran
	VerifyOK     bool      // the last VerifyRestore restored and passed integrity_check

	// From RunVerifyLoop. VerifyAttempts is the number of verifications in its
	// last run: 1 if the replica passed at once, 0 until a run has finished.
	// VerifyRetried counts the runs in which it failed and then passed on a
	// retry.
	VerifyAttempts int
	VerifyRetried  int

	// From the last maintenance pass; zero until one ran.
	LastSnapshotAt time.Time
	ReplicaObjects int
	ReplicaBytes   int64

	// The maintenance passes that completed while the replica was attached,
	// and the objects they pruned. Passes over it while it was not attached
	// count in the Collector's detached totals.
	MaintenancePasses int
	PrunedObjects     int

	// MaintenanceSkipped counts the passes that found the replica busy (a
	// snapshot after Attach, or the pruning of names not attached) and left
	// it to the next; such a pass neither ends nor adds to a failure streak.
	MaintenanceSkipped int

	// The WAL segments in the directory beside the database at the last
	// trim, which runs every 15 s, and the ones the trims removed since
	// Attach. A count that keeps growing means the replica falls behind:
	// a segment stays until it is uploaded.
	LocalL0Files   int
	LocalL0Bytes   int64
	LocalL0Removed int
}

ReplicaStatus is the state of one attached replica, read from memory.

type RestorePoint

Go
type RestorePoint struct {
	At       time.Time
	TXID     string
	Level    int
	Snapshot bool // a full image of the database, not a WAL segment
	Size     int64
}

RestorePoint is one point a restore can land on: an LTX file's time and position. A restore to a time lands on the last point at or before it.

type Timeline

Go
type Timeline struct {
	Earliest  time.Time
	Latest    time.Time
	Points    []RestorePoint
	Truncated bool // the listing stopped at maxRestorePoints: the newest WAL segments are missing
}

Timeline is every restore point of a replica, oldest first, for a point-in-time picker that shows where history is and where it has gaps.

type VerifyFailure

Go
type VerifyFailure struct {
	Name        string
	First, Last error
	Attempts    int
}

VerifyFailure is a replica that failed every verification of a run. First and Last are the errors of its first and last attempt. The failures of one run share Attempts, since each went through every retry.

type VerifyOptions

Go
type VerifyOptions struct {
	// Interval is the time from one run's start to the next one's; zero means
	// DefaultVerifyInterval.
	Interval time.Duration

	// FirstDelay is the wait from RunVerifyLoop's start to its first run;
	// zero means ten minutes, and it is never longer than Interval. Give a
	// server that restarts often, or is busy after a start, a longer one.
	FirstDelay time.Duration

	// Retries are the waits before each retry of the replicas still failing,
	// each from the end of the attempt before. Nil means 10 and 20 minutes,
	// about 10 and 30 minutes after the run; an empty slice means none. A
	// zero or negative wait retries at once. A retry that would start at or
	// after the next run's start is skipped, with every one after it. For a
	// provider whose incidents last hours, stretch them toward the interval,
	// such as 30m, 2h and 6h against 24h.
	Retries []time.Duration

	// Names, if set, lists the replicas each run verifies, in place of the
	// attached ones: an app that attaches its tenants on demand passes its
	// account list, since a tenant not attached at the time is otherwise
	// never verified. Each is verified by name with VerifyRestore, which
	// attaches nothing; a name with nothing replicated under it fails. Its
	// results show in Status and the metrics once the replica is attached.
	// When Names fails, the run verifies nothing and OnFail gets one failure
	// with an empty Name and Names' error.
	Names func(ctx context.Context) ([]string, error)

	// OnFail, if set, is called once per run, after its retries, with the
	// replicas that failed every attempt, sorted by name, and the run's span:
	// the time from its start to the end of its last attempt. A run in which
	// every replica passed, at once or on a retry, calls nothing, and a
	// replica purged during the run is left out. A panic in OnFail is logged
	// and the loop goes on.
	OnFail func(failures []VerifyFailure, span time.Duration)
}

VerifyOptions configure RunVerifyLoop.