2024-10-01 23:58:54 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"regexp"
|
|
|
|
|
"slices"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/navidrome/navidrome/conf"
|
|
|
|
|
"github.com/navidrome/navidrome/log"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
backupPrefix = "navidrome_backup"
|
|
|
|
|
backupRegexString = backupPrefix + "_(.+)\\.db"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var backupRegex = regexp.MustCompile(backupRegexString)
|
|
|
|
|
|
|
|
|
|
const backupSuffixLayout = "2006.01.02_15.04.05"
|
|
|
|
|
|
|
|
|
|
func backupPath(t time.Time) string {
|
|
|
|
|
return filepath.Join(
|
refactor(conf): replace eager dir creation with lazy Dir type (#5495)
* feat(conf): add Dir type with lazy directory creation
Introduces the Dir type that wraps a directory path string and defers
os.MkdirAll until the first call to Path() or MustPath(), using sync.Once
to ensure the creation happens exactly once. Implements fmt.Stringer,
encoding.TextMarshaler, and encoding.TextUnmarshaler for config integration.
Includes Ginkgo/Gomega tests covering all methods and error paths.
* refactor(conf): replace eager dir creation with lazy Dir type
Change DataFolder, CacheFolder, Plugins.Folder, and Backup.Path from
string to Dir. Remove all os.MkdirAll calls from Load() so directories
are created lazily on first Path()/MustPath() call. Artwork folder
creation was already handled at point-of-use in image_upload.go.
Add SnapshotConfig() to conf package for safe test config save/restore
that avoids copying sync.Once inside Dir fields. Fix copy-lock vet
warning in nativeapi/config.go by marshalling pointer instead of value.
* refactor(conf): migrate tests and db init to lazy Dir type
Update all test files to use conf.NewDir() for Dir field assignments.
Ensure DataFolder is created lazily when the database is first opened
in db.Db(). Remove eager directory creation from conf.Load() tests.
* fix(conf): address review findings for Dir type
- Use os.ModePerm for DataFolder/CacheFolder (was 0700, should match
original behavior). Add NewDirWithPerm for PluginsFolder (0700).
- Use Path() instead of MustPath() in db.Prune() to avoid logFatal
from background cron job.
- Panic on marshal/unmarshal errors in SnapshotConfig (test helper).
- Clean up redundant String()/MustPath() calls in plugin manager.
- Remove dead code in dir_test.go.
Signed-off-by: Deluan <deluan@navidrome.org>
* fix(conf): add GoString to Dir for clean config dump output
Implement fmt.GoStringer on Dir so pretty.Sprintf shows the path
string instead of internal struct fields (sync.Once, perm, err).
Also add TODO comment to configtest about removing the indirection.
* fix(dir): improve error logging in MustPath method
Signed-off-by: Deluan <deluan@navidrome.org>
* refactor(tests): remove redundant tests for unwritable DataFolder and CacheFolder
Signed-off-by: Deluan <deluan@navidrome.org>
* fix(conf): address PR review feedback
- Ensure Plugins.Folder always uses 0700, even when user-configured
(previously only the derived default got restrictive permissions).
- Create LogFile parent directory before opening, so LogFile paths
inside a not-yet-created DataFolder work correctly.
---------
Signed-off-by: Deluan <deluan@navidrome.org>
2026-05-13 17:44:22 -03:00
|
|
|
conf.Server.Backup.Path.MustPath(),
|
2024-10-01 23:58:54 +00:00
|
|
|
fmt.Sprintf("%s_%s.db", backupPrefix, t.Format(backupSuffixLayout)),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-18 15:52:13 +02:00
|
|
|
func backupOrRestore(ctx context.Context, isBackup bool, path string) error {
|
feat(pglite): run Navidrome on the embedded PostgreSQL end to end
Replace the 131 SQLite migrations with a single goose migration that creates
the whole schema in PostgreSQL (the user table becomes user_account, json
columns become jsonb, seed rows for the default library and transcodings).
Port the repositories' SQL just far enough for Navidrome to start, scan,
log in and browse artists and albums: json_agg/jsonb_* for the SQLite JSON
functions, lower() for COLLATE NOCASE, ON CONFLICT DO NOTHING, coalesce on
annotations, stricter GROUP BY, explicit casts under the simple protocol,
sequential queries in library.RefreshStats. db/ is now Postgres-only, with
backup/restore stubbed and DevExternalScanner forced off.
Embed the PGlite module like go-taglib: pglite.wasi.gz is go:embed'ed,
decompressed in memory and compiled by wazero with an on-disk compilation
cache, so the module itself is never written to disk. The cluster is
created by initdb at runtime on the first start; initdb runs in a
throwaway wasm instance and the backend in a fresh one, otherwise the next
start cannot find a valid checkpoint. Since initdb creates only template1,
a short-lived backend then runs CREATE DATABASE navidrome and the real
backend starts on it. The build recipe and patches for the -O2, no-wizer
module live in db/pglite/build.
2026-09-04 20:41:52 -04:00
|
|
|
return errors.New("database backup and restore are not supported with the embedded PGlite database")
|
2024-10-01 23:58:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-18 15:52:13 +02:00
|
|
|
func Backup(ctx context.Context) (string, error) {
|
|
|
|
|
destPath := backupPath(time.Now())
|
2025-01-09 13:23:59 -05:00
|
|
|
log.Debug(ctx, "Creating backup", "path", destPath)
|
2024-11-18 15:52:13 +02:00
|
|
|
err := backupOrRestore(ctx, true, destPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return destPath, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Restore(ctx context.Context, path string) error {
|
2025-01-09 13:23:59 -05:00
|
|
|
log.Debug(ctx, "Restoring backup", "path", path)
|
2024-11-18 15:52:13 +02:00
|
|
|
return backupOrRestore(ctx, false, path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Prune(ctx context.Context) (int, error) {
|
refactor(conf): replace eager dir creation with lazy Dir type (#5495)
* feat(conf): add Dir type with lazy directory creation
Introduces the Dir type that wraps a directory path string and defers
os.MkdirAll until the first call to Path() or MustPath(), using sync.Once
to ensure the creation happens exactly once. Implements fmt.Stringer,
encoding.TextMarshaler, and encoding.TextUnmarshaler for config integration.
Includes Ginkgo/Gomega tests covering all methods and error paths.
* refactor(conf): replace eager dir creation with lazy Dir type
Change DataFolder, CacheFolder, Plugins.Folder, and Backup.Path from
string to Dir. Remove all os.MkdirAll calls from Load() so directories
are created lazily on first Path()/MustPath() call. Artwork folder
creation was already handled at point-of-use in image_upload.go.
Add SnapshotConfig() to conf package for safe test config save/restore
that avoids copying sync.Once inside Dir fields. Fix copy-lock vet
warning in nativeapi/config.go by marshalling pointer instead of value.
* refactor(conf): migrate tests and db init to lazy Dir type
Update all test files to use conf.NewDir() for Dir field assignments.
Ensure DataFolder is created lazily when the database is first opened
in db.Db(). Remove eager directory creation from conf.Load() tests.
* fix(conf): address review findings for Dir type
- Use os.ModePerm for DataFolder/CacheFolder (was 0700, should match
original behavior). Add NewDirWithPerm for PluginsFolder (0700).
- Use Path() instead of MustPath() in db.Prune() to avoid logFatal
from background cron job.
- Panic on marshal/unmarshal errors in SnapshotConfig (test helper).
- Clean up redundant String()/MustPath() calls in plugin manager.
- Remove dead code in dir_test.go.
Signed-off-by: Deluan <deluan@navidrome.org>
* fix(conf): add GoString to Dir for clean config dump output
Implement fmt.GoStringer on Dir so pretty.Sprintf shows the path
string instead of internal struct fields (sync.Once, perm, err).
Also add TODO comment to configtest about removing the indirection.
* fix(dir): improve error logging in MustPath method
Signed-off-by: Deluan <deluan@navidrome.org>
* refactor(tests): remove redundant tests for unwritable DataFolder and CacheFolder
Signed-off-by: Deluan <deluan@navidrome.org>
* fix(conf): address PR review feedback
- Ensure Plugins.Folder always uses 0700, even when user-configured
(previously only the derived default got restrictive permissions).
- Create LogFile parent directory before opening, so LogFile paths
inside a not-yet-created DataFolder work correctly.
---------
Signed-off-by: Deluan <deluan@navidrome.org>
2026-05-13 17:44:22 -03:00
|
|
|
backupDir, err := conf.Server.Backup.Path.Path()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, fmt.Errorf("backup directory not available: %w", err)
|
|
|
|
|
}
|
|
|
|
|
files, err := os.ReadDir(backupDir)
|
2024-10-01 23:58:54 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return 0, fmt.Errorf("unable to read database backup entries: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var backupTimes []time.Time
|
|
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
|
if !file.IsDir() {
|
|
|
|
|
submatch := backupRegex.FindStringSubmatch(file.Name())
|
|
|
|
|
if len(submatch) == 2 {
|
|
|
|
|
timestamp, err := time.Parse(backupSuffixLayout, submatch[1])
|
|
|
|
|
if err == nil {
|
|
|
|
|
backupTimes = append(backupTimes, timestamp)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(backupTimes) <= conf.Server.Backup.Count {
|
|
|
|
|
return 0, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
slices.SortFunc(backupTimes, func(a, b time.Time) int {
|
|
|
|
|
return b.Compare(a)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
pruneCount := 0
|
|
|
|
|
var errs []error
|
|
|
|
|
|
|
|
|
|
for _, timeToPrune := range backupTimes[conf.Server.Backup.Count:] {
|
|
|
|
|
log.Debug(ctx, "Pruning backup", "time", timeToPrune)
|
|
|
|
|
path := backupPath(timeToPrune)
|
|
|
|
|
err = os.Remove(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
errs = append(errs, err)
|
|
|
|
|
} else {
|
|
|
|
|
pruneCount++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
|
err = errors.Join(errs...)
|
|
|
|
|
log.Error(ctx, "Failed to delete one or more files", "errors", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pruneCount, err
|
|
|
|
|
}
|