backup

package
v1.59.0 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2026 License: MIT Imports: 26 Imported by: 0

Documentation

Overview

Package backup exports selected cockpit and host state into one archive and applies such an archive back, so a freshly set up server continues where the old one stopped. The archive is a tar.gz with a manifest, always wrapped into the framed AES-256-GCM container (see crypt.go), a password is mandatory and only that .dcbackup shape imports. The import adapts to whatever sections the file contains.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewEncryptWriter

func NewEncryptWriter(w io.Writer, password string) (io.WriteCloser, error)

NewEncryptWriter writes the header and returns a writer that encrypts everything written to it. Close writes the final frame and must be called.

Types

type ApplyResult

type ApplyResult struct {
	Sections    int
	Files       int
	Skipped     int
	Overwritten int
}

ApplyResult summarizes one import run.

type GroupView

type GroupView struct {
	Label    string
	Sections []SectionView
}

GroupView groups sections for the settings page.

type Manifest

type Manifest struct {
	App        string            `json:"app"`
	Format     int               `json:"format"`
	AppVersion string            `json:"appVersion"`
	CreatedAt  time.Time         `json:"createdAt"`
	Host       string            `json:"host"`
	Sections   []ManifestSection `json:"sections"`
}

Manifest identifies a backup archive and lists what it contains.

type ManifestSection

type ManifestSection struct {
	ID    string `json:"id"`
	Label string `json:"label"`
	Files int    `json:"files"`
	Bytes int64  `json:"bytes"`
}

ManifestSection records one exported section with its footprint.

type MergeView

type MergeView struct {
	Entry    ReviewEntry
	Current  string
	Previous string
	Text     bool
}

MergeView carries everything the merge page shows for one entry.

type ReviewEntry

type ReviewEntry struct {
	ID        string    `json:"id"`
	Path      string    `json:"path"`
	Section   string    `json:"section"`
	CreatedAt time.Time `json:"createdAt"`
}

ReviewEntry is one overwritten file awaiting a decision.

func (ReviewEntry) PreImportPath

func (e ReviewEntry) PreImportPath() string

PreImportPath returns the location of the saved previous version.

type Section

type Section struct {
	ID          string
	Label       string
	Description string
	Group       string
	Sources     []Source
	Requires    []string
}

Section is one selectable unit of the export and import. Requires names sections this one only makes sense with, the export form enforces the dependency in both directions.

type SectionView

type SectionView struct {
	ID          string
	Label       string
	Description string
	Available   bool
	Requires    []string
}

SectionView is the UI facing shape of a section.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service holds the section registry, the pending import store, and the overwrite review list.

func New

func New(stateDir, projectsDir, version string) *Service

New builds the service for the given state and projects directories, so an import always lands in the running server's current paths. Host sections resolve against the current user's home directory.

func (*Service) Apply

func (s *Service) Apply(id string, ids []string) (ApplyResult, error)

Apply extracts the selected sections of a pending archive onto the host. Targets come from the current registry, never from the archive, and every file write verifies its real parent directory stays under the section source, so a crafted archive cannot escape through .. or symlinks.

func (*Service) BackupFile

func (s *Service) BackupFile(id string) (string, string, error)

BackupFile returns the archive path and download name of a finished backup.

func (*Service) CleanupPending

func (s *Service) CleanupPending()

CleanupPending drops pending archives older than the TTL.

func (*Service) CockpitPath

func (s *Service) CockpitPath(path string) bool

CockpitPath reports whether path sits in the state dir. Those files feed the server process itself, changing one wants a restart.

func (*Service) DeleteBackup

func (s *Service) DeleteBackup(id string) (StoredBackup, error)

DeleteBackup removes the archive and its entry and returns the removed entry. A running job cannot be deleted.

func (*Service) Discard

func (s *Service) Discard(id string)

Discard drops a pending archive.

func (*Service) Export

func (s *Service) Export(w io.Writer, ids []string) error

Export streams a tar.gz with the selected sections and a trailing manifest into w. Absent sources are skipped silently, that is what keeps the archive adaptive.

func (*Service) Groups

func (s *Service) Groups() []GroupView

Groups returns the registry grouped for the settings page, with a cheap availability probe per section (stat only, no directory walks).

