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
- Variables
- func Bytes(m Manifest, files []File, at time.Time) ([]byte, error)
- func Collect(st state.Store, withLog bool) ([]File, Stats, error)
- func CountItems(data []byte) int
- func Describe(a *Archive, id state.ID) string
- func FileName(host string, at time.Time) string
- func Label8(id state.ID) string
- func Notice(st state.Store, warnDays int, now time.Time) string
- func PreRestore(st state.Store, o ApplyOptions) (string, error)
- func RecordLast(st state.Store, at time.Time, name string) error
- func SessionIDs(a *Archive) []state.ID
- func Write(w io.Writer, m Manifest, files []File, at time.Time) error
- type Addition
- type AliasPlan
- type ApplyOptions
- type Archive
- type ConfChange
- type Entry
- type File
- type Last
- type LogPlan
- type Manifest
- type Options
- type Outcome
- type Plan
- type QueuePlan
- type Stats
- type Written
Constants ¶
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.
const DefaultWarnDays = 2
DefaultWarnDays is how old the last backup may be before sitrep says so, when SITREP_BACKUP_WARN does not say otherwise.
const FormatVersion = 1
FormatVersion is the version of the archive that this package writes and reads.
const KeepPreRestore = 3
KeepPreRestore is how many archives of the state as it was before a replace are kept: the newest.
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.
const MaxName = 128
MaxName is the longest name an archive may hold, in bytes. A session whose names would be longer is left out.
const TempPrefix = ".sitrep-backup-"
TempPrefix begins the name of an archive that is being written. A finished archive never has it.
Variables ¶
var ErrNoState = errors.New("there is no state folder to back up")
ErrNoState says there is no state folder to save.
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.
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.
var LogFiles = []string{"log/current.jsonl", "log/previous.jsonl"}
LogFiles are the files of the history log that are saved.
var SessionFiles = []string{"alias", "cwd", "notes", "pinned", "queue"}
SessionFiles are the files of a session that are saved. The others (state, count, stamp, pending, lock, nudge, context, blocked, snooze) describe a moment that has passed.
Functions ¶
func Collect ¶
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 ¶
CountItems is the number of items in a queue file of an archive: its non-blank lines.
func Describe ¶
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 ¶
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 Notice ¶
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 ¶
RecordLast writes the record. The caller has made the archive, and says so only then.
func SessionIDs ¶
SessionIDs are the sessions that the archive has a queue for, sorted.
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 ApplyOptions ¶
ApplyOptions are what Apply needs besides the plan: the time, and what the backup of the present state says about this machine.
type Archive ¶
Archive is an archive that has been read and checked: every name is one that a backup makes, every file matches the manifest.
type ConfChange ¶
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 Last ¶
Last is what the record says.
func ReadLast ¶
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 ¶
Age is how long before now the backup was made; a record from the future (the clock moved back) is no age.
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.
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 colours 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 ¶
Outcome says what happened to one part of a plan.
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 ¶
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 ¶
MakePlanWith is MakePlan with the options of a restore that does more than add.
func (Plan) Adds ¶
Adds is how many items the plan would add, or with Replace how many the queues it changes will hold.
func (Plan) Any ¶
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) MarkRunning ¶
MarkRunning says which queues belong to running sessions now, from the registry (ok is false when it could not be read).
func (Plan) RunningQueues ¶
RunningQueues is how many of the queues that the plan changes belong to running sessions.
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 ¶
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.