Documentation
¶
Index ¶
- func BackupDirFromEnv() string
- func CopyFromReaderIntoFile(r io.Reader, path string, useGzip bool) error
- func DefaultBackupPath(backupDir, dbName string, gzipOut bool) string
- func DockerAvailable(ctx context.Context) bool
- func DockerBackup(ctx context.Context, dsn, outPath string, schemaOnly, dataOnly bool, ...) error
- func DockerRestore(ctx context.Context, dsn, inPath string) error
- func GoBackup(ctx context.Context, dsn, outPath string, schemaOnly, dataOnly bool, ...) error
- func GoRestore(ctx context.Context, dsn, inPath string) error
- func Latest(dir string) (string, error)
- func NativeAvailable() bool
- func NativeBackup(ctx context.Context, dsn, outPath string, schemaOnly, dataOnly bool, ...) error
- func NativeRestore(ctx context.Context, dsn, inPath string) error
- func ReadSQLStatements(r io.Reader) ([]string, error)
- func RunBackup(ctx context.Context, opts BackupOptions) (outPath string, err error)
- func RunPgDumpStdout(ctx context.Context, dsn string, schemaOnly, dataOnly bool) (io.ReadCloser, error)
- func RunPsqlStdin(ctx context.Context, dsn string, r io.Reader) error
- func RunRestore(ctx context.Context, opts RestoreOptions) error
- type BackupEntry
- type BackupOptions
- type PGEnv
- type RestoreOptions
- type Strategy
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 ¶
CopyFromReaderIntoFile reads from r and writes to path; if path ends in .gz, compresses.
func DefaultBackupPath ¶
DefaultBackupPath returns database/backups/dbname_20060102_150405.sql.gz
func DockerAvailable ¶
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 ¶
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 ¶
GoRestore restores a plain SQL or .sql.gz backup using pgx. Does not support custom .dump format.
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 ¶
NativeRestore runs psql or pg_restore to restore from file.
func ReadSQLStatements ¶
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 ¶
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 ¶
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 ¶
PGEnv holds PostgreSQL connection components for passing to exec or Docker.
func ParseDSN ¶
ParseDSN extracts connection components from a postgres:// DSN. Used to set PGHOST, PGPORT, PGUSER, PGPASSWORD, PGDATABASE for native/Docker.
func (*PGEnv) HostForDocker ¶
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.