Compare commits
2 Commits
7774a5b956
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| f615b03794 | |||
|
|
3c11a79252 |
2
Makefile
2
Makefile
@@ -8,7 +8,7 @@ LDFLAGS=-ldflags "-X main.Version=${VERSION} -X main.BuildTime=${BUILD_TIME}"
|
||||
all: build-linux
|
||||
|
||||
test:
|
||||
go test ./...
|
||||
go test -v ./...
|
||||
|
||||
build-linux:
|
||||
GOOS=linux GOARCH=amd64 go build ${LDFLAGS} -o bin/thanks ./cmd/thanks
|
||||
|
||||
11
internal/executor/executor.go
Normal file
11
internal/executor/executor.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package executor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
type Executor interface {
|
||||
CombinedOutput(context.Context, string, ...string) ([]byte, error)
|
||||
CommandContext(context.Context, string, ...string) *exec.Cmd
|
||||
}
|
||||
16
internal/executor/local.go
Normal file
16
internal/executor/local.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package executor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
type LocalExecutor struct{}
|
||||
|
||||
func (l LocalExecutor) CombinedOutput(ctx context.Context, name string, arg ...string) ([]byte, error) {
|
||||
return l.CommandContext(ctx, name, arg...).CombinedOutput()
|
||||
}
|
||||
|
||||
func (l LocalExecutor) CommandContext(ctx context.Context, name string, arg ...string) *exec.Cmd {
|
||||
return exec.CommandContext(ctx, name, arg...)
|
||||
}
|
||||
21
internal/executor/ssh.go
Normal file
21
internal/executor/ssh.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package executor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
type SSHExecutor struct {
|
||||
SSHTarget string
|
||||
}
|
||||
|
||||
func (s SSHExecutor) CombinedOutput(ctx context.Context, name string, arg ...string) ([]byte, error) {
|
||||
return s.CommandContext(ctx, name, arg...).CombinedOutput()
|
||||
}
|
||||
|
||||
func (s SSHExecutor) CommandContext(ctx context.Context, name string, arg ...string) *exec.Cmd {
|
||||
newArg := append([]string{s.SSHTarget, name}, arg...)
|
||||
cmd := exec.CommandContext(ctx, "ssh", newArg...)
|
||||
|
||||
return cmd
|
||||
}
|
||||
@@ -7,12 +7,26 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.gentoo.party/sam/thanks/internal/executor"
|
||||
"git.gentoo.party/sam/thanks/internal/zfs"
|
||||
)
|
||||
|
||||
type BackupHookCommand struct {
|
||||
Command string `yaml:"command"`
|
||||
Args []string `yaml:"args"`
|
||||
Env []string `yaml:"env"`
|
||||
}
|
||||
|
||||
type BackupHooks struct {
|
||||
Completed BackupHookCommand `yaml:"completed"` // called upon successful completion of a backup job
|
||||
Error BackupHookCommand `yaml:"error"`
|
||||
}
|
||||
|
||||
type BackupJob struct {
|
||||
Source string `yaml:"source"` // the source dataset (e.g., zroot)
|
||||
TargetHost string `yaml:"targetHost"` // SSH-compatible host
|
||||
@@ -21,15 +35,14 @@ type BackupJob struct {
|
||||
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
|
||||
Hooks BackupHooks `yaml:"hooks"` // external programs (libnotify, sendmail, etc) to call
|
||||
|
||||
localExecutor executor.Executor
|
||||
remoteExecutor executor.Executor
|
||||
}
|
||||
|
||||
// func (j *BackupJob) getBaseSnap() {
|
||||
// params := "zfs get -j -d 1 -t snapshot guid,creation %s"
|
||||
// srcCmd := fmt.Sprintf(params, j.Source)
|
||||
// dstCmd := fmt.Sprintf(params, j.Target)
|
||||
// }
|
||||
func (j *BackupJob) listSnapshots(ctx context.Context) ([]zfs.Snapshot, error) {
|
||||
allSnaps, err := zfs.Snapshots(ctx, j.Source)
|
||||
allSnaps, err := zfs.Snapshots(ctx, j.localExecutor, j.Source)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -46,7 +59,6 @@ func (j *BackupJob) listSnapshots(ctx context.Context) ([]zfs.Snapshot, error) {
|
||||
}
|
||||
|
||||
func (j *BackupJob) snapName() string {
|
||||
// ts := time.Now().Format(time.RFC3339)
|
||||
ts := time.Now().UnixMicro()
|
||||
if j.Prefix == "" {
|
||||
return fmt.Sprintf("%d", ts)
|
||||
@@ -58,20 +70,16 @@ func (j *BackupJob) snapName() string {
|
||||
func (j *BackupJob) Snapshot(ctx context.Context) (string, error) {
|
||||
snapName := j.snapName()
|
||||
snap := fmt.Sprintf("%s@%s", j.Source, snapName)
|
||||
|
||||
out, err := zfs.Cmd(
|
||||
err := zfs.CreateSnapshot(
|
||||
ctx,
|
||||
"zfs snapshot %s",
|
||||
snap,
|
||||
j.localExecutor,
|
||||
j.Source,
|
||||
snapName,
|
||||
)
|
||||
if err != nil {
|
||||
log.Printf("zfs-snapshot: error: %s", out)
|
||||
return snapName, nil
|
||||
}
|
||||
return snap, err
|
||||
}
|
||||
|
||||
func (j *BackupJob) FullSend(ctx context.Context, snap string) {
|
||||
func (j *BackupJob) FullSend(ctx context.Context, snap string) error {
|
||||
out, err := zfs.FullSend(
|
||||
ctx,
|
||||
snap,
|
||||
@@ -79,8 +87,9 @@ func (j *BackupJob) FullSend(ctx context.Context, snap string) {
|
||||
j.Target,
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatalf("zfs-send: error: %s", out)
|
||||
return fmt.Errorf("zfs-send: error: %s - %s", err.Error(), out)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (j *BackupJob) IncrementalSend(ctx context.Context, prevSnap *zfs.Snapshot, newSnap string) ([]byte, error) {
|
||||
@@ -122,10 +131,40 @@ func (j *BackupJob) Retain(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (j *BackupJob) Do(ctx context.Context) {
|
||||
func (j *BackupJob) findCommonAnscestor(ctx context.Context) {
|
||||
localSnapshots, err := j.listSnapshots(ctx)
|
||||
}
|
||||
|
||||
type HookErr struct {
|
||||
message string
|
||||
}
|
||||
|
||||
func (e *HookErr) Error() string {
|
||||
return fmt.Sprintf("error while running hook: %s", e.message)
|
||||
}
|
||||
|
||||
func (j *BackupJob) Do(ctx context.Context) error {
|
||||
err := j.do(ctx)
|
||||
if err != nil {
|
||||
log.Print("job encountered error, running error hook\n")
|
||||
err = j.runHookError(ctx, err)
|
||||
if err != nil {
|
||||
log.Printf("error running hook! %s", err.Error())
|
||||
}
|
||||
return &HookErr{message: err.Error()}
|
||||
|
||||
} else {
|
||||
err = j.runHookCompleted(ctx)
|
||||
return &HookErr{message: err.Error()}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (j *BackupJob) do(ctx context.Context) error {
|
||||
currentSnaps, err := j.listSnapshots(ctx)
|
||||
if err != nil {
|
||||
log.Fatal("failed to list existing snapshots")
|
||||
return fmt.Errorf("failed to list existing snapshots: %s", err.Error())
|
||||
}
|
||||
var prev *zfs.Snapshot
|
||||
|
||||
@@ -135,14 +174,70 @@ func (j *BackupJob) Do(ctx context.Context) {
|
||||
|
||||
newSnapshot, err := j.Snapshot(ctx)
|
||||
if err != nil {
|
||||
log.Fatalf("error creating new snapshot: %s", err.Error())
|
||||
return fmt.Errorf("error creating new snapshot: %s", err.Error())
|
||||
}
|
||||
|
||||
var sendErr error
|
||||
if prev == nil {
|
||||
// Full Send
|
||||
j.FullSend(ctx, newSnapshot)
|
||||
sendErr = j.FullSend(ctx, newSnapshot)
|
||||
} else {
|
||||
// Inc send
|
||||
j.IncrementalSend(ctx, prev, newSnapshot)
|
||||
_, sendErr = j.IncrementalSend(ctx, prev, newSnapshot)
|
||||
}
|
||||
|
||||
if sendErr != nil {
|
||||
return fmt.Errorf("error sending backup: %s", sendErr.Error())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (j *BackupJob) runHookCompleted(ctx context.Context) error {
|
||||
cmdDef := j.Hooks.Completed
|
||||
if cmdDef.Command == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
timeoutCtx, cancel := context.WithTimeout(
|
||||
ctx,
|
||||
time.Second*3,
|
||||
)
|
||||
cmd := exec.CommandContext(timeoutCtx, cmdDef.Command, cmdDef.Args...)
|
||||
defer cancel()
|
||||
cmd.Env = []string{
|
||||
fmt.Sprintf("THANKS_JOB_SOURCE=%s", j.Source),
|
||||
fmt.Sprintf("THANKS_JOB_TARGET=%s", j.Target),
|
||||
}
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
func (j *BackupJob) runHookError(ctx context.Context, err error) error {
|
||||
cmdDef := j.Hooks.Error
|
||||
if cmdDef.Command == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
timeoutCtx, cancel := context.WithTimeout(
|
||||
ctx,
|
||||
time.Second*3,
|
||||
)
|
||||
cmd := exec.CommandContext(timeoutCtx, cmdDef.Command, cmdDef.Args...)
|
||||
defer cancel()
|
||||
cmd.Env = append(
|
||||
[]string{
|
||||
fmt.Sprintf("THANKS_JOB_SOURCE=%s", j.Source),
|
||||
fmt.Sprintf("THANKS_JOB_TARGET=%s", j.Target),
|
||||
fmt.Sprintf("THANKS_JOB_ERROR=%s", err.Error()),
|
||||
fmt.Sprintf("DISPLAY=%s", os.Getenv("DISPLAY")),
|
||||
},
|
||||
cmdDef.Env...,
|
||||
)
|
||||
|
||||
out, runErr := cmd.CombinedOutput()
|
||||
if runErr != nil {
|
||||
log.Printf("%s", out)
|
||||
}
|
||||
|
||||
return runErr
|
||||
}
|
||||
|
||||
36
internal/jobs/jobs_test.go
Normal file
36
internal/jobs/jobs_test.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package jobs_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.gentoo.party/sam/thanks/internal/jobs"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_JobHookError(t *testing.T) {
|
||||
job := jobs.BackupJob{
|
||||
Target: "znothing/nothing",
|
||||
TargetHost: "devnull",
|
||||
Source: "zempty/empty",
|
||||
Keep: 1,
|
||||
MaxAge: 1 * time.Second,
|
||||
Recursive: false,
|
||||
Hooks: jobs.BackupHooks{
|
||||
Error: jobs.BackupHookCommand{
|
||||
Command: "/usr/bin/notify-send",
|
||||
Args: []string{"--app-name=thanks", "--urgency=CRITICAL", "Backup Failure", "Backup failed"},
|
||||
Env: []string{
|
||||
"XDG_RUNTIME_DIR=/run/user/1000/",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
var hookErr *jobs.HookErr
|
||||
|
||||
err := job.Do(context.Background())
|
||||
assert.NotNil(t, err, "expected job to produce an error")
|
||||
assert.NotErrorAsf(t, err, &hookErr, "expected a non-hook error, got %w", err)
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.gentoo.party/sam/thanks/internal/executor"
|
||||
"git.gentoo.party/sam/thanks/internal/runner"
|
||||
)
|
||||
|
||||
@@ -37,6 +38,20 @@ func Cmd(ctx context.Context, arg string, a ...any) ([]byte, error) {
|
||||
return out, err
|
||||
}
|
||||
|
||||
func CreateSnapshot(ctx context.Context, e executor.Executor, dataset, name string) error {
|
||||
_, err := e.CombinedOutput(
|
||||
ctx,
|
||||
"zfs",
|
||||
"snapshot",
|
||||
fmt.Sprintf("%s@%s", dataset, name),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("zfs-snapshot error: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ParseSnapshots(reader io.Reader) ([]Snapshot, error) {
|
||||
scanner := bufio.NewScanner(reader)
|
||||
|
||||
@@ -78,8 +93,8 @@ func ParseSnapshots(reader io.Reader) ([]Snapshot, error) {
|
||||
return snaps, nil
|
||||
}
|
||||
|
||||
func Snapshots(ctx context.Context, target string) ([]Snapshot, error) {
|
||||
cmd := exec.CommandContext(
|
||||
func Snapshots(ctx context.Context, e executor.Executor, target string) ([]Snapshot, error) {
|
||||
cmd := e.CommandContext(
|
||||
ctx,
|
||||
"zfs",
|
||||
"list",
|
||||
@@ -157,7 +172,6 @@ func IncrementalSend(ctx context.Context, prevSnapName, newSnapName, sshTarget,
|
||||
)
|
||||
}
|
||||
|
||||
func Destroy(ctx context.Context, target string) ([]byte, error) {
|
||||
cmd := exec.CommandContext(ctx, "zfs", "destroy", target)
|
||||
return cmd.CombinedOutput()
|
||||
func Destroy(ctx context.Context, e executor.Executor, target string) ([]byte, error) {
|
||||
return e.CombinedOutput(ctx, "zfs", target)
|
||||
}
|
||||
|
||||
14
thanks.yaml.example
Normal file
14
thanks.yaml.example
Normal file
@@ -0,0 +1,14 @@
|
||||
jobs:
|
||||
- source: "zroot/home/sam/thanks"
|
||||
target: "zrust/backup/weller/thanks"
|
||||
targetHost: "backup@woodford.gentoo.party"
|
||||
keep: 30
|
||||
prefix: "thanks-"
|
||||
recursive: false
|
||||
hooks:
|
||||
completed:
|
||||
cmd: "backup-success.sh"
|
||||
args: []
|
||||
error:
|
||||
cmd: "backup-error.sh"
|
||||
args: []
|
||||
Reference in New Issue
Block a user