dbbackup

package
v0.14.3 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: AGPL-3.0 Imports: 39 Imported by: 0

Documentation

Overview

Package dbbackup executes database backups on fallbck-agent (and, through the db-worker mode, inside kubernetes dump pods): it detects the client tools, checks the server, dumps into a spool directory, writes the manifest and hands the spool to the pxar upload engine.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanSpool

func CleanSpool(cfg dbspec.Config) error

CleanSpool removes leftovers of earlier runs (no run is in flight at agent start)

func FreeBytes added in v0.12.0

func FreeBytes(path string) uint64

FreeBytes reports the free space on the filesystem holding path (0 when unknown)

func IsPrecheck

func IsPrecheck(err error) bool

IsPrecheck reports whether err is (or wraps) a PrecheckError

func Run

Run executes one database backup run end to end

Types

type Binary

type Binary struct {
	Path    string
	Version string
	Major   int
	Minor   int
	Patch   int
	// Flavor distinguishes mysql/mariadb tools
	Flavor string
}

Binary is a detected client tool

func (Binary) String

func (b Binary) String() string

type Command

type Command struct {
	// Name is the logical tool name for logs and errors
	Name   string
	Path   string
	Args   []string
	Env    []string
	Dir    string
	Stdout io.Writer
	// RunAs names the OS user to run the tool as (peer / socket authentication on the
	// database host); "" keeps the agent's own user. Unix only; the agent must be root.
	RunAs string
	// Secrets are masked in everything the tool writes to stderr before it reaches
	// error messages or the agent log (mongodump echoes its URI, password included)
	Secrets []string
}

