This commit is contained in:
2026-02-13 21:50:00 +01:00
parent dbfd99f410
commit 1825b50dec
11 changed files with 276 additions and 30 deletions

View File

@@ -4,7 +4,6 @@ package client
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io"
@@ -15,6 +14,7 @@ import (
"time"
"github.com/mistifyio/go-zfs"
"github.com/pierrec/lz4/v4"
)
// Client handles snapshot backup operations to a remote server.
@@ -110,7 +110,7 @@ func (c *Client) SendSnapshot(snapshot *zfs.Dataset) error {
}
// streamToS3 streams a ZFS snapshot to S3 storage via HTTP.
// The snapshot is optionally compressed with gzip before transmission.
// The snapshot is optionally compressed with LZ4 before transmission.
func (c *Client) streamToS3(snapshot *zfs.Dataset, uploadURL, storageKey string) error {
fmt.Printf("→ Streaming snapshot to S3...\n")
@@ -127,17 +127,18 @@ func (c *Client) streamToS3(snapshot *zfs.Dataset, uploadURL, storageKey string)
var reader io.Reader = zfsOut
// Apply gzip compression if enabled
// Apply LZ4 compression if enabled
if c.config.Compress {
fmt.Printf(" Compressing with gzip...\n")
fmt.Printf(" Compressing with LZ4...\n")
pr, pw := io.Pipe()
gzWriter := gzip.NewWriter(pw)
lz4Writer := lz4.NewWriter(pw)
lz4Writer.Apply(lz4.BlockSizeOption(lz4.BlockSize(4 * 1024 * 1024))) // 4MB blocks for better performance
go func() {
// Copy zfs output to gzip writer
io.Copy(gzWriter, zfsOut)
// Close gzip writer first to flush footer, then close pipe
gzWriter.Close()
// Copy zfs output to LZ4 writer
io.Copy(lz4Writer, zfsOut)
// Close LZ4 writer first to flush, then close pipe
lz4Writer.Close()
pw.Close()
}()
@@ -340,3 +341,38 @@ func (c *Client) GetRotationPolicy() (*ServerRotationPolicy, error) {
return &policyResp, nil
}
// ChangePassword changes the client's API key on the server.
// Requires the current API key for authentication and the new key.
func (c *Client) ChangePassword(newAPIKey string) error {
reqBody, _ := json.Marshal(map[string]string{
"client_id": c.config.ClientID,
"current_key": c.config.APIKey,
"new_key": newAPIKey,
})
resp, err := http.Post(c.config.ServerURL+"/client/change-password", "application/json", bytes.NewBuffer(reqBody))
if err != nil {
return fmt.Errorf("failed to change password: %v", err)
}
defer resp.Body.Close()
var result struct {
Success bool `json:"success"`
Message string `json:"message"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return fmt.Errorf("failed to decode response: %v", err)
}
if !result.Success {
return fmt.Errorf("failed to change password: %s", result.Message)
}
// Update local config with new key
c.config.APIKey = newAPIKey
fmt.Printf("✓ Password changed successfully\n")
return nil
}

View File

@@ -19,7 +19,7 @@ type Config struct {
ServerURL string `json:"server_url"`
// LocalDataset is the ZFS dataset to backup
LocalDataset string `json:"local_dataset"`
// Compress enables gzip compression for transfers
// Compress enables LZ4 compression for transfers
Compress bool `json:"compress"`
}

View File

@@ -5,7 +5,6 @@ package client
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io"
@@ -17,6 +16,7 @@ import (
"time"
"github.com/mistifyio/go-zfs"
"github.com/pierrec/lz4/v4"
)
// SnapshotPolicy defines retention settings for automatic snapshots.
@@ -220,17 +220,18 @@ func (c *Client) streamIncrementalToS3(snapshot *zfs.Dataset, base, uploadURL, s
var reader io.Reader = zfsOut
// Apply gzip compression if enabled
// Apply LZ4 compression if enabled
if c.config.Compress {
fmt.Printf(" Compressing with gzip...\n")
fmt.Printf(" Compressing with LZ4...\n")
pr, pw := io.Pipe()
gzWriter := gzip.NewWriter(pw)
lz4Writer := lz4.NewWriter(pw)
lz4Writer.Apply(lz4.BlockSizeOption(lz4.BlockSize(4 * 1024 * 1024))) // 4MB blocks for better performance
go func() {
// Copy zfs output to gzip writer
io.Copy(gzWriter, zfsOut)
// Close gzip writer first to flush footer, then close pipe
gzWriter.Close()
// Copy zfs output to LZ4 writer
io.Copy(lz4Writer, zfsOut)
// Close LZ4 writer first to flush, then close pipe
lz4Writer.Close()
pw.Close()
}()