nomad-temporal-jobs

backup-activities

import "munchbox/temporal-workers/backup/activities"

Index

type Activities

Activities holds shared dependencies for backup activities. Register an instance with the Temporal worker to expose all exported methods as activity implementations.

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

func New

func New(cfg Config) (*Activities, error)

New creates an Activities instance with a pre-configured S3 client. Returns an error if the config is invalid.

func (*Activities) BackupPostgresDatabase

func (a *Activities) BackupPostgresDatabase(ctx context.Context, database string) (string, error)

BackupPostgresDatabase dumps a single database to its own gzipped file. Smaller per-database files restore faster and spread across storage more evenly than one cluster-wide dump. Long-running for large databases, so it heartbeats while the dump runs. Requires PGPASSWORD in the environment.

func (*Activities) BackupPostgresGlobals

func (a *Activities) BackupPostgresGlobals(ctx context.Context) (string, error)

BackupPostgresGlobals dumps cluster-wide objects (roles, tablespaces, and grants) that per-database pg_dump does not capture. Without this, a full-cluster restore would come up with no roles or permissions. Requires PGPASSWORD in the environment.

func (*Activities) CleanupOldBackups

func (a *Activities) CleanupOldBackups(ctx context.Context, retentionDays int) error

CleanupOldBackups removes local backup files older than the retention period across all backup directories.

func (*Activities) CleanupOldS3Backups

func (a *Activities) CleanupOldS3Backups(ctx context.Context, retentionDays int) error

CleanupOldS3Backups removes S3 backup objects older than the retention period by listing all objects under the backups/ prefix and deleting those whose LastModified exceeds the cutoff.

func (*Activities) ListPostgresDatabases

func (a *Activities) ListPostgresDatabases(ctx context.Context) ([]string, error)

ListPostgresDatabases returns the names of all non-template, connectable databases in the cluster. The workflow fans these out into per-database dumps. Requires PGPASSWORD in the environment.

func (*Activities) TakeConsulSnapshot

func (a *Activities) TakeConsulSnapshot(ctx context.Context) (string, error)

TakeConsulSnapshot creates a Raft snapshot of the Consul cluster state including KV store, service catalog, ACLs, sessions, and intentions. Vault data is included since Vault uses Consul as its storage backend. Requires CONSUL_HTTP_TOKEN with snapshot permissions in the environment.

func (*Activities) TakeNomadSnapshot

func (a *Activities) TakeNomadSnapshot(ctx context.Context) (string, error)

TakeNomadSnapshot creates a Raft snapshot of the Nomad cluster state including job specs, allocations, ACLs, and scheduler configuration. Requires NOMAD_TOKEN with snapshot permissions in the environment.

func (*Activities) TakeRegistryBackup

func (a *Activities) TakeRegistryBackup(ctx context.Context) (string, error)

TakeRegistryBackup creates a gzipped tarball of the container registry data directory including all pushed images, layers, manifests, and metadata.

func (*Activities) UploadToS3

func (a *Activities) UploadToS3(ctx context.Context, localPath string, keyPrefix string) (string, error)

UploadToS3 uploads a local backup file to S3 storage. The S3 key is constructed from the prefix and the original filename. If the upload fails with a 507 quota error, the oldest backup under the same prefix is evicted and the upload is retried up to 3 times.

type BackupConfig

BackupConfig holds workflow-level configuration passed as input so values are deterministic across replays.

type BackupConfig struct {
    // LocalDays is the local-backup retention window. Default 7.
    LocalDays int `json:"local_days"`
    // S3Days is the S3-backup retention window. Default 30.
    S3Days int `json:"s3_days"`
    // DumpConcurrency bounds how many per-database pg_dump activities run
    // at once so the parallel dumps don't overwhelm the primary. Default 4.
    DumpConcurrency int `json:"dump_concurrency"`
}

func (*BackupConfig) ApplyDefaults

func (c *BackupConfig) ApplyDefaults()

ApplyDefaults fills in unset fields with their defaults. Called by the workflow before any activities run so the values are deterministic across replay.

type BackupResult

BackupResult contains the outcome of a backup workflow execution. The PostgreSQL leg now produces one dump per database (plus a globals dump) rather than a single cluster-wide file.

type BackupResult struct {
    NomadSnapshot  string `json:"nomad_snapshot"`
    ConsulSnapshot string `json:"consul_snapshot"`
    NomadS3Key     string `json:"nomad_s3_key"`
    ConsulS3Key    string `json:"consul_s3_key"`

    PostgresGlobals      string           `json:"postgres_globals"`
    PostgresGlobalsS3Key string           `json:"postgres_globals_s3_key,omitempty"`
    PostgresDatabases    []DatabaseBackup `json:"postgres_databases"`

    Timestamp time.Time `json:"timestamp"`
    Success   bool      `json:"success"`
    Error     string    `json:"error,omitempty"`
}

type Config

Config holds environment-driven settings for backup activities.

type Config struct {
    S3Endpoint  string
    S3Bucket    string
    S3AccessKey string
    S3SecretKey string

    NomadBackupDir    string // default: /mnt/gdrive/nomad-snapshots
    ConsulBackupDir   string // default: /mnt/gdrive/consul-snapshots
    PostgresBackupDir string // default: /mnt/gdrive/postgres-backups
    RegistryBackupDir string // default: /mnt/gdrive/registry-backups
    RegistryDataDir   string // default: /mnt/gdrive/munchbox-data/registry

    PostgresHost string // default: postgres-primary.service.consul
    PostgresUser string // default: postgres
}

func (*Config) Validate

func (c *Config) Validate() error

Validate checks that required S3 fields are present and applies defaults for optional directory paths.

type DatabaseBackup

DatabaseBackup records the outcome of backing up a single PostgreSQL database: the local dump path and, if the upload succeeded, its S3 key.

type DatabaseBackup struct {
    Database  string `json:"database"`
    LocalPath string `json:"local_path"`
    S3Key     string `json:"s3_key,omitempty"`
}

Generated by gomarkdoc