v0.12.7: mandatory HDD backup, pre-dump, restore for all apps

Fix 1: HDD data backup is now mandatory for all deployed apps.
resolveAppBackupPaths() iterates ListDeployedStacks() directly — no
longer reads GetAppBackupMap() or checks the Enabled flag. DiscoverAppData()
drops backupPrefs parameter; BackupEnabled is set from HasHDDData.
Five dead settings methods removed: IsAppBackupEnabled, SetAppBackup,
GetAppBackupMap, SetAppBackupBulk, GetAppBackupPrefs.

Fix 2: Cross-drive backup now triggers a fresh DB dump (DumpStackDB)
before running. DBDumper interface added to crossdrive.go; Manager
implements it; SetDBDumper wired in main.go. Non-fatal — proceeds with
user data backup even if DB dump fails.

Fix 3: Restore dropdown shows ALL deployed apps (not just HDD+enabled).
restore.go rewritten: always restores config+DB, adds user data if hasHDD.
UI shows restore type banner (full / config+DB / config only) with
color-coded styling. Snapshot API clarified for non-HDD apps.

Fix 4: "Docker kötetek" → "Konfiguráció" — named volumes are not in
the restic backup paths; compose files + app.yaml are what's backed up.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-18 10:38:51 +01:00
parent 263b58dea0
commit 6c1762141a
11 changed files with 225 additions and 124 deletions
+3
View File
@@ -461,8 +461,11 @@ func (r *Router) backupSnapshots(w http.ResponseWriter, req *http.Request) {
if stackName := req.URL.Query().Get("stack"); stackName != "" {
mounts := r.backupMgr.GetStackHDDMounts(stackName)
if len(mounts) > 0 {
// App has HDD data — filter to snapshots containing those paths
snapshots = filterSnapshotsByPaths(snapshots, mounts)
}
// Apps without HDD mounts: return all snapshots (they all contain
// the stacks dir + DB dumps which cover this app's config and database)
}
if snapshots == nil {
+4 -2
View File
@@ -58,7 +58,8 @@ type AppDockerVolume struct {
}
// DiscoverAppData discovers backup-relevant data for all deployed apps.
func DiscoverAppData(provider StackDataProvider, backupPrefs map[string]bool, discoveredDBs []DiscoveredDB) []AppBackupInfo {
// All apps with HDD data are backed up automatically (mandatory — no opt-in).
func DiscoverAppData(provider StackDataProvider, discoveredDBs []DiscoveredDB) []AppBackupInfo {
if provider == nil {
return nil
}
@@ -97,7 +98,8 @@ func DiscoverAppData(provider StackDataProvider, backupPrefs map[string]bool, di
}
}
info.BackupEnabled = backupPrefs[stack.Name]
// All apps with HDD data are backed up automatically (mandatory)
info.BackupEnabled = info.HasHDDData
result = append(result, info)
}
+53 -15
View File
@@ -425,24 +425,18 @@ func (m *Manager) GetStackHDDMounts(name string) []string {
return m.stackProvider.GetStackHDDMounts(name)
}
// resolveAppBackupPaths returns HDD paths for all enabled app backups.
// resolveAppBackupPaths returns HDD paths for ALL deployed apps.
// User data backup is mandatory — every app with HDD mounts is included.
func (m *Manager) resolveAppBackupPaths() []string {
if m.stackProvider == nil || m.settings == nil {
return nil
}
appBackupMap := m.settings.GetAppBackupMap()
if len(appBackupMap) == 0 {
if m.stackProvider == nil {
return nil
}
var paths []string
seen := make(map[string]bool)
for stackName, enabled := range appBackupMap {
if !enabled {
continue
}
hddMounts := m.stackProvider.GetStackHDDMounts(stackName)
for _, stack := range m.stackProvider.ListDeployedStacks() {
hddMounts := m.stackProvider.GetStackHDDMounts(stack.Name)
for _, mount := range hddMounts {
if seen[mount] {
continue
@@ -450,13 +444,58 @@ func (m *Manager) resolveAppBackupPaths() []string {
if _, err := os.Stat(mount); err == nil {
paths = append(paths, mount)
seen[mount] = true
m.logger.Printf("[DEBUG] Including app data: %s (from %s)", mount, stackName)
m.logger.Printf("[DEBUG] Including app data: %s (from %s)", mount, stack.Name)
}
}
}
return paths
}
// DumpStackDB runs a database dump for containers belonging to a specific stack.
// Used by cross-drive backup to ensure DB state matches user data.
func (m *Manager) DumpStackDB(ctx context.Context, stackName string) error {
dbs, err := DiscoverDatabases(ctx, m.logger)
if err != nil {
return fmt.Errorf("database discovery failed: %w", err)
}
var stackDBs []DiscoveredDB
for _, db := range dbs {
if db.StackName == stackName {
stackDBs = append(stackDBs, db)
}
}
if len(stackDBs) == 0 {
m.logger.Printf("[DEBUG] No databases found for stack %s — skipping pre-backup dump", stackName)
return nil
}
m.logger.Printf("[INFO] Running pre-backup DB dump for %s (%d database(s))", stackName, len(stackDBs))
results := DumpAll(ctx, stackDBs, m.cfg.Paths.DBDumpDir, m.logger)
for _, r := range results {
if r.Error != nil {
return fmt.Errorf("DB dump failed for %s: %w", r.DB.ContainerName, r.Error)
}
m.logger.Printf("[INFO] Pre-backup DB dump OK: %s (%s)", r.DB.ContainerName, humanizeBytes(r.Size))
// Persist validation to settings
if m.settings != nil && r.FilePath != "" {
filename := filepath.Base(r.FilePath)
cache := settings.DBValidationCache{
ValidatedAt: time.Now().Format(time.RFC3339),
TableCount: r.Validation.TableCount,
HasHeader: r.Validation.Valid,
}
if !r.Validation.Valid {
cache.Error = r.Validation.Error
}
_ = m.settings.SetDBValidation(filename, cache)
}
}
return nil
}
func shouldPrune(schedule string) bool {
loc, err := time.LoadLocation("Europe/Budapest")
if err != nil {
@@ -542,10 +581,9 @@ func (m *Manager) RefreshCache(nextDBDump, nextBackup time.Time) {
status.DiscoveredDBs = dbs
}
// Discover app data (for per-app backup toggles)
// Discover app data — all deployed stacks, backup is mandatory
if m.stackProvider != nil {
backupPrefs := m.settings.GetAppBackupMap()
status.AppDataInfo = DiscoverAppData(m.stackProvider, backupPrefs, status.DiscoveredDBs)
status.AppDataInfo = DiscoverAppData(m.stackProvider, status.DiscoveredDBs)
// Include enabled app backup paths in the displayed BackupPaths
appPaths := m.resolveAppBackupPaths()
+20
View File
@@ -15,10 +15,16 @@ import (
"gitea.dooplex.hu/admin/felhom-controller/internal/system"
)
// DBDumper can run a database dump for a specific stack.
type DBDumper interface {
DumpStackDB(ctx context.Context, stackName string) error
}
// CrossDriveRunner handles per-app backup to secondary storage.
type CrossDriveRunner struct {
sett *settings.Settings
stackProvider StackDataProvider
dbDumper DBDumper
logger *log.Logger
mu sync.Mutex
running map[string]bool // per-app running state
@@ -34,6 +40,12 @@ func NewCrossDriveRunner(sett *settings.Settings, provider StackDataProvider, lo
}
}
// SetDBDumper sets the DB dumper for pre-backup database dumps.
// Called after backup manager is initialized (avoids circular init dependency).
func (r *CrossDriveRunner) SetDBDumper(d DBDumper) {
r.dbDumper = d
}
// RunAppBackup runs cross-drive backup for a single app.
func (r *CrossDriveRunner) RunAppBackup(ctx context.Context, stackName string) error {
cfg := r.sett.GetCrossDriveConfig(stackName)
@@ -64,6 +76,14 @@ func (r *CrossDriveRunner) RunAppBackup(ctx context.Context, stackName string) e
r.logger.Printf("[INFO] Cross-drive backup starting: %s → %s (method: %s)",
stackName, cfg.DestinationPath, cfg.Method)
// Trigger fresh DB dump for this app before cross-drive backup
if r.dbDumper != nil {
if err := r.dbDumper.DumpStackDB(ctx, stackName); err != nil {
r.logger.Printf("[WARN] Pre-backup DB dump failed for %s: %v — proceeding with user data backup", stackName, err)
// Non-fatal: user data backup is still valuable without fresh dump
}
}
if err := r.ValidateDestination(cfg.DestinationPath); err != nil {
r.updateStatus(stackName, "error", err.Error(), time.Since(start), "")
return fmt.Errorf("destination validation failed: %w", err)
+47 -25
View File
@@ -2,35 +2,26 @@ package backup
import (
"fmt"
"path/filepath"
"regexp"
)
// snapshotIDRe validates restic snapshot IDs: 8-64 lowercase hex characters.
var snapshotIDRe = regexp.MustCompile(`^[0-9a-f]{8,64}$`)
// RestoreApp restores an app's HDD data from a restic snapshot.
// RestoreApp restores an app from a restic snapshot.
// All apps get config + DB dump restored. Apps with HDD data also get user data restored.
func (m *Manager) RestoreApp(stackName, snapshotID string) error {
// Validate app has backup enabled
if !m.settings.IsAppBackupEnabled(stackName) {
return fmt.Errorf("backup not enabled for %s", stackName)
}
// Resolve HDD paths for this app
if m.stackProvider == nil {
return fmt.Errorf("stack provider not configured")
}
hddMounts := m.stackProvider.GetStackHDDMounts(stackName)
if len(hddMounts) == 0 {
return fmt.Errorf("no HDD data paths found for %s", stackName)
}
// H4: Validate snapshot ID format by regex instead of listing all snapshots (list caps at 100).
// restic restore will return a clear error if the snapshot ID doesn't exist.
// Validate snapshot ID format
if !snapshotIDRe.MatchString(snapshotID) {
return fmt.Errorf("invalid snapshot ID: must be 8-64 lowercase hex characters")
}
// Use the running flag to prevent concurrent backup/restore
// Prevent concurrent operations
m.mu.Lock()
if m.running {
m.mu.Unlock()
@@ -38,35 +29,66 @@ func (m *Manager) RestoreApp(stackName, snapshotID string) error {
}
m.running = true
m.mu.Unlock()
defer func() {
m.mu.Lock()
m.running = false
m.mu.Unlock()
}()
m.logger.Printf("[WARN] RESTORE starting: stack=%s, snapshot=%s, paths=%v", stackName, snapshotID, hddMounts)
// Determine what to restore
hddMounts := m.stackProvider.GetStackHDDMounts(stackName)
hasHDD := len(hddMounts) > 0
// Stop the app before restore to avoid data corruption
if err := m.stackProvider.StopStack(stackName); err != nil {
m.logger.Printf("[WARN] RESTORE could not stop %s before restore: %v (proceeding anyway)", stackName, err)
// Build list of paths to restore from the snapshot
var restorePaths []string
// Always restore the stack's config dir (compose + app.yaml + .felhom.yml)
composePath, ok := m.stackProvider.GetStackComposePath(stackName)
if ok {
stackDir := filepath.Dir(composePath)
restorePaths = append(restorePaths, stackDir)
}
// Execute restore
if err := m.restic.RestoreAppData(snapshotID, hddMounts); err != nil {
// Restore DB dump files for this stack
if m.cfg.Paths.DBDumpDir != "" {
restorePaths = append(restorePaths, m.cfg.Paths.DBDumpDir)
}
// Restore HDD data (always included for apps that have it — backup is mandatory)
if hasHDD {
restorePaths = append(restorePaths, hddMounts...)
}
if len(restorePaths) == 0 {
return fmt.Errorf("no restorable paths found for %s", stackName)
}
m.logger.Printf("[WARN] RESTORE starting: stack=%s, snapshot=%s, paths=%v, hasHDD=%v",
stackName, snapshotID, restorePaths, hasHDD)
// Stop the app before restore
if err := m.stackProvider.StopStack(stackName); err != nil {
m.logger.Printf("[WARN] RESTORE could not stop %s: %v (proceeding anyway)", stackName, err)
}
// Execute restore via restic
if err := m.restic.RestoreAppData(snapshotID, restorePaths); err != nil {
m.logger.Printf("[ERROR] RESTORE failed for %s: %v", stackName, err)
// Try to restart the app even on failure
if startErr := m.stackProvider.StartStack(stackName); startErr != nil {
m.logger.Printf("[WARN] RESTORE could not restart %s after failed restore: %v", stackName, startErr)
m.logger.Printf("[WARN] RESTORE could not restart %s after failure: %v", stackName, startErr)
}
return err
}
// Restart the app after successful restore
// Restart the app
if err := m.stackProvider.StartStack(stackName); err != nil {
m.logger.Printf("[WARN] RESTORE could not restart %s after restore: %v", stackName, err)
}
m.logger.Printf("[INFO] RESTORE completed: stack=%s, snapshot=%s", stackName, snapshotID)
restoreType := "config+DB"
if hasHDD {
restoreType = "full (config+DB+userdata)"
}
m.logger.Printf("[INFO] RESTORE completed: stack=%s, snapshot=%s, type=%s", stackName, snapshotID, restoreType)
return nil
}
-65
View File
@@ -232,71 +232,6 @@ func (s *Settings) SetNotificationPrefs(prefs *NotificationPrefs) error {
return s.save()
}
// IsAppBackupEnabled returns whether backup is enabled for the given stack.
func (s *Settings) IsAppBackupEnabled(stackName string) bool {
s.mu.RLock()
defer s.mu.RUnlock()
if s.AppBackup == nil {
return false
}
return s.AppBackup[stackName].Enabled
}
// SetAppBackup enables or disables backup for a stack and saves to disk.
// Preserves existing CrossDrive config.
func (s *Settings) SetAppBackup(stackName string, enabled bool) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.AppBackup == nil {
s.AppBackup = make(map[string]AppBackupPrefs)
}
existing := s.AppBackup[stackName]
existing.Enabled = enabled
s.AppBackup[stackName] = existing
return s.save()
}
// GetAppBackupMap returns a map of stack_name -> enabled for all app backup prefs.
func (s *Settings) GetAppBackupMap() map[string]bool {
s.mu.RLock()
defer s.mu.RUnlock()
if s.AppBackup == nil {
return nil
}
result := make(map[string]bool, len(s.AppBackup))
for k, v := range s.AppBackup {
result[k] = v.Enabled
}
return result
}
// SetAppBackupBulk updates backup prefs for all stacks at once and saves to disk.
// Preserves existing CrossDrive configs and stacks not present in the input.
func (s *Settings) SetAppBackupBulk(prefs map[string]bool) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.AppBackup == nil {
s.AppBackup = make(map[string]AppBackupPrefs)
}
for name, enabled := range prefs {
existing := s.AppBackup[name] // preserves CrossDrive
existing.Enabled = enabled
s.AppBackup[name] = existing
}
return s.save()
}
// GetAppBackupPrefs returns the full AppBackupPrefs for a stack.
func (s *Settings) GetAppBackupPrefs(stackName string) (AppBackupPrefs, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
if s.AppBackup == nil {
return AppBackupPrefs{}, false
}
prefs, ok := s.AppBackup[stackName]
return prefs, ok
}
// GetCrossDriveConfig returns the cross-drive backup config for a stack (nil if not set).
func (s *Settings) GetCrossDriveConfig(stackName string) *CrossDriveBackup {
s.mu.RLock()
+23 -4
View File
@@ -281,7 +281,7 @@
</div>
<!-- Volume layer -->
<div class="backup-layer-row">
<span class="layer-label">Docker kötetek</span>
<span class="layer-label">Konfiguráció</span>
<span class="layer-badge">Auto</span>
{{if .VolumeLastRun}}
<span class="layer-last">Utolsó: {{.VolumeLastRun}}
@@ -450,9 +450,7 @@
<select id="restore-app" class="restore-select" onchange="onRestoreAppChange()">
<option value="">— Válasszon —</option>
{{range .Backup.AppDataInfo}}
{{if and .HasHDDData .BackupEnabled}}
<option value="{{.StackName}}">{{.DisplayName}}</option>
{{end}}
<option value="{{.StackName}}" data-has-hdd="{{.HasHDDData}}" data-has-db="{{.HasDBDump}}">{{.DisplayName}}</option>
{{end}}
</select>
</div>
@@ -462,6 +460,8 @@
<option value="">— Válasszon alkalmazást —</option>
</select>
</div>
<div id="restore-type-info" class="restore-info" style="display:none;margin-bottom:0.5rem">
</div>
<div id="restore-no-snapshots" class="restore-warning" style="display:none;">
Még nincs mentés felhasználói adattal.
</div>
@@ -606,16 +606,35 @@ function onRestoreAppChange() {
var appName = sel.value;
var snapSel = document.getElementById('restore-snapshot');
var noSnaps = document.getElementById('restore-no-snapshots');
var typeInfo = document.getElementById('restore-type-info');
document.getElementById('restore-confirm-cb').checked = false;
document.getElementById('restore-btn').disabled = true;
noSnaps.style.display = 'none';
typeInfo.style.display = 'none';
if (!appName) {
snapSel.innerHTML = '<option value="">— Válasszon alkalmazást —</option>';
return;
}
// Determine restore type from data attributes
var opt = sel.options[sel.selectedIndex];
var hasHDD = opt.getAttribute('data-has-hdd') === 'true';
var hasDB = opt.getAttribute('data-has-db') === 'true';
if (hasHDD) {
typeInfo.innerHTML = '🔄 Teljes visszaállítás: adatbázis + konfiguráció + felhasználói adatok a kiválasztott pillanatképből.';
typeInfo.className = 'restore-info';
} else if (hasDB) {
typeInfo.innerHTML = ' Adatbázis és konfiguráció visszaállítása — az alkalmazásnak nincs külön felhasználói adata.';
typeInfo.className = 'restore-info restore-info-partial';
} else {
typeInfo.innerHTML = ' Csak konfiguráció visszaállítása (compose fájlok, beállítások).';
typeInfo.className = 'restore-info restore-info-partial';
}
typeInfo.style.display = 'block';
snapSel.innerHTML = '<option value="">— Betöltés... —</option>';
fetch('/api/backup/snapshots?stack=' + encodeURIComponent(appName))
@@ -1921,6 +1921,21 @@ a.stat-card:hover {
line-height: 1.4;
}
/* --- Backup page: Restore type info banners --- */
.restore-info {
padding: 0.5rem 0.75rem;
border-radius: 6px;
font-size: 0.85rem;
background: rgba(59, 130, 246, 0.1);
border: 1px solid rgba(59, 130, 246, 0.3);
color: #93c5fd;
}
.restore-info-partial {
background: rgba(251, 191, 36, 0.1);
border-color: rgba(251, 191, 36, 0.3);
color: #fcd34d;
}
/* --- Backup page: Restore section --- */
.restore-section {
display: flex;