Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: include separately mounted projects in system backups (#4144)
Kyle Mendell081e638de800
diff --git a/backend/cli/recovery/recovery.go b/backend/cli/recovery/recovery.goindex 7c98b3081..b9ca4fa32 100644--- a/backend/cli/recovery/recovery.go+++ b/backend/cli/recovery/recovery.go@@ -16,13 +16,14 @@ import ("github.com/getarcaneapp/arcane/backend/v2/cli/upgrade""github.com/getarcaneapp/arcane/backend/v2/internal/activity""github.com/getarcaneapp/arcane/backend/v2/internal/database"- recoverytypes "github.com/getarcaneapp/arcane/backend/v2/internal/recovery"+ "github.com/getarcaneapp/arcane/backend/v2/internal/settings""github.com/getarcaneapp/arcane/backend/v2/internal/systembackup"dockerutil "github.com/getarcaneapp/arcane/backend/v2/pkg/dockerutil""github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane"rusticruntime "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/rustic"activitytypes "github.com/getarcaneapp/arcane/types/v2/activity"backuptypes "github.com/getarcaneapp/arcane/types/v2/backup"+ recoverytypes "github.com/getarcaneapp/arcane/types/v2/recovery""github.com/libtnb/sqlite""github.com/moby/moby/api/types/container""github.com/moby/moby/api/types/mount"@@ -56,6 +57,9 @@ func runRestoreInternal(_ *cobra.Command, _ []string) error {return fmt.Errorf("decode recovery request: %w", err)}_ = os.Remove(requestPath)+ if len(request.Stages) == 0 {+ return errors.New("recovery request has no restore stages")+ }dockerClient, err := client.New(client.FromEnv)if err != nil {return fmt.Errorf("connect to Docker: %w", err)@@ -65,30 +69,59 @@ func runRestoreInternal(_ *cobra.Command, _ []string) error {if err != nil {return fmt.Errorf("inspect Arcane container: %w", err)}+ if _, err := dockerClient.ImageInspect(ctx, rusticImage); err != nil {+ reader, pullErr := dockerClient.ImagePull(ctx, rusticImage, client.ImagePullOptions{})+ if pullErr != nil {+ return fmt.Errorf("pull Arcane tools image for Rustic: %w", pullErr)+ }+ if pullErr = dockerutil.RenderJSONMessageStream(reader, nil); pullErr != nil {+ _ = reader.Close()+ return fmt.Errorf("pull Arcane tools image for Rustic: %w", pullErr)+ }+ _ = reader.Close()+ }if _, err := dockerClient.ContainerStop(ctx, request.ContainerID, client.ContainerStopOptions{Timeout: new(30)}); err != nil {return fmt.Errorf("stop Arcane container: %w", err)}- if err := restoreSnapshotInternal(ctx, dockerClient, request); err != nil {- _, _ = dockerClient.ContainerStart(ctx, request.ContainerID, client.ContainerStartOptions{})- return err+ restart := func() { _, _ = dockerClient.ContainerStart(ctx, request.ContainerID, client.ContainerStartOptions{}) }+ if err := runStagesInternal(ctx, dockerClient, request, request.Stages); err != nil {+ // Arcane restarts only when the rollback succeeds; otherwise it stays+ // stopped rather than running with mismatched data and projects.+ err = fmt.Errorf("rustic system restore failed: %w", err)+ if len(request.RollbackStages) == 0 {+ return errors.Combine(err, errors.New("no pre-restore system backup is available for rollback; Arcane was left stopped"))+ }+ if rollbackErr := runStagesInternal(context.WithoutCancel(ctx), dockerClient, request, request.RollbackStages); rollbackErr != nil {+ return errors.Combine(err, fmt.Errorf("restoring the pre-restore system backup failed; Arcane was left stopped: %w", rollbackErr))+ }+ restart()+ return fmt.Errorf("%w; the pre-restore system backup was restored", err)+ }+ if !request.ProjectsIncluded {+ slog.Warn("the restored system backup did not include the projects directory; the current projects directory was left untouched")}manifestData, err := os.ReadFile("/app/data/.arcane-recovery.json")if err != nil {- _, _ = dockerClient.ContainerStart(ctx, request.ContainerID, client.ContainerStartOptions{})+ restart()return fmt.Errorf("read restored recovery manifest: %w", err)}var manifest recoverytypes.Manifestif err := json.Unmarshal(manifestData, &manifest); err != nil {- _, _ = dockerClient.ContainerStart(ctx, request.ContainerID, client.ContainerStartOptions{})+ restart()return fmt.Errorf("decode restored recovery manifest: %w", err)}_ = os.Remove("/app/data/.arcane-recovery.json")- if manifest.FormatVersion != 1 || len(manifest.Environment) == 0 {- _, _ = dockerClient.ContainerStart(ctx, request.ContainerID, client.ContainerStartOptions{})+ if (manifest.FormatVersion != 1 && manifest.FormatVersion != recoverytypes.ManifestFormatVersion) || len(manifest.Environment) == 0 {+ restart()return errors.New("unsupported or incomplete Arcane recovery manifest")}+ // Projects were restored into the current directory, so the recovered+ // configuration must keep pointing there rather than at the backup-time path.+ if request.ProjectsSetting != "" {+ manifest.Environment["PROJECTS_DIRECTORY"] = request.ProjectsSetting+ }if err := finalizeRestoredBackupInternal(ctx, manifest.Environment["DATABASE_URL"], manifest.BackupID, manifest.ActivityID, request); err != nil {- _, _ = dockerClient.ContainerStart(ctx, request.ContainerID, client.ContainerStartOptions{})+ restart()return fmt.Errorf("finalize restored system backup: %w", err)}if err := upgrade.UpgradeContainer(ctx, dockerClient, inspect.Container, request.ContainerImage, manifest.Environment); err != nil {@@ -121,6 +154,15 @@ func finalizeRestoredBackupInternal(ctx context.Context, databaseURL, manifestBaif err := preserveSafetyBackupInternal(db, request.SafetyBackup); err != nil {return err}+ // Keep the restored database pointing at the directory projects were restored into.+ if value := strings.TrimSpace(request.ProjectsSetting); value != "" {+ err := db.Model(&settings.SettingVariable{}).+ Clauses(clause.OnConflict{Columns: []clause.Column{{Name: "key"}}, DoUpdates: clause.Assignments(map[string]any{"value": value})}).+ Create(map[string]any{"key": "projectsDirectory", "value": value}).Error+ if err != nil {+ return err+ }+ }return finalizeRestoredActivityInternal(db, manifestActivityID)}@@ -212,30 +254,20 @@ func finalizeRestoredActivityInternal(db *gorm.DB, activityID string) error {}).Error}-func restoreSnapshotInternal(ctx context.Context, dockerClient *client.Client, request recoverytypes.RestoreRequest) error {- if _, err := dockerClient.ImageInspect(ctx, rusticImage); err != nil {- reader, pullErr := dockerClient.ImagePull(ctx, rusticImage, client.ImagePullOptions{})- if pullErr != nil {- return fmt.Errorf("pull Arcane tools image for Rustic: %w", pullErr)+// runStagesInternal restores each stage in order, replacing the target's+// contents with the snapshot path.+func runStagesInternal(ctx context.Context, dockerClient *client.Client, request recoverytypes.RestoreRequest, stages []recoverytypes.RestoreStage) error {+ for _, stage := range stages {+ if len(stage.Target.Mounts) == 0 || strings.TrimSpace(stage.Target.Path) == "" {+ return fmt.Errorf("restore stage for %s has no destination", stage.SourcePath)}- if pullErr = dockerutil.RenderJSONMessageStream(reader, nil); pullErr != nil {- _ = reader.Close()- return fmt.Errorf("pull Arcane tools image for Rustic: %w", pullErr)+ mounts := append([]mount.Mount{}, stage.Repository.Mounts...)+ mounts = append(mounts, stage.Target.Mounts...)+ command := []string{"restore", "--delete", stage.SnapshotID + ":" + stage.SourcePath, stage.Target.Path}+ if _, err := rusticruntime.Run(ctx, dockerClient, request.RecoveryKey, command, stage.Repository.Environment, mounts, container.NetworkMode(request.NetworkMode)); err != nil {+ slog.Error("Rustic system restore stage failed", "source", stage.SourcePath, "error", err)+ return fmt.Errorf("restore %s: %w", stage.SourcePath, err)}- _ = reader.Close()- }- mounts := append([]mount.Mount{}, request.RepositoryMounts...)- request.AppDataMount.Target = "/restore"- request.AppDataMount.ReadOnly = false- mounts = append(mounts, request.AppDataMount)- snapshotPath := strings.TrimSpace(request.SnapshotPath)- if snapshotPath == "" {- snapshotPath = "/"- }- command := []string{"restore", "--delete", request.SnapshotID + ":" + snapshotPath, "/restore"}- if _, err := rusticruntime.Run(ctx, dockerClient, request.RecoveryKey, command, request.RepositoryEnvironment, mounts, container.NetworkMode(request.NetworkMode)); err != nil {- slog.Error("Rustic system restore failed", "error", err)- return fmt.Errorf("rustic system restore failed: %w", err)}return nil}diff --git a/backend/cli/recovery/recovery_test.go b/backend/cli/recovery/recovery_test.goindex ca9756524..7a12f31d7 100644--- a/backend/cli/recovery/recovery_test.go+++ b/backend/cli/recovery/recovery_test.go@@ -8,9 +8,10 @@ import ("github.com/getarcaneapp/arcane/backend/v2/internal/activity""github.com/getarcaneapp/arcane/backend/v2/internal/database"- recoverytypes "github.com/getarcaneapp/arcane/backend/v2/internal/recovery"+ "github.com/getarcaneapp/arcane/backend/v2/internal/settings""github.com/getarcaneapp/arcane/backend/v2/internal/systembackup"activitytypes "github.com/getarcaneapp/arcane/types/v2/activity"+ recoverytypes "github.com/getarcaneapp/arcane/types/v2/recovery""github.com/libtnb/sqlite""github.com/stretchr/testify/require""gorm.io/gorm"@@ -23,7 +24,8 @@ func newRestoredDatabaseForTestInternal(t *testing.T, runIDs ...string) (string,require.NoError(t, err)db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})require.NoError(t, err)- require.NoError(t, db.AutoMigrate(&systembackup.SystemBackupRun{}, &activity.Activity{}))+ require.NoError(t, db.AutoMigrate(&systembackup.SystemBackupRun{}, &activity.Activity{}, &settings.SettingVariable{}))+ require.NoError(t, db.Create(&settings.SettingVariable{Key: "projectsDirectory", Value: "/app/data/projects"}).Error)for i, id := range runIDs {require.NoError(t, db.Create(&systembackup.SystemBackupRun{ID: id, CreatedAt: time.Date(2026, 1, 1+i, 0, 0, 0, 0, time.UTC),@@ -47,6 +49,7 @@ func TestFinalizeRestoredBackupInternal(t *testing.T) {require.NoError(t, finalizeRestoredBackupInternal(context.Background(), databaseURL, "selected", "activity-1", recoverytypes.RestoreRequest{BackupID: "request-id", RemoteSnapshotID: "snapshot-1", S3DestinationID: "destination-1", Size: 1234,+ ProjectsSetting: "/srv/projects",SafetyBackup: &recoverytypes.SafetyBackup{ID: "safety", LocalSnapshotID: "safety-snapshot", Size: 4321,CreatedAt: time.Date(2026, 1, 3, 0, 0, 0, 0, time.UTC),@@ -71,6 +74,9 @@ func TestFinalizeRestoredBackupInternal(t *testing.T) {var entry activity.Activityrequire.NoError(t, db.First(&entry, "id = ?", "activity-1").Error)require.Equal(t, activitytypes.StatusSuccess, entry.Status)+ var projectsSetting settings.SettingVariable+ require.NoError(t, db.First(&projectsSetting, "key = ?", "projectsDirectory").Error)+ require.Equal(t, "/srv/projects", projectsSetting.Value)}func TestFinalizeRestoredBackupInternalFallsBackForLegacyManifest(t *testing.T) {@@ -87,3 +93,8 @@ func TestFinalizeRestoredBackupInternalFallsBackForLegacyManifest(t *testing.T)require.NoError(t, db.First(&entry, "id = ?", "activity-1").Error)require.Equal(t, activitytypes.StatusSuccess, entry.Status)}++func TestRunStagesInternalRejectsStagesWithoutDestination(t *testing.T) {+ err := runStagesInternal(context.Background(), nil, recoverytypes.RestoreRequest{}, []recoverytypes.RestoreStage{{SourcePath: "/data"}})+ require.ErrorContains(t, err, "restore stage for /data has no destination")+}diff --git a/backend/internal/backup/engine.go b/backend/internal/backup/engine.goindex e713dbb63..8b233e5c1 100644--- a/backend/internal/backup/engine.go+++ b/backend/internal/backup/engine.go@@ -70,6 +70,37 @@ type RestoreOptions struct {// DestinationPath overrides the restore target path inside the helper// container; the target mount's path is used when empty.DestinationPath string+ // ExtraMounts are attached alongside the target, e.g. mounts nested inside it.+ ExtraMounts []mount.Mount+}++// CreateSnapshotInput describes what one snapshot captures: the mounts to+// attach to the helper and the helper paths to back up. AsPath rewrites the+// snapshot path of a single source and cannot combine with several.+type CreateSnapshotInput struct {+ Mounts []mount.Mount+ Sources []string+ AsPath string+}++// RootSnapshotInput captures one mount with its contents at the snapshot root.+func RootSnapshotInput(source mount.Mount) CreateSnapshotInput {+ return CreateSnapshotInput{Mounts: []mount.Mount{source}, Sources: []string{source.Target}, AsPath: "/"}+}++func snapshotCommandInternal(label string, input CreateSnapshotInput) ([]string, error) {+ if len(input.Sources) == 0 {+ return nil, errors.New("at least one snapshot source is required")+ }+ if input.AsPath != "" && len(input.Sources) > 1 {+ return nil, errors.New("a snapshot path rewrite requires a single source")+ }+ command := []string{"backup", "--init", "--json", "--host", "arcane", "--label", label}+ if input.AsPath != "" {+ command = append(command, "--as-path", input.AsPath)+ }+ command = append(command, "--")+ return append(command, input.Sources...), nil}// Engine executes typed Rustic operations through the official Rustic image.@@ -126,13 +157,14 @@ func (e *Engine) Stop(ctx context.Context) error {return stopErr}-// CreateSnapshot backs the source mount up into the repository and returns the-// created snapshot. The repository is initialized on first use.-func (e *Engine) CreateSnapshot(ctx context.Context, dockerClient *client.Client, repository Repository, password, label string, source mount.Mount) (Snapshot, error) {- output, err := e.runInternal(ctx, dockerClient, repository, password,- []string{"backup", "--init", "--json", "--as-path", "/", "--host", "arcane", "--label", label, "--", source.Target},- source,- )+// CreateSnapshot backs the input's sources up into the repository as one+// snapshot. The repository is initialized on first use.+func (e *Engine) CreateSnapshot(ctx context.Context, dockerClient *client.Client, repository Repository, password, label string, input CreateSnapshotInput) (Snapshot, error) {+ command, err := snapshotCommandInternal(label, input)+ if err != nil {+ return Snapshot{}, err+ }+ output, err := e.runInternal(ctx, dockerClient, repository, password, command, input.Mounts...)if err != nil {return Snapshot{}, err}@@ -161,7 +193,8 @@ func (e *Engine) RestoreSnapshot(ctx context.Context, dockerClient *client.Cliendestination = target.Target}command = append(command, "--", source, destination)- _, err := e.runInternal(ctx, dockerClient, repository, password, command, target)+ mounts := append([]mount.Mount{target}, options.ExtraMounts...)+ _, err := e.runInternal(ctx, dockerClient, repository, password, command, mounts...)return err}@@ -328,7 +361,7 @@ func (e *Engine) Replicate(ctx context.Context, dockerClient *client.Client, froreturn Snapshot{}, fmt.Errorf("failed to load Rustic snapshot for replication: %w", err)}copyMount.ReadOnly = true- snapshot, err := e.CreateSnapshot(ctx, dockerClient, to, password, label, copyMount)+ snapshot, err := e.CreateSnapshot(ctx, dockerClient, to, password, label, RootSnapshotInput(copyMount))if err != nil {return Snapshot{}, fmt.Errorf("failed to replicate Rustic snapshot: %w", err)}diff --git a/backend/internal/backup/engine_test.go b/backend/internal/backup/engine_test.goindex 1a4390067..dbb3fa59a 100644--- a/backend/internal/backup/engine_test.go+++ b/backend/internal/backup/engine_test.go@@ -5,6 +5,7 @@ import ("github.com/getarcaneapp/arcane/backend/v2/internal/database""github.com/libtnb/sqlite"+ "github.com/moby/moby/api/types/mount""github.com/stretchr/testify/require""go.getarcane.app/sys/crypto""gorm.io/gorm"@@ -108,3 +109,18 @@ func TestRecoveryKeyValidation(t *testing.T) {require.NoError(t, err)require.NoError(t, ValidateRecoveryKey(key))}++func TestSnapshotCommandInternal(t *testing.T) {+ single, err := snapshotCommandInternal("volume", RootSnapshotInput(mount.Mount{Type: mount.TypeVolume, Source: "data", Target: "/volume"}))+ require.NoError(t, err)+ require.Equal(t, []string{"backup", "--init", "--json", "--host", "arcane", "--label", "volume", "--as-path", "/", "--", "/volume"}, single)++ multi, err := snapshotCommandInternal("arcane-system-recovery", CreateSnapshotInput{Sources: []string{"/data", "/projects"}})+ require.NoError(t, err)+ require.Equal(t, []string{"backup", "--init", "--json", "--host", "arcane", "--label", "arcane-system-recovery", "--", "/data", "/projects"}, multi)++ _, err = snapshotCommandInternal("x", CreateSnapshotInput{Sources: []string{"/data", "/projects"}, AsPath: "/"})+ require.ErrorContains(t, err, "single source")+ _, err = snapshotCommandInternal("x", CreateSnapshotInput{})+ require.ErrorContains(t, err, "at least one snapshot source")+}diff --git a/backend/internal/recovery/request.go b/backend/internal/recovery/request.godeleted file mode 100644index 0b3905940..000000000--- a/backend/internal/recovery/request.go+++ /dev/null@@ -1,44 +0,0 @@-package recovery--import (- "time"-- "github.com/moby/moby/api/types/mount"-)--type RestoreRequest struct {- BackupID string `json:"backupId"`- ContainerID string `json:"containerId"`- ContainerImage string `json:"containerImage"`- SnapshotID string `json:"snapshotId"`- SnapshotPath string `json:"snapshotPath"`- LocalSnapshotID string `json:"localSnapshotId,omitempty"`- RemoteSnapshotID string `json:"remoteSnapshotId,omitempty"`- S3DestinationID string `json:"s3DestinationId,omitempty"`- Size int64 `json:"size,omitempty"`- SafetyBackup *SafetyBackup `json:"safetyBackup,omitempty"`- RecoveryKey string `json:"recoveryKey"`- RepositoryEnvironment []string `json:"repositoryEnvironment"`- RepositoryMounts []mount.Mount `json:"repositoryMounts"`- AppDataMount mount.Mount `json:"appDataMount"`- // NetworkMode attaches the detached Rustic restore container to one of- // Arcane's networks so network-local S3 endpoints stay reachable while- // Arcane itself is stopped.- NetworkMode string `json:"networkMode,omitempty"`-}--type SafetyBackup struct {- ID string `json:"id"`- LocalSnapshotID string `json:"localSnapshotId"`- Size int64 `json:"size"`- CreatedAt time.Time `json:"createdAt"`-}--type Manifest struct {- FormatVersion int `json:"formatVersion"`- ArcaneVersion string `json:"arcaneVersion"`- BackupID string `json:"backupId,omitempty"`- ActivityID string `json:"activityId,omitempty"`- CreatedAt time.Time `json:"createdAt"`- Environment map[string]string `json:"environment"`-}diff --git a/backend/internal/systembackup/service.go b/backend/internal/systembackup/service.goindex a1856648a..514601b7f 100644--- a/backend/internal/systembackup/service.go+++ b/backend/internal/systembackup/service.go@@ -15,8 +15,6 @@ import ("strings""time"- "github.com/getarcaneapp/arcane/backend/v2/pkg/scheduler/jobcontext"-"emperror.dev/errors""github.com/getarcaneapp/arcane/backend/v2/internal/activity"@@ -26,7 +24,6 @@ import ("github.com/getarcaneapp/arcane/backend/v2/internal/config""github.com/getarcaneapp/arcane/backend/v2/internal/database""github.com/getarcaneapp/arcane/backend/v2/internal/docker"- recoverytypes "github.com/getarcaneapp/arcane/backend/v2/internal/recovery"s3domain "github.com/getarcaneapp/arcane/backend/v2/internal/s3""github.com/getarcaneapp/arcane/backend/v2/internal/settings""github.com/getarcaneapp/arcane/backend/v2/internal/system"@@ -37,10 +34,12 @@ import ("github.com/getarcaneapp/arcane/backend/v2/pkg/pagination""github.com/getarcaneapp/arcane/backend/v2/pkg/projects""github.com/getarcaneapp/arcane/backend/v2/pkg/scheduler/entityjobs"+ "github.com/getarcaneapp/arcane/backend/v2/pkg/scheduler/jobcontext""github.com/getarcaneapp/arcane/backend/v2/pkg/utils""github.com/getarcaneapp/arcane/backend/v2/pkg/utils/backupbrowser"activitytypes "github.com/getarcaneapp/arcane/types/v2/activity"backuptypes "github.com/getarcaneapp/arcane/types/v2/backup"+ recoverytypes "github.com/getarcaneapp/arcane/types/v2/recovery"schedulertypes "github.com/getarcaneapp/arcane/types/v2/scheduler""github.com/google/uuid"containertypes "github.com/moby/moby/api/types/container"@@ -141,16 +140,15 @@ func (s *SystemBackupService) recoveryEnvironmentInternal(ctx context.Context) m}}visit(reflect.ValueOf(s.config))- if s.settingsService != nil {- result["PROJECTS_DIRECTORY"] = s.settingsService.GetStringSetting(ctx, "projectsDirectory", result["PROJECTS_DIRECTORY"])+ if value := s.projectsSettingInternal(ctx); value != "" {+ result["PROJECTS_DIRECTORY"] = value}return result}-// dataDirectoryInternal resolves the directory holding the SQLite database:-// /app/data in the container, the local data directory in host development.-// The recovery manifest must live inside it so every snapshot includes it.-func (s *SystemBackupService) dataDirectoryInternal() (string, error) {+// databaseFileInternal resolves the SQLite database file: under /app/data in+// the container, the local data directory in host development.+func (s *SystemBackupService) databaseFileInternal() (string, error) {databasePath, err := utils.SQLitePathFromDSN(s.config.DatabaseURL)if err != nil {return "", fmt.Errorf("failed to parse database URL: %w", err)@@ -162,54 +160,36 @@ func (s *SystemBackupService) dataDirectoryInternal() (string, error) {if err != nil {return "", fmt.Errorf("failed to resolve the Arcane data directory: %w", err)}- return filepath.Dir(absolute), nil+ return absolute, nil}-func (s *SystemBackupService) manifestPathInternal() (string, error) {- directory, err := s.dataDirectoryInternal()+// dataDirectoryInternal resolves the directory holding the SQLite database.+// The recovery manifest must live inside it so every snapshot includes it.+func (s *SystemBackupService) dataDirectoryInternal() (string, error) {+ databaseFile, err := s.databaseFileInternal()if err != nil {return "", err}- return filepath.Join(directory, systemRecoveryManifestName), nil+ return filepath.Dir(databaseFile), nil}-func (s *SystemBackupService) writeManifestInternal(ctx context.Context, backupID string) error {- manifest := recoverytypes.Manifest{FormatVersion: 1, ArcaneVersion: config.Version, BackupID: backupID, ActivityID: activitylib.IDFromContext(ctx), CreatedAt: time.Now().UTC(), Environment: s.recoveryEnvironmentInternal(ctx)}+func (s *SystemBackupService) writeManifestInternal(ctx context.Context, backupID string, layout backupSourceLayoutInternal) error {+ manifest := recoverytypes.Manifest{+ FormatVersion: recoverytypes.ManifestFormatVersion, ArcaneVersion: config.Version, BackupID: backupID,+ ActivityID: activitylib.IDFromContext(ctx), CreatedAt: time.Now().UTC(),+ DataPath: snapshotDataPath, ProjectsPath: layout.projectsPath, DatabasePath: layout.databaseName,+ Environment: s.recoveryEnvironmentInternal(ctx),+ }data, err := json.Marshal(manifest, jsontext.WithIndent(" "))if err != nil {return fmt.Errorf("failed to encode recovery manifest: %w", err)}- manifestPath, err := s.manifestPathInternal()- if err != nil {- return err- }- if err := os.WriteFile(manifestPath, data, 0o600); err != nil {+ if err := os.WriteFile(layout.manifestPathInternal(), data, 0o600); err != nil {return fmt.Errorf("failed to write recovery manifest: %w", err)}return nil}-func (s *SystemBackupService) appDataMountInternal(ctx context.Context, dockerClient *client.Client, target string, readOnly bool) (mount.Mount, error) {- if _, containerErr := cgroup.CurrentContainerID(); containerErr != nil {- // Host development: bind-mount the local data directory into the- // Rustic helper directly instead of re-sharing the container's mount.- directory, dirErr := s.dataDirectoryInternal()- if dirErr != nil {- return mount.Mount{}, dirErr- }- return mount.Mount{Type: mount.TypeBind, Source: directory, Target: target, ReadOnly: readOnly}, nil- }- dataMount, err := dockerutil.MountForCurrentContainerSubpath(ctx, dockerClient, "/app/data", target)- if err != nil {- return mount.Mount{}, fmt.Errorf("failed to inspect Arcane data mount: %w", err)- }- if dataMount == nil {- return mount.Mount{}, errors.New("arcane system backups require /app/data to be mounted into the Arcane container")- }- dataMount.ReadOnly = readOnly- return *dataMount, nil-}-func (s *SystemBackupService) localRepositoryInternal(ctx context.Context, dockerClient *client.Client, readOnly bool) (backup.Repository, error) {storage, err := s.volumeService.BackupStorageMount(ctx, dockerClient, "/repository", readOnly)if err != nil {@@ -233,26 +213,8 @@ func (s *SystemBackupService) remoteRepositoryInternal(ctx context.Context, dest}, nil}-func (s *SystemBackupService) createSnapshotInternal(ctx context.Context, dockerClient *client.Client, repository backup.Repository, recoveryKey string) (backup.Snapshot, error) {- dataMount, err := s.appDataMountInternal(ctx, dockerClient, "/arcane-data", true)- if err != nil {- return backup.Snapshot{}, err- }- return s.engine.CreateSnapshot(ctx, dockerClient, repository, recoveryKey, "arcane-system-recovery", dataMount)-}--func systemSnapshotPathFromFilesInternal(files []string) (string, error) {- for _, file := range files {- if strings.TrimPrefix(file, "/") == ".arcane-recovery.json" {- return "/", nil- }- }- for _, file := range files {- if strings.TrimPrefix(file, "/") == "app/data/.arcane-recovery.json" {- return "/app/data", nil- }- }- return "", errors.New("system recovery snapshot does not contain an Arcane recovery manifest")+func (s *SystemBackupService) createSnapshotInternal(ctx context.Context, dockerClient *client.Client, repository backup.Repository, recoveryKey string, layout backupSourceLayoutInternal) (backup.Snapshot, error) {+ return s.engine.CreateSnapshot(ctx, dockerClient, repository, recoveryKey, "arcane-system-recovery", backup.CreateSnapshotInput{Mounts: layout.mounts, Sources: layout.sources})}func (s *SystemBackupService) recoveryKeyInternal(ctx context.Context, supplied string) (string, error) {@@ -457,14 +419,14 @@ func (s *SystemBackupService) executeBackupInternal(ctx context.Context, prepare// snapshotWithDatabaseLockInternal takes a snapshot while the SQLite database// is checkpointed and exclusively locked, and releases the lock before returning.func (s *SystemBackupService) snapshotWithDatabaseLockInternal(ctx context.Context, dockerClient *client.Client, repository backup.Repository, recoveryKey, backupID string) (backup.Snapshot, error) {- if err := s.writeManifestInternal(ctx, backupID); err != nil {+ layout, err := s.backupSourceLayoutInternal(ctx, dockerClient)+ if err != nil {return backup.Snapshot{}, err}- defer func() {- if manifestPath, pathErr := s.manifestPathInternal(); pathErr == nil {- _ = os.Remove(manifestPath)- }- }()+ if err := s.writeManifestInternal(ctx, backupID, layout); err != nil {+ return backup.Snapshot{}, err+ }+ defer func() { _ = os.Remove(layout.manifestPathInternal()) }()sqlDB, err := s.db.SQLDB()if err != nil {return backup.Snapshot{}, err@@ -481,7 +443,7 @@ func (s *SystemBackupService) snapshotWithDatabaseLockInternal(ctx context.Contereturn backup.Snapshot{}, fmt.Errorf("failed to lock Arcane database for backup: %w", err)}defer func() { _, _ = connection.ExecContext(context.WithoutCancel(ctx), "ROLLBACK") }()- snapshot, err := s.createSnapshotInternal(ctx, dockerClient, repository, recoveryKey)+ snapshot, err := s.createSnapshotInternal(ctx, dockerClient, repository, recoveryKey, layout)if err != nil {return backup.Snapshot{}, fmt.Errorf("failed to create system recovery snapshot: %w", err)}@@ -531,20 +493,22 @@ type systemBackupSnapshotLocationInternal struct {snapshotID string}+func (location systemBackupSnapshotLocationInternal) restoreRepositoryInternal() recoverytypes.RestoreRepository {+ return recoverytypes.RestoreRepository{Environment: location.repository.Environment, Mounts: location.repository.Mounts}+}+type systemBackupSnapshotInternal struct {systemBackupSnapshotLocationInternal- files []string- entries []backuptypes.BackupFileEntry- snapshotPath string- projectsPath string+ layout snapshotLayoutInternal+ entries []backuptypes.BackupFileEntry}type systemBackupSafetySnapshotInternal struct {systemBackupSnapshotLocationInternal- snapshotPath string- dataPaths map[string]struct{}+ layout snapshotLayoutInternal+ paths map[string]struct{}}// systemBackupSnapshotSessionInternal carries the Docker client and resolved@@ -592,15 +556,11 @@ func (s *SystemBackupService) backupSnapshotLocationsInternal(ctx context.Contex}func (session systemBackupSnapshotSessionInternal) inspectReadableSnapshotInternal(ctx context.Context, location systemBackupSnapshotLocationInternal) (systemBackupSnapshotInternal, error) {- files, err := session.service.engine.ListSnapshotFiles(ctx, session.dockerClient, location.repository, session.recoveryKey, location.snapshotID, "", true)- if err != nil {- return systemBackupSnapshotInternal{}, fmt.Errorf("inspect %s system recovery snapshot: %w", location.name, err)- }- snapshotPath, err := systemSnapshotPathFromFilesInternal(files)+ layout, err := session.readSnapshotLayoutInternal(ctx, location)if err != nil {return systemBackupSnapshotInternal{}, fmt.Errorf("inspect %s system recovery snapshot: %w", location.name, err)}- return systemBackupSnapshotInternal{systemBackupSnapshotLocationInternal: location, files: files, snapshotPath: snapshotPath}, nil+ return systemBackupSnapshotInternal{systemBackupSnapshotLocationInternal: location, layout: layout}, nil}func (session systemBackupSnapshotSessionInternal) firstReadableSnapshotInternal(ctx context.Context, locations []systemBackupSnapshotLocationInternal, setupErr error) (systemBackupSnapshotInternal, error) {@@ -618,124 +578,30 @@ func (session systemBackupSnapshotSessionInternal) firstReadableSnapshotInternalreturn systemBackupSnapshotInternal{}, fmt.Errorf("failed to open system recovery snapshot: %w", inspectErr)}-func recoveryManifestDatabasePathInternal(manifest recoverytypes.Manifest) (string, error) {- databaseURL := strings.TrimSpace(manifest.Environment["DATABASE_URL"])- if databaseURL == "" {- return "", errors.New("system recovery manifest does not record the database path")- }- databasePath, err := utils.SQLitePathFromDSN(databaseURL)- if err != nil {- return "", fmt.Errorf("parse database URL from system recovery manifest: %w", err)- }- databasePath = strings.ReplaceAll(strings.TrimSpace(databasePath), `\`, "/")- if databasePath == "" {- return "", errors.New("system recovery manifest contains an empty database path")- }- return path.Clean(databasePath), nil-}--func portablePathIsAbsInternal(filePath string) bool {- return path.IsAbs(filePath) || (len(filePath) >= 3 && filePath[1] == ':' && filePath[2] == '/')-}--func projectsRelativePathFromManifestInternal(manifest recoverytypes.Manifest) (string, error) {- configured := strings.TrimSpace(manifest.Environment["PROJECTS_DIRECTORY"])- if configured == "" {- return "", errors.New("system recovery manifest does not record the projects directory")- }- if strings.HasPrefix(configured, "/") {- if separator := strings.Index(configured, ":"); separator > 0 {- configured = configured[:separator]- }- }- projectsPath := path.Clean(strings.ReplaceAll(configured, `\`, "/"))- databasePath, err := recoveryManifestDatabasePathInternal(manifest)- if err != nil {- return "", err- }- dataPath := path.Dir(databasePath)- dataAbsolute := portablePathIsAbsInternal(dataPath)- projectsAbsolute := portablePathIsAbsInternal(projectsPath)- if !dataAbsolute && projectsAbsolute {- relativeDataPath := strings.Trim(dataPath, "/")- if relativeDataPath == "." {- return "", errors.New("cannot relate the absolute projects directory to the relative database path in the system recovery manifest")- }- marker := "/" + relativeDataPath- index := strings.LastIndex(projectsPath, marker)- if index < 0 || (len(projectsPath) > index+len(marker) && projectsPath[index+len(marker)] != '/') {- return "", errors.New("cannot relate the projects directory to the database path in the system recovery manifest")- }- dataPath = projectsPath[:index+len(marker)]- dataAbsolute = true- } else if dataAbsolute && !projectsAbsolute {- projectsPath = path.Join(path.Dir(dataPath), projectsPath)- projectsAbsolute = true- }- if dataAbsolute != projectsAbsolute {- return "", errors.New("projects and database paths in the system recovery manifest use incompatible roots")- }- if projectsPath == dataPath {- return "", nil- }- if !utils.FilePathMatches(projectsPath, dataPath) {- return "", errors.New("the backup-time projects directory is outside Arcane's system backup data")- }- return strings.TrimPrefix(projectsPath, dataPath+"/"), nil-}-func (session systemBackupSnapshotSessionInternal) inspectProjectSnapshotInternal(ctx context.Context, location systemBackupSnapshotLocationInternal) (systemBackupSnapshotInternal, error) {- snapshot, databasePath, err := session.inspectProjectManifestInternal(ctx, location)+ snapshot, err := session.inspectProjectManifestInternal(ctx, location)if err != nil {return systemBackupSnapshotInternal{}, err}- root := path.Join(snapshot.snapshotPath, snapshot.projectsPath)- listed, err := session.service.engine.ListSnapshotFiles(ctx, session.dockerClient, location.repository, session.recoveryKey, location.snapshotID, root+"/", true)+ listed, err := session.service.engine.ListSnapshotFiles(ctx, session.dockerClient, location.repository, session.recoveryKey, location.snapshotID, snapshot.layout.projectsPath+"/", true)if err != nil {return systemBackupSnapshotInternal{}, fmt.Errorf("inspect %s project files: %w", location.name, err)}- snapshot.entries = projectEntriesFromSnapshotInternal(listed, snapshot.snapshotPath, snapshot.projectsPath, databasePath, "", true)+ snapshot.entries = projectEntriesFromSnapshotInternal(listed, snapshot.layout, "", true)return snapshot, nil}-func (session systemBackupSnapshotSessionInternal) inspectProjectManifestInternal(ctx context.Context, location systemBackupSnapshotLocationInternal) (systemBackupSnapshotInternal, string, error) {- var manifestData string- var snapshotPath string- var readErr error- found := false- for _, candidate := range []string{"/", "/app/data"} {- data, err := session.service.engine.ReadSnapshotTextFile(ctx, session.dockerClient, location.repository, session.recoveryKey, location.snapshotID, path.Join(candidate, systemRecoveryManifestName))- if err != nil {- readErr = errors.Combine(readErr, err)- continue- }- manifestData, snapshotPath = data, candidate- found = true- break- }- if !found {- return systemBackupSnapshotInternal{}, "", fmt.Errorf("read %s system recovery manifest: %w", location.name, readErr)- }- var manifest recoverytypes.Manifest- if err := json.Unmarshal([]byte(manifestData), &manifest); err != nil {- return systemBackupSnapshotInternal{}, "", fmt.Errorf("decode %s system recovery manifest: %w", location.name, err)- }- if manifest.FormatVersion != 1 {- return systemBackupSnapshotInternal{}, "", fmt.Errorf("%s system recovery manifest uses unsupported format %d", location.name, manifest.FormatVersion)- }- projectsPath, err := projectsRelativePathFromManifestInternal(manifest)+// inspectProjectManifestInternal opens a snapshot for project browsing;+// backups that omitted projects are rejected with an actionable error.+func (session systemBackupSnapshotSessionInternal) inspectProjectManifestInternal(ctx context.Context, location systemBackupSnapshotLocationInternal) (systemBackupSnapshotInternal, error) {+ snapshot, err := session.inspectReadableSnapshotInternal(ctx, location)if err != nil {- return systemBackupSnapshotInternal{}, "", fmt.Errorf("resolve projects directory from %s system recovery manifest: %w", location.name, err)+ return systemBackupSnapshotInternal{}, err}- fullDatabasePath, err := recoveryManifestDatabasePathInternal(manifest)- if err != nil {- return systemBackupSnapshotInternal{}, "", fmt.Errorf("resolve database path from %s system recovery manifest: %w", location.name, err)+ if !snapshot.layout.projectsIncludedInternal() {+ return systemBackupSnapshotInternal{}, errProjectsNotInBackupInternal}- return systemBackupSnapshotInternal{- systemBackupSnapshotLocationInternal: location,- snapshotPath: snapshotPath,- projectsPath: projectsPath,- }, path.Base(fullDatabasePath), nil+ return snapshot, nil}func (session systemBackupSnapshotSessionInternal) availableProjectSnapshotsInternal(ctx context.Context, locations []systemBackupSnapshotLocationInternal, setupErr error, firstOnly bool) ([]systemBackupSnapshotInternal, error) {@@ -784,20 +650,15 @@ func protectedSystemBackupPathInternal(candidate, databasePath string) bool {return candidate == databasePath || candidate == databasePath+"-wal" || candidate == databasePath+"-shm" || candidate == databasePath+"-journal"}-func projectEntriesFromSnapshotInternal(files []string, snapshotPath, projectsPath, databasePath, browsePath string, recursive bool) []backuptypes.BackupFileEntry {+// projectEntriesFromSnapshotInternal maps a snapshot listing to entries+// relative to the projects root, dropping protected Arcane data files.+func projectEntriesFromSnapshotInternal(files []string, layout snapshotLayoutInternal, browsePath string, recursive bool) []backuptypes.BackupFileEntry {eligible := make([]string, 0, len(files))for _, file := range files {- dataRelative, ok := snapshotRelativePathInternal(file, snapshotPath)- if !ok || protectedSystemBackupPathInternal(dataRelative, databasePath) {+ projectRelative, ok := snapshotRelativePathInternal(file, layout.projectsPath)+ if !ok || layout.protectedInternal(projectRelative) {continue}- projectRelative := dataRelative- if projectsPath != "" {- if dataRelative == projectsPath || !utils.FilePathMatches(dataRelative, projectsPath) {- continue- }- projectRelative = strings.TrimPrefix(dataRelative, projectsPath+"/")- }normalized, err := utils.NormalizeRelativePath(projectRelative)if err != nil {continue@@ -835,18 +696,18 @@ func (s *SystemBackupService) BrowseBackupFiles(ctx context.Context, id, recoverlocations, setupErr := s.backupSnapshotLocationsInternal(ctx, dockerClient, run)browseErr := setupErrfor _, location := range locations {- snapshot, databasePath, inspectErr := session.inspectProjectManifestInternal(ctx, location)+ snapshot, inspectErr := session.inspectProjectManifestInternal(ctx, location)if inspectErr != nil {browseErr = errors.Combine(browseErr, inspectErr)continue}- snapshotRoot := path.Join(snapshot.snapshotPath, snapshot.projectsPath, listPath)+ snapshotRoot := path.Join(snapshot.layout.projectsPath, listPath)listed, listErr := s.engine.ListSnapshotFiles(ctx, dockerClient, location.repository, key, location.snapshotID, snapshotRoot+"/", recursive)if listErr != nil {browseErr = errors.Combine(browseErr, fmt.Errorf("browse %s system recovery snapshot: %w", location.name, listErr))continue}- entries := projectEntriesFromSnapshotInternal(listed, snapshot.snapshotPath, snapshot.projectsPath, databasePath, listPath, recursive)+ entries := projectEntriesFromSnapshotInternal(listed, snapshot.layout, listPath, recursive)items, page := backupbrowser.Browse(entries, params)return items, page, nil}@@ -854,7 +715,7 @@ func (s *SystemBackupService) BrowseBackupFiles(ctx context.Context, id, recover}// openSafetySnapshotInternal resolves the safety run's snapshot in the same-// repository the restore reads from and indexes the data paths it holds.+// repository the restore reads from and indexes the project paths it holds.func (session systemBackupSnapshotSessionInternal) openSafetySnapshotInternal(ctx context.Context, source systemBackupSnapshotInternal, safetyRun *SystemBackupRun) (systemBackupSafetySnapshotInternal, error) {location := source.systemBackupSnapshotLocationInternallocation.name = "safety"@@ -866,45 +727,39 @@ func (session systemBackupSnapshotSessionInternal) openSafetySnapshotInternal(ctif location.snapshotID == "" {return systemBackupSafetySnapshotInternal{}, errors.New("pre-restore system backup has no snapshot in the selected repository")}- snapshot, err := session.inspectReadableSnapshotInternal(ctx, location)+ snapshot, err := session.inspectProjectSnapshotInternal(ctx, location)if err != nil {return systemBackupSafetySnapshotInternal{}, fmt.Errorf("open pre-restore system backup: %w", err)}- dataPaths := make(map[string]struct{}, len(snapshot.files))- for _, file := range snapshot.files {- if relative, ok := snapshotRelativePathInternal(file, snapshot.snapshotPath); ok {- dataPaths[relative] = struct{}{}- }+ paths := make(map[string]struct{}, len(snapshot.entries))+ for _, entry := range snapshot.entries {+ paths[entry.Path] = struct{}{}}- return systemBackupSafetySnapshotInternal{- systemBackupSnapshotLocationInternal: location,- snapshotPath: snapshot.snapshotPath,- dataPaths: dataPaths,- }, nil+ return systemBackupSafetySnapshotInternal{systemBackupSnapshotLocationInternal: location, layout: snapshot.layout, paths: paths}, nil}-func (s *SystemBackupService) removeProjectFileInternal(ctx context.Context, projectsPath, selectedPath string) error {- dataDirectory, err := s.dataDirectoryInternal()- if err != nil {- return err+func removeProjectFileInternal(ctx context.Context, projectsDirectory, selectedPath string) error {+ if selectedPath == "" {+ return errors.New("refusing to remove the projects directory itself")}- return acfs.RemoveAll(ctx, dataDirectory, path.Join(projectsPath, selectedPath))+ return acfs.RemoveAll(ctx, projectsDirectory, selectedPath)}-func (session systemBackupSnapshotSessionInternal) restoreEntryInternal(ctx context.Context, snapshots []systemBackupSnapshotInternal, projectsPath string, selected backuptypes.BackupFileEntry, dataMount mount.Mount) error {+func (session systemBackupSnapshotSessionInternal) restoreEntryInternal(ctx context.Context, snapshots []systemBackupSnapshotInternal, selected backuptypes.BackupFileEntry, destination projectsRestoreDestinationInternal) error {var restoreErr errorfor _, snapshot := range snapshots {- if snapshot.projectsPath != projectsPath || !snapshotContainsProjectEntryInternal(snapshot, selected) {+ if !snapshotContainsProjectEntryInternal(snapshot, selected) {continue}- sourcePath := path.Join(snapshot.snapshotPath, projectsPath, selected.Path)+ sourcePath := path.Join(snapshot.layout.projectsPath, selected.Path)if selected.IsDirectory {sourcePath += "/"}- err := session.service.engine.RestoreSnapshot(ctx, session.dockerClient, snapshot.repository, session.recoveryKey, snapshot.snapshotID, dataMount, backup.RestoreOptions{+ err := session.service.engine.RestoreSnapshot(ctx, session.dockerClient, snapshot.repository, session.recoveryKey, snapshot.snapshotID, destination.target.Mounts[0], backup.RestoreOptions{DeleteExtra: selected.IsDirectory,SourcePath: sourcePath,- DestinationPath: path.Join(dataMount.Target, projectsPath, selected.Path),+ DestinationPath: path.Join(destination.target.Path, selected.Path),+ ExtraMounts: destination.target.Mounts[1:],})if err == nil {return nil@@ -929,12 +784,17 @@ func snapshotContainsProjectEntryInternal(snapshot systemBackupSnapshotInternal,return false}-func safetySnapshotContainsPathInternal(safety systemBackupSafetySnapshotInternal, dataRelative string) bool {- if _, exists := safety.dataPaths[dataRelative]; exists {+// safetySnapshotContainsPathInternal reports whether the safety snapshot holds+// a project-relative path; the projects root itself always exists.+func safetySnapshotContainsPathInternal(safety systemBackupSafetySnapshotInternal, projectRelative string) bool {+ if projectRelative == "" {return true}- prefix := strings.TrimSuffix(dataRelative, "/") + "/"- for candidate := range safety.dataPaths {+ if _, exists := safety.paths[projectRelative]; exists {+ return true+ }+ prefix := strings.TrimSuffix(projectRelative, "/") + "/"+ for candidate := range safety.paths {if strings.HasPrefix(candidate, prefix) {return true}@@ -942,24 +802,24 @@ func safetySnapshotContainsPathInternal(safety systemBackupSafetySnapshotInternareturn false}-func (session systemBackupSnapshotSessionInternal) rollbackInternal(ctx context.Context, safety systemBackupSafetySnapshotInternal, projectsPath string, selected []backuptypes.BackupFileEntry, dataMount mount.Mount) error {+func (session systemBackupSnapshotSessionInternal) rollbackInternal(ctx context.Context, safety systemBackupSafetySnapshotInternal, selected []backuptypes.BackupFileEntry, destination projectsRestoreDestinationInternal) error {var rollbackErr errorfor _, selectedEntry := range slices.Backward(selected) {- dataRelative := path.Join(projectsPath, selectedEntry.Path)- if !safetySnapshotContainsPathInternal(safety, dataRelative) {- if err := session.service.removeProjectFileInternal(ctx, projectsPath, selectedEntry.Path); err != nil {+ if !safetySnapshotContainsPathInternal(safety, selectedEntry.Path) {+ if err := removeProjectFileInternal(ctx, destination.directory, selectedEntry.Path); err != nil {rollbackErr = errors.Combine(rollbackErr, fmt.Errorf("remove newly restored project path %s: %w", selectedEntry.Path, err))}continue}- sourcePath := path.Join(safety.snapshotPath, dataRelative)+ sourcePath := path.Join(safety.layout.projectsPath, selectedEntry.Path)if selectedEntry.IsDirectory {sourcePath += "/"}- if err := session.service.engine.RestoreSnapshot(ctx, session.dockerClient, safety.repository, session.recoveryKey, safety.snapshotID, dataMount, backup.RestoreOptions{+ if err := session.service.engine.RestoreSnapshot(ctx, session.dockerClient, safety.repository, session.recoveryKey, safety.snapshotID, destination.target.Mounts[0], backup.RestoreOptions{DeleteExtra: selectedEntry.IsDirectory,SourcePath: sourcePath,- DestinationPath: path.Join(dataMount.Target, dataRelative),+ DestinationPath: path.Join(destination.target.Path, selectedEntry.Path),+ ExtraMounts: destination.target.Mounts[1:],}); err != nil {rollbackErr = errors.Combine(rollbackErr, fmt.Errorf("restore project path %s from safety backup: %w", selectedEntry.Path, err))}@@ -967,14 +827,13 @@ func (session systemBackupSnapshotSessionInternal) rollbackInternal(ctx context.return rollbackErr}-func (session systemBackupSnapshotSessionInternal) restoreSelectedInternal(ctx context.Context, snapshots []systemBackupSnapshotInternal, safety systemBackupSafetySnapshotInternal, selected []backuptypes.BackupFileEntry, dataMount mount.Mount) error {- projectsPath := snapshots[0].projectsPath+func (session systemBackupSnapshotSessionInternal) restoreSelectedInternal(ctx context.Context, snapshots []systemBackupSnapshotInternal, safety systemBackupSafetySnapshotInternal, selected []backuptypes.BackupFileEntry, destination projectsRestoreDestinationInternal) error {restored := make([]backuptypes.BackupFileEntry, 0, len(selected))for _, selectedEntry := range selected {- if err := session.restoreEntryInternal(ctx, snapshots, projectsPath, selectedEntry, dataMount); err != nil {+ if err := session.restoreEntryInternal(ctx, snapshots, selectedEntry, destination); err != nil {affected := slices.Clone(restored)affected = append(affected, selectedEntry)- rollbackErr := session.rollbackInternal(context.WithoutCancel(ctx), safety, projectsPath, affected, dataMount)+ rollbackErr := session.rollbackInternal(context.WithoutCancel(ctx), safety, affected, destination)restoreErr := fmt.Errorf("failed to restore project path %s from system backup: %w", selectedEntry.Path, err)if rollbackErr != nil {return errors.Combine(restoreErr, fmt.Errorf("failed to roll back project files from pre-restore system backup: %w", rollbackErr))@@ -986,7 +845,8 @@ func (session systemBackupSnapshotSessionInternal) restoreSelectedInternal(ctx creturn nil}-// RestoreBackupFiles restores selected project files after creating a safety snapshot.+// RestoreBackupFiles restores selected project files into the current projects+// directory after creating a safety snapshot.func (s *SystemBackupService) RestoreBackupFiles(ctx context.Context, id string, request backuptypes.RestoreSystemBackupFilesRequest, user common.User) error {lease, admitted, err := s.engine.TryAcquireRun(ctx, backup.SystemAdmissionScope, systemAdmissionID)if err != nil {@@ -1021,7 +881,7 @@ func (s *SystemBackupService) RestoreBackupFiles(ctx context.Context, id string,if err != nil {return fmt.Errorf("%w: %w", common.ErrInvalidBackupSelection, err)}- dataMount, err := s.appDataMountInternal(ctx, dockerClient, "/arcane-data", false)+ destination, err := s.projectsRestoreDestinationInternal(ctx, dockerClient)if err != nil {return err}@@ -1041,12 +901,15 @@ func (s *SystemBackupService) RestoreBackupFiles(ctx context.Context, id string,if err != nil {return err}- return session.restoreSelectedInternal(ctx, snapshots, safety, selected, dataMount)+ return session.restoreSelectedInternal(ctx, snapshots, safety, selected, destination)}+// normalizeSystemBackupSelectionInternal collapses a plain select-all into the+// projects root, except when projects share the data root and a root restore+// would replace Arcane's own files.func normalizeSystemBackupSelectionInternal(selection backuptypes.RestoreSelection, snapshot systemBackupSnapshotInternal) ([]backuptypes.BackupFileEntry, error) {- if selection.SelectAll && strings.TrimSpace(selection.Search) == "" && snapshot.projectsPath != "" {- return backupbrowser.NormalizeSelection(selection, []backuptypes.BackupFileEntry{{Path: "", Name: path.Base(snapshot.projectsPath), IsDirectory: true}})+ if selection.SelectAll && strings.TrimSpace(selection.Search) == "" && snapshot.layout.projectsPath != snapshot.layout.dataPath {+ return backupbrowser.NormalizeSelection(selection, []backuptypes.BackupFileEntry{{Path: "", Name: path.Base(snapshot.layout.projectsPath), IsDirectory: true}})}return backupbrowser.NormalizeSelection(selection, snapshot.entries)}@@ -1266,15 +1129,6 @@ func (s *SystemBackupService) RestoreBackup(ctx context.Context, id, recoveryKeyif err != nil {return err}- // A local safety snapshot is created before the detached helper is allowed to- // stop Arcane and replace its data.- safetyBackup, err := s.CreateBackup(ctx, user, SystemBackupTriggerSafety, backuptypes.CreateSystemBackupRequest{- Destination: backuptypes.SystemBackupDestinationLocal,- RecoveryKey: key,- })- if err != nil {- return fmt.Errorf("failed to create pre-restore system backup: %w", err)- }dockerClient, err := s.dockerService.GetClient(ctx)if err != nil {return err@@ -1288,24 +1142,58 @@ func (s *SystemBackupService) RestoreBackup(ctx context.Context, id, recoveryKeyreturn fmt.Errorf("inspect Arcane container: %w", err)}current := inspectResult.Container+ // The helper reads the restored manifest and database through /app/data.appDataMount := dockerutil.MountForDestination(current.Mounts, "/app/data", "/app/data")if appDataMount == nil {return errors.New("arcane system restore requires /app/data to be mounted")}+ dataDirectory, err := s.dataDirectoryInternal()+ if err != nil {+ return err+ }+ projectsDirectory := s.projectsDirectoryInternal(ctx)+ session := s.snapshotSessionInternal(dockerClient, key)locations, setupErr := s.backupSnapshotLocationsInternal(ctx, dockerClient, run)- snapshot, err := s.snapshotSessionInternal(dockerClient, key).firstReadableSnapshotInternal(ctx, locations, setupErr)+ snapshot, err := session.firstReadableSnapshotInternal(ctx, locations, setupErr)if err != nil {return err}+ stages, err := restoreStagesInternal(current.Mounts, dataDirectory, projectsDirectory, snapshot.restoreRepositoryInternal(), snapshot.snapshotID, snapshot.layout)+ if err != nil {+ return fmt.Errorf("plan system restore: %w", err)+ }+ // A local safety snapshot is created before the detached helper is allowed to+ // stop Arcane and replace its data.+ safetyBackup, err := s.CreateBackup(ctx, user, SystemBackupTriggerSafety, backuptypes.CreateSystemBackupRequest{+ Destination: backuptypes.SystemBackupDestinationLocal,+ RecoveryKey: key,+ })+ if err != nil {+ return fmt.Errorf("failed to create pre-restore system backup: %w", err)+ }+ localRepository, err := s.localRepositoryInternal(ctx, dockerClient, true)+ if err != nil {+ return err+ }+ safetyLocation := systemBackupSnapshotLocationInternal{name: "safety", destination: backuptypes.SystemBackupDestinationLocal, repository: localRepository, snapshotID: safetyBackup.LocalSnapshotID}+ safetyLayout, err := session.readSnapshotLayoutInternal(ctx, safetyLocation)+ if err != nil {+ return fmt.Errorf("open pre-restore system backup: %w", err)+ }+ rollbackStages, err := restoreStagesInternal(current.Mounts, dataDirectory, projectsDirectory, safetyLocation.restoreRepositoryInternal(), safetyBackup.LocalSnapshotID, safetyLayout)+ if err != nil {+ return fmt.Errorf("plan system restore rollback: %w", err)+ }request := recoverytypes.RestoreRequest{- BackupID: run.ID, ContainerID: current.ID, ContainerImage: current.Config.Image, SnapshotID: snapshot.snapshotID, SnapshotPath: snapshot.snapshotPath, RecoveryKey: key,+ BackupID: run.ID, ContainerID: current.ID, ContainerImage: current.Config.Image, SnapshotID: snapshot.snapshotID, RecoveryKey: key,LocalSnapshotID: run.LocalSnapshotID, RemoteSnapshotID: run.RemoteSnapshotID, S3DestinationID: run.S3DestinationID,Size: run.Size, NetworkMode: rusticRestoreNetworkModeInternal(¤t),SafetyBackup: &recoverytypes.SafetyBackup{ID: safetyBackup.ID, LocalSnapshotID: safetyBackup.LocalSnapshotID,Size: safetyBackup.Size, CreatedAt: safetyBackup.CreatedAt,},- RepositoryEnvironment: snapshot.repository.Environment, RepositoryMounts: snapshot.repository.Mounts, AppDataMount: *appDataMount,+ ProjectsSetting: s.projectsSettingInternal(ctx), ProjectsIncluded: snapshot.layout.projectsIncludedInternal(),+ Stages: stages, RollbackStages: rollbackStages,}requestData, err := json.Marshal(request)if err != nil {@@ -1710,3 +1598,410 @@ func (s *SystemBackupService) disableMissingS3Internal(ctx context.Context, polis.rescheduleSystemBackupPolicyInternal(ctx, policy)return true, nil}++const (+ snapshotDataPath = "/data"+ snapshotProjectsPath = "/projects"+)++// backupSourceLayoutInternal is what one system backup snapshots and where+// each source appears inside the snapshot.+type backupSourceLayoutInternal struct {+ dataDirectory string+ projectsDirectory string+ databaseName string+ projectsPath string+ mounts []mount.Mount+ sources []string+}++func (layout backupSourceLayoutInternal) manifestPathInternal() string {+ return filepath.Join(layout.dataDirectory, systemRecoveryManifestName)+}++// projectsSnapshotPathInternal classifies the projects directory against the+// data directory and returns its snapshot path; external reports whether it+// needs its own snapshot source.+func projectsSnapshotPathInternal(dataDirectory, projectsDirectory string) (projectsPath string, external bool, err error) {+ data := path.Clean(filepath.ToSlash(dataDirectory))+ projectsDir := path.Clean(filepath.ToSlash(projectsDirectory))+ switch {+ case projectsDir == data:+ return snapshotDataPath, false, nil+ case utils.FilePathMatches(projectsDir, data):+ return path.Join(snapshotDataPath, strings.TrimPrefix(projectsDir, data+"/")), false, nil+ case utils.FilePathMatches(data, projectsDir):+ return "", false, fmt.Errorf("the projects directory %s contains Arcane's data directory %s; move it inside or outside the data directory before creating system backups", projectsDir, data)+ default:+ return snapshotProjectsPath, true, nil+ }+}++// projectsSettingInternal returns the effective projectsDirectory setting.+func (s *SystemBackupService) projectsSettingInternal(ctx context.Context) string {+ fallback := ""+ if s.config != nil {+ fallback = s.config.ProjectsDirectory+ }+ if s.settingsService == nil {+ return fallback+ }+ return s.settingsService.GetStringSetting(ctx, "projectsDirectory", fallback)+}++// projectsDirectoryInternal resolves the configured projects directory to the+// container path Arcane reads, dropping any host-path mapping suffix.+func (s *SystemBackupService) projectsDirectoryInternal(ctx context.Context) string {+ return projects.ResolveConfiguredContainerDirectory(s.projectsSettingInternal(ctx), "/app/data/projects")+}++// currentMountsInternal returns the Arcane container's mounts, or inContainer+// false in host development where directories are bound directly.+func currentMountsInternal(ctx context.Context, dockerClient *client.Client) (mounts []containertypes.MountPoint, inContainer bool, err error) {+ _, containerErr := cgroup.CurrentContainerID()+ if inContainer = containerErr == nil; !inContainer {+ return nil, false, nil+ }+ inspect, err := libarcane.InspectCurrentArcaneContainer(ctx, dockerClient)+ if err != nil {+ return nil, true, fmt.Errorf("failed to inspect Arcane container mounts: %w", err)+ }+ return inspect.Mounts, true, nil+}++// sourceMountsInternal exposes containerPath read-only at target: the+// container's own mount plus every mount nested beneath it, so the helper+// sees the same files Arcane does.+func sourceMountsInternal(mounts []containertypes.MountPoint, inContainer bool, containerPath, target string) ([]mount.Mount, error) {+ if !inContainer {+ return []mount.Mount{{Type: mount.TypeBind, Source: containerPath, Target: target, ReadOnly: true}}, nil+ }+ primary := dockerutil.MountForSubpath(mounts, containerPath, target)+ if primary == nil {+ return nil, fmt.Errorf("%s must be mounted into the Arcane container from a bind or named volume for system backups", containerPath)+ }+ result := append([]mount.Mount{*primary}, dockerutil.NestedMounts(mounts, containerPath, target)...)+ for index := range result {+ result[index].ReadOnly = true+ }+ return result, nil+}++// backupSourceLayoutInternal resolves the sources of one system backup once so+// the manifest and the snapshot describe the same tree.+func (s *SystemBackupService) backupSourceLayoutInternal(ctx context.Context, dockerClient *client.Client) (backupSourceLayoutInternal, error) {+ databaseFile, err := s.databaseFileInternal()+ if err != nil {+ return backupSourceLayoutInternal{}, err+ }+ layout := backupSourceLayoutInternal{+ dataDirectory: filepath.Dir(databaseFile),+ projectsDirectory: s.projectsDirectoryInternal(ctx),+ databaseName: filepath.Base(databaseFile),+ sources: []string{snapshotDataPath},+ }+ projectsPath, external, err := projectsSnapshotPathInternal(layout.dataDirectory, layout.projectsDirectory)+ if err != nil {+ return backupSourceLayoutInternal{}, err+ }+ layout.projectsPath = projectsPath+ mounts, inContainer, err := currentMountsInternal(ctx, dockerClient)+ if err != nil {+ return backupSourceLayoutInternal{}, err+ }+ layout.mounts, err = sourceMountsInternal(mounts, inContainer, layout.dataDirectory, snapshotDataPath)+ if err != nil {+ return backupSourceLayoutInternal{}, errors.WrapIf(err, "resolve Arcane data source")+ }+ if !external {+ return layout, nil+ }+ projectMounts, err := sourceMountsInternal(mounts, inContainer, layout.projectsDirectory, snapshotProjectsPath)+ if err != nil {+ return backupSourceLayoutInternal{}, errors.WrapIf(err, "resolve projects source")+ }+ layout.mounts = append(layout.mounts, projectMounts...)+ layout.sources = append(layout.sources, snapshotProjectsPath)+ return layout, nil+}++const (+ recoveryDataRestoreTarget = "/restore"+ recoveryProjectsRestoreTarget = "/restore-projects"+ selectiveProjectsRestoreTarget = "/arcane-projects"+)++var (+ errProjectsOutsideDataInternal = errors.New("the backup-time projects directory is outside Arcane's system backup data")+ errProjectsNotInBackupInternal = errors.New("this system backup does not include the projects directory because it was created before Arcane backed up separately mounted projects; create a new system backup to restore project files")+ manifestCandidateRootsInternal = []string{snapshotDataPath, "/", "/app/data"}+)++// snapshotLayoutInternal locates Arcane data and projects inside one snapshot.+type snapshotLayoutInternal struct {+ dataPath string // "/" or "/app/data" for version 1, "/data" for version 2+ projectsPath string // absolute snapshot path; empty when the backup omitted projects+ databaseName string+}++func (layout snapshotLayoutInternal) projectsIncludedInternal() bool {+ return layout.projectsPath != ""+}++// projectsDataRelativeInternal returns the projects root relative to the data+// root when projects live inside it.+func (layout snapshotLayoutInternal) projectsDataRelativeInternal() (string, bool) {+ if !layout.projectsIncludedInternal() {+ return "", false+ }+ if layout.projectsPath == layout.dataPath {+ return "", true+ }+ return snapshotRelativePathInternal(layout.projectsPath, layout.dataPath)+}++// protectedInternal reports whether a project-relative path is an Arcane data+// file that must never be listed or restored as a project file.+func (layout snapshotLayoutInternal) protectedInternal(projectRelative string) bool {+ relative, inside := layout.projectsDataRelativeInternal()+ if !inside {+ return false+ }+ return protectedSystemBackupPathInternal(path.Join(relative, projectRelative), layout.databaseName)+}++// projectsCoveredByDataInternal reports whether restoring the data root already+// puts projects where the current projects directory is.+func (layout snapshotLayoutInternal) projectsCoveredByDataInternal(dataDirectory, projectsDirectory string) bool {+ relative, inside := layout.projectsDataRelativeInternal()+ if !inside {+ return false+ }+ return path.Clean(filepath.ToSlash(projectsDirectory)) == path.Join(path.Clean(filepath.ToSlash(dataDirectory)), relative)+}++func (session systemBackupSnapshotSessionInternal) readSnapshotLayoutInternal(ctx context.Context, location systemBackupSnapshotLocationInternal) (snapshotLayoutInternal, error) {+ var manifestData, manifestRoot string+ var readErr error+ for _, candidate := range manifestCandidateRootsInternal {+ data, err := session.service.engine.ReadSnapshotTextFile(ctx, session.dockerClient, location.repository, session.recoveryKey, location.snapshotID, path.Join(candidate, systemRecoveryManifestName))+ if err != nil {+ readErr = errors.Combine(readErr, err)+ continue+ }+ manifestData, manifestRoot = data, candidate+ break+ }+ if manifestRoot == "" {+ return snapshotLayoutInternal{}, fmt.Errorf("read %s system recovery manifest: %w", location.name, readErr)+ }+ var manifest recoverytypes.Manifest+ if err := json.Unmarshal([]byte(manifestData), &manifest); err != nil {+ return snapshotLayoutInternal{}, fmt.Errorf("decode %s system recovery manifest: %w", location.name, err)+ }+ layout, err := snapshotLayoutFromManifestInternal(manifest, manifestRoot)+ if err != nil {+ return snapshotLayoutInternal{}, fmt.Errorf("%s system recovery manifest: %w", location.name, err)+ }+ return layout, nil+}++func snapshotLayoutFromManifestInternal(manifest recoverytypes.Manifest, manifestRoot string) (snapshotLayoutInternal, error) {+ switch manifest.FormatVersion {+ case 1:+ return legacySnapshotLayoutInternal(manifest, manifestRoot)+ case recoverytypes.ManifestFormatVersion:+ dataPath, err := confinedSnapshotPathInternal(manifest.DataPath)+ if err != nil {+ return snapshotLayoutInternal{}, fmt.Errorf("invalid data path: %w", err)+ }+ if dataPath != manifestRoot {+ return snapshotLayoutInternal{}, fmt.Errorf("records data path %s but was found at %s", dataPath, manifestRoot)+ }+ projectsPath, err := confinedSnapshotPathInternal(manifest.ProjectsPath)+ if err != nil {+ return snapshotLayoutInternal{}, fmt.Errorf("invalid projects path: %w", err)+ }+ databaseName, err := utils.NormalizeRelativePath(manifest.DatabasePath)+ if err != nil {+ return snapshotLayoutInternal{}, fmt.Errorf("invalid database path: %w", err)+ }+ return snapshotLayoutInternal{dataPath: dataPath, projectsPath: projectsPath, databaseName: databaseName}, nil+ default:+ return snapshotLayoutInternal{}, fmt.Errorf("uses unsupported format %d", manifest.FormatVersion)+ }+}++// confinedSnapshotPathInternal validates a recorded snapshot path and returns+// it in absolute form.+func confinedSnapshotPathInternal(value string) (string, error) {+ relative, err := utils.NormalizeRelativePath(strings.TrimPrefix(strings.TrimSpace(value), "/"))+ if err != nil {+ return "", err+ }+ return "/" + relative, nil+}++// legacySnapshotLayoutInternal derives a version-1 layout from the manifest+// environment. Projects outside the captured data are reported as omitted.+func legacySnapshotLayoutInternal(manifest recoverytypes.Manifest, manifestRoot string) (snapshotLayoutInternal, error) {+ databasePath, err := recoveryManifestDatabasePathInternal(manifest)+ if err != nil {+ return snapshotLayoutInternal{}, err+ }+ layout := snapshotLayoutInternal{dataPath: manifestRoot, databaseName: path.Base(databasePath)}+ relative, err := projectsRelativePathFromManifestInternal(manifest)+ if errors.Is(err, errProjectsOutsideDataInternal) {+ return layout, nil+ }+ if err != nil {+ return snapshotLayoutInternal{}, fmt.Errorf("resolve projects directory: %w", err)+ }+ layout.projectsPath = path.Join(manifestRoot, relative)+ return layout, nil+}++func recoveryManifestDatabasePathInternal(manifest recoverytypes.Manifest) (string, error) {+ databaseURL := strings.TrimSpace(manifest.Environment["DATABASE_URL"])+ if databaseURL == "" {+ return "", errors.New("system recovery manifest does not record the database path")+ }+ databasePath, err := utils.SQLitePathFromDSN(databaseURL)+ if err != nil {+ return "", fmt.Errorf("parse database URL from system recovery manifest: %w", err)+ }+ databasePath = strings.ReplaceAll(strings.TrimSpace(databasePath), `\`, "/")+ if databasePath == "" {+ return "", errors.New("system recovery manifest contains an empty database path")+ }+ return path.Clean(databasePath), nil+}++func portablePathIsAbsInternal(filePath string) bool {+ return path.IsAbs(filePath) || (len(filePath) >= 3 && filePath[1] == ':' && filePath[2] == '/')+}++// projectsRelativePathFromManifestInternal relates a version-1 manifest's+// projects directory to its data directory.+func projectsRelativePathFromManifestInternal(manifest recoverytypes.Manifest) (string, error) {+ configured := strings.TrimSpace(manifest.Environment["PROJECTS_DIRECTORY"])+ if configured == "" {+ return "", errors.New("system recovery manifest does not record the projects directory")+ }+ if strings.HasPrefix(configured, "/") {+ if separator := strings.Index(configured, ":"); separator > 0 {+ configured = configured[:separator]+ }+ }+ projectsPath := path.Clean(strings.ReplaceAll(configured, `\`, "/"))+ databasePath, err := recoveryManifestDatabasePathInternal(manifest)+ if err != nil {+ return "", err+ }+ dataPath := path.Dir(databasePath)+ dataAbsolute := portablePathIsAbsInternal(dataPath)+ projectsAbsolute := portablePathIsAbsInternal(projectsPath)+ if !dataAbsolute && projectsAbsolute {+ relativeDataPath := strings.Trim(dataPath, "/")+ if relativeDataPath == "." {+ return "", errors.New("cannot relate the absolute projects directory to the relative database path in the system recovery manifest")+ }+ marker := "/" + relativeDataPath+ index := strings.LastIndex(projectsPath, marker)+ if index < 0 || (len(projectsPath) > index+len(marker) && projectsPath[index+len(marker)] != '/') {+ return "", errors.New("cannot relate the projects directory to the database path in the system recovery manifest")+ }+ dataPath = projectsPath[:index+len(marker)]+ dataAbsolute = true+ } else if dataAbsolute && !projectsAbsolute {+ projectsPath = path.Join(path.Dir(dataPath), projectsPath)+ projectsAbsolute = true+ }+ if dataAbsolute != projectsAbsolute {+ return "", errors.New("projects and database paths in the system recovery manifest use incompatible roots")+ }+ if projectsPath == dataPath {+ return "", nil+ }+ if !utils.FilePathMatches(projectsPath, dataPath) {+ return "", errProjectsOutsideDataInternal+ }+ return strings.TrimPrefix(projectsPath, dataPath+"/"), nil+}++// restoreTargetInternal resolves the writable destination for containerPath+// from the Arcane container's mounts: its enclosing mount at target plus the+// mounts nested beneath it, minus any under exclude that a later stage writes+// through their own mount.+func restoreTargetInternal(mounts []containertypes.MountPoint, containerPath, target, exclude string) (recoverytypes.RestoreTarget, error) {+ enclosing, relative := dockerutil.MountForEnclosingPath(mounts, containerPath, target)+ if enclosing == nil {+ return recoverytypes.RestoreTarget{}, fmt.Errorf("%s must be mounted into the Arcane container from a bind or named volume to restore into it", containerPath)+ }+ if enclosing.ReadOnly {+ return recoverytypes.RestoreTarget{}, fmt.Errorf("%s is mounted read-only into the Arcane container", containerPath)+ }+ destination := path.Join(target, relative)+ candidates := slices.DeleteFunc(slices.Clone(mounts), func(m containertypes.MountPoint) bool {+ return exclude != "" && utils.FilePathMatches(m.Destination, exclude)+ })+ result := recoverytypes.RestoreTarget{Mounts: []mount.Mount{*enclosing}, Path: destination}+ result.Mounts = append(result.Mounts, dockerutil.NestedMounts(candidates, containerPath, destination)...)+ return result, nil+}++func hostRestoreTargetInternal(directory, target string) recoverytypes.RestoreTarget {+ return recoverytypes.RestoreTarget{Mounts: []mount.Mount{{Type: mount.TypeBind, Source: directory, Target: target}}, Path: target}+}++// projectsRestoreDestinationInternal is the current projects directory as a+// helper-container target plus its container path for confined cleanup.+type projectsRestoreDestinationInternal struct {+ directory string+ target recoverytypes.RestoreTarget+}++func (s *SystemBackupService) projectsRestoreDestinationInternal(ctx context.Context, dockerClient *client.Client) (projectsRestoreDestinationInternal, error) {+ directory := s.projectsDirectoryInternal(ctx)+ mounts, inContainer, err := currentMountsInternal(ctx, dockerClient)+ if err != nil {+ return projectsRestoreDestinationInternal{}, err+ }+ if !inContainer {+ return projectsRestoreDestinationInternal{directory: directory, target: hostRestoreTargetInternal(directory, selectiveProjectsRestoreTarget)}, nil+ }+ target, err := restoreTargetInternal(mounts, directory, selectiveProjectsRestoreTarget, "")+ if err != nil {+ return projectsRestoreDestinationInternal{}, errors.WrapIf(err, "resolve projects directory for restore")+ }+ return projectsRestoreDestinationInternal{directory: directory, target: target}, nil+}++// restoreStagesInternal plans the Rustic restores that put a snapshot's data+// and projects into the current container layout. Projects get their own+// stage unless the snapshot already holds them at their current place under+// the data root.+func restoreStagesInternal(mounts []containertypes.MountPoint, dataDirectory, projectsDirectory string, repository recoverytypes.RestoreRepository, snapshotID string, layout snapshotLayoutInternal) ([]recoverytypes.RestoreStage, error) {+ separate := layout.projectsIncludedInternal() && !layout.projectsCoveredByDataInternal(dataDirectory, projectsDirectory)+ if separate && layout.projectsPath == layout.dataPath {+ return nil, fmt.Errorf("the backup keeps projects in Arcane's data directory; set the projects directory to %s before a full restore, or restore individual project files instead", dataDirectory)+ }+ exclude := ""+ if separate {+ exclude = projectsDirectory+ }+ dataTarget, err := restoreTargetInternal(mounts, dataDirectory, recoveryDataRestoreTarget, exclude)+ if err != nil {+ return nil, err+ }+ stages := make([]recoverytypes.RestoreStage, 0, 2)+ stages = append(stages, recoverytypes.RestoreStage{Repository: repository, SnapshotID: snapshotID, SourcePath: layout.dataPath, Target: dataTarget})+ if !separate {+ return stages, nil+ }+ projectsTarget, err := restoreTargetInternal(mounts, projectsDirectory, recoveryProjectsRestoreTarget, "")+ if err != nil {+ return nil, err+ }+ return append(stages, recoverytypes.RestoreStage{Repository: repository, SnapshotID: snapshotID, SourcePath: layout.projectsPath, Target: projectsTarget}), nil+}diff --git a/backend/internal/systembackup/service_test.go b/backend/internal/systembackup/service_test.goindex 13c9c1688..7c117565d 100644--- a/backend/internal/systembackup/service_test.go+++ b/backend/internal/systembackup/service_test.go@@ -2,20 +2,24 @@ package systembackupimport ("context"+ "encoding/json/v2""net/http""net/http/httptest"+ "os"+ "path/filepath""slices""testing""time"+ "emperror.dev/errors""github.com/getarcaneapp/arcane/backend/v2/internal/actors""github.com/getarcaneapp/arcane/backend/v2/internal/backup""github.com/getarcaneapp/arcane/backend/v2/internal/config""github.com/getarcaneapp/arcane/backend/v2/internal/database"- recoverytypes "github.com/getarcaneapp/arcane/backend/v2/internal/recovery"s3domain "github.com/getarcaneapp/arcane/backend/v2/internal/s3""github.com/getarcaneapp/arcane/backend/v2/pkg/scheduler/entityjobs"backuptypes "github.com/getarcaneapp/arcane/types/v2/backup"+ recoverytypes "github.com/getarcaneapp/arcane/types/v2/recovery"schedulertypes "github.com/getarcaneapp/arcane/types/v2/scheduler""github.com/libtnb/sqlite"containertypes "github.com/moby/moby/api/types/container"@@ -178,35 +182,20 @@ func TestRecoveryEnvironmentInternalIncludesRuntimeSecrets(t *testing.T) {require.Equal(t, "0640", environment["FILE_PERM"])}-func TestSystemSnapshotPathFromFilesInternal(t *testing.T) {- path, err := systemSnapshotPathFromFilesInternal([]string{"/arcane.db", "/.arcane-recovery.json"})- require.NoError(t, err)- require.Equal(t, "/", path)-- path, err = systemSnapshotPathFromFilesInternal([]string{"/app/data/arcane.db", "/app/data/.arcane-recovery.json"})- require.NoError(t, err)- require.Equal(t, "/app/data", path)-- _, err = systemSnapshotPathFromFilesInternal([]string{"/arcane.db"})- require.ErrorContains(t, err, "does not contain an Arcane recovery manifest")-}-func TestProjectFilesFromSnapshotInternal(t *testing.T) {tests := []struct {- name string- files []string- snapshotPath string- projectsPath string- databasePath string- expected []string+ name string+ files []string+ layout snapshotLayoutInternal+ expected []string}{{- name: "current snapshot root",+ name: "version 1 snapshot root",files: []string{"/.arcane-recovery.json", "/arcane.db", "/arcane.db-wal", "/templates/demo.yaml","/projects/demo/docker-compose.yaml", "/projects/demo/.env", "/projects/demo/data/", "/projects/demo/.env",},- snapshotPath: "/", projectsPath: "projects", databasePath: "arcane.db",+ layout: snapshotLayoutInternal{dataPath: "/", projectsPath: "/projects", databaseName: "arcane.db"},expected: []string{"demo/.env", "demo/docker-compose.yaml"},},{@@ -215,21 +204,29 @@ func TestProjectFilesFromSnapshotInternal(t *testing.T) {"/app/data/.arcane-recovery.json", "/app/data/arcane.db", "/app/data/custom/projects/nested/app/compose.yaml","/app/data/projects/ignored/compose.yaml",},- snapshotPath: "/app/data", projectsPath: "custom/projects", databasePath: "arcane.db",+ layout: snapshotLayoutInternal{dataPath: "/app/data", projectsPath: "/app/data/custom/projects", databaseName: "arcane.db"},expected: []string{"nested/app/compose.yaml"},},{name: "projects directory is data root",files: []string{- "/.arcane-recovery.json", "/.arcane-recovery-request.json", "/custom.db", "/custom.db-shm", "/demo/config.yaml",+ "/data/.arcane-recovery.json", "/data/.arcane-recovery-request.json", "/data/custom.db", "/data/custom.db-shm", "/data/demo/config.yaml",},- snapshotPath: "/", projectsPath: "", databasePath: "custom.db",+ layout: snapshotLayoutInternal{dataPath: "/data", projectsPath: "/data", databaseName: "custom.db"},expected: []string{"demo/config.yaml"},},+ {+ name: "external projects keep database-like names",+ files: []string{+ "/data/.arcane-recovery.json", "/data/arcane.db", "/projects/arcane.db", "/projects/demo/compose.yaml",+ },+ layout: snapshotLayoutInternal{dataPath: "/data", projectsPath: "/projects", databaseName: "arcane.db"},+ expected: []string{"arcane.db", "demo/compose.yaml"},+ },}for _, test := range tests {t.Run(test.name, func(t *testing.T) {- entries := projectEntriesFromSnapshotInternal(test.files, test.snapshotPath, test.projectsPath, test.databasePath, "", true)+ entries := projectEntriesFromSnapshotInternal(test.files, test.layout, "", true)actual := make([]string, 0, len(entries))for _, entry := range entries {if !entry.IsDirectory {@@ -254,7 +251,7 @@ func TestProjectsRelativePathFromManifestInternal(t *testing.T) {{name: "relative default database path", databaseURL: "file:data/arcane.db", projectsDirectory: "/app/data/historical", expected: "historical"},{name: "projects mapping", databaseURL: "file:/app/data/arcane.db", projectsDirectory: "/app/data/historical:/host/projects", expected: "historical"},{name: "data root", databaseURL: "file:/app/data/arcane.db", projectsDirectory: "/app/data", expected: ""},- {name: "outside data", databaseURL: "file:/app/data/arcane.db", projectsDirectory: "/srv/projects", errorContains: "outside Arcane's system backup data"},+ {name: "outside data", databaseURL: "file:/app/data/arcane.db", projectsDirectory: "/srv/projects", errorContains: errProjectsOutsideDataInternal.Error()},}for _, test := range tests {t.Run(test.name, func(t *testing.T) {@@ -283,7 +280,7 @@ func TestProjectEntriesFromSnapshotSynthesizesFoldersAndExcludesProtectedDataInt"/arcane.db-journal","/demo/nested/compose.yaml","/z.txt",- }, "/", "", "arcane.db", "", false)+ }, snapshotLayoutInternal{dataPath: "/", projectsPath: "/", databaseName: "arcane.db"}, "", false)require.Equal(t, []backuptypes.BackupFileEntry{{Path: "demo", Name: "demo", IsDirectory: true},{Path: "z.txt", Name: "z.txt"},@@ -294,7 +291,7 @@ func TestProjectEntriesFromSnapshotNestedBrowseInternal(t *testing.T) {entries := projectEntriesFromSnapshotInternal([]string{"/app/data/custom/projects/demo/nested/","/app/data/custom/projects/demo/compose.yaml",- }, "/app/data", "custom/projects", "arcane.db", "demo", false)+ }, snapshotLayoutInternal{dataPath: "/app/data", projectsPath: "/app/data/custom/projects", databaseName: "arcane.db"}, "demo", false)require.Equal(t, []backuptypes.BackupFileEntry{{Path: "demo/nested", Name: "nested", IsDirectory: true},{Path: "demo/compose.yaml", Name: "compose.yaml"},@@ -303,7 +300,7 @@ func TestProjectEntriesFromSnapshotNestedBrowseInternal(t *testing.T) {func TestNormalizeSystemBackupSelectionInternal(t *testing.T) {snapshot := systemBackupSnapshotInternal{- projectsPath: "historical",+ layout: snapshotLayoutInternal{dataPath: "/data", projectsPath: "/data/historical", databaseName: "arcane.db"},entries: []backuptypes.BackupFileEntry{{Path: "demo", Name: "demo", IsDirectory: true},{Path: "demo/compose.yaml", Name: "compose.yaml"},@@ -325,9 +322,257 @@ func TestNormalizeSystemBackupSelectionInternal(t *testing.T) {)require.NoError(t, err)require.Equal(t, []backuptypes.BackupFileEntry{{Path: "demo", Name: "demo", IsDirectory: true}}, selected)++ snapshot.layout.projectsPath = snapshot.layout.dataPath+ selected, err = normalizeSystemBackupSelectionInternal(backuptypes.RestoreSelection{SelectAll: true}, snapshot)+ require.NoError(t, err)+ require.Equal(t, []backuptypes.BackupFileEntry{{Path: "demo", Name: "demo", IsDirectory: true}}, selected)}func (s *systemBackupPolicySchedulerInternal) Submit(_ context.Context, request schedulertypes.Request) (schedulertypes.Run, error) {s.submitted = append(s.submitted, request)return schedulertypes.Run{ID: request.RunID, JobID: request.JobID, EnvironmentID: request.EnvironmentID, Status: schedulertypes.Queued}, nil}++func TestSnapshotLayoutFromManifestInternal(t *testing.T) {+ legacy := map[string]string{"DATABASE_URL": "file:/app/data/arcane.db", "PROJECTS_DIRECTORY": "/app/data/projects"}+ tests := []struct {+ name string+ manifest recoverytypes.Manifest+ root string+ expected snapshotLayoutInternal+ errorContains string+ }{+ {+ name: "version 1 projects inside data", manifest: recoverytypes.Manifest{FormatVersion: 1, Environment: legacy}, root: "/",+ expected: snapshotLayoutInternal{dataPath: "/", projectsPath: "/projects", databaseName: "arcane.db"},+ },+ {+ name: "version 1 projects outside data are omitted",+ manifest: recoverytypes.Manifest{FormatVersion: 1, Environment: map[string]string{"DATABASE_URL": "file:/app/data/arcane.db", "PROJECTS_DIRECTORY": "/srv/projects"}},+ root: "/app/data", expected: snapshotLayoutInternal{dataPath: "/app/data", databaseName: "arcane.db"},+ },+ {+ name: "version 2 external projects", manifest: recoverytypes.Manifest{FormatVersion: 2, DataPath: "/data", ProjectsPath: "/projects", DatabasePath: "arcane.db"}, root: "/data",+ expected: snapshotLayoutInternal{dataPath: "/data", projectsPath: "/projects", databaseName: "arcane.db"},+ },+ {+ name: "version 2 mismatched root", manifest: recoverytypes.Manifest{FormatVersion: 2, DataPath: "/data", ProjectsPath: "/projects", DatabasePath: "arcane.db"}, root: "/",+ errorContains: "records data path /data but was found at /",+ },+ {+ name: "version 2 traversal", manifest: recoverytypes.Manifest{FormatVersion: 2, DataPath: "/data", ProjectsPath: "/../etc", DatabasePath: "arcane.db"}, root: "/data",+ errorContains: "invalid projects path",+ },+ {+ name: "version 2 malformed database path", manifest: recoverytypes.Manifest{FormatVersion: 2, DataPath: "/data", ProjectsPath: "/data", DatabasePath: ""}, root: "/data",+ errorContains: "invalid database path",+ },+ {name: "unsupported version", manifest: recoverytypes.Manifest{FormatVersion: 3}, root: "/data", errorContains: "unsupported format 3"},+ }+ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ layout, err := snapshotLayoutFromManifestInternal(test.manifest, test.root)+ if test.errorContains != "" {+ require.ErrorContains(t, err, test.errorContains)+ return+ }+ require.NoError(t, err)+ require.Equal(t, test.expected, layout)+ })+ }+}++func TestSnapshotLayoutProtectsDataFilesInternal(t *testing.T) {+ overlapping := snapshotLayoutInternal{dataPath: "/data", projectsPath: "/data", databaseName: "arcane.db"}+ require.True(t, overlapping.protectedInternal("arcane.db-wal"))+ require.True(t, overlapping.protectedInternal(".arcane-recovery.json"))+ require.False(t, overlapping.protectedInternal("demo/arcane.db"))+ nested := snapshotLayoutInternal{dataPath: "/data", projectsPath: "/data/projects", databaseName: "arcane.db"}+ require.False(t, nested.protectedInternal("arcane.db"))+ external := snapshotLayoutInternal{dataPath: "/data", projectsPath: "/projects", databaseName: "arcane.db"}+ require.False(t, external.protectedInternal(".arcane-recovery.json"))+ require.False(t, snapshotLayoutInternal{dataPath: "/data"}.projectsIncludedInternal())+}++func TestProjectsSnapshotPathInternal(t *testing.T) {+ tests := []struct {+ name, data, projects, expected string+ external bool+ errorContains string+ }{+ {name: "inside data", data: "/app/data", projects: "/app/data/projects", expected: "/data/projects"},+ {name: "nested deeper", data: "/app/data", projects: "/app/data/custom/projects/", expected: "/data/custom/projects"},+ {name: "equal to data", data: "/app/data", projects: "/app/data", expected: "/data"},+ {name: "external bind", data: "/app/data", projects: "/host/path/to/projects", expected: "/projects", external: true},+ {name: "sibling with shared prefix", data: "/app/data", projects: "/app/datasets", expected: "/projects", external: true},+ {name: "ancestor of data", data: "/app/data", projects: "/app", errorContains: "contains Arcane's data directory"},+ }+ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ projectsPath, external, err := projectsSnapshotPathInternal(test.data, test.projects)+ if test.errorContains != "" {+ require.ErrorContains(t, err, test.errorContains)+ return+ }+ require.NoError(t, err)+ require.Equal(t, test.expected, projectsPath)+ require.Equal(t, test.external, external)+ })+ }+}++func TestProjectsDirectoryInternalUsesContainerPathOfMapping(t *testing.T) {+ service := &SystemBackupService{config: &config.Config{ProjectsDirectory: "/app/data/projects:/host/projects"}}+ require.Equal(t, "/app/data/projects", service.projectsDirectoryInternal(context.Background()))+ require.Equal(t, "/app/data/projects:/host/projects", service.projectsSettingInternal(context.Background()))+}++func TestSourceMountsInternal(t *testing.T) {+ mounts := []containertypes.MountPoint{+ {Type: mounttypes.TypeVolume, Name: "arcane-data", Destination: "/app/data", RW: true},+ {Type: mounttypes.TypeBind, Source: "/host/projects", Destination: "/app/data/projects", RW: true},+ {Type: mounttypes.TypeBind, Source: "/host/external", Destination: "/srv/projects", RW: true},+ }+ data, err := sourceMountsInternal(mounts, true, "/app/data", "/data")+ require.NoError(t, err)+ require.Equal(t, []mounttypes.Mount{+ {Type: mounttypes.TypeVolume, Source: "arcane-data", Target: "/data", ReadOnly: true},+ {Type: mounttypes.TypeBind, Source: "/host/projects", Target: "/data/projects", ReadOnly: true},+ }, data)++ external, err := sourceMountsInternal(mounts, true, "/srv/projects", "/projects")+ require.NoError(t, err)+ require.Equal(t, []mounttypes.Mount{{Type: mounttypes.TypeBind, Source: "/host/external", Target: "/projects", ReadOnly: true}}, external)++ _, err = sourceMountsInternal(mounts, true, "/opt/projects", "/projects")+ require.ErrorContains(t, err, "/opt/projects must be mounted into the Arcane container")++ host, err := sourceMountsInternal(nil, false, "/tmp/data", "/data")+ require.NoError(t, err)+ require.Equal(t, []mounttypes.Mount{{Type: mounttypes.TypeBind, Source: "/tmp/data", Target: "/data", ReadOnly: true}}, host)+}++func TestWriteManifestInternalRecordsVersionTwoLayout(t *testing.T) {+ dataDirectory := t.TempDir()+ service := &SystemBackupService{config: &config.Config{DatabaseURL: "file:" + filepath.Join(dataDirectory, "arcane.db"), ProjectsDirectory: "/srv/projects"}}+ layout := backupSourceLayoutInternal{dataDirectory: dataDirectory, projectsDirectory: "/srv/projects", databaseName: "arcane.db", projectsPath: "/projects"}+ require.NoError(t, service.writeManifestInternal(context.Background(), "backup-1", layout))+ data, err := os.ReadFile(filepath.Join(dataDirectory, systemRecoveryManifestName))+ require.NoError(t, err)+ var manifest recoverytypes.Manifest+ require.NoError(t, json.Unmarshal(data, &manifest))+ require.Equal(t, recoverytypes.ManifestFormatVersion, manifest.FormatVersion)+ require.Equal(t, "/srv/projects", manifest.Environment["PROJECTS_DIRECTORY"])+ resolved, err := snapshotLayoutFromManifestInternal(manifest, "/data")+ require.NoError(t, err)+ require.Equal(t, snapshotLayoutInternal{dataPath: "/data", projectsPath: "/projects", databaseName: "arcane.db"}, resolved)+}++func TestRestoreTargetInternal(t *testing.T) {+ mounts := []containertypes.MountPoint{+ {Type: mounttypes.TypeVolume, Name: "arcane-data", Destination: "/app/data", RW: true},+ {Type: mounttypes.TypeBind, Source: "/host/projects", Destination: "/app/data/projects", RW: true},+ {Type: mounttypes.TypeBind, Source: "/host/templates", Destination: "/app/data/templates", RW: true},+ {Type: mounttypes.TypeBind, Source: "/host/ro", Destination: "/mnt/ro", RW: false},+ }+ target, err := restoreTargetInternal(mounts, "/app/data/custom/projects", "/restore-projects", "")+ require.NoError(t, err)+ require.Equal(t, "/restore-projects/custom/projects", target.Path)+ require.Equal(t, []mounttypes.Mount{{Type: mounttypes.TypeVolume, Source: "arcane-data", Target: "/restore-projects"}}, target.Mounts)++ data, err := restoreTargetInternal(mounts, "/app/data", "/restore", "/app/data/projects")+ require.NoError(t, err)+ require.Equal(t, "/restore", data.Path)+ require.Equal(t, []mounttypes.Mount{+ {Type: mounttypes.TypeVolume, Source: "arcane-data", Target: "/restore"},+ {Type: mounttypes.TypeBind, Source: "/host/templates", Target: "/restore/templates"},+ }, data.Mounts)++ _, err = restoreTargetInternal(mounts, "/mnt/ro/projects", "/restore-projects", "")+ require.ErrorContains(t, err, "mounted read-only")+ _, err = restoreTargetInternal(mounts, "/opt/projects", "/restore-projects", "")+ require.ErrorContains(t, err, "must be mounted into the Arcane container")+}++func TestRestoreStagesInternal(t *testing.T) {+ mounts := []containertypes.MountPoint{+ {Type: mounttypes.TypeVolume, Name: "arcane-data", Destination: "/app/data", RW: true},+ {Type: mounttypes.TypeBind, Source: "/host/nested", Destination: "/app/data/projects", RW: true},+ {Type: mounttypes.TypeBind, Source: "/host/external", Destination: "/srv/projects", RW: true},+ }+ repository := recoverytypes.RestoreRepository{Environment: []string{"RUSTIC_REPOSITORY=/repository"}}+ dataVolume := mounttypes.Mount{Type: mounttypes.TypeVolume, Source: "arcane-data", Target: "/restore"}+ nested := mounttypes.Mount{Type: mounttypes.TypeBind, Source: "/host/nested", Target: "/restore/projects"}++ t.Run("projects covered by data restore", func(t *testing.T) {+ layout := snapshotLayoutInternal{dataPath: "/data", projectsPath: "/data/projects", databaseName: "arcane.db"}+ stages, err := restoreStagesInternal(mounts, "/app/data", "/app/data/projects", repository, "snap", layout)+ require.NoError(t, err)+ require.Equal(t, []recoverytypes.RestoreStage{{Repository: repository, SnapshotID: "snap", SourcePath: "/data", Target: recoverytypes.RestoreTarget{Mounts: []mounttypes.Mount{dataVolume, nested}, Path: "/restore"}}}, stages)+ })+ t.Run("external projects restore separately", func(t *testing.T) {+ layout := snapshotLayoutInternal{dataPath: "/data", projectsPath: "/projects", databaseName: "arcane.db"}+ stages, err := restoreStagesInternal(mounts, "/app/data", "/srv/projects", repository, "snap", layout)+ require.NoError(t, err)+ require.Len(t, stages, 2)+ require.Equal(t, "/data", stages[0].SourcePath)+ require.Equal(t, []mounttypes.Mount{dataVolume, nested}, stages[0].Target.Mounts)+ require.Equal(t, "/projects", stages[1].SourcePath)+ require.Equal(t, recoverytypes.RestoreTarget{Mounts: []mounttypes.Mount{{Type: mounttypes.TypeBind, Source: "/host/external", Target: "/restore-projects"}}, Path: "/restore-projects"}, stages[1].Target)+ })+ t.Run("changed projects directory excludes the nested mount from the data stage", func(t *testing.T) {+ layout := snapshotLayoutInternal{dataPath: "/", projectsPath: "/projects", databaseName: "arcane.db"}+ stages, err := restoreStagesInternal(mounts, "/app/data", "/app/data/projects", repository, "snap", layout)+ require.NoError(t, err)+ require.Len(t, stages, 1)+ require.Equal(t, []mounttypes.Mount{dataVolume, nested}, stages[0].Target.Mounts)++ external := snapshotLayoutInternal{dataPath: "/data", projectsPath: "/projects", databaseName: "arcane.db"}+ stages, err = restoreStagesInternal(mounts, "/app/data", "/app/data/projects", repository, "snap", external)+ require.NoError(t, err)+ require.Len(t, stages, 2)+ require.Equal(t, []mounttypes.Mount{dataVolume}, stages[0].Target.Mounts)+ require.Equal(t, recoverytypes.RestoreTarget{Mounts: []mounttypes.Mount{{Type: mounttypes.TypeBind, Source: "/host/nested", Target: "/restore-projects"}}, Path: "/restore-projects"}, stages[1].Target)++ stages, err = restoreStagesInternal(mounts, "/app/data", "/app/data/custom/projects", repository, "snap", external)+ require.NoError(t, err)+ require.Len(t, stages, 2)+ require.Equal(t, []mounttypes.Mount{dataVolume, nested}, stages[0].Target.Mounts)+ require.Equal(t, recoverytypes.RestoreTarget{Mounts: []mounttypes.Mount{{Type: mounttypes.TypeVolume, Source: "arcane-data", Target: "/restore-projects"}}, Path: "/restore-projects/custom/projects"}, stages[1].Target)+ })+ t.Run("version 1 backup without projects restores data only", func(t *testing.T) {+ layout := snapshotLayoutInternal{dataPath: "/app/data", databaseName: "arcane.db"}+ stages, err := restoreStagesInternal(mounts, "/app/data", "/srv/projects", repository, "snap", layout)+ require.NoError(t, err)+ require.Len(t, stages, 1)+ require.Equal(t, "/app/data", stages[0].SourcePath)+ })+ t.Run("projects at data root cannot move", func(t *testing.T) {+ layout := snapshotLayoutInternal{dataPath: "/data", projectsPath: "/data", databaseName: "arcane.db"}+ _, err := restoreStagesInternal(mounts, "/app/data", "/srv/projects", repository, "snap", layout)+ require.ErrorContains(t, err, "keeps projects in Arcane's data directory")+ stages, err := restoreStagesInternal(mounts, "/app/data", "/app/data", repository, "snap", layout)+ require.NoError(t, err)+ require.Len(t, stages, 1)+ })+ t.Run("unmounted destination fails", func(t *testing.T) {+ layout := snapshotLayoutInternal{dataPath: "/data", projectsPath: "/projects", databaseName: "arcane.db"}+ _, err := restoreStagesInternal(mounts, "/app/data", "/opt/projects", repository, "snap", layout)+ require.ErrorContains(t, err, "/opt/projects must be mounted")+ })+}++func TestSafetySnapshotContainsPathInternal(t *testing.T) {+ safety := systemBackupSafetySnapshotInternal{paths: map[string]struct{}{"demo": {}, "demo/compose.yaml": {}}}+ require.True(t, safetySnapshotContainsPathInternal(safety, ""))+ require.True(t, safetySnapshotContainsPathInternal(safety, "demo"))+ require.True(t, safetySnapshotContainsPathInternal(safety, "demo/compose.yaml"))+ require.False(t, safetySnapshotContainsPathInternal(safety, "other"))+ require.ErrorContains(t, removeProjectFileInternal(context.Background(), t.TempDir(), ""), "refusing to remove")+}++func TestLegacyLayoutOmittedProjectsIsActionable(t *testing.T) {+ require.True(t, errors.Is(errProjectsNotInBackupInternal, errProjectsNotInBackupInternal))+ require.Contains(t, errProjectsNotInBackupInternal.Error(), "create a new system backup")+}diff --git a/backend/internal/volume/backup.go b/backend/internal/volume/backup.goindex 986134337..c6039ba51 100644--- a/backend/internal/volume/backup.go+++ b/backend/internal/volume/backup.go@@ -762,7 +762,7 @@ func (s *VolumeService) executeBackupInternal(ctx context.Context, entry *Volumeif passwordErr != nil {return passwordErr}- localSnapshot, err = s.engine.CreateSnapshot(ctx, dockerClient, repository, password, volumeName, volumeSourceMountInternal(volumeName))+ localSnapshot, err = s.engine.CreateSnapshot(ctx, dockerClient, repository, password, volumeName, backup.RootSnapshotInput(volumeSourceMountInternal(volumeName)))if err != nil {return fmt.Errorf("failed to create local Rustic snapshot: %w", err)}@@ -786,7 +786,7 @@ func (s *VolumeService) executeBackupInternal(ctx context.Context, entry *Volume}remoteSnapshot, err = s.engine.Replicate(ctx, dockerClient, localRepository, localSnapshot.ID, remoteRepository, password, volumeName)} else {- remoteSnapshot, err = s.engine.CreateSnapshot(ctx, dockerClient, remoteRepository, password, volumeName, volumeSourceMountInternal(volumeName))+ remoteSnapshot, err = s.engine.CreateSnapshot(ctx, dockerClient, remoteRepository, password, volumeName, backup.RootSnapshotInput(volumeSourceMountInternal(volumeName)))}if err != nil {return fmt.Errorf("failed to create S3 Rustic snapshot: %w", err)diff --git a/backend/pkg/dockerutil/mount_utils.go b/backend/pkg/dockerutil/mount_utils.goindex 4144a11ac..ae8979d98 100644--- a/backend/pkg/dockerutil/mount_utils.go+++ b/backend/pkg/dockerutil/mount_utils.go@@ -108,6 +108,57 @@ func MountForSubpath(mounts []container.MountPoint, containerPath string, target}}+// MountForEnclosingPath returns the most-specific mount covering containerPath+// mounted whole at target, plus containerPath's path relative to that mount.+// Unlike MountForSubpath the subpath need not exist yet, so restores can create+// it. Returns nil when no bind or volume mount covers containerPath.+func MountForEnclosingPath(mounts []container.MountPoint, containerPath string, target string) (*mount.Mount, string) {+ if strings.TrimSpace(containerPath) == "" {+ return nil, ""+ }+ var best *container.MountPoint+ for i := range mounts {+ m := &mounts[i]+ if m.Destination == "" || !pathHasPrefixInternal(containerPath, m.Destination) {+ continue+ }+ if best == nil || len(m.Destination) > len(best.Destination) {+ best = m+ }+ }+ if best == nil {+ return nil, ""+ }+ enclosing := MountForDestination(mounts, best.Destination, target)+ if enclosing == nil {+ return nil, ""+ }+ return enclosing, strings.TrimPrefix(strings.TrimPrefix(containerPath, best.Destination), "/")+}++// NestedMounts mirrors every bind or volume mount whose destination lies+// strictly below containerPath, re-targeted beneath target, so a helper+// container sees the same tree the current container does. Parents precede+// their children; unsupported mount types are skipped.+func NestedMounts(mounts []container.MountPoint, containerPath string, target string) []mount.Mount {+ containerPath = strings.TrimRight(containerPath, "/")+ if containerPath == "" {+ return nil+ }+ var nested []mount.Mount+ for _, m := range mounts {+ if m.Destination == containerPath || !pathHasPrefixInternal(m.Destination, containerPath) {+ continue+ }+ relative := strings.TrimPrefix(strings.TrimPrefix(m.Destination, containerPath), "/")+ if mirrored := MountForDestination(mounts, m.Destination, path.Join(target, relative)); mirrored != nil {+ nested = append(nested, *mirrored)+ }+ }+ slices.SortStableFunc(nested, func(a, b mount.Mount) int { return len(a.Target) - len(b.Target) })+ return nested+}+// pathHasPrefixInternal reports whether containerPath is at or under prefix,// treating both as POSIX-style paths. Avoids false positives like// "/app/datax" matching "/app/data".diff --git a/backend/pkg/dockerutil/mount_utils_test.go b/backend/pkg/dockerutil/mount_utils_test.goindex 86c6c16ba..0e5acfbf2 100644--- a/backend/pkg/dockerutil/mount_utils_test.go+++ b/backend/pkg/dockerutil/mount_utils_test.go@@ -235,3 +235,48 @@ func TestPreserveVolumeMounts(t *testing.T) {})}}++func TestMountForEnclosingPath(t *testing.T) {+ mounts := []containertypes.MountPoint{+ {Type: mounttypes.TypeVolume, Name: "arcane-data", Destination: "/app/data", RW: true},+ {Type: mounttypes.TypeBind, Source: "/host/projects", Destination: "/app/data/projects", RW: true},+ {Type: mounttypes.TypeTmpfs, Destination: "/app/data/tmp"},+ }+ enclosing, relative := MountForEnclosingPath(mounts, "/app/data/custom/projects", "/restore")+ require.Equal(t, &mounttypes.Mount{Type: mounttypes.TypeVolume, Source: "arcane-data", Target: "/restore"}, enclosing)+ require.Equal(t, "custom/projects", relative)++ enclosing, relative = MountForEnclosingPath(mounts, "/app/data/projects/demo", "/restore")+ require.Equal(t, &mounttypes.Mount{Type: mounttypes.TypeBind, Source: "/host/projects", Target: "/restore"}, enclosing)+ require.Equal(t, "demo", relative)++ enclosing, relative = MountForEnclosingPath(mounts, "/app/data", "/restore")+ require.Equal(t, "arcane-data", enclosing.Source)+ require.Equal(t, "", relative)++ enclosing, _ = MountForEnclosingPath(mounts, "/app/data/tmp/x", "/restore")+ require.Nil(t, enclosing)+ enclosing, _ = MountForEnclosingPath(mounts, "/srv/projects", "/restore")+ require.Nil(t, enclosing)+ enclosing, _ = MountForEnclosingPath(mounts, "", "/restore")+ require.Nil(t, enclosing)+}++func TestNestedMounts(t *testing.T) {+ mounts := []containertypes.MountPoint{+ {Type: mounttypes.TypeBind, Source: "/host/deep", Destination: "/app/data/projects/deep", RW: false},+ {Type: mounttypes.TypeVolume, Name: "arcane-data", Destination: "/app/data", RW: true},+ {Type: mounttypes.TypeBind, Source: "/host/projects", Destination: "/app/data/projects", RW: true},+ {Type: mounttypes.TypeTmpfs, Destination: "/app/data/tmp"},+ {Type: mounttypes.TypeBind, Source: "/host/other", Destination: "/app/datasets", RW: true},+ }+ require.Equal(t, []mounttypes.Mount{+ {Type: mounttypes.TypeBind, Source: "/host/projects", Target: "/data/projects"},+ {Type: mounttypes.TypeBind, Source: "/host/deep", Target: "/data/projects/deep", ReadOnly: true},+ }, NestedMounts(mounts, "/app/data", "/data"))+ require.Equal(t, []mounttypes.Mount{+ {Type: mounttypes.TypeBind, Source: "/host/deep", Target: "/projects/deep", ReadOnly: true},+ }, NestedMounts(mounts, "/app/data/projects/", "/projects"))+ require.Empty(t, NestedMounts(mounts, "/app/data/projects/deep", "/x"))+ require.Empty(t, NestedMounts(mounts, "", "/x"))+}diff --git a/frontend/messages/en.json b/frontend/messages/en.jsonindex 3233dcb01..af8141546 100644--- a/frontend/messages/en.json+++ b/frontend/messages/en.json@@ -1024,6 +1024,7 @@"volumes_backup_restore_discard_changes": "You have unsaved workspace changes. Continuing will discard all unsaved text and staged file operations after the restore succeeds.","backup_file_browser_select_all_matches": "Select all search matches","backup_file_browser_load_remaining_failed": "The remaining backup files could not be loaded.",+ "backup_file_browser_load_failed": "The backup files could not be loaded.","volumes_backup_safety_info": "A safety backup will be created automatically before restoring, so you can undo this operation if needed.","volume_backup_restore_files_lifecycle_info": "Arcane will stop containers using this volume, create a safety backup, restore the selected files, and then restart the containers.","volumes_backup_overwrite_warning": "Selected files will overwrite existing files at those paths.",@@ -3453,6 +3454,7 @@"system_backups_restore_failed": "Failed to start the Arcane restore.","system_backups_restore_files_description": "Select project files from this backup to restore.","system_backups_restore_files_lifecycle_info": "Arcane will create a complete local safety backup and then overwrite the selected project files. Arcane and project containers will keep running.",+ "system_backups_restore_files_current_directory": "Project files are restored into the currently configured projects directory, even if the backup was taken from a different location.","system_backups_load_files": "Load files","system_backups_restore_files_success": "{count} project file(s) restored successfully. A safety backup was created before restoring.","system_backups_restore_selection_success": "The selected project content was restored successfully. A safety backup was created before restoring.",diff --git a/frontend/src/lib/components/backup-file-picker.svelte b/frontend/src/lib/components/backup-file-picker.svelteindex 73d177b74..493754802 100644--- a/frontend/src/lib/components/backup-file-picker.svelte+++ b/frontend/src/lib/components/backup-file-picker.svelte@@ -1,20 +1,21 @@<script lang="ts">import { onDestroy, onMount, tick, untrack } from 'svelte';import { tryCatch } from '#lib/utils/try-catch.js';+ import { extractApiErrorMessage } from '#lib/utils/api.js';import { ArcaneButton } from '#lib/components/arcane-button/index.js';import FileTreeRow from '#lib/components/file-tree-row.svelte';import { Input } from '#lib/components/ui/input/index.js';import { Spinner } from '#lib/components/ui/spinner/index.js';import { createVirtualizer } from '#lib/components/ui/virtualizer.svelte.js';- import type { BackupFileEntry, BackupFileProvider } from '#lib/types/backup.js';+ import type { BackupFileEntry, BackupFileProvider, BackupFileRootLoadState } from '#lib/types/backup.js';import * as m from '#lib/paraglide/messages.js';type FolderPageState = {entries: BackupFileEntry[];continuationStart?: number;loading: boolean;- error: boolean;+ error: string | null;requestID: number;};@@ -26,12 +27,14 @@provider,selectedPaths = $bindable([]),selectAll = $bindable(false),- search = $bindable('')+ search = $bindable(''),+ onRootLoad}: {provider: BackupFileProvider;selectedPaths?: string[];selectAll?: boolean;search?: string;+ onRootLoad?: (state: BackupFileRootLoadState) => void;} = $props();let pages = $state<Record<string, FolderPageState>>({});@@ -51,6 +54,9 @@return result;});const rootLoading = $derived(pages['']?.loading === true && pages['']?.entries.length === 0);+ const rootError = $derived(+ pages[''] !== undefined && !pages['']?.loading && pages['']?.entries.length === 0 ? (pages['']?.error ?? null) : null+ );const rootEmpty = $derived(pages[''] !== undefined && !pages['']?.loading && !pages['']?.error && pages['']?.entries.length === 0);@@ -70,7 +76,7 @@}));function pageStateInternal(folder: string): FolderPageState {- return pages[folder] ?? { entries: [], loading: false, error: false, requestID: 0 };+ return pages[folder] ?? { entries: [], loading: false, error: null, requestID: 0 };}function updatePageInternal(folder: string, state: FolderPageState) {@@ -108,7 +114,9 @@const current = pageStateInternal(folder);if (current.loading) return;const requestID = ++nextRequestID;- updatePageInternal(folder, { ...current, loading: true, error: false, requestID });+ const rootLoad = folder === '' && replace;+ updatePageInternal(folder, { ...current, loading: true, error: null, requestID });+ if (rootLoad) onRootLoad?.('loading');const requestResult1 = await tryCatch((async () => {@@ -126,15 +134,17 @@entries: unique,continuationStart: continuationStart < page.pagination.totalItems ? continuationStart : undefined,loading: false,- error: false,+ error: null,requestID});+ if (rootLoad) onRootLoad?.('ready');})());if (requestResult1.error !== null) {const latest = pages[folder];if (generation !== requestGeneration || latest?.requestID !== requestID) return;- updatePageInternal(folder, { ...latest, loading: false, error: true });+ updatePageInternal(folder, { ...latest, loading: false, error: extractApiErrorMessage(requestResult1.error) });+ if (rootLoad) onRootLoad?.('error');return;}await tick();@@ -259,6 +269,18 @@<div class="flex items-center justify-center py-8"><Spinner class="size-5 text-muted-foreground" /></div>+ {:else if rootError !== null}+ <div class="flex flex-col items-center justify-center gap-2 px-4 py-8 text-center text-sm" data-backup-file-root-error>+ <span class="font-medium">{m.backup_file_browser_load_failed()}</span>+ <span class="text-xs text-muted-foreground">{rootError}</span>+ <ArcaneButton+ action="base"+ tone="ghost"+ size="sm"+ customLabel={m.common_retry()}+ onclick={() => loadPageInternal('', 0, true)}+ />+ </div>{:else if rootEmpty}<div class="flex items-center justify-center py-8 text-sm text-muted-foreground">{m.volume_backup_no_files()}@@ -304,6 +326,7 @@>{#if state?.error}<span>{m.backup_file_browser_load_remaining_failed()}</span>+ <span class="truncate" title={state.error}>{state.error}</span><ArcaneButtonaction="base"tone="ghost"diff --git a/frontend/src/lib/types/backup.ts b/frontend/src/lib/types/backup.tsindex f37e4cdb8..0ecf5af67 100644--- a/frontend/src/lib/types/backup.ts+++ b/frontend/src/lib/types/backup.ts@@ -77,3 +77,5 @@ export type BackupRestoreSelection = {export type BackupFileProvider = {browse(request: BackupFileBrowseRequest): Promise<Paginated<BackupFileEntry>>;};++export type BackupFileRootLoadState = 'loading' | 'ready' | 'error';diff --git a/frontend/src/routes/(app)/settings/backups/+page.svelte b/frontend/src/routes/(app)/settings/backups/+page.svelteindex 94c20fe75..b3106ff62 100644--- a/frontend/src/routes/(app)/settings/backups/+page.svelte+++ b/frontend/src/routes/(app)/settings/backups/+page.svelte@@ -43,7 +43,7 @@import SystemVolumeScopeFields from './system-volume-scope-fields.svelte';import BackupFilePicker from '#lib/components/backup-file-picker.svelte';import { activityToastOptions, extractActivityId } from '#lib/utils/activity-toast.js';- import type { BackupFileProvider } from '#lib/types/backup.js';+ import type { BackupFileProvider, BackupFileRootLoadState } from '#lib/types/backup.js';import { queryKeys } from '#lib/query/query-keys.js';import { useQueryClient } from '@tanstack/svelte-query';@@ -247,36 +247,46 @@actionOpen = true;}+ // Replacing the provider remounts the picker; its root-load callback is the+ // only thing that marks the dialog ready to restore.+ function resetRestoreFilesPicker() {+ restoreFilesProvider = null;+ restoreFilesLoaded = false;+ restoreFilesSelectedPaths = [];+ restoreFilesSelectAll = false;+ restoreFilesSearch = '';+ }+async function openRestoreFiles(backup: BackupHistoryEntry) {restoreFilesTarget = backup;restoreFilesRecoveryKey = '';- restoreFilesProvider = null;- restoreFilesLoaded = false;+ resetRestoreFilesPicker();restoreFilesOpen = true;if (policyCollection.recoveryKeyStored) loadRestoreFiles();}function closeRestoreFiles() {restoreFilesOpen = false;+ resetRestoreFilesPicker();}function updateRestoreFilesRecoveryKey(value: string) {restoreFilesRecoveryKey = value;- restoreFilesProvider = null;- restoreFilesLoaded = false;+ resetRestoreFilesPicker();}function loadRestoreFiles() {if (!restoreFilesTarget || restoreFilesKeyInvalid) return;- restoreFilesSelectedPaths = [];- restoreFilesSelectAll = false;- restoreFilesSearch = '';+ resetRestoreFilesPicker();const backupID = restoreFilesTarget.id;const recoveryKey = restoreFilesRecoveryKey.trim();restoreFilesProvider = {browse: (request) => systemBackupService.browseFiles(backupID, recoveryKey, request)};- restoreFilesLoaded = true;+ }++ function updateRestoreFilesRootLoad(state: BackupFileRootLoadState) {+ restoreFilesLoaded = state === 'ready';}async function restoreSelectedFiles() {@@ -790,6 +800,7 @@<InfoIcon class="size-4" /><Alert.Description class="text-xs">{m.system_backups_restore_files_lifecycle_info()}+ {m.system_backups_restore_files_current_directory()}</Alert.Description></Alert.Root>@@ -822,6 +833,7 @@bind:selectedPaths={restoreFilesSelectedPaths}bind:selectAll={restoreFilesSelectAll}bind:search={restoreFilesSearch}+ onRootLoad={updateRestoreFilesRootLoad}/>{/key}diff --git a/types/recovery/recovery.go b/types/recovery/recovery.gonew file mode 100644index 000000000..4d155b138--- /dev/null+++ b/types/recovery/recovery.go@@ -0,0 +1,83 @@+package recovery++import (+ "time"++ "github.com/moby/moby/api/types/mount"+)++// ManifestFormatVersion is the manifest format new system backups write.+const ManifestFormatVersion = 2++// Manifest is the recovery manifest stored inside every system backup.+// Version 2 records where Arcane data and projects live in the snapshot.+type Manifest struct {+ FormatVersion int `json:"formatVersion"`+ ArcaneVersion string `json:"arcaneVersion"`+ BackupID string `json:"backupId,omitempty"`+ ActivityID string `json:"activityId,omitempty"`+ CreatedAt time.Time `json:"createdAt"`+ // DataPath and ProjectsPath are snapshot-relative roots (version 2).+ DataPath string `json:"dataPath,omitempty"`+ ProjectsPath string `json:"projectsPath,omitempty"`+ // DatabasePath is the SQLite file relative to DataPath (version 2).+ DatabasePath string `json:"databasePath,omitempty"`+ Environment map[string]string `json:"environment"`+}++// RestoreTarget is a writable destination inside a helper container: the+// mounts to attach (the enclosing mount first, then mounts nested inside it)+// and the path beneath them that receives the restored files.+type RestoreTarget struct {+ Mounts []mount.Mount `json:"mounts"`+ Path string `json:"path"`+}++// RestoreRepository addresses the Rustic repository a stage reads from.+type RestoreRepository struct {+ Environment []string `json:"environment"`+ Mounts []mount.Mount `json:"mounts"`+}++// RestoreStage restores one snapshot path into one target.+type RestoreStage struct {+ Repository RestoreRepository `json:"repository"`+ SnapshotID string `json:"snapshotId"`+ SourcePath string `json:"sourcePath"`+ Target RestoreTarget `json:"target"`+}++// RestoreRequest is handed to the detached recovery helper.+type RestoreRequest struct {+ BackupID string `json:"backupId"`+ ContainerID string `json:"containerId"`+ ContainerImage string `json:"containerImage"`+ SnapshotID string `json:"snapshotId"`+ LocalSnapshotID string `json:"localSnapshotId,omitempty"`+ RemoteSnapshotID string `json:"remoteSnapshotId,omitempty"`+ S3DestinationID string `json:"s3DestinationId,omitempty"`+ Size int64 `json:"size,omitempty"`+ SafetyBackup *SafetyBackup `json:"safetyBackup,omitempty"`+ RecoveryKey string `json:"recoveryKey"`+ // ProjectsSetting is the current projectsDirectory setting; it survives the+ // restore so projects keep resolving to where they were restored.+ ProjectsSetting string `json:"projectsSetting,omitempty"`+ // ProjectsIncluded is false for backups that predate separately mounted+ // projects; the current projects directory is then left untouched.+ ProjectsIncluded bool `json:"projectsIncluded"`+ // Stages restore the backup in order; RollbackStages restore the safety+ // snapshot when any stage fails.+ Stages []RestoreStage `json:"stages"`+ RollbackStages []RestoreStage `json:"rollbackStages,omitempty"`+ // NetworkMode attaches the detached Rustic restore container to one of+ // Arcane's networks so network-local S3 endpoints stay reachable while+ // Arcane itself is stopped.+ NetworkMode string `json:"networkMode,omitempty"`+}++type SafetyBackup struct {+ ID string `json:"id"`+ LocalSnapshotID string `json:"localSnapshotId"`+ Size int64 `json:"size"`+ CreatedAt time.Time `json:"createdAt"`+}