The zfs backup tool
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.7 KiB

package main
import (
"bufio"
"bytes"
"container/list"
"fmt"
"os/exec"
"regexp"
"sort"
"strings"
)
var zfsRegex = regexp.MustCompile(ZfsSnapshotNameRegex)
var snapshotLineRegex = regexp.MustCompile("^" + PoolNameRegex + "@" +
ZfsSnapshotNameRegex + ".*$")
func main() {
var snapScanner, err = createSnapshotScanner("dpool")
var snapshots = make(map[string]snapshotList)
if err != nil {
fmt.Println("Error trying to list snapshots:", err.Error())
for snapScanner.Scan() {
fmt.Println(snapScanner.Text())
}
}
for snapScanner.Scan() {
if snapshotLineRegex.MatchString(snapScanner.Text()) {
var temp = strings.SplitN(snapScanner.Text(), "\t", 2)
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])
}
//Lets get the list of snapshots in the backup pool
snapScanner, err = createSnapshotScanner("backups")
if err != nil {
fmt.Println("Error trying to list snapshots:", err.Error())
for snapScanner.Scan() {
fmt.Println(snapScanner.Text())
}
}
for snapScanner.Scan() {
if snapshotLineRegex.MatchString(snapScanner.Text()) {
var temp = strings.SplitN(snapScanner.Text(), "\t", 2)
var snapshot, err = ParseSnapshot(temp[0])
if err != nil {
// Ignore the bad input
}
snapshots[snapshot.Path()].addSnapshot(snapshot)
}
}
for i := range snapshots {
sort.Sort(snapshots[i])
}
var missingFromBackup list.List
_ = missingFromBackup
for i := 0; i < len(snapshots["dpool"].Snapshots); i++ {
i3 := sort.Search(len(snapshots["backups/encrypted/mars/dpool"].
Snapshots), func(i2 int) bool {
return snapshots["backups/encrypted/mars/dpool"].Snapshots[i2].
TimeStamp.After(snapshots["dpool"].Snapshots[i].TimeStamp)
})
if i3 != len(snapshots["backups/encrypted/mars/dpool"].Snapshots) {
}
}
}
func createSnapshotScanner(pool string) (scanner *bufio.Scanner, err error) {
listCommand := exec.Command("zfs", "list", "-Hrt", "snapshot", pool)
output, err := listCommand.CombinedOutput()
return bufio.NewScanner(bytes.NewReader(output)), err
}