Files
felhom-controller/controller/internal/web/kept_copies.go
T

121 lines
4.7 KiB
Go

package web
import (
"errors"
"net/http"
"net/url"
"strings"
"gitea.dooplex.hu/admin/felhom-controller/internal/backup"
)
// D25 (operator ruling 2026-10-21; R-1175 + R-1181): the restore page lists the second-drive copies whose source is
// gone and lets the household release one with „Eltávolítás". Nothing else removes them.
// keptCopyRow is one line of the „Megőrzött másolatok" list.
type keptCopyRow struct {
ID string
Source string
Line string // „a(z) <drive> meghajtóról megőrzött másolat · <why>"
SizeHuman string
Since string
// Restorable (D47): a set-aside recovery unit of an earlier install — the row offers „Visszaállítás" too.
Restorable bool
}
func (s *Server) keptCopyRows(lang string) []keptCopyRow {
if s.backupMgr == nil {
return nil
}
var out []keptCopyRow
for _, k := range s.backupMgr.KeptCopies() {
why := s.msgLang(lang, "kept.reason."+k.Reason)
out = append(out, keptCopyRow{
ID: k.ID,
Source: k.Source,
Line: s.msgLang(lang, "kept.from_drive", k.Drive) + " · " + why,
SizeHuman: backup.HumanizeBytes(k.SizeBytes),
Since: k.Since,
Restorable: k.Kind == backup.KeptKindUnit,
})
}
return out
}
// keptCopyRemoveHandler is „Eltávolítás": POST /backup/kept/remove (id, confirm=1). It removes ONE kept copy by its
// record (backup.RemoveKeptCopy: the recorded path, inside its copy tree, same device + inode) and nothing else.
func (s *Server) keptCopyRemoveHandler(w http.ResponseWriter, r *http.Request) {
if s.backupMgr == nil {
offboxRedirectTo(w, r, "/backups/restore", "flash.offbox.mgr_unreachable", true)
return
}
_ = r.ParseForm()
id := strings.TrimSpace(r.FormValue("id"))
if r.FormValue("confirm") != "1" {
offboxRedirectTo(w, r, "/backups/restore", "flash.kept.needs_confirmation", true)
return
}
if msg, blocked := s.restoreOpBlocked(); blocked {
s.logger.Printf("[WARN] [web] kept-copy removal refused (%s): a backup/restore op is running", id)
offboxRedirectTo(w, r, "/backups/restore", msg, true)
return
}
switch err := s.backupMgr.RemoveKeptCopy(id); {
case err == nil:
offboxRedirectTo(w, r, "/backups/restore", "flash.kept.removed", false)
case errors.Is(err, backup.ErrKeptCopyNotFound):
offboxRedirectTo(w, r, "/backups/restore", "flash.kept.not_found", true)
case errors.Is(err, backup.ErrKeptCopyChanged):
offboxRedirectTo(w, r, "/backups/restore", "flash.kept.changed", true)
default:
s.logger.Printf("[WARN] [web] kept-copy removal %s failed: %v", id, err)
offboxRedirectTo(w, r, "/backups/restore", "flash.kept.remove_failed", true, err.Error())
}
}
// keptUnitRestoreHandler is „Visszaállítás" on a set-aside unit (D47): POST /backup/kept/restore (id, confirm=1). The
// unit is named only by its record (backup.SetAsideUnitFor: the recorded folder, inside its set-aside tree, same device
// + inode), and restored through the one unit-restore body, in the background like every restore.
func (s *Server) keptUnitRestoreHandler(w http.ResponseWriter, r *http.Request) {
if s.backupMgr == nil {
offboxRedirectTo(w, r, "/backups/restore", "flash.offbox.mgr_unreachable", true)
return
}
_ = r.ParseForm()
id := strings.TrimSpace(r.FormValue("id"))
if r.FormValue("confirm") != "1" {
offboxRedirectTo(w, r, "/backups/restore", "flash.kept.restore_needs_confirmation", true)
return
}
if msg, blocked := s.restoreOpBlocked(); blocked {
http.Redirect(w, r, "/backups/restore?flash_error="+url.QueryEscape(msg), http.StatusFound)
return
}
k, err := s.backupMgr.SetAsideUnitFor(id)
switch {
case errors.Is(err, backup.ErrKeptCopyNotFound):
offboxRedirectTo(w, r, "/backups/restore", "flash.kept.not_found", true)
return
case err != nil:
offboxRedirectTo(w, r, "/backups/restore", "flash.kept.changed", true)
return
}
if legs := s.backupMgr.DeclaredDriveFileLegs(k.Source); len(legs) > 0 {
msg := s.missingFileLegsRefusal(r.Context(), s.langFor(r), k.Source)
http.Redirect(w, r, "/backups/restore?flash_error="+url.QueryEscape(msg), http.StatusFound)
return
}
s.logger.Printf("[WARN] [web] Restore of %s from a set-aside unit requested (async): %s from %s", k.Source, k.Path, r.RemoteAddr)
s.backupMgr.BeginRestoreOp("restore", k.Source)
go func() {
app, res, err := s.backupMgr.RestoreSetAsideUnit(id)
if err != nil {
s.logger.Printf("[ERROR] [web] Restore from the set-aside unit failed (async): stack=%s: %v", app, err)
s.endRestoreFailed("note.restore.unit_failed", err, "")
return
}
s.backupMgr.EndRestoreOp(true, s.restoredVersionPrefix(app, res.VersionChanged, res.DataPins, res.DataAt)+s.unitRestoreOutcomeMsg(app, res))
}()
http.Redirect(w, r, "/backups/restore?"+flashQuery("flash", "flash.restore.started"), http.StatusFound)
}