|
|
|
@ -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()) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|