Compare commits

...

4 Commits

Author SHA1 Message Date
Sam Hoffman
7774a5b956 replace use of shell w/ direct calls 2026-01-31 01:55:24 -05:00
Sam Hoffman
297c499bba zfs: parse GUID 2026-01-31 01:18:50 -05:00
Sam Hoffman
b39d8d2d06 comment 2026-01-31 00:31:01 -05:00
Sam Hoffman
d6a01c4ee2 runner: pipe commands together 2026-01-31 00:29:33 -05:00
6 changed files with 242 additions and 47 deletions

View File

@@ -7,8 +7,6 @@ import (
"context"
"fmt"
"log"
"os/exec"
"strconv"
"strings"
"time"
@@ -20,6 +18,7 @@ type BackupJob struct {
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
}
@@ -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
}
}
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])
filtered = append(filtered, snap)
}
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),
}
}
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) {

View File

@@ -1,6 +1,7 @@
package runner
import (
"bytes"
"context"
"fmt"
"os/exec"
@@ -26,3 +27,35 @@ func (runner *ZRunner) Run(ctx context.Context, command string, formatArgs ...an
cmd := exec.Command(runner.Prog, args...)
return cmd.CombinedOutput()
}
// Pipeline accepts a slice of [exec.Cmd] and pipes them together
// then returns the result of the final command
func Pipeline(cmds ...*exec.Cmd) ([]byte, error) {
for i := 0; i < len(cmds)-1; i++ {
stdout, err := cmds[i].StdoutPipe()
if err != nil {
return nil, err
}
cmds[i+1].Stdin = stdout
}
out := bytes.Buffer{}
cmds[len(cmds)-1].Stdout = &out
for _, cmd := range cmds {
err := cmd.Start()
if err != nil {
return out.Bytes(), err
}
}
for _, cmd := range cmds {
err := cmd.Wait()
if err != nil {
return out.Bytes(), err
}
}
return out.Bytes(), nil
}

View File

@@ -2,9 +2,11 @@ package runner_test
import (
"context"
"os/exec"
"testing"
"git.gentoo.party/sam/thanks/internal/runner"
"github.com/stretchr/testify/assert"
)
func Test_ZCommand(t *testing.T) {
@@ -19,3 +21,12 @@ func Test_ZCommand(t *testing.T) {
t.Errorf("localRunner failed: %s\n\n%s", err.Error(), out)
}
}
func Test_Pipelie(t *testing.T) {
c1 := exec.Command("echo", "-n", "foo")
c2 := exec.Command("rev")
out, err := runner.Pipeline(c1, c2)
assert.Equal(t, "oof", string(out))
assert.Nil(t, err)
}

View File

@@ -0,0 +1,6 @@
zroot/home/sam/thanks@thanks-1769803874756987 1769803874 9856814317153087085
zroot/home/sam/thanks@thanks-1769804009334385 1769804009 10898692868281532431
zroot/home/sam/thanks@thanks-1769810365460466 1769810365 17939462811459040773
zroot/home/sam/thanks@thanks-1769810764161053 1769810764 17021914338902266865
zroot/home/sam/thanks@thanks-1769813076492805 1769813076 17716303459843516357
zroot/home/sam/thanks@thanks-1769813083041347 1769813083 905910106048059171

View File

@@ -1,10 +1,17 @@
package zfs
import (
"bufio"
"context"
"fmt"
"io"
"log"
"os/exec"
"strconv"
"strings"
"time"
"git.gentoo.party/sam/thanks/internal/runner"
)
type Snapshot struct {
@@ -12,7 +19,7 @@ type Snapshot struct {
Name string // Full dataset@snapname
Dataset string
Creation time.Time
GUID int64
GUID uint64
}
func Cmd(ctx context.Context, arg string, a ...any) ([]byte, error) {
@@ -29,3 +36,128 @@ func Cmd(ctx context.Context, arg string, a ...any) ([]byte, error) {
}
return out, err
}
func ParseSnapshots(reader io.Reader) ([]Snapshot, error) {
scanner := bufio.NewScanner(reader)
snaps := make([]Snapshot, 0)
for scanner.Scan() {
line := scanner.Text()
params := strings.Split(line, "\t") // "zroot@foo\tTIME" -> ["zroot@foo", "TIME"]
if len(params) != 3 {
continue
}
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"]
GUID, err := strconv.ParseUint(params[2], 10, 64)
if err != nil {
log.Fatalf("invalid GUID: %s - %s", params[2], err.Error())
}
snaps = append(
snaps,
Snapshot{
SnapshotName: identifier[1],
Dataset: identifier[0],
Name: params[0],
Creation: time.Unix(t, 0),
GUID: GUID,
},
)
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("error parsing zfs-list output: %s", err.Error())
}
return snaps, nil
}
func Snapshots(ctx context.Context, target string) ([]Snapshot, error) {
cmd := exec.CommandContext(
ctx,
"zfs",
"list",
"-Hp",
"-o",
"name,creation,guid",
"-t",
"snapshot",
"-s",
"creation",
target,
)
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
defer stdout.Close()
err = cmd.Start()
if err != nil {
return nil, err
}
snaps, err := ParseSnapshots(stdout)
if err != nil {
return nil, err
}
err = cmd.Wait()
if err != nil {
return nil, err
}
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()
}

View File

@@ -2,8 +2,12 @@ package zfs_test
import (
"fmt"
"os"
"os/exec"
"testing"
"git.gentoo.party/sam/thanks/internal/zfs"
"github.com/stretchr/testify/assert"
)
type zfscmd struct {
@@ -43,3 +47,14 @@ func Test_ZFSCommand(t *testing.T) {
t.Errorf("SSH zrunner failed: %s \n\n%s", err.Error(), out)
}
}
func Test_Snapshots(t *testing.T) {
f, err := os.Open("./zfs-list_snapshot.txt")
assert.Nil(t, err, "failed to open test data")
out, err := zfs.ParseSnapshots(f)
assert.Nil(t, err, "ParseSnapshots returned non-nil error")
for _, snap := range out {
assert.Contains(t, snap.SnapshotName, "thanks")
}
}