Documentation
¶
Overview ¶
Package backup packs what the user set up in magpie into one file, sealed with a passphrase, to carry to another machine: the providers (their keys too, unless left out), the pictures picked for them, the settings, the profiles, every agent's model and the library (the instructions, MCP servers and skills magpie gives the agents). Sign-ins (subscription accounts and the plugins') are in it only when the user asks for them (WithSignIns): each is the sign-in of an agent on this machine, and without them each machine signs in on its own. The usage history is in it when asked for (WithUsage), as the CLI and the Settings page's Export do unless told not to; a sync's and an automatic backup's never have it.
The file is JSON: a header naming how it is sealed, and the bundle encrypted with AES-256-GCM under a key derived from the passphrase with PBKDF2-SHA256. Nothing in it can be read without the passphrase.
Index ¶
- Constants
- Variables
- func AddPlugins(es []plugin.Entry) ([]string, map[string]string)
- func AutoDefaultDir() string
- func AutoRestore(name, app string) (Result, AutoBackup, error)
- func Flag(keys bool) *bool
- func OpenData(sealed []byte, pass string) ([]byte, error)
- func RunAuto(ctx context.Context)
- func SaveAuto(c AutoConfig) error
- func Seal(b Bundle, pass string) ([]byte, error)
- func SealData(data []byte, pass string) ([]byte, error)
- func Secret(name string) bool
- func WithSignIns(b *Bundle) error
- func WithUsage(b *Bundle) error
- type AutoBackup
- type AutoConfig
- type AutoView
- type Bundle
- type Parts
- type Result
- type Usage
Constants ¶
const ( DefaultHours = 24 DefaultKeep = 7 )
On by default: losing the setup is what this is for, and a user who never found the switch is the one it saves. Once a day, deduplicated (a day with no change makes none), so the last week of changes is kept.
const ( ReasonAuto = "auto" ReasonRestore = "restore" )
Reasons a backup was made: by the schedule (or Back up now), or as what was here before a restore, to undo it.
const BundleVersion = 2
BundleVersion is the bundle's structure: 2 carries the per-part credential markers (ProvidersKeys, SettingsKeys, LibraryKeys) as their own three-state flags; 1 and before had the whole-bundle Keys alone.
const Ext = ".magpie-backup"
Ext is the extension backups are saved with.
Variables ¶
var ( AutoHours = []int{1, 6, 24, 168} AutoKeeps = []int{3, 7, 14, 30} )
AutoHours and AutoKeeps are what Settings offers.
var All = Parts{Providers: true, Settings: true, Profiles: true, Agents: true, Library: true, SignIns: true, Memory: true, Usage: true}
All is every part.
var ErrCorrupt = errors.New("not a magpie backup")
ErrCorrupt is a file that isn't a backup at all, or one cut short: its JSON, format, or key-derivation fields don't read. It is told from a wrong passphrase and from a newer magpie's file, so a sync can rebuild the damaged one from this computer rather than fail on it forever.
var ErrNotBackup = errors.New("not a magpie backup")
ErrNotBackup is a file that doesn't begin like a backup at all: an empty one, a page a server answered with, another app's file. It reads as ErrCorrupt does, but is never rebuilt over by a sync on its own.
var ErrPassphrase = errors.New("wrong passphrase, or the file was changed")
ErrPassphrase is a passphrase that does not open the backup.
Functions ¶
func AddPlugins ¶ added in v0.1.1225
AddPlugins installs the plugins a backup or a sync carries that aren't here (provider.AddPlugins), giving each its time: a first one downloads Bun. It says why each it couldn't add wasn't, by spec.
func AutoDefaultDir ¶ added in v0.1.2715
func AutoDefaultDir() string
AutoDefaultDir is where backups go unless the user picked a folder. Not settings.Dir()/backups: that is the library's, which prunes it.
func AutoRestore ¶ added in v0.1.2715
func AutoRestore(name, app string) (Result, AutoBackup, error)
AutoRestore puts the backup by name back, every part, as an import does. What is here now is backed up first (reason restore), so a restore is undone by restoring that one; if it can't be, nothing is put back.
func Flag ¶ added in v0.1.878
Flag is a per-part credential marker as a bundle field takes one: the part's own, written when a sync merged it. Absent (nil) is a whole collect's or an older magpie's.
func RunAuto ¶ added in v0.1.2715
RunAuto makes backups on their schedule until ctx ends. A failure is logged once and shown in Settings, not on every try.
func SaveAuto ¶ added in v0.1.2715
func SaveAuto(c AutoConfig) error
SaveAuto writes the setup, after checking it is one Settings offers.
func Secret ¶ added in v0.1.182
Secret reports whether a header or an environment variable by that name looks like it holds a key: what a backup without keys leaves empty.
func WithSignIns ¶ added in v0.1.2835
WithSignIns puts this computer's sign-ins in b. One that can't be read is an error, never none.
Types ¶
type AutoBackup ¶ added in v0.1.2715
type AutoBackup struct {
Name string `json:"name"`
Made time.Time `json:"made"`
Reason string `json:"reason"`
App string `json:"app,omitempty"`
Size int64 `json:"size"`
Providers int `json:"providers"`
Library bool `json:"library"`
// SignIns: the backup has the sign-ins in it, sealed with the
// passphrase set when it was made.
SignIns bool `json:"signIns,omitempty"`
// contains filtered or unexported fields
}
AutoBackup is one backup in the folder.
func AutoNow ¶ added in v0.1.2715
func AutoNow(app string) (AutoBackup, error)
AutoNow makes a scheduled backup at once, changed or not (Back up now).
func AutoTick ¶ added in v0.1.2715
func AutoTick(now time.Time, app string) (*AutoBackup, error)
AutoTick makes a backup if one is due at now: on, the interval gone by since the last one (or the clock gone back before it), and the setup changed since the newest scheduled backup. It says the backup made, or nothing. A setup that can't be read whole makes none (Collect's error).
type AutoConfig ¶ added in v0.1.2715
type AutoConfig struct {
Off bool `json:"off,omitempty"` // turned off by the user; on unless so
Hours int `json:"hours,omitempty"` // between backups; 0 is DefaultHours
Keep int `json:"keep,omitempty"` // backups kept of each kind; 0 is DefaultKeep
Dir string `json:"dir,omitempty"` // the folder; "" is AutoDefaultDir
// SignIns puts the sign-ins (subscription accounts, the plugins'
// sign-ins) in each backup too, sealed with Passphrase: the rest of an
// automatic backup is plain JSON without credentials. Off unless the
// user turns it on.
SignIns bool `json:"signIns,omitempty"`
Passphrase string `json:"passphrase,omitempty"`
}
AutoConfig is how backups are made by themselves. It is this computer's own, in autobackup.json beside the settings, never in a bundle or a sync. The zero value is the default, so a field the user never set follows it and one they set is kept as set.
func LoadAuto ¶ added in v0.1.2715
func LoadAuto() (AutoConfig, error)
LoadAuto reads the setup. A file that isn't there is the defaults; one that can't be read or parsed is an error, never the defaults: the user may have turned backups off in it.
type AutoView ¶ added in v0.1.2715
type AutoView struct {
On bool `json:"on"`
Hours int `json:"hours"`
Keep int `json:"keep"`
Dir string `json:"dir"`
Custom bool `json:"custom"` // a folder the user picked
// Inside: the folder is in magpie's own, so a wiped disk or a
// reinstalled system takes the backups along with what they back up
// (two_2025. on Discord: everything is on C:, lost on a reinstall).
// Settings says so beside the folder, for one on another disk.
Inside bool `json:"inside,omitempty"`
Error string `json:"error,omitempty"`
Backups []AutoBackup `json:"backups"`
// SignIns: backups have the sign-ins in them, sealed with the
// passphrase set (never sent: Passphrase says one is).
SignIns bool `json:"signIns,omitempty"`
Passphrase bool `json:"passphrase,omitempty"`
}
AutoView is what Settings shows.
func AutoStatus ¶ added in v0.1.2715
func AutoStatus() AutoView
AutoStatus is the setup and the backups in its folder.
type Bundle ¶
type Bundle struct {
Version int `json:"version"`
Created time.Time `json:"created"`
App string `json:"app,omitempty"` // the magpie that made it
Keys bool `json:"keys"` // whole-backup credential policy
Providers []provider.Provider `json:"providers"`
Icons map[string][]byte `json:"icons,omitempty"` // pictures picked for providers, by file name
Groups []provider.Group `json:"groups,omitempty"` // the user's model groups
Settings *settings.Settings `json:"settings,omitempty"`
GatewayKeys *[]access.Key `json:"gatewayKeys,omitempty"`
// AccountGroups are the account groups gateway keys name (LeiWang_2000
// on X), carried with the keys; nil from a magpie before them.
AccountGroups *[]access.AccountGroup `json:"accountGroups,omitempty"`
Profiles map[string]profile.Profile `json:"profiles,omitempty"`
Agents map[string]string `json:"agents,omitempty"` // every agent's fields as they are now
Library *library.Bundle `json:"library,omitempty"` // nil from a magpie before it, or with none
// Searches are the web search APIs (#419), their keys with the
// providers'; nil from a magpie before them.
Searches *[]provider.SearchAPI `json:"searches,omitempty"`
// Order is the order the user put the providers in (#499), by id;
// none from a magpie before it went, or when they were never arranged.
Order []string `json:"order,omitempty"`
// GroupOrder is the order the user put the routing groups in (#779),
// by id; none from a magpie before it, or when never arranged.
GroupOrder []string `json:"groupOrder,omitempty"`
// Plugins are the OpenCode plugins and middlewares added from npm or
// git (provider.Carried), installed where the backup is put back:
// the providers they serve come with them. Their sign-ins don't, as a
// subscription's don't. Nil from a magpie before them.
Plugins *[]plugin.Entry `json:"plugins,omitempty"`
// Proxies are the proxies kept by name, which providers, accounts and
// Settings name as "@id". They travel with the providers, passwords
// only when the providers' keys do; nil from a magpie before them.
Proxies *[]settings.NamedProxy `json:"proxies,omitempty"`
// Sync can carry one part's credentials without changing the others'
// Keys policy; these are absent from older backups and whole collects.
ProvidersKeys *bool `json:"providersKeys,omitempty"`
SettingsKeys *bool `json:"settingsKeys,omitempty"`
LibraryKeys *bool `json:"libraryKeys,omitempty"`
// SignIns are the subscription accounts and the plugins' sign-ins,
// with their credentials: only when the user asked for them
// (WithSignIns). Nil otherwise, and from a magpie before them.
SignIns *provider.SignIns `json:"signIns,omitempty"`
// Memory is what the agents know (memory.Collect): merged where it is
// put back, memory by memory, never put over what is there. Nil from
// a magpie before it, or with no memory.
Memory *memory.Bundle `json:"memory,omitempty"`
// Usage is the history of the calls made here and of the accounts'
// windows (WithUsage): merged where it is put back, never put over
// what is there. Nil when left out, and from a magpie before it.
Usage *Usage `json:"usage,omitempty"`
}
Bundle is what a backup holds.
func Collect ¶
Collect gathers the bundle; without keys, providers carry none, nor any header that looks like one, and the library's servers no environment variable or header that looks like one (their names stay).
type Parts ¶
type Parts struct {
Providers, Settings, Profiles, Agents, Library bool
// SignIns puts the backup's sign-ins in, when it has them
// (provider.RestoreSignIns: nothing renewed later here is written over).
SignIns bool
// Memory merges the backup's memory into this computer's
// (memory.Put): a memory here is never taken away by it.
Memory bool
// Usage merges the backup's usage history into this computer's
// (usage.MergeLog): a call here already is never added again.
Usage bool
}
Parts picks what a restore puts back.
type Result ¶
type Result struct {
Added, Replaced int // providers
NeedKey []string // providers that came without a key and have none here
// SignIn is the subscriptions whose settings came with no account
// here: a backup carries no sign-in, so they are listed once one is
SignIn []string `json:",omitempty"`
// Plugins are the plugins installed for it, PluginErrors why the others
// couldn't be, by spec
Plugins []string `json:",omitempty"`
PluginErrors map[string]string `json:",omitempty"`
Settings bool
Profiles int
Agents int // agent fields changed
Skipped []string // agent fields left alone: the agent is not on this machine, or the value failed
Library bool // the library was put in
Memory bool `json:",omitempty"` // memory changed: the backup's merged in
// Usage is how many of the backup's calls were added to the usage
// history here; nil when it had none, or they weren't asked for.
Usage *int `json:",omitempty"`
LibraryProblems []library.Problem `json:",omitempty"` // what of it couldn't be given to an agent
// SignIns is what putting the backup's sign-ins in did; nil when it
// had none, or they weren't asked for.
SignIns *provider.SignInsResult `json:",omitempty"`
// SignInsLocked: the backup has sign-ins sealed with a passphrase
// other than the one set now, so they were left out.
SignInsLocked bool `json:",omitempty"`
}
Result says what a restore did.
type Usage ¶
type Usage struct {
Calls int `json:"calls"` // how many lines Log has
Log []byte `json:"log,omitempty"` // gzipped usage.jsonl
Quotas []byte `json:"quotas,omitempty"` // gzipped quota-history.json
}
Usage is the usage history a backup carries, so a restore on a new computer has it (JasonLeeForOnly on Discord): usage.jsonl's lines, gzipped (usage.LogBackup), and the quota cards' history as provider.QuotaHistoryData keeps it, gzipped too.