backup

package
v1.6.7 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 25 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 an archive of the state that matters: the queues, the inbox, the names of sessions, and the notes that say what a queue was for. See docs/BACKUP.md. It reads the state folder and changes nothing in it. The archive is a gzipped tar with a manifest first, and its names are a fixed set, so that a restore can accept nothing else.

Index

Constants

View Source
const (
	MaxCompressed = 16 << 20
	MaxExpanded   = 48 << 20
	MaxEntries    = 2600 // 500 sessions of five files, the inbox, the conf, two log files and the manifest make 2,505
	MaxSessions   = 500
	MaxQueueItems = 5000
	MaxAddedItems = 20000
)

The limits on an archive that is read. A real one is about half a megabyte, and these are generous against that and small against the harm that a hostile one could do.

View Source
const DefaultWarnDays = 2

DefaultWarnDays is how old the last backup may be before sitrep says so, when SITREP_BACKUP_WARN does not say otherwise.

View Source
const FormatVersion = 1

FormatVersion is the version of the archive that this package writes and reads.

View Source
const KeepPreRestore = 3

KeepPreRestore is how many archives of the state as it was before a replace are kept: the newest.

View Source
const LastFile = "last-backup"

LastFile is the record of the last backup that reached a folder: one line, the time in epoch seconds and the name of the archive. It is the one file that a backup writes in the state folder. A backup that failed writes nothing, so a job that has stopped shows as a record that gets older.

View Source
const MaxName = 128

MaxName is the longest name an archive may hold, in bytes. A session whose names would be longer is left out.

View Source
const TempPrefix = ".sitrep-backup-"

TempPrefix begins the name of an archive that is being written. A finished archive never has it.

View Source
const WhyNotHere = "that session is not here"

WhyNotHere is the reason given for a name that is not set because its session is not on this machine.

Variables

View Source
var ErrNoState = errors.New("there is no state folder to back up")

ErrNoState says there is no state folder to save.

View Source
var ErrOldLayout = errors.New("the state folder is still in the old layout: run 'sitrep migrate' first")

ErrOldLayout says the state folder is still in the layout of before 0.5.0, which sitrep migrate moves.

View Source
var ErrTimeout = errors.New("the destination did not finish in time (is the share still mounted?)")

ErrTimeout says the archive was not written in time. A stalled network mount cannot be interrupted from inside a program, so the temporary file may be left behind: its name begins with TempPrefix.

View Source
var LogFiles = []string{"log/current.jsonl", "log/previous.jsonl"}

LogFiles are the files of the history log that are saved.

View Source
var SessionFiles = state.SavedFiles()

SessionFiles are the files of a session that are saved (state.SessionFiles says which). The others describe a moment that has passed.

Functions

func Bytes

func Bytes(m Manifest, files []File, at time.Time) ([]byte, error)

Bytes is the archive in memory.

func Collect

func Collect(st state.Store, withLog bool) ([]File, Stats, error)

Collect reads the files to be saved. The queues, the inbox and the names of the sessions are read under the folder's lock, which is released before it returns, so that a move between two queues is in the copy once and never twice or not at all. The history log is read under its own lock, which keeps it from rolling to its second file between the two reads. withLog says whether to save the log.

func CountItems

func CountItems(data []byte) int

CountItems is the number of items in a queue file of an archive: its non-blank lines.

func Describe

func Describe(a *Archive, id state.ID) string

Describe says what a session of the archive was: its name or the start of its id, the folder it worked in, and its Objective, all text from the archive, cleaned.

func FileName

func FileName(host string, at time.Time) string

FileName is the name of an archive: the host, the time to the second, and four random hex digits, so that two archives never meet.

func Label8

func Label8(id state.ID) string

Label8 is label8 for the command that prints the plan.

func Notice

func Notice(st state.Store, warnDays int, now time.Time) string

Notice is the line that the status line shows when the last backup is too old: empty when no backup has ever been made (nobody is nagged who does not back up), when the warning is off, and when the backup is recent.

func PreRestore

func PreRestore(st state.Store, o ApplyOptions) (string, error)

PreRestore saves the present state in <state>/backups, so that a replace can be undone by a restore of that archive. It returns the path, or "" when there is no state to save. The folder is owner-only and is never a link; the file is made as every archive is. The newest KeepPreRestore archives stay, and the older ones are removed, each by its own name.