func (*Service) HomeDotfiles

func (s *Service) HomeDotfiles() []string

HomeDotfiles lists the regular dot files (and dot symlinks) directly in the home directory, the dynamic source set of the dotfiles section. The create form shows the list, so the selection is transparent.

func (*Service) Inspect

func (s *Service) Inspect(id string) (*Manifest, error)

Inspect reads the manifest of a pending archive.

func (*Service) Known

func (s *Service) Known(ids []string) []string

Known filters ids down to registered section ids.

func (*Service) LastFinished

func (s *Service) LastFinished() (StoredBackup, bool)

LastFinished returns the newest entry that is no longer running, the one a just fired notification is about. ok is false while nothing finished yet.

func (*Service) ListBackups

func (s *Service) ListBackups() []StoredBackup

ListBackups returns the stored backups newest first, dropping done entries whose archive disappeared externally.

func (*Service) Merge

func (s *Service) Merge(id string) (*MergeView, error)

Merge loads both versions of an entry for the merge page. Binary or large files come back with Text false, the page then only offers keep or restore.

func (*Service) MergeSave

func (s *Service) MergeSave(id, content string) error

MergeSave writes the merged content over the imported file and resolves the entry.

func (*Service) PendingReviewCount

func (s *Service) PendingReviewCount() int

PendingReviewCount returns how many overwrite reviews are open, a cheap load-and-count (no per file stat) for the nav badge rendered on every page. The backup page itself prunes stale entries.

func (*Service) ReviewKeep

func (s *Service) ReviewKeep(id string) error

ReviewKeep keeps the imported file and drops the previous copy.

func (*Service) ReviewKeepAll

func (s *Service) ReviewKeepAll() int

ReviewKeepAll resolves every open entry toward the imported files and returns how many copies were dropped.

func (*Service) ReviewList

func (s *Service) ReviewList() []ReviewEntry

ReviewList returns the open entries, dropping any whose pre-import copy is gone, so an externally cleaned up file heals the list.

func (*Service) ReviewNeedsRestart

func (s *Service) ReviewNeedsRestart(id string) bool

ReviewNeedsRestart reports whether resolving this entry toward the previous or a merged version changes a cockpit file.

func (*Service) ReviewRestore

func (s *Service) ReviewRestore(id string) error

ReviewRestore puts the previous version back over the imported file.

func (*Service) SavePending

func (s *Service) SavePending(r io.Reader, password string) (string, error)

SavePending stores an uploaded archive decrypted for the apply step and validates it by reading the manifest. It returns the pending id. Only the encrypted .dcbackup container is accepted, backups exist in no other shape.

func (*Service) Section

func (s *Service) Section(id string) (SectionView, bool)

Section returns the UI view of one registered section.

func (*Service) StartBackup

func (s *Service) StartBackup(ids []string, password string, done func(StoredBackup)) (StoredBackup, error)

StartBackup validates the selection and launches the background job. done runs after the job finished, in both outcomes, with the final entry. A password is mandatory, backups only exist encrypted.

type Source

type Source struct {
	Name string
	Path string
	Skip []string
}

Source maps one archive key to one host path. Name is a single path component below data/<section>/ in the archive, Path is the absolute host location, a file or a directory. The mapping is resolved against the current registry on import, so an archive stays portable across homes. Skip names relative slash paths below Path the export leaves out, a directory in it takes its subtree with it. The import knows nothing about it: an older archive that still carries such a file keeps importing it.

type StoredBackup

type StoredBackup struct {
	ID        string    `json:"id"`
	Name      string    `json:"name"`
	CreatedAt time.Time `json:"createdAt"`
	Bytes     int64     `json:"bytes"`
	Sections  []string  `json:"sections"`
	Encrypted bool      `json:"encrypted"`
	State     string    `json:"state"`
	Error     string    `json:"error,omitempty"`
}

StoredBackup is one archive in the backups directory.

func (StoredBackup) Done

func (b StoredBackup) Done() bool

Done reports whether the archive is complete and downloadable.

func (StoredBackup) Running

func (b StoredBackup) Running() bool

Running reports whether the job behind this entry is still writing.

Jump to

Keyboard shortcuts

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