Browse Source

Finished adding exec functionality

master
Robert R. Russell 1 year ago
parent
commit
f218d035f1
  1. 79
      main.go
  2. 2
      regex.go
  3. 49
      snapshot.go

79
main.go

@ -2,70 +2,37 @@ package main
import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"regexp"
"zfsbackup/zfsbackup"
"strings"
)
var zfsRegex = regexp.MustCompile(zfsbackup.ZfsSnapshotNameRegex)
var _ = exec.Cmd{}
var _ = bufio.Reader{}
func testSnapshot(possible string, increment string) (bool, bool) {
var matches = zfsRegex.FindStringSubmatch(possible)
if matches == nil {
return false, false
}
var isASnapshot = true
if matches[1] == increment {
return isASnapshot, true
}
return isASnapshot, false
}
func isAYearlySnapshot(possible string) bool {
_, isYearly := testSnapshot(possible, "yearly")
return isYearly
}
func isAMonthlySnapshot(possible string) bool {
_, isMonthly := testSnapshot(possible, "monthly")
return isMonthly
}
func isAWeeklySnapshot(possible string) bool {
_, isWeekly := testSnapshot(possible, "weekly")
return isWeekly
}
func isADailySnapshot(possible string) bool {
_, isDaily := testSnapshot(possible, "daily")
return isDaily
}
func isAnHourlySnapshot(possible string) bool {
_, isHourly := testSnapshot(possible, "hourly")
return isHourly
}
func isAFrequentSnapshot(possible string) bool {
_, isFrequent := testSnapshot(possible, "frequent")
return isFrequent
}
var snapshotLineRegex = regexp.MustCompile("^" + zfsbackup.PoolNameRegex + "@" + zfsbackup.ZfsSnapshotNameRegex + ".*$")
var zfsRegex = regexp.MustCompile(ZfsSnapshotNameRegex)
var snapshotLineRegex = regexp.MustCompile("^" + PoolNameRegex + "@" + ZfsSnapshotNameRegex + ".*$")
func main() {
//fmt.Println(snapshotLineRegex.MatchString("dpool/www@zfs-auto-snap_frequent-2020-08-04-1830\t0B\t-\t201M\t-"))
input := bufio.NewScanner(os.Stdin)
for input.Scan() {
if snapshotLineRegex.MatchString(input.Text()) {
fmt.Println(snapshotLineRegex.FindStringSubmatch(input.Text()))
fmt.Println(snapshotLineRegex.SubexpNames())
} else {
fmt.Printf("%s\t%s\n", input.Text(), "Is not a snapshot.")
var listCommand = exec.Command("zfs", "list", "-Hrt", "snapshot", "dpool")
var snapList, err = listCommand.CombinedOutput()
var snapScanner = bufio.NewScanner(bytes.NewReader(snapList))
if err != nil {
fmt.Println(listCommand)
fmt.Println("Error trying to list snapshots:", err.Error())
for snapScanner.Scan() {
fmt.Println(snapScanner.Text())
}
}
if err := input.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading Standard Input:", err)
for snapScanner.Scan() {
if snapshotLineRegex.MatchString(snapScanner.Text()) {
var temp = strings.SplitN(snapScanner.Text(), "\t", 2)
var snapshot = ParseSnapshot(temp[0])
if snapshot != nil {
fmt.Println("I found snapshot", snapshot.Name(), "at", snapshot.Path())
}
}
}
}

2
zfsbackup/regex.go → regex.go

@ -1,4 +1,4 @@
package zfsbackup
package main
/*
PoolNameRegex will match a zfs pool and filesystem tree

49
zfsbackup/snapshot.go → snapshot.go

@ -1,4 +1,4 @@
package zfsbackup
package main
import (
"errors"
@ -66,6 +66,7 @@ func ParseSnapshot(input string) *Snapshot {
var paths []string = strings.Split(splitInput[0], "/")
theSnapshot.pool = paths[0]
if len(paths) > 1 {
theSnapshot.fsTree = make([]string, len(paths)-1)
copy(theSnapshot.fsTree, paths[1:])
}
return &theSnapshot
@ -108,7 +109,7 @@ func (s Snapshot) Name() string {
var temp strings.Builder
temp.WriteString("zfs-auto-snap_")
temp.WriteString(intervalUIntToString(s.Interval) + "-")
fmt.Fprintf(&temp, "%d-%d-%d-%d%d", s.TimeStamp.Year(), s.TimeStamp.Month(), s.TimeStamp.Day(), s.TimeStamp.Hour(), s.TimeStamp.Minute())
fmt.Fprintf(&temp, "%04d-%02d-%02d-%02d%02d", s.TimeStamp.Year(), s.TimeStamp.Month(), s.TimeStamp.Day(), s.TimeStamp.Hour(), s.TimeStamp.Minute())
return temp.String()
}
@ -188,3 +189,47 @@ func CompareSnapshotDates(x Snapshot, y Snapshot) (int, error) {
}
return 0, nil
}
/*
func testSnapshot(possible string, increment string) (bool, bool) {
var matches = zfsRegex.FindStringSubmatch(possible)
if matches == nil {
return false, false
}
var isASnapshot = true
if matches[1] == increment {
return isASnapshot, true
}
return isASnapshot, false
}
func isAYearlySnapshot(possible string) bool {
_, isYearly := testSnapshot(possible, "yearly")
return isYearly
}
func isAMonthlySnapshot(possible string) bool {
_, isMonthly := testSnapshot(possible, "monthly")
return isMonthly
}
func isAWeeklySnapshot(possible string) bool {
_, isWeekly := testSnapshot(possible, "weekly")
return isWeekly
}
func isADailySnapshot(possible string) bool {
_, isDaily := testSnapshot(possible, "daily")
return isDaily
}
func isAnHourlySnapshot(possible string) bool {
_, isHourly := testSnapshot(possible, "hourly")
return isHourly
}
func isAFrequentSnapshot(possible string) bool {
_, isFrequent := testSnapshot(possible, "frequent")
return isFrequent
}
*/
Loading…
Cancel
Save