This commit is contained in:
2026-02-15 13:13:11 +01:00
parent 8b592db3dd
commit 2a5221c29a
8 changed files with 243 additions and 35 deletions

View File

@@ -6,12 +6,15 @@ import (
"io"
"log"
"net/http"
"os"
"os/exec"
"strings"
"time"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/mistifyio/go-zfs"
"github.com/pierrec/lz4/v4"
)
// StorageBackend defines the interface for different storage types
@@ -138,6 +141,41 @@ func (l *LocalBackend) Upload(ctx context.Context, key string, data io.Reader, s
return fmt.Errorf("local backend upload not supported via storage interface, use zfs receive endpoint")
}
// Receive receives a ZFS snapshot stream and restores it to the local dataset
func (l *LocalBackend) Receive(snapshotName string, data io.Reader, compressed bool) error {
// Extract the target dataset from the snapshot name
// snapshotName format: dataset@name -> we want just the dataset part
parts := strings.Split(snapshotName, "@")
if len(parts) != 2 {
return fmt.Errorf("invalid snapshot name format: %s", snapshotName)
}
targetDataset := parts[0]
log.Printf("Receiving ZFS snapshot to %s (compressed: %v)", targetDataset, compressed)
// If compressed, decompress with LZ4 first
var reader io.Reader = data
if compressed {
lz4Reader := lz4.NewReader(data)
reader = lz4Reader
}
// Use go-zfs library to receive the snapshot (with -F force flag)
// Note: The library's ReceiveSnapshot doesn't support -F, so we use exec.Command
cmd := exec.Command("zfs", "receive", "-F", snapshotName)
cmd.Stdin = reader
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("zfs receive failed: %v", err)
}
log.Printf("Successfully received snapshot: %s", snapshotName)
return nil
}
// Download creates a zfs send stream
func (l *LocalBackend) Download(ctx context.Context, key string) (io.ReadCloser, error) {
cmd := exec.CommandContext(ctx, "zfs", "send", key)