Browse Source

Created the parser for Snapshots

master
Robert R. Russell 1 year ago
parent
commit
02a1807c93
  1. 81
      zfsbackup/snapshot.go

81
zfsbackup/snapshot.go

@ -1,16 +1,23 @@
package zfsbackup
import (
"regexp"
"strconv"
"strings"
)
/*
Snapshot represents a snapshot on a zfs pool and filesystem.
*/
type Snapshot struct {
pool string
fsTree string
Year uint
Month uint
Day uint
Hour uint
Minute uint
pool string
fsTree []string
Interval uint64
Year uint64
Month uint64
Day uint64
Hour uint64
Minute uint64
}
/*
@ -19,5 +26,63 @@ ParseSnapshot parses a string into a Snapshot.
It returns nil on error.
*/
func ParseSnapshot(input string) *Snapshot {
return nil
var snapshotOnly, err = regexp.Compile("^" + PoolNameRegex + "@" + ZfsSnapshotNameRegex + "$")
if err != nil {
return nil
}
if !snapshotOnly.MatchString(input) {
return nil
}
var snapshotPieces []string = snapshotOnly.FindStringSubmatch(input)
var theSnapshot = Snapshot{}
theSnapshot.Interval = intervalStringToInt(snapshotPieces[1])
theSnapshot.Year, err = strconv.ParseUint(snapshotPieces[2], 10, 64)
if err != nil {
return nil
}
theSnapshot.Month, err = strconv.ParseUint(snapshotPieces[3], 10, 64)
if err != nil {
return nil
}
theSnapshot.Day, err = strconv.ParseUint(snapshotPieces[4], 10, 64)
if err != nil {
return nil
}
theSnapshot.Hour, err = strconv.ParseUint(snapshotPieces[5], 10, 64)
if err != nil {
return nil
}
theSnapshot.Minute, err = strconv.ParseUint(snapshotPieces[6], 10, 64)
if err != nil {
return nil
}
var splitInput []string = strings.Split(input, "@")
if len(splitInput) != 2 {
return nil
}
var paths []string = strings.Split(splitInput[0], "/")
theSnapshot.pool = paths[0]
if len(paths) > 1 {
copy(theSnapshot.fsTree, paths[1:])
}
return &theSnapshot
}
func intervalStringToInt(input string) uint64 {
if input == "yearly" {
return 0
}
if input == "monthly" {
return 1
}
if input == "weekly" {
return 2
}
if input == "daily" {
return 3
}
if input == "hourly" {
return 4
}
return 5
}
Loading…
Cancel
Save