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.

57 lines
1.4 KiB

package main
import (
"bufio"
"bytes"
"fmt"
"io"
"os/exec"
"regexp"
"strings"
)
var zfsRegex = regexp.MustCompile(ZfsSnapshotNameRegex)
var snapshotLineRegex = regexp.MustCompile("^" + PoolNameRegex + "@" +
ZfsSnapshotNameRegex + ".*$")
func main() {
var listCommand = exec.Command("zfs", "list", "-Hrt", "snapshot", "dpool")
var snapList, err = listCommand.CombinedOutput()
var snapScanner = bufio.NewScanner(bytes.NewReader(snapList))
if err != nil {
fmt.Println(listCommand)
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 = 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
}
}
}
}