Documentation
¶
Overview ¶
Package backup provides durable recovery files and the purge ledger.
Index ¶
- Constants
- Variables
- func CleanupTemps(dataDir, backupDir string) (removed []string, err error)
- func OpenLedger(path string) (*Ledger, LedgerHealth, error)
- func ResumeSwap(ctx context.Context, journalPath string) error
- func RollbackSwap(ctx context.Context, journalPath string) error
- func Swap(ctx context.Context, journalPath string, j *SwapJournal, ...) error
- func Verify(ctx context.Context, path string) (schemaVersion int, err error)
- func WithStepHook(ctx context.Context, h func(point string) error) context.Context
- type FileSet
- type Kind
- type Ledger
- type LedgerHealth
- type LedgerRecord
- type Manager
- type Retention
- type SnapshotInfo
- type Staged
- type SwapJournal
- type SwapKind
Constants ¶
const ( // LedgerPending records the intent to purge an exact set of entities. LedgerPending string = "pending" // LedgerComplete records completion of a purge and its recovery-file step. LedgerComplete string = "complete" )
const ( // MinKeepDaily is the minimum number of UTC calendar days retained. MinKeepDaily = 3 // MinKeepWeekly is the minimum number of ISO calendar weeks retained. MinKeepWeekly = 1 )
Variables ¶
var ( // ErrNoJournal indicates there is no interrupted swap to recover. ErrNoJournal = errors.New("backup: no swap journal") // ErrNoRollback indicates a scrub or a swap whose old set was deleted. ErrNoRollback = errors.New("backup: this swap cannot be rolled back") // ErrRollbackInProgress prevents forward recovery once rollback has begun. ErrRollbackInProgress = errors.New("backup: rollback in progress; run `tkr restore --rollback`") )
Functions ¶
func CleanupTemps ¶
CleanupTemps removes interrupted temporary artifacts, preserving journaled recovery paths. Callers must hold the instance lock throughout cleanup. The returned paths are removed files or whole removed directory trees.
func OpenLedger ¶
func OpenLedger(path string) (*Ledger, LedgerHealth, error)
OpenLedger loads valid records, retaining complete corrupt lines on disk and truncating only an unterminated final line. A missing file is empty and healthy.
func ResumeSwap ¶
ResumeSwap completes a forward swap without invoking its original callback. Callers inspect OwnerTokenPending before resuming to handle lost owner tokens.
func RollbackSwap ¶
RollbackSwap restores a restore/import's old file set. Its own progress is journaled so a second rollback safely resumes after any interrupted rename.
func Swap ¶
Swap starts a journaled replacement. An outstanding journal must be recovered first. onInstalled runs once after installation and before journal removal; ResumeSwap never replays it, even if it failed or was interrupted.
AIDEV-NOTE: prepared -> moved-old -> installed -> (scrub: aside-deleted). Each action and both affected directories are synced before advancing the atomic journal. A missing source with a present destination means a rename already happened: old members in Aside are skipped individually, and New absent with Target present at moved-old means installation finished. Deletion tolerates absent members. Rollback journals its discard destination BEFORE renaming the installed file, then restores old members individually; replay must never mistake an already-restored Target for the new file to discard.
func Verify ¶
Verify checks a snapshot or staged copy with SQLite and both FTS indexes, returning its highest migration version. It opens the copy in DELETE mode; never pass a live database. No SQLite sidecars may remain after closing it.
func WithStepHook ¶
WithStepHook installs a crash-test hook scoped to ctx. A hook error returns immediately without cleanup, leaving recovery artifacts exactly where they are. Rollback member hooks use file basenames, e.g. rollback:moved:taskronin.db-wal.
Types ¶
type FileSet ¶
type FileSet struct{ DB string }
FileSet names a database and its optional -wal sibling. SHM is not copied.
type Kind ¶
type Kind string
Kind identifies why a snapshot was taken.
const ( // KindDaily identifies a scheduled daily snapshot. KindDaily Kind = "daily" // KindManual identifies a user-requested snapshot. KindManual Kind = "manual" // KindPremigrate identifies a snapshot taken before a schema migration. KindPremigrate Kind = "premigrate" // KindPrescrub identifies a snapshot taken before scrubbing recovery files. KindPrescrub Kind = "prescrub" )
type Ledger ¶
type Ledger struct {
Path string
// contains filtered or unexported fields
}
Ledger serializes access to a file and its cached valid records. OpenLedger must be called under the application's instance lock; Path must not be changed.
func (*Ledger) Append ¶
func (l *Ledger) Append(r LedgerRecord) (err error)
Append computes the checksum and durably appends one record. A damaged ledger refuses both pending and complete records until explicitly repaired.
func (*Ledger) Health ¶
func (l *Ledger) Health() LedgerHealth
Health returns a copy of the ledger's corruption status.
func (*Ledger) Pending ¶
func (l *Ledger) Pending() ([]LedgerRecord, error)
Pending returns pending records with no later complete line for the same Rec, in file order. Timestamps do not determine completion order.
func (*Ledger) Records ¶
func (l *Ledger) Records() ([]LedgerRecord, error)
Records returns independent copies of all valid records in file order.
type LedgerHealth ¶
type LedgerHealth struct {
Damaged bool
DroppedLines []int // 1-based line numbers; retained on disk until Repair
}
LedgerHealth identifies complete corrupt lines skipped while loading a ledger.
type LedgerRecord ¶
type LedgerRecord struct {
V int `json:"v"`
Rec string `json:"rec"` // random 128-bit hex
At int64 `json:"at"`
State string `json:"state"` // "pending" | "complete"
Tasks []domain.TaskID `json:"tasks,omitempty"`
Entries []domain.EntryID `json:"entries,omitempty"`
RecoveryFiles string `json:"recovery_files,omitempty"` // complete lines: "deleted", "kept", or "none" (H1)
Sum string `json:"sum"` // hex SHA-256 of this record's JSON encoding with Sum = ""
}
LedgerRecord is one checksummed JSON line describing a purge intent or completion.
type Manager ¶
type Manager struct {
Dir string
Clock clock.Clock
Snap *sql.DB
// contains filtered or unexported fields
}
Manager serializes snapshots and rotations in Dir. Configure its fields before use; offline callers must also hold the application's instance lock.
type Retention ¶
type Retention struct {
Daily, Weekly int
}
Retention specifies the number of recent calendar days and ISO weeks to keep.
type SnapshotInfo ¶
SnapshotInfo describes a snapshot file. List leaves SchemaVersion at zero.
func List ¶
func List(dir string) ([]SnapshotInfo, error)
List returns regular snapshot files newest first, using only names and file metadata. It never opens databases; SchemaVersion remains zero.
func ParseSnapshotName ¶
func ParseSnapshotName(name string) (SnapshotInfo, bool)
ParseSnapshotName parses a snapshot basename, rejecting foreign files and invalid dates. Path, Size and SchemaVersion are left at their zero values.
type Staged ¶
type Staged struct{ Dir, Path string }
Staged owns a disposable directory containing a self-contained database.
type SwapJournal ¶
type SwapJournal struct {
Version int `json:"version"`
Kind SwapKind `json:"kind"`
Step string `json:"step"`
New string `json:"new"`
Target string `json:"target"`
Aside string `json:"aside"`
DeleteAside bool `json:"delete_aside"`
OwnerTokenPending bool `json:"owner_token_pending"`
StartedAt time.Time `json:"started_at"`
Rollback string `json:"rollback"` // "", started, discarding, new-discarded, old-restored
Discard string `json:"discard"` // journaled before the installed file moves here
}
SwapJournal records durable progress. New must be a verified, fsynced, self-contained DELETE-mode database. Callers hold the instance lock throughout Swap, ResumeSwap and RollbackSwap, and keep all database connections closed.
func ReadJournal ¶
func ReadJournal(path string) (*SwapJournal, error)
ReadJournal reads recovery metadata without modifying it.