271 lines
6.3 KiB
Go
271 lines
6.3 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os/exec"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var _ = exec.Command("echo", "hi")
|
|
|
|
/*
|
|
Snapshot represents a snapshot on a zfs pool and filesystem.
|
|
*/
|
|
type Snapshot struct {
|
|
pool string
|
|
fsTree []string
|
|
Interval frequency
|
|
TimeStamp time.Time
|
|
}
|
|
|
|
/*
|
|
ParseSnapshot parses a string into a Snapshot.
|
|
|
|
It returns nil on error.
|
|
*/
|
|
func ParseSnapshot(input string) (result Snapshot, err error) {
|
|
var snapshotOnly = regexp.MustCompile("^" + PoolNameRegex + "@" + ZfsSnapshotNameRegex + "$")
|
|
if !snapshotOnly.MatchString(input) {
|
|
err = errors.New("Input is not a snapshot")
|
|
return
|
|
}
|
|
var snapshotPieces []string = snapshotOnly.FindStringSubmatch(input)
|
|
var theSnapshot = Snapshot{}
|
|
theSnapshot.Interval = frequency(intervalStringToUInt(snapshotPieces[1]))
|
|
var year, month, day, hour, minute int
|
|
year, err = strconv.Atoi(snapshotPieces[2])
|
|
if err != nil {
|
|
return
|
|
}
|
|
month, err = strconv.Atoi(snapshotPieces[3])
|
|
if err != nil {
|
|
return
|
|
}
|
|
day, err = strconv.Atoi(snapshotPieces[4])
|
|
if err != nil {
|
|
return
|
|
}
|
|
hour, err = strconv.Atoi(snapshotPieces[5])
|
|
if err != nil {
|
|
return
|
|
}
|
|
minute, err = strconv.Atoi(snapshotPieces[6])
|
|
if err != nil {
|
|
return
|
|
}
|
|
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
|
|
}
|
|
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, nil
|
|
}
|
|
|
|
func intervalStringToUInt(input string) uint64 {
|
|
switch input {
|
|
case "yearly":
|
|
return 0
|
|
case "monthly":
|
|
return 1
|
|
case "weekly":
|
|
return 2
|
|
case "daily":
|
|
return 3
|
|
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(intervalToString(s.Interval) + "-")
|
|
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()
|
|
}
|
|
|
|
func intervalToString(x frequency) string {
|
|
switch x {
|
|
case yearly:
|
|
return "yearly"
|
|
case monthly:
|
|
return "monthly"
|
|
case weekly:
|
|
return "weekly"
|
|
case daily:
|
|
return "daily"
|
|
case hourly:
|
|
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
|
|
}
|
|
|
|
/*
|
|
IncrementalSender sets up a zfs send command which will send an incremental snapshot starting
|
|
at from and including through. Sender will be overwritten.
|
|
*/
|
|
func IncrementalSender(from, through Snapshot, sender *exec.Cmd) (snapData io.ReadCloser, err error) {
|
|
sender = exec.Command("zfs", "send", "-c", "-i", from.Name(), through.Name())
|
|
snapData, err = sender.StdoutPipe()
|
|
return
|
|
}
|
|
|
|
/*
|
|
Sender sets up a zfs send command which will send a full snapshot. Sender will be overwritten.
|
|
*/
|
|
func Sender(through Snapshot, sender *exec.Cmd) (snapdata io.ReadCloser, err error) {
|
|
sender = exec.Command("zfs", "send", "-c", through.Name())
|
|
snapdata, err = sender.StdoutPipe()
|
|
return
|
|
}
|
|
|
|
/*
|
|
Receiver sets up a zfs receive command which will receive a snapshot
|
|
*/
|
|
func Receiver(backupPath string, snap Snapshot, receiver *exec.Cmd) (snapdata io.WriteCloser, err error) {
|
|
receiver = exec.Command("zfs", "receive", "-x", "mountpoint", backupPath+snap.Path())
|
|
snapdata, err = receiver.StdinPipe()
|
|
return
|
|
}
|
|
|
|
/*
|
|
Frequency returns the frequency of a Snapshot
|
|
*/
|
|
func (s Snapshot) frequency() frequency {
|
|
return s.Interval
|
|
}
|
|
|
|
/*
|
|
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
|
|
}
|
|
*/
|