func RecordLast

func RecordLast(st state.Store, at time.Time, name string) error

RecordLast writes the record. The caller has made the archive, and says so only then.

func SessionIDs

func SessionIDs(a *Archive) []state.ID

SessionIDs are the sessions that the archive has a queue for, sorted.

func Write

func Write(w io.Writer, m Manifest, files []File, at time.Time) error

Write writes the archive: the manifest first, then the files, in a single gzip member of regular entries with mode 0600.

Types

type Addition

type Addition struct {
	Text textsafe.Text
	From string // empty for the archive's own inbox; otherwise the session it was queued on, with what it was doing, cleaned
}

Addition is one item that a merge would add, and where in the archive it came from.

type AliasPlan

type AliasPlan struct {
	ID   state.ID
	Name string
	Set  bool
	Why  string // when it is not set
}

AliasPlan is a name that the archive has, and whether a restore would set it.

type ApplyOptions

type ApplyOptions struct {
	Now           time.Time
	Host, Version string
}

ApplyOptions are what Apply needs besides the plan: the time, and what the backup of the present state says about this machine.

type Archive

type Archive struct {
	Manifest Manifest
	Files    map[string][]byte
}

Archive is an archive that has been read and checked: every name is one that a backup makes, every file matches the manifest.

func Read

func Read(r io.Reader) (*Archive, error)

Read reads and checks an archive, all of it, before anything is done with it. The archive is untrusted and unauthenticated: its manifest catches damage and nothing else. Anything that is not exactly what Write makes is refused, and the whole archive with it.

type ConfChange

type ConfChange struct {
	Name     string
	Old, New string
}

ConfChange is one setting of sitrep.conf that a restore would change, as the lines read.

type Entry

type Entry struct {
	Name   string `json:"name"`
	Size   int64  `json:"size"`
	SHA256 string `json:"sha256"`
}

Entry is one file in the manifest.

type File

type File struct {
	Name string
	Data []byte
}

File is one file of the archive.

type Last

type Last struct {
	At   time.Time
	Name textsafe.Text
}

Last is what the record says.

func ReadLast

func ReadLast(st state.Store) (Last, bool)

ReadLast reads the record, if there is one that makes sense: a regular file that is not a link, a positive time and a name, all of it cleaned. A missing or broken file is no record.

func (Last) Age

func (l Last) Age(now time.Time) time.Duration

Age is how long before now the backup was made; a record from the future (the clock moved back) is no age.

func (Last) Days

func (l Last) Days(now time.Time) int

Days is the age in whole days, for a short message.

func (Last) Stale

func (l Last) Stale(now time.Time, warnDays int) bool

Stale says whether the record is older than warnDays days; 0 days turns the warning off.

type LogPlan

type LogPlan struct {
	Current, Previous []byte // the log's own form of the archive's files; nil for a file the archive does not have
	Kept, Dropped     int    // the lines that were kept, and those that were not valid, or were repeats
}

LogPlan is the history log that a replace would put in place of the present one.

type Manifest

type Manifest struct {
	Version int     `json:"version"`
	Sitrep  string  `json:"sitrep"`
	Host    string  `json:"host"`
	Time    string  `json:"time"`
	Files   []Entry `json:"files"`
}

Manifest is the first file of an archive. It says what the archive holds, and checks its integrity and nothing else: whoever makes an archive can make a manifest that matches it.

func NewManifest

func NewManifest(files []File, sitrepVersion, host string, at time.Time) Manifest

NewManifest describes files.

type Options

type Options struct {
	Replace  bool   // make the queues, and the log, what the archive has, and not only add to them
	WithConf bool   // set the theme and the colors that the archive has
	Only     string // restore only the queue of this session of the archive: an id or a prefix of one, its name, or "inbox"; "" for all of them
}

Options say what a restore may do beyond the merge.

type Outcome

type Outcome struct {
	What string
	Err  error
}

Outcome says what happened to one part of a plan.

func Apply

func Apply(st state.Store, p Plan) []Outcome

Apply does the plan with the time of now.

func ApplyWith

func ApplyWith(st state.Store, p Plan, o ApplyOptions) []Outcome

