backup

package
v2.10.3 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 31 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 writes everything a Lectern server knows into one archive and puts it back: the database (sessions, tasks, projects, workflows, approvals, settings), posted media, the ~/.lectern folder (plugins, hooks, Scribe chats), the agents' own chat transcripts for every workspace a session ran in, and the server's LECTERN_* configuration.

The archive is a gzip'd tar, encrypted with age (a passphrase, scrypt) when one is given, so a copy kept off this machine is useless to anyone without the passphrase and still opens with the stock `age -d` if Lectern itself is gone. Layout inside the tar:

manifest.json       what was saved, from where, and what could not be
db/lectern.db       a consistent snapshot (VACUUM INTO), safe while serving
files/<abs path>    files from this machine, at their original paths
remote/<target>/... transcripts read from SSH targets
env/lectern.env     the server's LECTERN_* settings, secrets included

Index

Constants

View Source
const FormatVersion = 1

FormatVersion is bumped when the layout changes in a way Restore must know.

Variables

View Source
var ErrBusy = errors.New("a backup is already running")

ErrBusy means a backup is already being made.

View Source
var ErrPassphrase = errors.New("backup: wrong or missing passphrase")

ErrPassphrase means the archive is encrypted and the passphrase given does not open it (or none was given).

Functions

func GitHubToken

func GitHubToken(ctx context.Context) (token, source string)

GitHubToken finds a token: LECTERN_BACKUP_GITHUB_TOKEN, then GH_TOKEN or GITHUB_TOKEN, then the GitHub CLI's own login (`gh auth token`).

func NewPassphrase

func NewPassphrase() string

NewPassphrase returns 30 random characters (150 bits) in groups of five: far past guessing, and short enough to write down.

func Open

func Open(r io.Reader, passphrase string) (io.Reader, error)

Open returns the plain tar.gz stream of an archive, decrypting it when it is age-encrypted.

Types

type APIError

type APIError struct {
	Status  int
	Message string
}

APIError is a non-2xx answer.

func (*APIError) Error

func (e *APIError) Error() string

type Asset

type Asset struct {
	ID   int64  `json:"id"`
	Name string `json:"name"`
	Size int64  `json:"size"`
	URL  string `json:"url"`
}

Asset is one uploaded file of a release.

type GitHub

type GitHub struct {
	Repo  string // owner/name
	Token string
	API   string // https://api.github.com unless a test points elsewhere
	HTTP  *http.Client
}

GitHub talks to one repository.

func (*GitHub) Download

func (g *GitHub) Download(ctx context.Context, r *Release, w io.Writer) error

Download writes a backup's parts, in order, to w.

func (*GitHub) EnsureRepo

func (g *GitHub) EnsureRepo(ctx context.Context) (created bool, err error)

EnsureRepo makes the repository exist, private and with a first commit (a release needs one). It refuses a public repository.

func (*GitHub) Find

func (g *GitHub) Find(ctx context.Context, tag string) (*Release, error)

Find returns the backup with this tag, or the newest for "latest" or "".

func (*GitHub) List

func (g *GitHub) List(ctx context.Context) ([]Release, error)

List returns the backups, newest first.

func (*GitHub) Prune

func (g *GitHub) Prune(ctx context.Context, keep int) (int, error)

Prune deletes all but the newest keep backups.

func (*GitHub) RestoreRelease

func (g *GitHub) RestoreRelease(ctx context.Context, rel *Release, passphrase string, to Target, tmpDir string, progress func(string)) (*Result, error)

RestoreRelease downloads a backup and restores it; for a differential one, its full backup is restored first and the changes laid over it.

func (*GitHub) Upload

func (g *GitHub) Upload(ctx context.Context, path string, m *Manifest) (*Release, error)

Upload publishes the archive at path as a new release, split into parts when it is larger than one asset may be.

type Manifest

type Manifest struct {
	Format    int       `json:"format"`
	CreatedAt time.Time `json:"created_at"`
	Version   string    `json:"lectern_version"`
	Host      string    `json:"host"`
	Home      string    `json:"home"`
	DBPath    string    `json:"db_path"`
	MediaDir  string    `json:"media_dir,omitempty"`
	// Base is the full backup this one holds the changes since; "" = full.
	Base     string    `json:"base,omitempty"`
	Since    time.Time `json:"since,omitzero"`
	Sessions int       `json:"sessions"`
	Files    int       `json:"files"`
	Bytes    int64     `json:"bytes"`
	// Skipped lists what could not be saved and why; a backup with entries
	// here still holds everything else.
	Skipped []string `json:"skipped,omitempty"`
}

Manifest describes an archive.

func Verify

func Verify(r io.Reader, passphrase string) (*Manifest, error)

Verify reads an archive through to the end without unpacking it: it opens with the passphrase, every entry is intact, and the manifest is there.

func Write

func Write(ctx context.Context, w io.Writer, src Source, passphrase string) (*Manifest, error)

Write saves src to w. With a passphrase the stream is age-encrypted.

type Release

type Release struct {
	ID        int64     `json:"id"`
	Tag       string    `json:"tag_name"`
	Name      string    `json:"name"`
	URL       string    `json:"html_url"`
	CreatedAt time.Time `json:"created_at"`
	Body      string    `json:"body"`
	Assets    []Asset   `json:"assets"`
}

Release is one backup.

func (Release) Base

func (r Release) Base() string

Base is the full backup a differential one builds on; "" for a full one.

func (Release) Size

func (r Release) Size() int64

Size is the backup's total size.

type Result

type Result struct {
	Manifest *Manifest
	Files    int
	Kept     int // already present, left alone
	Database bool
	EnvFile  string
	Remote   []string
	// Placed is every file this restore wrote.
	Placed map[string]bool
}

