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>
56 lines
1.0 KiB
Go
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
|
|
}
|