Documentation
¶
Index ¶
- Constants
- Variables
- func HooksJSON(hooks []Hook) string
- func SetSpoolDir(dir string)
- func UploadRestoreMaxBytes() int64
- func ValidateCron(expr string) error
- func ValidateHooks(phase string, hooks []Hook, services, strategyServices []string) error
- func ValidatePath(p string) error
- func ValidatePaths(strategy string, paths []string) error
- func ValidatePathsConfig(strategy, raw string) error
- func ValidateS3Endpoint(ctx context.Context, endpoint string) error
- func ValidateSQLitePath(p string) error
- func VerifyChecksum(r io.Reader, expectedHex string) error
- type BackupAlertFunc
- type BackupOpts
- type BackupResult
- type BackupStore
- type ChecksumWriter
- type ContainerExecutor
- type ContainerLister
- type DetectedService
- type DetectionResult
- type Detector
- type DockerHookExecutor
- func (e *DockerHookExecutor) ExecInService(ctx context.Context, project, service, command string) (string, error)
- func (e *DockerHookExecutor) StartService(ctx context.Context, project, service string) error
- func (e *DockerHookExecutor) StopService(ctx context.Context, project, service string) error
- type EventPublisher
- type Hook
- type HookRunner
- type LocalTarget
- func (t *LocalTarget) Delete(ctx context.Context, path string) error
- func (t *LocalTarget) Download(ctx context.Context, path string) (io.ReadCloser, error)
- func (t *LocalTarget) FilePath(filename string) string
- func (t *LocalTarget) Test(ctx context.Context) error
- func (t *LocalTarget) Type() string
- func (t *LocalTarget) Upload(ctx context.Context, filename string, data io.Reader) (string, int64, error)
- type MongoStrategy
- type MySQLStrategy
- type Pipeline
- type PipelineResult
- type PostgresStrategy
- type RedisStrategy
- type RestoreOpts
- type S3Config
- type S3Target
- func (t *S3Target) Delete(ctx context.Context, path string) error
- func (t *S3Target) Download(ctx context.Context, path string) (io.ReadCloser, error)
- func (t *S3Target) PresignedURL(ctx context.Context, key string, expiry time.Duration) (string, error)
- func (t *S3Target) Test(ctx context.Context) error
- func (t *S3Target) Type() string
- func (t *S3Target) Upload(ctx context.Context, filename string, data io.Reader) (string, int64, error)
- type SQLiteStrategy
- type Scheduler
- func (s *Scheduler) CheckMissed()
- func (s *Scheduler) GetDetector() *Detector
- func (s *Scheduler) GetStrategy(name string) (Strategy, bool)
- func (s *Scheduler) RegisterStrategy(name string, strategy Strategy)
- func (s *Scheduler) RegisterTargetFactory(name string, factory func(string) (Target, error))
- func (s *Scheduler) RunBackup(ctx context.Context, cfgID int64) error
- func (s *Scheduler) RunRestore(ctx context.Context, runID int64) error
- func (s *Scheduler) ScheduleConfig(configID int64, cronExpr string) error
- func (s *Scheduler) SetAlertFunc(fn BackupAlertFunc)
- func (s *Scheduler) SetEventBus(b EventPublisher)
- func (s *Scheduler) Start() error
- func (s *Scheduler) Stop()
- func (s *Scheduler) UnscheduleConfig(configID int64)
- type Strategy
- type Target
- type TargetFactory
- type VolumeStrategy
Constants ¶
const ( HookTypeStop = "stop" HookTypeStart = "start" HookTypeFlushRedis = "flush_redis" HookTypeFlushMySQL = "flush_mysql" HookTypeExec = "exec" )
const ( MaxHooks = 20 MaxHookTimeout = 3600 // seconds MaxHookCommandLen = 4096 )
Limits on stored hooks, checked on save and again before a run.
const ( PreHookPhase = "Pre-backup" PostHookPhase = "Post-backup" )
Phase names for ValidateHooks.
const AllowPrivateS3Env = "SIMPLEDEPLOY_ALLOW_PRIVATE_S3"
AllowPrivateS3Env, when set to "1", lets custom S3 endpoints resolve to private/loopback/reserved addresses (e.g. a MinIO on the same host).
const NoDecompressedLimit int64 = -1
NoDecompressedLimit as RestoreOpts.MaxDecompressedBytes disables the restore size cap.
const RestoreMaxGBEnv = "SIMPLEDEPLOY_RESTORE_MAX_GB"
RestoreMaxGBEnv overrides the restore size cap, in whole GiB.
Variables ¶
var ErrS3EndpointBlocked = errors.New("S3 endpoint points to a private or reserved network address")
ErrS3EndpointBlocked is returned (wrapped) when a custom S3 endpoint resolves to a private or reserved address.
Functions ¶
func SetSpoolDir ¶ added in v1.4.3
func SetSpoolDir(dir string)
SetSpoolDir sets the directory restore archives are staged in.
func UploadRestoreMaxBytes ¶ added in v1.4.3
func UploadRestoreMaxBytes() int64
UploadRestoreMaxBytes is the size cap for archives uploaded through the API: RestoreMaxGBEnv when set, else 8 GiB.
func ValidateCron ¶ added in v1.4.3
ValidateCron parses expr with the same parser the scheduler's cron runner uses. An empty expression (no schedule) is valid.
func ValidateHooks ¶ added in v1.4.5
ValidateHooks checks one hook list; phase (PreHookPhase, PostHookPhase) prefixes messages. services, when non-nil, lists the app's compose services every hook must name. A pre-hook may not stop any of strategyServices (the services the backup and restore commands may run inside), since those commands need it running. Errors are human-readable.
func ValidatePath ¶ added in v1.4.3
ValidatePath checks a container path that ends up as a tar/cp operand. It must be absolute, printable, in clean form apart from an optional trailing '/' (no '//', '.' or '..' parts), not "/" itself, and no element may start with '-' (so it can never be parsed as a command-line flag).
func ValidatePaths ¶ added in v1.4.3
ValidatePaths validates paths for the given strategy. SQLite paths get the extra character check.
func ValidatePathsConfig ¶ added in v1.4.3
ValidatePathsConfig parses a stored paths value (JSON array or comma-separated list, same as the scheduler) and validates it.
func ValidateS3Endpoint ¶ added in v1.4.3
ValidateS3Endpoint checks a custom S3 endpoint before it is saved. Empty (AWS default) is always allowed. Otherwise it must be an http(s) URL and, unless SIMPLEDEPLOY_ALLOW_PRIVATE_S3=1, every address the host resolves to must be public. The connection-time dialer re-checks this.
func ValidateSQLitePath ¶ added in v1.4.3
ValidateSQLitePath applies ValidatePath, refuses quotes, backslash, backtick and '$', and requires a file name (no trailing '/').
Types ¶
type BackupAlertFunc ¶
BackupAlertFunc is a callback for backup-related alerts.
type BackupOpts ¶
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)
DeleteBackupRun(id int64) 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
type ContainerExecutor ¶
type ContainerExecutor interface {
ExecInService(ctx context.Context, project, service, command string) (string, error)
StopService(ctx context.Context, project, service string) error
StartService(ctx context.Context, project, service string) error
}
ContainerExecutor runs hook actions on one compose service of one compose project. Implementations must act only on containers labeled with both the project and the service, so a hook can never reach another app's containers, SimpleDeploy itself, or other containers on the host.
type ContainerLister ¶ added in v1.4.5
type ContainerLister interface {
ContainerList(ctx context.Context, opts container.ListOptions) ([]container.Summary, error)
}
ContainerLister is the part of docker.Client the hook executor needs.
type DetectedService ¶
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
type DockerHookExecutor ¶ added in v1.4.5
type DockerHookExecutor struct {
// contains filtered or unexported fields
}
DockerHookExecutor runs backup hooks with the docker CLI. It finds containers only by their compose project and service labels and acts on them by ID, so a hook's service can never name an arbitrary container (another app's, SimpleDeploy's, or any other on the host).
func NewDockerHookExecutor ¶ added in v1.4.5
func NewDockerHookExecutor(client ContainerLister) *DockerHookExecutor
NewDockerHookExecutor returns an executor that lists containers through client and runs docker exec/stop/start for them.
func (*DockerHookExecutor) ExecInService ¶ added in v1.4.5
func (e *DockerHookExecutor) ExecInService(ctx context.Context, project, service, command string) (string, error)
ExecInService runs command with sh -c in the service's first running container.
func (*DockerHookExecutor) StartService ¶ added in v1.4.5
func (e *DockerHookExecutor) StartService(ctx context.Context, project, service string) error
StartService starts every container of the service.
func (*DockerHookExecutor) StopService ¶ added in v1.4.5
func (e *DockerHookExecutor) StopService(ctx context.Context, project, service string) error
StopService stops every container of the service.
type EventPublisher ¶ added in v1.4.5
EventPublisher receives realtime notify-only events. *events.Bus satisfies it.
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
}
func CheckHooks ¶ added in v1.4.5
func CheckHooks(rawPre, rawPost string, composeCfg *compose.AppConfig, strategy Strategy) (pre, post []Hook, legacy int, err error)
CheckHooks parses and validates a config's stored pre and post hooks. With composeCfg (nil when the compose file can't be read) hooks must name one of its services, and with strategy too a pre-hook may not stop a service that strategy detects (it runs in one of them). legacy counts skipped entries from the old wizard format (see ParseHooks).
func ParseHooks ¶ added in v1.4.5
ParseHooks decodes a stored hooks value: a JSON list, with "" or "null" meaning none. Entries whose service is an object were saved by an older backup wizard and never ran; they are skipped and counted in legacy.
type HookRunner ¶
type HookRunner struct {
// contains filtered or unexported fields
}
func NewHookRunner ¶
func NewHookRunner(exec ContainerExecutor, project string, defaultTimeout time.Duration) *HookRunner
NewHookRunner returns a runner whose hooks act only on services of the compose project (simpledeploy-<slug>).
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) 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) Type ¶
func (t *LocalTarget) Type() string
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
Warnings []string // post-hook failures; the backup itself succeeded
}
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 (s *PostgresStrategy) Detect(cfg *compose.AppConfig) []DetectedService
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 bytes a strategy produces from the
// (possibly gzipped) Reader, and the raw archive size where it is
// staged on disk. 0 means defaultMaxDecompressed; NoDecompressedLimit
// (any negative value) disables the cap.
MaxDecompressedBytes int64
}
type S3Config ¶
type S3Config struct {
Endpoint string `json:"endpoint"`
Bucket string `json:"bucket"`
Prefix string `json:"prefix"`
AccessKey string `json:"access_key"`
SecretKey string `json:"secret_key"`
Region string `json:"region"`
}
S3Config holds connection settings for an S3-compatible object store.
Its JSON form (backup_configs.target_config_json and the test-s3 request body) uses the snake_case keys the UI and docs send: endpoint, bucket, prefix, access_key, secret_key, region. UnmarshalJSON also accepts the Go field names (Endpoint, AccessKey, ...) that older clients sent before the struct had tags, so configs stored that way keep working.
func (*S3Config) UnmarshalJSON ¶ added in v1.4.4
UnmarshalJSON decodes snake_case keys and falls back to the legacy AccessKey/SecretKey keys when the snake_case ones are missing or empty. The other legacy keys (Endpoint, Bucket, ...) already match their tags because encoding/json matches keys case-insensitively.
type S3Target ¶
type S3Target struct {
// contains filtered or unexported fields
}
S3Target stores backups in an S3-compatible object store.
func NewS3Target ¶
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 ¶
GetDetector returns a Detector with all registered strategies.
func (*Scheduler) GetStrategy ¶
GetStrategy returns a registered strategy by name.
func (*Scheduler) RegisterStrategy ¶
RegisterStrategy registers a named backup strategy.
func (*Scheduler) RegisterTargetFactory ¶
RegisterTargetFactory registers a factory function for the named target type.
func (*Scheduler) RunRestore ¶
RunRestore restores data from the backup run identified by runID.
func (*Scheduler) ScheduleConfig ¶
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) SetEventBus ¶ added in v1.4.5
func (s *Scheduler) SetEventBus(b EventPublisher)
SetEventBus wires the realtime events bus, told when a backup or restore run starts and finishes so open Backups views refresh. nil disables it.
func (*Scheduler) Start ¶
Start loads all backup configs and schedules cron entries, then starts the cron runner.
func (*Scheduler) UnscheduleConfig ¶
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 TargetFactory ¶
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