backup

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BackupDirFromEnv

func BackupDirFromEnv() string

BackupDirFromEnv returns the backup directory from DB_BACKUP_PATH, or default database/backups.

func CopyFromReaderIntoFile

func CopyFromReaderIntoFile(r io.Reader, path string, useGzip bool) error

CopyFromReaderIntoFile reads from r and writes to path; if path ends in .gz, compresses.

func DefaultBackupPath

func DefaultBackupPath(backupDir, dbName string, gzipOut bool) string

DefaultBackupPath returns database/backups/dbname_20060102_150405.sql.gz

func DockerAvailable

func DockerAvailable(ctx context.Context) bool

DockerAvailable returns true if docker is in PATH and can run the postgres image.

func DockerBackup

func DockerBackup(ctx context.Context, dsn, outPath string, schemaOnly, dataOnly bool, gzipOut bool) error

DockerBackup runs pg_dump inside a postgres container and streams output to outPath. For localhost DB, uses host.docker.internal so the container can reach the host.

func DockerRestore

func DockerRestore(ctx context.Context, dsn, inPath string) error

DockerRestore runs psql or pg_restore inside a postgres container. For .dump files uses pg_restore; for .sql/.sql.gz uses psql with stdin.

func GoBackup

func GoBackup(ctx context.Context, dsn, outPath string, schemaOnly, dataOnly bool, gzipOut bool) error

GoBackup performs a full backup using pgx: schema (CREATE TABLE) + data (COPY format) + sequences. Writes plain SQL to outPath; if gzipOut is true, compresses with gzip.

func GoRestore

func GoRestore(ctx context.Context, dsn, inPath string) error

GoRestore restores a plain SQL or .sql.gz backup using pgx. Does not support custom .dump format.

func Latest

func Latest(dir string) (string, error)

Latest returns the path of the most recent backup in dir, or "" if none.

func NativeAvailable

func NativeAvailable() bool

NativeAvailable returns true if pg_dump is in PATH.

func NativeBackup

func NativeBackup(ctx context.Context, dsn, outPath string, schemaOnly, dataOnly bool, format string, gzipOut bool) error

NativeBackup runs pg_dump and writes to outPath. If gzipOut is true, compresses with gzip. format is "plain" or "custom". For custom, outPath should end in .dump and gzipOut is ignored.

func NativeRestore

func NativeRestore(ctx context.Context, dsn, inPath string) error

NativeRestore runs psql or pg_restore to restore from file.

func ReadSQLStatements

func ReadSQLStatements(r io.Reader) ([]string, error)

ReadSQLStatements reads a .sql (or decompressed) stream and yields statements. Used by Go restore. Splits by semicolon outside of quoted strings (simple approach).

func RunBackup

func RunBackup(ctx context.Context, opts BackupOptions) (outPath string, err error)

RunBackup creates a backup using the best available strategy (native -> Docker -> Go).

func RunPgDumpStdout

func RunPgDumpStdout(ctx context.Context, dsn string, schemaOnly, dataOnly bool) (io.ReadCloser, error)

RunPgDumpStdout runs pg_dump writing to stdout (for Docker-style capture).

func RunPsqlStdin

func RunPsqlStdin(ctx context.Context, dsn string, r io.Reader) error

RunPsqlStdin runs psql with stdin from r (for Docker restore).

func RunRestore

func RunRestore(ctx context.Context, opts RestoreOptions) error

RunRestore restores from a backup file using the best available strategy.

Types

type BackupEntry

type BackupEntry struct {
	Name string
	Path string
	Size int64
	Mod  time.Time
}

BackupEntry describes a single backup file in the backup directory.

func List

func List(dir string) ([]BackupEntry, error)

List returns backup files in dir, sorted by modification time descending (latest first). Only includes .sql, .sql.gz, and .dump files.

type BackupOptions

type BackupOptions struct {
	DSN         string
	OutputPath  string
	BackupDir   string
	SchemaOnly  bool
	DataOnly    bool
	Format      string // "plain" or "custom"
	Compress    bool
	UseStrategy Strategy
}

BackupOptions configures a backup run.

type PGEnv

type PGEnv struct {
	Host     string
	Port     string
	User     string
	Password string
	Database string
}

PGEnv holds PostgreSQL connection components for passing to exec or Docker.

func ParseDSN

func ParseDSN(dsn string) (*PGEnv, error)

ParseDSN extracts connection components from a postgres:// DSN. Used to set PGHOST, PGPORT, PGUSER, PGPASSWORD, PGDATABASE for native/Docker.

func (*PGEnv) EnvMap

func (e *PGEnv) EnvMap() map[string]string

EnvMap returns a string map suitable for os/exec.Cmd.Env (KEY=value).

func (*PGEnv) EnvSlice

func (e *PGEnv) EnvSlice() []string

EnvSlice returns a slice of "KEY=value" for exec.Cmd.Env.

func (*PGEnv) HostForDocker

func (e *PGEnv) HostForDocker() string

HostForDocker returns the host to use inside a Docker container. For "localhost" or "127.0.0.1", returns "host.docker.internal" so the container can reach the host's Postgres.

func (*PGEnv) PortInt

func (e *PGEnv) PortInt() int

PortInt returns port as int for use in URL building.

type RestoreOptions

type RestoreOptions struct {
	DSN         string
	InputPath   string // path to file, or "latest" to use latest in backup dir
	BackupDir   string
	UseStrategy Strategy
}

RestoreOptions configures a restore run.

type Strategy

type Strategy int

Strategy is the backup/restore implementation to use.

const (
	StrategyAuto Strategy = iota
	StrategyNative
	StrategyDocker
	StrategyGo
)

Jump to

Keyboard shortcuts

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