Browse Source

End of ZFS Backup Tool Part 3

master
Robert R. Russell 1 year ago
parent
commit
2947f459bf
  1. 150
      zfsbackup/snapshot.go

150
zfsbackup/snapshot.go

@ -1,23 +1,24 @@
package zfsbackup
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"time"
)
var _ = time.Time{}
/*
Snapshot represents a snapshot on a zfs pool and filesystem.
*/
type Snapshot struct {
pool string
fsTree []string
Interval uint64
Year uint64
Month uint64
Day uint64
Hour uint64
Minute uint64
pool string
fsTree []string
Interval uint64
TimeStamp time.Time
}
/*
@ -35,27 +36,29 @@ func ParseSnapshot(input string) *Snapshot {
}
var snapshotPieces []string = snapshotOnly.FindStringSubmatch(input)
var theSnapshot = Snapshot{}
theSnapshot.Interval = intervalStringToInt(snapshotPieces[1])
theSnapshot.Year, err = strconv.ParseUint(snapshotPieces[2], 10, 64)
theSnapshot.Interval = intervalStringToUInt(snapshotPieces[1])
var year, month, day, hour, minute int
year, err = strconv.Atoi(snapshotPieces[2])
if err != nil {
return nil
}
theSnapshot.Month, err = strconv.ParseUint(snapshotPieces[3], 10, 64)
month, err = strconv.Atoi(snapshotPieces[3])
if err != nil {
return nil
}
theSnapshot.Day, err = strconv.ParseUint(snapshotPieces[4], 10, 64)
day, err = strconv.Atoi(snapshotPieces[4])
if err != nil {
return nil
}
theSnapshot.Hour, err = strconv.ParseUint(snapshotPieces[5], 10, 64)
hour, err = strconv.Atoi(snapshotPieces[5])
if err != nil {
return nil
}
theSnapshot.Minute, err = strconv.ParseUint(snapshotPieces[6], 10, 64)
minute, err = strconv.Atoi(snapshotPieces[6])
if err != nil {
return nil
}
theSnapshot.TimeStamp = time.Date(year, time.Month(month), day, hour, minute, 0, 0, time.UTC)
var splitInput []string = strings.Split(input, "@")
if len(splitInput) != 2 {
return nil
@ -68,21 +71,120 @@ func ParseSnapshot(input string) *Snapshot {
return &theSnapshot
}
func intervalStringToInt(input string) uint64 {
if input == "yearly" {
func intervalStringToUInt(input string) uint64 {
switch input {
case "yearly":
return 0
}
if input == "monthly" {
case "monthly":
return 1
}
if input == "weekly" {
case "weekly":
return 2
}
if input == "daily" {
case "daily":
return 3
}
if input == "hourly" {
case "hourly":
return 4
}
return 5
}
/*
Path returns a string containing the path of the snapshot
*/
func (s Snapshot) Path() string {
var temp strings.Builder
temp.WriteString(s.pool)
if len(s.fsTree) > 0 {
for _, v := range s.fsTree {
temp.WriteString("/" + v)
}
}
return temp.String()
}
/*
Name returns a string containing the full name of snapshot
*/
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())
return temp.String()
}
func intervalUIntToString(x uint64) string {
switch x {
case 0:
return "yearly"
case 1:
return "monthly"
case 2:
return "weekly"
case 3:
return "daily"
case 4:
return "hourly"
}
return "frequent"
}
/*
String returns a string equal to s.Path() + "@" + s.Name() for Snapshot s
*/
func (s Snapshot) String() string {
return s.Path() + "@" + s.Name()
}
/*
CompareSnapshotDates returns -2 if x occured before y and would include y in its interval
returns -1 if x occured before y
returns 0 if x and y are the same snapshot
returns +1 if y occured after x
err is non nill if the snapshots do not have the same path
*/
func CompareSnapshotDates(x Snapshot, y Snapshot) (int, error) {
if x.Path() != y.Path() {
return 0, errors.New("Can only compare snapshots with the same path")
}
if x.Interval == y.Interval {
if x.TimeStamp.Equal(y.TimeStamp) {
return 0, nil
}
if x.TimeStamp.Before(y.TimeStamp) {
return -1, nil
}
return 1, nil
}
if x.Interval < y.Interval { // y is from a more frequent backup interval than x
var interval time.Time
switch x.Interval {
case 0:
interval = x.TimeStamp.AddDate(-1, 0, 0)
case 1:
interval = x.TimeStamp.AddDate(0, -1, 0)
case 2:
interval = x.TimeStamp.AddDate(0, 0, -7)
case 3:
interval = x.TimeStamp.AddDate(0, 0, -1)
case 4:
interval = x.TimeStamp.Add(time.Hour * -1)
case 5:
interval = x.TimeStamp.Add(time.Minute * -15)
}
if x.TimeStamp.Before(y.TimeStamp) {
return 1, nil
}
if interval.Before(y.TimeStamp) {
return -2, nil
}
return -1, nil
}
// y is from a less frequent backup interval than x
if x.TimeStamp.Before(y.TimeStamp) {
return -1, nil
}
if x.TimeStamp.After(y.TimeStamp) {
return 1, nil
}
return 0, nil
}
Loading…
Cancel
Save