Result says what a restore did.

func Restore

func Restore(r io.Reader, passphrase string, to Target) (*Result, error)

Restore unpacks an archive onto this machine. Stop the Lectern server first: the database is replaced underneath it otherwise.

type Runner

type Runner struct {
	Store   Store
	Source  func(context.Context) (Source, error)
	TempDir string
	Log     *slog.Logger
	// GitHub returns the client for a repository; nil uses GitHubToken.
	GitHub func(ctx context.Context, repo string) (*GitHub, error)
	// contains filtered or unexported fields
}

Runner makes backups for a server: on demand and on a schedule.

func (*Runner) Due

func (r *Runner) Due(now time.Time) bool

Due reports whether the schedule wants a backup now.

func (*Runner) Export

func (r *Runner) Export(ctx context.Context, w io.Writer) (*Manifest, error)

Export writes an encrypted backup straight to w (a download).

func (*Runner) Loop

func (r *Runner) Loop(ctx context.Context)

Loop makes scheduled backups until ctx ends.

func (*Runner) Repo

func (r *Runner) Repo(ctx context.Context) (*GitHub, error)

Repo returns the configured repository's client.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context) (Status, error)

Run makes a backup and uploads it to the repository, then deletes the oldest beyond Keep. Backups leave this machine only encrypted.

func (*Runner) SaveSettings

func (r *Runner) SaveSettings(s Settings) error

SaveSettings stores the configuration as given.

func (*Runner) Settings

func (r *Runner) Settings() Settings

Settings returns the effective configuration.

func (*Runner) Status

func (r *Runner) Status() Status

Status returns the last backup's outcome.

type Settings

type Settings struct {
	Repo       string `json:"repo"`
	Keep       int    `json:"keep"`
	EveryHours int    `json:"every_hours"` // 0 = only when asked
	// FullEveryDays: a full backup this often; the ones between hold only
	// the files changed since it (transcripts run to gigabytes).
	FullEveryDays int    `json:"full_every_days"`
	Passphrase    string `json:"passphrase,omitempty"`
}

Settings is the operator's backup configuration. LECTERN_BACKUP_REPO and LECTERN_BACKUP_PASSPHRASE, when set, win over what is stored.

func (Settings) HasPassphrase

func (s Settings) HasPassphrase() bool

HasPassphrase reports whether backups can be encrypted.

type Source

type Source struct {
	DBPath   string
	MediaDir string
	Home     string
	// Workspaces on this machine; their Claude and Codex transcripts are saved.
	Workspaces []Workspace
	// Remote returns a gzip'd tar of one SSH target's transcripts for its
	// workspaces, and a warning when some were left out. nil skips remote
	// targets.
	Remote func(ctx context.Context, target string, ws []Workspace) (data []byte, warning string, err error)
	// RemoteWorkspaces are grouped by target name and handed to Remote.
	RemoteWorkspaces map[string][]Workspace
	// Env is saved as env/lectern.env (KEY=VALUE lines).
	Env []string
	// Version and Host are recorded in the manifest.
	Version, Host string
	// TempDir holds the database snapshot while it is copied; "" is the default.
	TempDir string
	// Base and Since make a differential backup: only files changed after
	// Since are saved, and Restore lays it over the full backup Base. The
	// database and settings are always saved whole.
	Base  string
	Since time.Time
}

Source says what to save.

func FromLectern

func FromLectern(db *store.DB, dbPath, mediaDir string, reg *executor.Registry) (Source, error)

FromLectern describes a Lectern server's data: its database, media, home and the workspaces of every session it has run, local and remote.

type Status

type Status struct {
	Running   bool      `json:"running"`
	StartedAt time.Time `json:"started_at,omitzero"`
	LastAt    time.Time `json:"last_at,omitzero"`
	LastOKAt  time.Time `json:"last_ok_at,omitzero"`
	Error     string    `json:"error,omitempty"`
	Tag       string    `json:"tag,omitempty"`
	URL       string    `json:"url,omitempty"`
	Bytes     int64     `json:"bytes,omitempty"`
	Sessions  int       `json:"sessions,omitempty"`
	Files     int       `json:"files,omitempty"`
	Skipped   []string  `json:"skipped,omitempty"`
	// Full is the last full backup, which the next ones build on.
	Full   string    `json:"full,omitempty"`
	FullAt time.Time `json:"full_at,omitzero"`
}

Status is the outcome of the last backup.

type Store

type Store interface {
	Setting(key string) string
	SetSetting(key, value string) error
}

Store is where settings and status are kept (store.DB).

type Target

type Target struct {
	// DBPath receives the database. An existing database is moved aside to
	// DBPath + ".before-restore" first, never deleted.
	DBPath string
	// Home replaces the backed-up home folder in every saved path, so a
	// restore onto a machine with a different user name lands correctly.
	Home string
	// MediaDir, when set, replaces the backed-up media folder.
	MediaDir string
	// Extra receives what has no fixed place here: the saved LECTERN_*
	// settings and remote targets' transcripts.
	Extra string
	// Overwrite replaces files that already exist; otherwise they are kept
	// and counted as skipped.
	Overwrite bool
	// SkipDB leaves the database out (a full backup under a differential,
	// whose own database is newer).
	SkipDB bool
	// Replace names files an earlier layer of this same restore wrote, which
	// a later layer may replace even without Overwrite.
	Replace map[string]bool
}

Target says where a restore puts things on this machine.

type Workspace

type Workspace struct {
	Agent  string `json:"agent"`
	Dir    string `json:"dir"`
	Target string `json:"target"`
}

Workspace is one place agents ran: the transcripts Lectern shows as a session's chat live under the agent's home, keyed by this folder.

Jump to

Keyboard shortcuts

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