Help us shape the future of Go! Take the 2026 Go Developer Survey

backup

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HookTypeStop       = "stop"
	HookTypeStart      = "start"
	HookTypeFlushRedis = "flush_redis"
	HookTypeFlushMySQL = "flush_mysql"
	HookTypeExec       = "exec"
)

Variables

This section is empty.

Functions

func VerifyChecksum

func VerifyChecksum(r io.Reader, expectedHex string) error

Types

type BackupAlertFunc

type BackupAlertFunc func(appName, strategy, message, eventType string)

BackupAlertFunc is a callback for backup-related alerts.

type BackupOpts

type BackupOpts struct {
	ContainerName string
	Paths         []string
	Credentials   map[string]string
}

type BackupResult

type BackupResult struct {
	Reader   io.ReadCloser
	Filename string
}

type BackupStore

type BackupStore interface {
	ListBackupConfigs(appID *int64) ([]store.BackupConfig, error)
	GetBackupConfig(id int64) (*store.BackupConfig, error)
	CreateBackupRun(configID int64) (*store.BackupRun, error)
	UpdateBackupRunSuccess(id int64, sizeBytes int64, filePath, checksum string) error
	UpdateBackupRunFailed(id int64, errMsg string) error
	ListOldBackupRuns(configID int64, keepCount int) ([]store.BackupRun, error)
	ListOldBackupRunsByTime(configID int64, maxAgeDays int) ([]store.BackupRun, error)
	GetAppByID(id int64) (*store.App, error)
	GetBackupRun(id int64) (*store.BackupRun, error)
}

BackupStore is the subset of store.Store needed by Scheduler.

type ChecksumWriter

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

func NewChecksumWriter

func NewChecksumWriter() *ChecksumWriter

func (*ChecksumWriter) Sum

func (c *ChecksumWriter) Sum() string

func (*ChecksumWriter) TeeReader

func (c *ChecksumWriter) TeeReader(r io.Reader) io.Reader

type ContainerExecutor

type ContainerExecutor interface {
	ExecInContainer(ctx context.Context, container, command string) (string, error)
	StopContainer(ctx context.Context, container string) error
	StartContainer(ctx context.Context, container string) error
}

type DetectedService

type DetectedService struct {
	ServiceName   string            `json:"service_name"`
	ContainerName string            `json:"container_name"`
	Label         string            `json:"label"`
	Paths         []string          `json:"paths,omitempty"`
	Metadata      map[string]string `json:"metadata,omitempty"`
}

type DetectionResult

type DetectionResult struct {
	StrategyType string            `json:"strategy_type"`
	Label        string            `json:"label"`
	Services     []DetectedService `json:"services"`
	Available    bool              `json:"available"`
	Description  string            `json:"description"`
}

DetectionResult summarizes what a single strategy found for an app.

type Detector

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

Detector runs all registered strategies against a compose config.

func NewDetector

func NewDetector() *Detector

func (*Detector) DetectAll

func (d *Detector) DetectAll(cfg *compose.AppConfig) []DetectionResult

func (*Detector) Register

func (d *Detector) Register(s Strategy)

type Hook

type Hook struct {
	Type    string `json:"type"`
	Service string `json:"service"`
	Command string `json:"command,omitempty"`
	Timeout int    `json:"timeout,omitempty"` // seconds, 0 = use default
}

type HookRunner

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

func NewHookRunner

func NewHookRunner(exec ContainerExecutor, defaultTimeout time.Duration) *HookRunner

func (*HookRunner) RunPost

func (hr *HookRunner) RunPost(ctx context.Context, hooks []Hook) []string

RunPost executes hooks sequentially, collecting warnings on failure.

func (*HookRunner) RunPre

func (hr *HookRunner) RunPre(ctx context.Context, hooks []Hook) error

RunPre executes hooks sequentially, aborting on first failure.

type LocalTarget

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

LocalTarget stores backups on the local filesystem.

func NewLocalTarget

func NewLocalTarget(dir string) *LocalTarget

func (*LocalTarget) Delete

func (t *LocalTarget) Delete(ctx context.Context, path string) error

func (*LocalTarget) Download

func (t *LocalTarget) Download(ctx context.Context, path string) (io.ReadCloser, error)

func (*LocalTarget) FilePath

func (t *LocalTarget) FilePath(filename string) string

FilePath returns absolute filesystem path for download streaming.

func (*LocalTarget) Test

func (t *LocalTarget) Test(ctx context.Context) error

func (*LocalTarget) Type

func (t *LocalTarget) Type() string

func (*LocalTarget) Upload

func (t *LocalTarget) Upload(ctx context.Context, filename string, data io.Reader) (string, int64, error)

type MongoStrategy

type MongoStrategy struct{}

MongoStrategy backs up and restores MongoDB via mongodump/mongorestore.

