// Package jobs provides definitions and features for Backup Jobs themselves. // [BackupJob] is the primary resource here. // [BackupJob.Do] is the main entrypoint for running through the backup process. package jobs import ( "context" "fmt" "log" "os/exec" "strconv" "strings" "time" "git.gentoo.party/sam/thanks/internal/zfs" ) type BackupJob struct { Source string // the source dataset (e.g., zroot) TargetHost string // SSH-compatible host Target string // the target dataset Keep int // number of snapshots to keep Prefix string // name each snapshot with this prefix Recursive bool // create recursive snapshots } // 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) { out, err := zfs.Cmd(ctx, "zfs list -Hp -o name,creation -t snapshot -s creation %s | grep '%s'", j.Source, j.Prefix) 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 { 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), } } return snaps, nil } func (j *BackupJob) snapName() string { // ts := time.Now().Format(time.RFC3339) ts := time.Now().UnixMicro() if j.Prefix == "" { return fmt.Sprintf("%d", ts) } else { return fmt.Sprintf("%s%d", j.Prefix, ts) } } func (j *BackupJob) Snapshot(ctx context.Context) (string, error) { snapName := j.snapName() snap := fmt.Sprintf("%s@%s", j.Source, snapName) out, err := zfs.Cmd( ctx, "zfs snapshot %s", snap, ) 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) { out, err := zfs.Cmd( ctx, "zfs send -Lec %s | ssh %s zfs recv -Fu %s", snap, j.TargetHost, j.Target, ) if err != nil { log.Fatalf("zfs-send: error: %s", out) } } func (j *BackupJob) IncrementalSend(ctx context.Context, prevSnap *zfs.Snapshot, newSnap string) { out, err := zfs.Cmd( 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) Do(ctx context.Context) { currentSnaps, err := j.listSnapshots(ctx) if err != nil { log.Fatal("failed to list existing snapshots") } var prev *zfs.Snapshot if len(currentSnaps) > 0 { prev = ¤tSnaps[len(currentSnaps)-1] } newSnapshot, err := j.Snapshot(ctx) if err != nil { log.Fatalf("error creating new snapshot: %s", err.Error()) } if prev == nil { // Full Send j.FullSend(ctx, newSnapshot) } else { // Inc send j.IncrementalSend(ctx, prev, newSnapshot) } }