Browse Source

Added the requirements for the "sort" package to work on a snapshotList

master
Robert R. Russell 2 years ago
parent
commit
3705d22d0e
Signed by: robert GPG Key ID: 5F49E26CC6B3E4B6
  1. 53
      main.go
  2. 24
      snapshot.go
  3. 31
      snapshotlist.go

53
main.go

@ -6,6 +6,7 @@ import (
"fmt"
"os/exec"
"regexp"
"sort"
"strings"
)
@ -29,31 +30,35 @@ func main() {
for snapScanner.Scan() {
if snapshotLineRegex.MatchString(snapScanner.Text()) {
var temp = strings.SplitN(snapScanner.Text(), "\t", 2)
var snapshot = ParseSnapshot(temp[0])
if snapshot != nil {
/* var sender = exec.Command("zfs", "send", "-c",
snapshot.String())
var inData, err = sender.StdoutPipe()
if err != nil {
break
}
var receiver = exec.Command("zfs", "receive", "-x",
"mountpoint", "backups/test/"+snapshot.Path())
var outData io.WriteCloser
outData, err = receiver.StdinPipe()
if err != nil {
break
}
fmt.Println("Writing ", snapshot.String(), " to backups/test")
buf := make([]byte, 4*1024*1024)
sender.Start()
receiver.Start()
io.CopyBuffer(outData, inData, buf)
sender.Wait()
receiver.Wait()
break */
var snapshot, err = ParseSnapshot(temp[0])
if err != nil {
// Ignore the bad input
}
snapshots[snapshot.Path()].addSnapshot(snapshot)
/* var sender = exec.Command("zfs", "send", "-c",
snapshot.String())
var inData, err = sender.StdoutPipe()
if err != nil {
break
}
var receiver = exec.Command("zfs", "receive", "-x",
"mountpoint", "backups/test/"+snapshot.Path())
var outData io.WriteCloser
outData, err = receiver.StdinPipe()
if err != nil {
break
}
fmt.Println("Writing ", snapshot.String(), " to backups/test")
buf := make([]byte, 4*1024*1024)
sender.Start()
receiver.Start()
io.CopyBuffer(outData, inData, buf)
sender.Wait()
receiver.Wait()
break */
}
}
for i := range snapshots {
sort.Sort(snapshots[i])
}
}

24
snapshot.go

@ -28,13 +28,11 @@ ParseSnapshot parses a string into a Snapshot.
It returns nil on error.
*/
func ParseSnapshot(input string) *Snapshot {
var snapshotOnly, err = regexp.Compile("^" + PoolNameRegex + "@" + ZfsSnapshotNameRegex + "$")
if err != nil {
return nil
}
func ParseSnapshot(input string) (result Snapshot, err error) {
var snapshotOnly = regexp.MustCompile("^" + PoolNameRegex + "@" + ZfsSnapshotNameRegex + "$")
if !snapshotOnly.MatchString(input) {
return nil
err = errors.New("Input is not a snapshot")
return
}
var snapshotPieces []string = snapshotOnly.FindStringSubmatch(input)
var theSnapshot = Snapshot{}
@ -42,28 +40,28 @@ func ParseSnapshot(input string) *Snapshot {
var year, month, day, hour, minute int
year, err = strconv.Atoi(snapshotPieces[2])
if err != nil {
return nil
return
}
month, err = strconv.Atoi(snapshotPieces[3])
if err != nil {
return nil
return
}
day, err = strconv.Atoi(snapshotPieces[4])
if err != nil {
return nil
return
}
hour, err = strconv.Atoi(snapshotPieces[5])
if err != nil {
return nil
return
}
minute, err = strconv.Atoi(snapshotPieces[6])
if err != nil {
return 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 nil
return
}
var paths []string = strings.Split(splitInput[0], "/")
theSnapshot.pool = paths[0]
@ -71,7 +69,7 @@ func ParseSnapshot(input string) *Snapshot {
theSnapshot.fsTree = make([]string, len(paths)-1)
copy(theSnapshot.fsTree, paths[1:])
}
return &theSnapshot
return theSnapshot, nil
}
func intervalStringToUInt(input string) uint64 {

31
snapshotlist.go

@ -3,3 +3,34 @@ package main
type snapshotList struct {
Snapshots []Snapshot
}
func (snapList snapshotList) addSnapshot(snap Snapshot) {
if len(snapList.Snapshots) == 0 {
snapList.Snapshots = append(snapList.Snapshots, snap)
}
snapList.Snapshots = append(snapList.Snapshots, snap)
}
/*
Len is required for the sort interface.
*/
func (snapList snapshotList) Len() int {
return len(snapList.Snapshots)
}
/*
Less is required for the sort interface.
*/
func (snapList snapshotList) Less(i, j int) bool {
return snapList.Snapshots[i].TimeStamp.Before(
snapList.Snapshots[j].TimeStamp)
}
/*
Swap is required for the sort interface.
*/
func (snapList snapshotList) Swap(i, j int) {
t := snapList.Snapshots[i]
snapList.Snapshots[i] = snapList.Snapshots[j]
snapList.Snapshots[j] = t
}

Loading…
Cancel
Save