Command describes a client tool invocation. Env is the complete environment (the agent's own environment, which carries its token, is never inherited).

type DetectResult

type DetectResult struct {
	Engine string
	Tools  ToolInfo
	Err    error
}

DetectResult is one line of `fallbck-agent tools`

func DetectAll

func DetectAll(ctx context.Context, r Runner, tools dbspec.ToolConfig) []DetectResult

DetectAll runs Detect for every engine (diagnostics)

func (DetectResult) Format

func (d DetectResult) Format() string

Format renders a DetectResult for humans

type DumpRequest

type DumpRequest struct {
	RunID string
	JobID string
	Label string
	Spec  *dbspec.Spec
	Creds dbspec.Credentials
	Tools ToolInfo
	// Server is filled by Precheck
	Server ServerInfo
	// Databases is the resolved list to dump (filled by Precheck; nil = all-in-one dump)
	Databases []string
	// SpoolDir exists and is empty on entry; SecretDir (0700) holds credential files
	SpoolDir  string
	SecretDir string
	// Snapshot is the PBS snapshot this run writes (type/id/time); engines that keep
	// a chain record it so later runs can point back at it
	Snapshot string
	// PrevState is the last successful run's state (nil on the first run)
	PrevState *dbspec.JobState
	Progress  ProgressFunc
	Runner    Runner
}

DumpRequest carries everything an engine needs for one run

type Engine

type Engine interface {
	Name() string
	// Detect finds the client binaries and their versions; it never contacts a server
	Detect(ctx context.Context, r Runner, tools dbspec.ToolConfig) (ToolInfo, error)
	// Precheck connects to the server and verifies versions, privileges and options
	// the dump would otherwise fail on late; configuration problems are *PrecheckError
	Precheck(ctx context.Context, req *DumpRequest) (ServerInfo, error)
	// Dump writes everything for this run under req.SpoolDir
	Dump(ctx context.Context, req *DumpRequest) (*EngineResult, error)
}

Engine is one database family

func Lookup

func Lookup(name string) (Engine, bool)

Lookup returns the engine for a canonical engine name

type EngineResult

type EngineResult struct {
	ConsistentAsOf time.Time
	Databases      []dbspec.DatabaseEntry
	Warnings       []string
	Notes          []string
	Postgres       *dbspec.PostgresManifest
	MySQL          *dbspec.MySQLManifest
	MongoDB        *dbspec.MongoManifest
	MSSQL          *dbspec.MSSQLManifest
	Restore        dbspec.Restore
	// State is the engine part of the job state saved after a successful upload
	State *dbspec.JobStateRecord
}

EngineResult is the engine-specific outcome of Dump

type FileStateStore added in v0.9.5

type FileStateStore struct{ Dir string }

FileStateStore keeps <dir>/<jobID>.json; an empty dir disables persistence

func (FileStateStore) Load added in v0.9.5

func (f FileStateStore) Load(_ context.Context, jobID string) (*dbspec.JobState, error)

func (FileStateStore) Save added in v0.9.5

func (f FileStateStore) Save(_ context.Context, jobID string, st *dbspec.JobState) error

type MemoryStateStore added in v0.9.5

type MemoryStateStore struct {
	Prev  *dbspec.JobState
	Saved *dbspec.JobState
}

MemoryStateStore holds the previous state supplied by the caller and captures the state a run wants saved (worker pods hand it back in their termination message)

func (*MemoryStateStore) Load added in v0.9.5

func (*MemoryStateStore) Save added in v0.9.5

type PrecheckError

type PrecheckError struct {
	Msg  string
	Hint string
	Err  error
}

PrecheckError is a configuration or environment problem that a retry will not fix

func (*PrecheckError) Error

func (e *PrecheckError) Error() string

func (*PrecheckError) Unwrap

func (e *PrecheckError) Unwrap() error

type Progress

type Progress struct {
	// Phase: detect, precheck, dump, basebackup, binlog, manifest, upload
	Phase string
	// Current names the database/file being processed
	Current      string
	BytesInSpool int64
}

Progress is reported while a run executes

func (Progress) Describe

func (p Progress) Describe() string

Describe renders the progress for heartbeats

type ProgressFunc

type ProgressFunc func(Progress)

ProgressFunc receives progress updates

type Result

type Result struct {
	ExitCode int
	Stderr   string
	Duration time.Duration
}

Result describes a finished command

type RunOptions

type RunOptions struct {
	Session *temporal.BackupSessionInfo
	Spec    *dbspec.Spec
	// Password is the server-stored job password ("" = resolve from an agent profile)
	Password string
	Config   dbspec.Config
	Upload   UploadFunc
	Progress ProgressFunc
	// Runner overrides the process runner (tests)
	Runner       Runner
	Hostname     string
	AgentVersion string
	// PollInterval overrides the spool poll interval (tests)
	PollInterval time.Duration
	// State overrides where the job's chain state is read and written (default: JSON
	// files under Config.StateDir)
	State StateStore
	// Generator names the agent in the manifest (default fallbck-agent)
	Generator string
}

RunOptions parameterise Run

type Runner

type Runner interface {
	Run(ctx context.Context, c Command) (Result, error)
	LookPath(name string) (string, error)
}

Runner executes client tools; tests substitute a fake

func NewRunner

func NewRunner(killGrace time.Duration) Runner

NewRunner returns the process runner used by the agent

type ServerInfo

type ServerInfo struct {
	Version    string
	VersionNum int
	Flavor     string
	IsReplica  bool
	// EstimatedBytes of the dump (0 = unknown)
	EstimatedBytes int64
	// Superuser reports whether the backup role is a superuser (PostgreSQL)
	Superuser bool
	// Attrs carries engine-specific server facts needed by Dump (binlog state, gtid mode)
	Attrs map[string]string
	Notes []string
}

ServerInfo is the result of Engine.Precheck

type ServerVersion added in v0.9.5

type ServerVersion struct {
	Major   int
	Flavor  string // postgres, mysql, mariadb
	Version string
}

ServerVersion is the minimum a caller needs to pick matching client tools before a run: the kubernetes agent uses it to choose the dump pod's image tag.

func ProbeServerVersion added in v0.9.5

func ProbeServerVersion(ctx context.Context, spec *dbspec.Spec, creds dbspec.Credentials) (ServerVersion, error)

ProbeServerVersion connects with the run's credentials and reads the server version. MongoDB is not supported (the agent carries no driver): the caller falls back to its default image.

type StateStore added in v0.9.5

type StateStore interface {
	// Load returns the job state or nil when none exists
	Load(ctx context.Context, jobID string) (*dbspec.JobState, error)
	Save(ctx context.Context, jobID string, st *dbspec.JobState) error
}

StateStore persists the per-job chain state (last binlog position, WAL LSN) between runs. The db agent keeps one JSON file per job; the kubernetes worker pod is ephemeral and receives/returns the state through the agent instead.

type ToolError

type ToolError struct {
	Tool     string
	ExitCode int
	Stderr   string
	// Hint explains known failure messages; Config marks configuration problems
	Hint   string
	Config bool
}

ToolError is a non-zero exit of a client tool

func (*ToolError) Error

func (e *ToolError) Error() string

type ToolInfo

type ToolInfo struct {
	// Binaries by logical name (pg_dump, pg_dumpall, pg_basebackup; dump, binlog; mongodump)
	Binaries map[string]Binary
	// Alternatives are complete tool sets found in other well-known directories
	// (several PostgreSQL majors installed side by side); Precheck may switch to one
	Alternatives []map[string]Binary
}

ToolInfo is the result of Engine.Detect

func (ToolInfo) Manifest

func (t ToolInfo) Manifest() map[string]dbspec.Tool

Manifest converts the detected tools for the manifest

type UploadFunc

type UploadFunc func(ctx context.Context, spoolDir string, blobs []gopbs.Blob) (*temporal.BackupExecutionResult, error)

UploadFunc uploads the spool directory (as the archive) plus blobs to PBS; the activity binds it to the pxar engine with the run's session.

Jump to

Keyboard shortcuts

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