func NewMongoStrategy

func NewMongoStrategy() *MongoStrategy

func (*MongoStrategy) Backup

func (s *MongoStrategy) Backup(ctx context.Context, opts BackupOpts) (*BackupResult, error)

func (*MongoStrategy) Detect

func (s *MongoStrategy) Detect(cfg *compose.AppConfig) []DetectedService

func (*MongoStrategy) Restore

func (s *MongoStrategy) Restore(ctx context.Context, opts RestoreOpts) error

func (*MongoStrategy) Type

func (s *MongoStrategy) Type() string

type MySQLStrategy

type MySQLStrategy struct{}

MySQLStrategy backs up and restores MySQL/MariaDB via mysqldump.

func NewMySQLStrategy

func NewMySQLStrategy() *MySQLStrategy

func (*MySQLStrategy) Backup

func (s *MySQLStrategy) Backup(ctx context.Context, opts BackupOpts) (*BackupResult, error)

func (*MySQLStrategy) Detect

func (s *MySQLStrategy) Detect(cfg *compose.AppConfig) []DetectedService

func (*MySQLStrategy) Restore

func (s *MySQLStrategy) Restore(ctx context.Context, opts RestoreOpts) error

func (*MySQLStrategy) Type

func (s *MySQLStrategy) Type() string

type Pipeline

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

Pipeline orchestrates backup and restore flows through strategy, target, and hooks.

func NewPipeline

func NewPipeline(strategy Strategy, target Target, hooks *HookRunner) *Pipeline

NewPipeline creates a Pipeline with the given strategy, target, and optional hook runner.

func (*Pipeline) RunBackup

func (p *Pipeline) RunBackup(ctx context.Context, opts BackupOpts, preHooks, postHooks []Hook) (*PipelineResult, error)

RunBackup: pre-hooks -> strategy.Backup -> tee through checksum -> target.Upload -> post-hooks

func (*Pipeline) RunRestore

func (p *Pipeline) RunRestore(ctx context.Context, opts RestoreOpts, filePath, expectedChecksum string, preHooks, postHooks []Hook) error

RunRestore: download -> verify checksum -> pre-hooks -> strategy.Restore -> post-hooks

type PipelineResult

type PipelineResult struct {
	FilePath  string
	SizeBytes int64
	Checksum  string
}

PipelineResult holds the outcome of a backup pipeline run.

type PostgresStrategy

type PostgresStrategy struct{}

PostgresStrategy backs up and restores a Postgres container via pg_dump/psql.

func NewPostgresStrategy

func NewPostgresStrategy() *PostgresStrategy

func (*PostgresStrategy) Backup

func (s *PostgresStrategy) Backup(ctx context.Context, opts BackupOpts) (*BackupResult, error)

func (*PostgresStrategy) Detect

func (*PostgresStrategy) Restore

func (s *PostgresStrategy) Restore(ctx context.Context, opts RestoreOpts) error

func (*PostgresStrategy) Type

func (s *PostgresStrategy) Type() string

type RedisStrategy

type RedisStrategy struct{}

RedisStrategy backs up and restores Redis by triggering BGSAVE then copying the RDB file.

func NewRedisStrategy

func NewRedisStrategy() *RedisStrategy

func (*RedisStrategy) Backup

func (s *RedisStrategy) Backup(ctx context.Context, opts BackupOpts) (*BackupResult, error)

func (*RedisStrategy) Detect

func (s *RedisStrategy) Detect(cfg *compose.AppConfig) []DetectedService

func (*RedisStrategy) Restore

func (s *RedisStrategy) Restore(ctx context.Context, opts RestoreOpts) error

func (*RedisStrategy) Type

func (s *RedisStrategy) Type() string

type RestoreOpts

type RestoreOpts struct {
	ContainerName string
	Paths         []string
	Credentials   map[string]string
	Reader        io.ReadCloser
	// MaxDecompressedBytes caps the total bytes a strategy is willing to
	// produce from the (potentially gzipped) Reader. 0 means use the default
	// from defaultMaxDecompressed. Set explicitly to override.
	MaxDecompressedBytes int64
}

type S3Config

type S3Config struct {
	Endpoint  string
	Bucket    string
	Prefix    string
	AccessKey string
	SecretKey string
	Region    string
}

S3Config holds connection settings for an S3-compatible object store.

type S3Target

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

S3Target stores backups in an S3-compatible object store.

func NewS3Target

func NewS3Target(cfg S3Config) (*S3Target, error)

func (*S3Target) Delete

func (t *S3Target) Delete(ctx context.Context, path string) error

func (*S3Target) Download

func (t *S3Target) Download(ctx context.Context, path string) (io.ReadCloser, error)

func (*S3Target) PresignedURL