ApplyWith does the plan, through the same writers as the commands: a merge or a replace for each queue, the alias writer for each name, conf.Edit for the settings, and the log's own writer for the log. A replace first saves the state as it is, and does nothing at all if it cannot. A busy lock stops the queues, and says which were not done, since running the restore again finishes the job and adds nothing twice.

type Plan

type Plan struct {
	Manifest Manifest
	Options  Options
	Queues   []QueuePlan
	Aliases  []AliasPlan
	Log      *LogPlan     // with Replace, when the archive holds the log
	Conf     []ConfChange // with WithConf
	Vanished int          // sessions of the archive that are not here, and whose items go to the inbox
	// RegistryOK says that Running was filled in from the list of running sessions; when it was not, the plan cannot say which are running.
	RegistryOK bool
	// contains filtered or unexported fields
}

Plan is what restoring an archive would do. It is made from the archive and the state as they are, and nothing is written.

func MakePlan

func MakePlan(st state.Store, a *Archive) (Plan, error)

MakePlan works out what a merge of the archive would do. A queue of a session that exists here is merged into that session's queue; the queue of any other session, and the archive's inbox, into the inbox. No session folder is ever made.

func MakePlanWith

func MakePlanWith(st state.Store, a *Archive, o Options) (Plan, error)

MakePlanWith is MakePlan with the options of a restore that does more than add.

func (Plan) Adds

func (p Plan) Adds() int

Adds is how many items the plan would add, or with Replace how many the queues it changes will hold.

func (Plan) Any

func (p Plan) Any() bool

Any says whether the plan would change anything. A replace changes a queue that is not the archive's already, and the log, a name or a setting.

func (Plan) Changed

func (p Plan) Changed() int

Changed is how many queues the plan would change.

func (Plan) Lost

func (p Plan) Lost() int

Lost is how many items a replace would take out of the queues.

func (*Plan) MarkRunning

func (p *Plan) MarkRunning(running func(state.ID) bool, ok bool)

MarkRunning says which queues belong to running sessions now, from the registry (ok is false when it could not be read).

func (Plan) Names

func (p Plan) Names() int

Names is how many names the plan would set.

func (Plan) RunningQueues

func (p Plan) RunningQueues() int

RunningQueues is how many of the queues that the plan changes belong to running sessions.

func (Plan) Verb

func (p Plan) Verb() string

Verb is the word for the kind of restore, for the log.

type QueuePlan

type QueuePlan struct {
	Dest    state.ID
	Add     []Addition      // what is missing from the queue, in the order it would be added; with Replace, everything the queue will hold
	Skipped int             // what the queue holds already
	Lost    []textsafe.Text // with Replace: what the queue holds now and the archive does not
	Running bool            // the session is running now, as far as the caller could tell (see Plan.MarkRunning)
	Same    bool            // with Replace: the queue is the archive's already, in the same order, and a replace would change nothing
	NoRoom  int             // with Replace: how many of the archive's items would not be written, as the queue file would be full
	Items   []textsafe.Text // everything the archive has for this queue, in order: what Merge or Replace is given
}

QueuePlan is what a restore would do to one queue.

type Stats

type Stats struct {
	Sessions   int      // sessions with their files
	InboxItems int      // items in the inbox
	QueueItems int      // items in the queues of the sessions
	Files      int      // files, the manifest apart
	HasLog     bool     // the history log is in the archive
	HasConf    bool     // sitrep.conf is in the archive
	Bytes      int      // their size together
	Skipped    []string // what could not be saved, and why: a link, a file that is not regular, a name that is too long
}

Stats says what was saved.

type Written

type Written struct {
	Path     string
	Size     int64
	ModeKept bool
}

Written says where an archive went and how big it is. ModeKept is false when the destination did not give the file the mode 0600, as an SMB or NFS share may not.

func WriteFile

func WriteFile(dir, name string, timeout time.Duration, write func(io.Writer) error) (Written, error)

WriteFile makes an archive in dir under name. dir must be an existing folder, and is never made, so that an unmounted share does not turn into a folder on the local disk. It is resolved once, links followed, since a share is often reached through one, and every step after that uses the resolved path. write runs in a goroutine, and when it has not finished after timeout the temporary file is removed as well as it can be and ErrTimeout is returned. The file is made with O_EXCL and mode 0600 under a temporary name and renamed only when it is complete and synced, so a half-written archive never has the name of a finished one. A name that exists is not replaced.

Jump to

Keyboard shortcuts

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