replace use of shell w/ direct calls
This commit is contained in:
@@ -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,
|
||||
)
|
||||
if err != nil {
|
||||
// FIXME: return an error so we can cleanup
|
||||
log.Fatalf("zfs-send: error: %s", out)
|
||||
}
|
||||
|
||||
func (j *BackupJob) Retain(ctx context.Context) error {
|
||||
snaps, err := j.listSnapshots(ctx)
|
||||
if err != nil {
|
||||
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) {
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user