From 7774a5b95680e0877472b732312e508bac5ee3fc Mon Sep 17 00:00:00 2001 From: Sam Hoffman Date: Sat, 31 Jan 2026 01:55:24 -0500 Subject: [PATCH] replace use of shell w/ direct calls --- internal/jobs/jobs.go | 90 +++++++++++++++++++++---------------------- internal/zfs/zfs.go | 48 +++++++++++++++++++++++ 2 files changed, 92 insertions(+), 46 deletions(-) diff --git a/internal/jobs/jobs.go b/internal/jobs/jobs.go index 7c1f890..8c0d4c8 100644 --- a/internal/jobs/jobs.go +++ b/internal/jobs/jobs.go @@ -7,8 +7,6 @@ import ( "context" "fmt" "log" - "os/exec" - "strconv" "strings" "time" @@ -16,12 +14,13 @@ import ( ) type BackupJob struct { - Source string `yaml:"source"` // the source dataset (e.g., zroot) - TargetHost string `yaml:"targetHost"` // SSH-compatible host - Target string `yaml:"target"` // the target dataset - Keep int `yaml:"keep"` // number of snapshots to keep - Recursive bool `yaml:"recursive"` // create recursive snapshots - Prefix string `yaml:"prefix"` // name each snapshot with this prefix + Source string `yaml:"source"` // the source dataset (e.g., zroot) + TargetHost string `yaml:"targetHost"` // SSH-compatible host + Target string `yaml:"target"` // the target dataset + Keep int `yaml:"keep"` // number of snapshots to keep + MaxAge time.Duration `yaml:"maxAge"` // age at which to delete snapshot (if Keep is met) + Recursive bool `yaml:"recursive"` // create recursive snapshots + Prefix string `yaml:"prefix"` // name each snapshot with this prefix } // func (j *BackupJob) getBaseSnap() { @@ -30,44 +29,20 @@ type BackupJob struct { // dstCmd := fmt.Sprintf(params, j.Target) // } func (j *BackupJob) listSnapshots(ctx context.Context) ([]zfs.Snapshot, error) { - out, err := zfs.Cmd(ctx, "zfs list -Hp -o name,creation -t snapshot -s creation %s | grep '%s'", j.Source, j.Prefix) + allSnaps, err := zfs.Snapshots(ctx, j.Source) if err != nil { - execErr, ok := err.(*exec.ExitError) - if !ok { - log.Printf("%s\n", out) - return nil, err - } - - if execErr.ExitCode() == 1 { - return nil, nil - } + return nil, err } - snapList := strings.Split(string(out), "\n") - snaps := make([]zfs.Snapshot, len(snapList)-1) - - for i, snap := range snapList { - params := strings.Split(snap, "\t") // "zroot@foo\tTIME" -> ["zroot@foo", "TIME"] - if len(params) != 2 { + filtered := make([]zfs.Snapshot, 0) + for _, snap := range allSnaps { + if !strings.Contains(snap.Name, j.Prefix) { continue } - log.Printf("%+v\n", params) - t, err := strconv.ParseInt(params[1], 10, 64) - if err != nil { - log.Fatalf("invalid time %s", params[1]) - } - - identifier := strings.Split(params[0], "@") // zroot@foo -> ["zroot", "foo"] - - snaps[i] = zfs.Snapshot{ - SnapshotName: identifier[1], - Dataset: identifier[0], - Name: params[0], - Creation: time.Unix(t, 0), - } + filtered = append(filtered, snap) } - return snaps, nil + return filtered, nil } func (j *BackupJob) snapName() string { @@ -97,9 +72,8 @@ func (j *BackupJob) Snapshot(ctx context.Context) (string, error) { } func (j *BackupJob) FullSend(ctx context.Context, snap string) { - out, err := zfs.Cmd( + out, err := zfs.FullSend( ctx, - "zfs send -Lec %s | ssh %s zfs recv -Fu %s", snap, j.TargetHost, j.Target, @@ -109,19 +83,43 @@ func (j *BackupJob) FullSend(ctx context.Context, snap string) { } } -func (j *BackupJob) IncrementalSend(ctx context.Context, prevSnap *zfs.Snapshot, newSnap string) { - out, err := zfs.Cmd( +func (j *BackupJob) IncrementalSend(ctx context.Context, prevSnap *zfs.Snapshot, newSnap string) ([]byte, error) { + return zfs.IncrementalSend( ctx, - "zfs send -I@%s %s | ssh %s zfs recv %s", prevSnap.SnapshotName, newSnap, j.TargetHost, j.Target, ) +} + +func (j *BackupJob) Retain(ctx context.Context) error { + snaps, err := j.listSnapshots(ctx) if err != nil { - // FIXME: return an error so we can cleanup - log.Fatalf("zfs-send: error: %s", out) + return fmt.Errorf("retain: failure listing snapshots: %s", err.Error()) } + + forDeletion := make([]zfs.Snapshot, 0, len(snaps)) + now := time.Now() + + if len(snaps) < j.Keep { + return nil + } + + deleteCount := len(snaps) - j.Keep + + for i, snap := range snaps { + age := now.Sub(snap.Creation) + if age >= j.MaxAge { + forDeletion[i] = snap + } + + if len(forDeletion) == deleteCount { + break + } + } + + return nil } func (j *BackupJob) Do(ctx context.Context) { diff --git a/internal/zfs/zfs.go b/internal/zfs/zfs.go index c642863..3dfa0c9 100644 --- a/internal/zfs/zfs.go +++ b/internal/zfs/zfs.go @@ -10,6 +10,8 @@ import ( "strconv" "strings" "time" + + "git.gentoo.party/sam/thanks/internal/runner" ) type Snapshot struct { @@ -113,3 +115,49 @@ func Snapshots(ctx context.Context, target string) ([]Snapshot, error) { return snaps, err } + +func FullSend(ctx context.Context, snap, sshTarget, target string) ([]byte, error) { + return runner.Pipeline( + exec.CommandContext( + ctx, + "zfs", + "send", + "-Lec", + snap, + ), + exec.CommandContext( + ctx, + "ssh", + sshTarget, + "zfs", + "recv", + "-Fu", + target, + ), + ) +} + +func IncrementalSend(ctx context.Context, prevSnapName, newSnapName, sshTarget, target string) ([]byte, error) { + return runner.Pipeline( + exec.CommandContext( + ctx, + "zfs", + "send", + fmt.Sprintf("-I@%s", prevSnapName), + newSnapName, + ), + exec.CommandContext( + ctx, + "ssh", + sshTarget, + "zfs", + "recv", + target, + ), + ) +} + +func Destroy(ctx context.Context, target string) ([]byte, error) { + cmd := exec.CommandContext(ctx, "zfs", "destroy", target) + return cmd.CombinedOutput() +}