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
- Variables
- func GitHubToken(ctx context.Context) (token, source string)
- func NewPassphrase() string
- func Open(r io.Reader, passphrase string) (io.Reader, error)
- type APIError
- type Asset
- type GitHub
- func (g *GitHub) Download(ctx context.Context, r *Release, w io.Writer) error
- func (g *GitHub) EnsureRepo(ctx context.Context) (created bool, err error)
- func (g *GitHub) Find(ctx context.Context, tag string) (*Release, error)
- func (g *GitHub) List(ctx context.Context) ([]Release, error)
- func (g *GitHub) Prune(ctx context.Context, keep int) (int, error)
- func (g *GitHub) RestoreRelease(ctx context.Context, rel *Release, passphrase string, to Target, tmpDir string, ...) (*Result, error)
- func (g *GitHub) Upload(ctx context.Context, path string, m *Manifest) (*Release, error)
- type Manifest
- type Release
- type Result
- type Runner
- func (r *Runner) Due(now time.Time) bool
- func (r *Runner) Export(ctx context.Context, w io.Writer) (*Manifest, error)
- func (r *Runner) Loop(ctx context.Context)
- func (r *Runner) Repo(ctx context.Context) (*GitHub, error)
- func (r *Runner) Run(ctx context.Context) (Status, error)
- func (r *Runner) SaveSettings(s Settings) error
- func (r *Runner) Settings() Settings
- func (r *Runner) Status() Status
- type Settings
- type Source
- type Status
- type Store
- type Target
- type Workspace
Constants ¶
const FormatVersion = 1
FormatVersion is bumped when the layout changes in a way Restore must know.
Variables ¶
var ErrBusy = errors.New("a backup is already running")
ErrBusy means a backup is already being made.
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 ¶
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.
Types ¶
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) EnsureRepo ¶
EnsureRepo makes the repository exist, private and with a first commit (a release needs one). It refuses a public repository.
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.
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.
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.
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.
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) Run ¶
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 ¶
SaveSettings stores the configuration as given.
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 ¶
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.
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 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.