Compare commits
4 Commits
1f1e2db229
...
7774a5b956
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7774a5b956 | ||
|
|
297c499bba | ||
|
|
b39d8d2d06 | ||
|
|
d6a01c4ee2 |
@@ -7,8 +7,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os/exec"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -20,6 +18,7 @@ type BackupJob struct {
|
|||||||
TargetHost string `yaml:"targetHost"` // SSH-compatible host
|
TargetHost string `yaml:"targetHost"` // SSH-compatible host
|
||||||
Target string `yaml:"target"` // the target dataset
|
Target string `yaml:"target"` // the target dataset
|
||||||
Keep int `yaml:"keep"` // number of snapshots to keep
|
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
|
Recursive bool `yaml:"recursive"` // create recursive snapshots
|
||||||
Prefix string `yaml:"prefix"` // name each snapshot with this prefix
|
Prefix string `yaml:"prefix"` // name each snapshot with this prefix
|
||||||
}
|
}
|
||||||
@@ -30,44 +29,20 @@ type BackupJob struct {
|
|||||||
// dstCmd := fmt.Sprintf(params, j.Target)
|
// dstCmd := fmt.Sprintf(params, j.Target)
|
||||||
// }
|
// }
|
||||||
func (j *BackupJob) listSnapshots(ctx context.Context) ([]zfs.Snapshot, error) {
|
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 {
|
if err != nil {
|
||||||
execErr, ok := err.(*exec.ExitError)
|
|
||||||
if !ok {
|
|
||||||
log.Printf("%s\n", out)
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if execErr.ExitCode() == 1 {
|
filtered := make([]zfs.Snapshot, 0)
|
||||||
return nil, nil
|
for _, snap := range allSnaps {
|
||||||
}
|
if !strings.Contains(snap.Name, j.Prefix) {
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
log.Printf("%+v\n", params)
|
filtered = append(filtered, snap)
|
||||||
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"]
|
return filtered, nil
|
||||||
|
|
||||||
snaps[i] = zfs.Snapshot{
|
|
||||||
SnapshotName: identifier[1],
|
|
||||||
Dataset: identifier[0],
|
|
||||||
Name: params[0],
|
|
||||||
Creation: time.Unix(t, 0),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return snaps, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *BackupJob) snapName() string {
|
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) {
|
func (j *BackupJob) FullSend(ctx context.Context, snap string) {
|
||||||
out, err := zfs.Cmd(
|
out, err := zfs.FullSend(
|
||||||
ctx,
|
ctx,
|
||||||
"zfs send -Lec %s | ssh %s zfs recv -Fu %s",
|
|
||||||
snap,
|
snap,
|
||||||
j.TargetHost,
|
j.TargetHost,
|
||||||
j.Target,
|
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) {
|
func (j *BackupJob) IncrementalSend(ctx context.Context, prevSnap *zfs.Snapshot, newSnap string) ([]byte, error) {
|
||||||
out, err := zfs.Cmd(
|
return zfs.IncrementalSend(
|
||||||
ctx,
|
ctx,
|
||||||
"zfs send -I@%s %s | ssh %s zfs recv %s",
|
|
||||||
prevSnap.SnapshotName,
|
prevSnap.SnapshotName,
|
||||||
newSnap,
|
newSnap,
|
||||||
j.TargetHost,
|
j.TargetHost,
|
||||||
j.Target,
|
j.Target,
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j *BackupJob) Retain(ctx context.Context) error {
|
||||||
|
snaps, err := j.listSnapshots(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// FIXME: return an error so we can cleanup
|
return fmt.Errorf("retain: failure listing snapshots: %s", err.Error())
|
||||||
log.Fatalf("zfs-send: error: %s", out)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
func (j *BackupJob) Do(ctx context.Context) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package runner
|
package runner
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
@@ -26,3 +27,35 @@ func (runner *ZRunner) Run(ctx context.Context, command string, formatArgs ...an
|
|||||||
cmd := exec.Command(runner.Prog, args...)
|
cmd := exec.Command(runner.Prog, args...)
|
||||||
return cmd.CombinedOutput()
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,9 +2,11 @@ package runner_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"os/exec"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.gentoo.party/sam/thanks/internal/runner"
|
"git.gentoo.party/sam/thanks/internal/runner"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_ZCommand(t *testing.T) {
|
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)
|
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)
|
||||||
|
}
|
||||||
|
|||||||
6
internal/zfs/zfs-list_snapshot.txt
Normal file
6
internal/zfs/zfs-list_snapshot.txt
Normal 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
|
||||||
@@ -1,10 +1,17 @@
|
|||||||
package zfs
|
package zfs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.gentoo.party/sam/thanks/internal/runner"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Snapshot struct {
|
type Snapshot struct {
|
||||||
@@ -12,7 +19,7 @@ type Snapshot struct {
|
|||||||
Name string // Full dataset@snapname
|
Name string // Full dataset@snapname
|
||||||
Dataset string
|
Dataset string
|
||||||
Creation time.Time
|
Creation time.Time
|
||||||
GUID int64
|
GUID uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
func Cmd(ctx context.Context, arg string, a ...any) ([]byte, error) {
|
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
|
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()
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,8 +2,12 @@ package zfs_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.gentoo.party/sam/thanks/internal/zfs"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
type zfscmd struct {
|
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)
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user