Add database backups and two-digit disc numbers

Disc numbers: the organizer writes two-digit disc numbers by default
(Disc 01, 01-04 Title, {disc}), with an option for the plain style
(Disc 2, 2-04). Migration 8 adds the per-pair setting.

Backups (Settings -> Backups), similar to Sonarr:
- A backup is a zip with a consistent snapshot of the database (VACUUM
  INTO, safe while running) and backup.json. Configurable folder,
  schedule (off/1/3/7/14 days) and retention for scheduled backups;
  manual backups are kept until deleted. List, download, delete.
- Restore from a listed backup or an uploaded zip: validated first
  (integrity check, playlink tables, not a newer schema), staged next to
  the live database, then the server restarts; at startup the staged
  database is swapped in and the previous one kept as
  playlink.db.pre-restore-<time>. Refused while a job runs. Backup names
  are validated, so downloads/deletes cannot reach other files.
- Optional backup before every action in live mode: once per action the
  user starts (link, promote, bulk, build/remove, tag edit, folder
  renames, undo, purge) and before scans auto-link or auto-organize,
  never per file and never in dry run. If the backup fails the action
  does not run. The newest 10 action backups are kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
cdnutterandClaude Opus 5.5 committed 2026-09-29 14:26:34 -07:00
1 parent 96ea856dfd
commit 49fc32096a
18 files changed
+1235 -47

No files matched your search

