feat(backup): Off-Site-Upload nach S3 + SFTP

Schutz gegen Box-Total-Loss — lokale Backups in /var/backups/edgeguard
helfen nicht, wenn die Disk stirbt oder die Box brennt. Nach jedem
erfolgreichen lokalen Backup wird die tar.gz an alle aktiven
Off-Site-Ziele hochgeladen.

Migration 0022: backup_remotes (kind=s3|sftp, target_url, settings
JSONB, active, last_upload_at, last_error) + backups.remote_uploads
JSONB (per-Target-Result).

internal/services/backup/remote/:
  - UploadAll() — pro aktivem Target ein Upload, Failures non-fatal
  - S3 via minio-go/v7 — funktioniert mit AWS, MinIO, Backblaze B2,
    Cloudflare R2, Hetzner Object Storage (alle S3-API-kompatibel)
  - SFTP via golang.org/x/crypto/ssh + pkg/sftp. Password + Private-
    Key (OpenSSH, base64-encoded) als Auth. Optional host_key_
    fingerprint-Pinning (SHA256:...); leer = TOFU (unsicher vs MitM,
    OK für initial setup).
  - Test() lädt eine 1KB-Probe + löscht sie wieder — Operator-UI hat
    einen „Verbindung testen"-Button.

backup.Service.RemoteUploader-Interface: nach erfolgreichem
recordSuccess() läuft UploadAll, Results landen in backups.remote_
uploads JSONB. last_upload_at/last_error in backup_remotes pro Target
gepflegt. API + Scheduler injizieren beide den Adapter.

internal/handlers/backup_remotes.go: CRUD + POST /:id/test. Sensitive
Felder (secret_key, password, private_key) werden in GET-Responses
durch ***SET*** maskiert; UpdateChannel merged das zurück damit der
Operator bei Edit ohne Re-Eingabe speichern kann.

UI: Backups-Page jetzt mit Tabs "Sicherungen" + "Off-Site-Ziele".
Tab 2 hat CRUD-Tabelle mit kind-konditionalem Form (S3-Felder oder
SFTP-Felder), Test-Button pro Row, last_upload-Status mit FAIL-Tag
bei Errors.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Debian
2026-05-13 18:49:02 +02:00
parent 81a8217493
commit 27ac7b53fc
16 changed files with 1520 additions and 329 deletions

View File

@@ -68,6 +68,7 @@ type Result struct {
StartedAt time.Time
FinishedAt time.Time
Error error
RemoteUploads []RemoteResult
}
// Manifest ist der content von manifest.json im tarball.
@@ -80,6 +81,39 @@ type Manifest struct {
FilesBytes int64 `json:"files_bytes"`
}
// RemoteUploader ist das Service-Interface das nach erfolgreichem
// Backup die tar.gz in Off-Site-Targets (S3/SFTP) hochlädt. Wird im
// Konstruktor optional gesetzt; nil = kein Off-Site.
type RemoteUploader interface {
UploadAll(ctx context.Context, localPath string) ([]RemoteUploadInfo, error)
}
// RemoteUploadInfo ist die minimale Sicht die der Service braucht.
// remote.UploadResult passt direkt rein.
type RemoteUploadInfo = remoteUploadResult
// remoteUploadResult ist das Struct das remote.UploadResult spiegelt
// (separate Datei um Import-Cycle zu vermeiden). Die remote/-Package
// implementiert RemoteUploader.
type remoteUploadResult struct {
RemoteID int64
RemoteName string
OK bool
SizeBytes int64
DurationMs int64
Error string
}
// RemoteResult ist die UI-Sicht, eine Liste davon landet im Result
// der Run()-Methode.
type RemoteResult struct {
Name string `json:"name"`
OK bool `json:"ok"`
Bytes int64 `json:"bytes,omitempty"`
DurationMs int64 `json:"duration_ms,omitempty"`
Error string `json:"error,omitempty"`
}
// Service bündelt Backup + Restore + Retention. Stateless — alle
// Konfig kommt als Konstruktor-Param + Methode-Param.
type Service struct {
@@ -92,6 +126,10 @@ type Service struct {
// Tests einen fake-binary einschleusen.
PGDumpCmd func(ctx context.Context, w io.Writer) (int64, error)
NowFn func() time.Time
// RemoteUploader: nach erfolgreichem Backup wird das tarball in
// alle aktiven backup_remotes hochgeladen. nil = kein Off-Site.
RemoteUploader RemoteUploader
}
func New(pool *pgxpool.Pool) *Service {
@@ -217,6 +255,25 @@ func (s *Service) Run(ctx context.Context, kind Kind, version string) (*Result,
// arbeitet nur über DB-Rows).
return res, fmt.Errorf("db record: %w", err)
}
// Off-Site-Uploads. Failures hier sind nicht-fatal — das lokale
// Backup ist erfolgreich, Operator sieht in der UI welcher Target
// erfolgreich war + welcher nicht. RemoteUploader ist optional;
// wenn nicht injiziert, läuft kein Off-Site.
if s.RemoteUploader != nil {
uploads, _ := s.RemoteUploader.UploadAll(ctx, outPath)
if len(uploads) > 0 {
jsonBytes, _ := json.Marshal(uploads)
_, _ = s.Pool.Exec(ctx,
`UPDATE backups SET remote_uploads = $1 WHERE id = $2`,
jsonBytes, res.ID)
for _, u := range uploads {
res.RemoteUploads = append(res.RemoteUploads, RemoteResult{
Name: u.RemoteName, OK: u.OK, Bytes: u.SizeBytes,
DurationMs: u.DurationMs, Error: u.Error,
})
}
}
}
return res, nil
}