From ed68bffea5b9eb384e23284015d1c1145dcaacab Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Sun, 12 Nov 2017 23:05:18 +0100 Subject: [PATCH] bookmark every snapshot replication logic already supports bookmarks \o/ refs #34 --- cmd/autosnap.go | 13 ++++++++++--- cmd/prune.go | 1 + zfs/zfs.go | 35 ++++++++++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/cmd/autosnap.go b/cmd/autosnap.go index 8018089..8fce1dd 100644 --- a/cmd/autosnap.go +++ b/cmd/autosnap.go @@ -129,14 +129,21 @@ func (a *IntervalAutosnap) doSnapshots(didSnaps chan struct{}) { suffix := time.Now().In(time.UTC).Format("20060102_150405_000") snapname := fmt.Sprintf("%s%s", a.Prefix, suffix) - a.log.WithField(logFSField, d.ToString()). - WithField("snapname", snapname). - Info("create snapshot") + l := a.log.WithField(logFSField, d.ToString()). + WithField("snapname", snapname) + l.Info("create snapshot") err := zfs.ZFSSnapshot(d, snapname, false) if err != nil { a.log.WithError(err).Error("cannot create snapshot") } + + l.Info("create corresponding bookmark") + err = zfs.ZFSBookmark(d, snapname, snapname) + if err != nil { + a.log.WithError(err).Error("cannot create bookmark") + } + } select { diff --git a/cmd/prune.go b/cmd/prune.go index a59cd00..a43aac5 100644 --- a/cmd/prune.go +++ b/cmd/prune.go @@ -46,6 +46,7 @@ func (p *Pruner) Run(ctx context.Context) (r []PruneResult, err error) { log := log.WithField(logFSField, fs.ToString()) + // only prune snapshots, bookmarks are kept forever snapshotFilter := NewTypedPrefixFilter(p.SnapshotPrefix, zfs.Snapshot) fsversions, err := zfs.ZFSListFilesystemVersions(fs, snapshotFilter) if err != nil { diff --git a/zfs/zfs.go b/zfs/zfs.go index 2b8005a..755af10 100644 --- a/zfs/zfs.go +++ b/zfs/zfs.go @@ -286,9 +286,17 @@ func ZFSDestroy(dataset string) (err error) { } +func zfsBuildSnapName(fs *DatasetPath, name string) string { // TODO defensive + return fmt.Sprintf("%s@%s", fs.ToString(), name) +} + +func zfsBuildBookmarkName(fs *DatasetPath, name string) string { // TODO defensive + return fmt.Sprintf("%s#%s", fs.ToString(), name) +} + func ZFSSnapshot(fs *DatasetPath, name string, recursive bool) (err error) { - snapname := fmt.Sprintf("%s@%s", fs.ToString(), name) + snapname := zfsBuildSnapName(fs, name) cmd := exec.Command(ZFS_BINARY, "snapshot", snapname) stderr := bytes.NewBuffer(make([]byte, 0, 1024)) @@ -308,3 +316,28 @@ func ZFSSnapshot(fs *DatasetPath, name string, recursive bool) (err error) { return } + +func ZFSBookmark(fs *DatasetPath, snapshot, bookmark string) (err error) { + + snapname := zfsBuildSnapName(fs, snapshot) + bookmarkname := zfsBuildBookmarkName(fs, bookmark) + + cmd := exec.Command(ZFS_BINARY, "bookmark", snapname, bookmarkname) + + stderr := bytes.NewBuffer(make([]byte, 0, 1024)) + cmd.Stderr = stderr + + if err = cmd.Start(); err != nil { + return err + } + + if err = cmd.Wait(); err != nil { + err = ZFSError{ + Stderr: stderr.Bytes(), + WaitErr: err, + } + } + + return + +}