func (t *S3Target) PresignedURL(ctx context.Context, key string, expiry time.Duration) (string, error)

PresignedURL returns a pre-signed GET URL valid for the given duration.

func (*S3Target) Test

func (t *S3Target) Test(ctx context.Context) error

func (*S3Target) Type

func (t *S3Target) Type() string

func (*S3Target) Upload

func (t *S3Target) Upload(ctx context.Context, filename string, data io.Reader) (string, int64, error)

type SQLiteStrategy

type SQLiteStrategy struct{}

SQLiteStrategy backs up SQLite databases inside containers using the .backup command for consistency. Label-only detection (no image keywords).

func NewSQLiteStrategy

func NewSQLiteStrategy() *SQLiteStrategy

func (*SQLiteStrategy) Backup

func (s *SQLiteStrategy) Backup(ctx context.Context, opts BackupOpts) (*BackupResult, error)

func (*SQLiteStrategy) Detect

func (s *SQLiteStrategy) Detect(cfg *compose.AppConfig) []DetectedService

func (*SQLiteStrategy) Restore

func (s *SQLiteStrategy) Restore(ctx context.Context, opts RestoreOpts) error

func (*SQLiteStrategy) Type

func (s *SQLiteStrategy) Type() string

type Scheduler

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

Scheduler runs scheduled backups and handles restore requests.

func NewScheduler

func NewScheduler(st BackupStore, hookExec ContainerExecutor) *Scheduler

NewScheduler creates a new Scheduler backed by st.

func (*Scheduler) CheckMissed

func (s *Scheduler) CheckMissed()

CheckMissed iterates configs and alerts if last run is overdue (>2x cron interval).

func (*Scheduler) GetDetector

func (s *Scheduler) GetDetector() *Detector

GetDetector returns a Detector with all registered strategies.

func (*Scheduler) GetStrategy

func (s *Scheduler) GetStrategy(name string) (Strategy, bool)

GetStrategy returns a registered strategy by name.

func (*Scheduler) RegisterStrategy

func (s *Scheduler) RegisterStrategy(name string, strategy Strategy)

RegisterStrategy registers a named backup strategy.

func (*Scheduler) RegisterTargetFactory

func (s *Scheduler) RegisterTargetFactory(name string, factory func(string) (Target, error))

RegisterTargetFactory registers a factory function for the named target type.

func (*Scheduler) RunBackup

func (s *Scheduler) RunBackup(ctx context.Context, cfgID int64) error

RunBackup executes a backup for the given config ID using Pipeline.

func (*Scheduler) RunRestore

func (s *Scheduler) RunRestore(ctx context.Context, runID int64) error

RunRestore restores data from the backup run identified by runID.

func (*Scheduler) ScheduleConfig

func (s *Scheduler) ScheduleConfig(configID int64, cronExpr string) error

ScheduleConfig adds or replaces a cron entry for the given config.

func (*Scheduler) SetAlertFunc

func (s *Scheduler) SetAlertFunc(fn BackupAlertFunc)

SetAlertFunc sets the callback for backup alerts.

func (*Scheduler) Start

func (s *Scheduler) Start() error

Start loads all backup configs and schedules cron entries, then starts the cron runner.

func (*Scheduler) Stop

func (s *Scheduler) Stop()

Stop stops the cron scheduler.

func (*Scheduler) UnscheduleConfig

func (s *Scheduler) UnscheduleConfig(configID int64)

UnscheduleConfig removes the cron entry for the given config.

type Strategy

type Strategy interface {
	Type() string
	Detect(cfg *compose.AppConfig) []DetectedService
	Backup(ctx context.Context, opts BackupOpts) (*BackupResult, error)
	Restore(ctx context.Context, opts RestoreOpts) error
}

type Target

type Target interface {
	Type() string
	Upload(ctx context.Context, filename string, data io.Reader) (path string, size int64, err error)
	Download(ctx context.Context, path string) (io.ReadCloser, error)
	Delete(ctx context.Context, path string) error
	Test(ctx context.Context) error
}

type TargetFactory

type TargetFactory func(configJSON string) (Target, error)

type VolumeStrategy

type VolumeStrategy struct{}

VolumeStrategy backs up and restores container volumes via tar.

func NewVolumeStrategy

func NewVolumeStrategy() *VolumeStrategy

func (*VolumeStrategy) Backup

func (s *VolumeStrategy) Backup(ctx context.Context, opts BackupOpts) (*BackupResult, error)

func (*VolumeStrategy) Detect

func (s *VolumeStrategy) Detect(cfg *compose.AppConfig) []DetectedService

func (*VolumeStrategy) Restore

func (s *VolumeStrategy) Restore(ctx context.Context, opts RestoreOpts) error

func (*VolumeStrategy) Type

func (s *VolumeStrategy) Type() string

Jump to

Keyboard shortcuts

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