Files
online-backend/internal/backup/dump.go
GuwandykA 28509eb4ca
Some checks failed
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled
CI/CD / Deploy binary to server (push) Has been cancelled
Add backups, currencies and dashboard APIs, and scope access by guard_name.
Web users may only CRUD their own records, operators stay in assigned branches, and admin/manager/superadmin keep full access.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-31 18:14:47 +05:00

56 lines
1.0 KiB
Go

package backup
import (
"context"
"fmt"
"os"
"os/exec"
"strings"
"time"
"online-back/internal/config"
)
const dumpTimeout = 10 * time.Minute
type Dumper interface {
Dump(ctx context.Context, destPath string) error
}
type PgDumper struct {
storage config.StorageConfig
}
func NewPgDumper(storage config.StorageConfig) *PgDumper {
return &PgDumper{storage: storage}
}
func (d *PgDumper) Dump(ctx context.Context, destPath string) error {
ctx, cancel := context.WithTimeout(ctx, dumpTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, "pg_dump",
"-h", d.storage.Host,
"-p", d.storage.Port,
"-U", d.storage.Username,
"-d", d.storage.Database,
"--no-owner",
"--no-acl",
"-f", destPath,
)
cmd.Env = append(os.Environ(), "PGPASSWORD="+d.storage.Password)
out, err := cmd.CombinedOutput()
if err != nil {
msg := strings.TrimSpace(string(out))
if msg == "" {
msg = err.Error()
}
if len(msg) > 512 {
msg = msg[:512]
}
return fmt.Errorf("pg_dump: %s", msg)
}
return nil
}