Browse Source

let's if we can recognize snapshots

master
Robert R. Russell 1 year ago
parent
commit
6d38b8b463
  1. 3
      go.mod
  2. 84
      main.go

3
go.mod

@ -0,0 +1,3 @@
module zfsbackup
go 1.13

84
main.go

@ -0,0 +1,84 @@
package main
import (
"bufio"
"fmt"
"os"
"regexp"
)
const zfsRegexStart string = "zfs-auto-snap"
const zfsRegexIncrement string = "(?P<increment>yearly|monthly|weekly|daily|hourly|frequent)"
const zfsRegexDateStamp string = "(?P<month>[[:digit:]]{2})-(?P<day>[[:digit:]]{2})-(?P<hour>[[:digit:]]{2})(?P<minute>[[:digit:]]{2})"
var zfsRegex = regexp.MustCompile(zfsRegexStart + "_" + zfsRegexIncrement + "-" + zfsRegexDateStamp)
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
} else {
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
}
func main() {
input := bufio.NewScanner(os.Stdin)
for input.Scan() {
if isAYearlySnapshot(input.Text()) {
fmt.Printf("%s\t%s\n", input.Text(), "Is a yearly snapshot.")
}
if isAMonthlySnapshot(input.Text()) {
fmt.Printf("%s\t%s\n", input.Text(), "Is a monthly snapshot.")
}
if isAWeeklySnapshot(input.Text()) {
fmt.Printf("%s\t%s\n", input.Text(), "Is a weekly snapshot.")
}
if isADailySnapshot(input.Text()) {
fmt.Printf("%s\t%s\n", input.Text(), "Is a daily snapshot.")
}
if isAnHourlySnapshot(input.Text()) {
fmt.Printf("%s\t%s\n", input.Text(), "Is an hourly snapshot.")
}
if isAFrequentSnapshot(input.Text()) {
fmt.Printf("%s\t%s\n", input.Text(), "Is a frequent snapshot.")
}
}
if err := input.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading Standard Input:", err)
}
}
Loading…
Cancel
Save