+152
View File
@@ -0,0 +1,152 @@
package api
import (
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"time"
"github.com/go-chi/chi/v5"
"github.com/cdnutter/playlink/internal/backup"
"github.com/cdnutter/playlink/internal/store"
)
func (s *Server) backupRoutes(r chi.Router) {
r.Get("/backups", s.handleListBackups)
r.Put("/backups/config", s.handleBackupConfig)
r.Post("/backups", s.handleCreateBackup)
r.Post("/backups/restore-upload", s.handleRestoreUpload)
r.Get("/backups/{name}/download", s.handleDownloadBackup)
r.Post("/backups/{name}/restore", s.handleRestoreBackup)
r.Delete("/backups/{name}", s.handleDeleteBackup)
}
func (s *Server) handleListBackups(w http.ResponseWriter, r *http.Request) {
list, err := s.cfg.Backups.List(r.Context())
if err != nil {
writeErr(w, 500, err)
return
}
writeJSON(w, 200, map[string]any{"config": s.cfg.Backups.Config(r.Context()), "backups": list})
}
func (s *Server) handleBackupConfig(w http.ResponseWriter, r *http.Request) {
var c backup.Config
if !readJSON(w, r, &c) {
return
}
if err := s.cfg.Backups.SetConfig(r.Context(), c); err != nil {
writeErr(w, 400, err)
return
}
writeJSON(w, 200, s.cfg.Backups.Config(r.Context()))
}
func (s *Server) handleCreateBackup(w http.ResponseWriter, r *http.Request) {
b, err := s.cfg.Backups.Create(r.Context(), backup.TypeManual, "manual")
if err != nil {
writeErr(w, 500, err)
return
}
writeJSON(w, 200, b)
}
func (s *Server) backupPath(w http.ResponseWriter, r *http.Request) (string, bool) {
p, err := s.cfg.Backups.Path(r.Context(), chi.URLParam(r, "name"))
switch {
case errors.Is(err, backup.ErrBadName):
writeErr(w, 400, err)
case err != nil:
writeErr(w, 404, errors.New("backup not found"))
default:
return p, true
}
return "", false
}
func (s *Server) handleDownloadBackup(w http.ResponseWriter, r *http.Request) {
p, ok := s.backupPath(w, r)
if !ok {
return
}
w.Header().Set("Content-Type", "application/zip")
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filepath.Base(p)))
http.ServeFile(w, r, p)
}
func (s *Server) handleDeleteBackup(w http.ResponseWriter, r *http.Request) {
if _, ok := s.backupPath(w, r); !ok {
return
}
if err := s.cfg.Backups.Delete(r.Context(), chi.URLParam(r, "name")); err != nil {
writeErr(w, 500, err)
return
}
writeJSON(w, 200, map[string]any{"ok": true})
}
// restoreBusy refuses a restore while any job runs: the database is about to
// be replaced.
func (s *Server) restoreBusy(w http.ResponseWriter, r *http.Request) bool {
pairs, _ := s.st.Pairs(r.Context())
for _, p := range pairs {
if j := s.eng.Job(p.ID); j != nil && j.State == "running" {
writeErr(w, 409, fmt.Errorf("a job is running for %q; wait for it to finish before restoring", p.Name))
return true
}
}
return false
}
func (s *Server) handleRestoreBackup(w http.ResponseWriter, r *http.Request) {
p, ok := s.backupPath(w, r)
if !ok || s.restoreBusy(w, r) {
return
}
s.stageAndRestart(w, r, p)
}
func (s *Server) handleRestoreUpload(w http.ResponseWriter, r *http.Request) {
if s.restoreBusy(w, r) {
return
}
file, _, err := r.FormFile("backup")
if err != nil {
writeErr(w, 400, errors.New("choose a backup .zip file to upload"))
return
}
defer file.Close()
tmp, err := os.CreateTemp(s.cfg.Backups.Config(r.Context()).Folder, ".upload-*.zip")
if err != nil {
writeErr(w, 500, err)
return
}
defer os.Remove(tmp.Name())
if _, err := io.Copy(tmp, file); err != nil {
tmp.Close()
writeErr(w, 400, err)
return
}
tmp.Close()
s.stageAndRestart(w, r, tmp.Name())
}
func (s *Server) stageAndRestart(w http.ResponseWriter, r *http.Request, zipPath string) {
if err := s.cfg.Backups.Stage(r.Context(), zipPath, store.SchemaVersion()); err != nil {
writeErr(w, 400, err)
return
}
writeJSON(w, 202, map[string]any{"ok": true, "restarting": true})
if s.cfg.Restart != nil {
// Let the response reach the browser before shutting down.
go func() {
time.Sleep(500 * time.Millisecond)
s.log.Warn("restarting to apply the restored backup")
s.cfg.Restart()
}()
}
}
+61 -4
View File
@@ -19,6 +19,7 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/cdnutter/playlink/internal/backup"
"github.com/cdnutter/playlink/internal/engine"
"github.com/cdnutter/playlink/internal/fsops"
"github.com/cdnutter/playlink/internal/store"
@@ -28,6 +29,8 @@ import (
type Config struct {
SecureCookies bool
Version string
Backups *backup.Manager
Restart func() // stops the server so the process restarts (after a restore)
}
type Server struct {
@@ -54,7 +57,11 @@ func (s *Server) Handler() http.Handler {
r.Use(s.csrf)
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, 1<<20)
limit := int64(1 << 20)
if strings.HasSuffix(r.URL.Path, "/backups/restore-upload") {
limit = 20 << 30 // backup zips can be large
}
r.Body = http.MaxBytesReader(w, r.Body, limit)
w.Header().Set("Cache-Control", "no-store")
next.ServeHTTP(w, r)
})
@@ -72,6 +79,9 @@ func (s *Server) Handler() http.Handler {
r.Get("/settings", s.handleGetSettings)
r.Put("/settings", s.handlePutSettings)
r.Get("/browse", s.handleBrowse)
if s.cfg.Backups != nil {
s.backupRoutes(r)
}
r.Get("/pairs", s.handleListPairs)
r.Post("/pairs", s.handleSavePair)
@@ -340,6 +350,7 @@ type pairInput struct {
OrganizeTemplate string `json:"organize_template"`
OrganizeMultiDisc string `json:"organize_multidisc"`
AutoOrganize string `json:"auto_organize"`
OrganizeDiscDigits int `json:"organize_disc_digits"`
}
func (in pairInput) apply(p *store.Pair) {
@@ -348,6 +359,7 @@ func (in pairInput) apply(p *store.Pair) {
p.ScanIntervalMinutes, p.RetentionDays, p.SettleMinutes = in.ScanIntervalMinutes, in.RetentionDays, in.SettleMinutes
p.ApproxMatching, p.ApproxThreshold = in.ApproxMatching, in.ApproxThreshold
p.OrganizeTemplate, p.OrganizeMultiDisc, p.AutoOrganize = in.OrganizeTemplate, in.OrganizeMultiDisc, in.AutoOrganize
p.OrganizeDiscDigits = in.OrganizeDiscDigits
}
func (s *Server) handleValidatePair(w http.ResponseWriter, r *http.Request) {
@@ -469,6 +481,9 @@ func (s *Server) handleMatches(w http.ResponseWriter, r *http.Request) {
func (s *Server) handleLinkIdentical(w http.ResponseWriter, r *http.Request) {
id, _ := idParam(r)
if !s.preAction(w, r, id, "link identical") {
return
}
j, err := s.eng.StartLinkIdentical(id)
if err != nil {
opErr(w, err)
@@ -492,14 +507,14 @@ type pathsBody struct {
}
func (s *Server) handleBuild(w http.ResponseWriter, r *http.Request) {
s.pathsJob(w, r, s.eng.StartBuild)
s.pathsJob(w, r, "build", s.eng.StartBuild)
}
func (s *Server) handleRemove(w http.ResponseWriter, r *http.Request) {
s.pathsJob(w, r, s.eng.StartRemove)
s.pathsJob(w, r, "remove from slave", s.eng.StartRemove)
}
func (s *Server) pathsJob(w http.ResponseWriter, r *http.Request, start func(int64, []string) (*engine.Job, error)) {
func (s *Server) pathsJob(w http.ResponseWriter, r *http.Request, reason string, start func(int64, []string) (*engine.Job, error)) {
id, _ := idParam(r)
var body pathsBody
if !readJSON(w, r, &body) {
@@ -515,6 +530,9 @@ func (s *Server) pathsJob(w http.ResponseWriter, r *http.Request, start func(int
return
}
}
if !s.preAction(w, r, id, reason) {
return
}
j, err := start(id, body.Paths)
if err != nil {
opErr(w, err)
@@ -534,6 +552,9 @@ func (s *Server) handleLink(w http.ResponseWriter, r *http.Request) {
if !readJSON(w, r, &body) {
return
}
if !s.preAction(w, r, 0, "link") {
return
}
op, err := s.eng.Link(r.Context(), id, engine.LinkOptions{AllowDowngrade: body.AllowDowngrade, KeepFolder: body.KeepFolder})
if err != nil {
opErr(w, err)
@@ -550,6 +571,9 @@ func (s *Server) handlePromote(w http.ResponseWriter, r *http.Request) {
if !readJSON(w, r, &body) {
return
}
if body.Confirm && !s.preAction(w, r, 0, "promote") {
return
}
op, err := s.eng.Promote(r.Context(), id, body.Confirm)
if err != nil {
opErr(w, err)
@@ -590,6 +614,9 @@ func (s *Server) handleOps(w http.ResponseWriter, r *http.Request) {
func (s *Server) handleUndo(w http.ResponseWriter, r *http.Request) {
id, _ := idParam(r)
if !s.preAction(w, r, 0, "undo") {
return
}
if err := s.eng.Undo(r.Context(), id); err != nil {
opErr(w, err)
return
@@ -599,6 +626,9 @@ func (s *Server) handleUndo(w http.ResponseWriter, r *http.Request) {
func (s *Server) handlePurge(w http.ResponseWriter, r *http.Request) {
id, _ := idParam(r)
if !s.preAction(w, r, 0, "purge quarantine") {
return
}
if err := s.eng.Purge(r.Context(), id); err != nil {
opErr(w, err)
return
@@ -653,6 +683,9 @@ func (s *Server) handleBulk(w http.ResponseWriter, r *http.Request) {
}
writeJSON(w, 200, map[string]any{"changed": n})
default:
if !s.preAction(w, r, id, "bulk "+b.Action) {
return
}
j, err := s.eng.StartBulk(id, b.IDs, b.BulkOptions)
if err != nil {
opErr(w, err)
@@ -690,6 +723,9 @@ func (s *Server) handleRenames(w http.ResponseWriter, r *http.Request) {
return
}
}
if !s.preAction(w, r, id, "rename folders") {
return
}
j, err := s.eng.StartRenames(id, body.Only)
if err != nil {
opErr(w, err)
@@ -735,6 +771,9 @@ func (s *Server) handleEditTags(w http.ResponseWriter, r *http.Request) {
if !readJSON(w, r, &body) {
return
}
if !s.preAction(w, r, id, "edit tags") {
return
}
j, err := s.eng.StartTagEdit(id, body)
if err != nil {
opErr(w, err)
@@ -743,3 +782,21 @@ func (s *Server) handleEditTags(w http.ResponseWriter, r *http.Request) {
s.log.Info("tag edit started", "pair", id, "side", body.Side, "files", len(body.Paths), "fields", len(body.Set), "separate", body.Separate)
writeJSON(w, 202, j)
}
// preAction runs the optional database backup before an action that changes
// files (live mode only). It refuses up front when the pair is busy, so no
// backup is made for an action that could not start.
func (s *Server) preAction(w http.ResponseWriter, r *http.Request, pairID int64, reason string) bool {
if pairID != 0 {
if j := s.eng.Job(pairID); j != nil && j.State == "running" {
writeErr(w, 409, engine.ErrBusy)
return false
}
}
if err := s.eng.BeforeAction(r.Context(), reason); err != nil {
s.log.Warn("pre-action backup failed", "action", reason, "err", err)
writeErr(w, 500, err)
return false
}
return true
}