restore
This commit is contained in:
@@ -7,11 +7,13 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"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 +140,36 @@ 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
|
||||
_, err := zfs.ReceiveSnapshot(reader, snapshotName)
|
||||
if 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)
|
||||
|
||||
Reference in New Issue
Block a user