From 2c9b353cbff8317811f215fa68bb538e3c7f497d Mon Sep 17 00:00:00 2001 From: Rob Martin Date: Sun, 15 Nov 2026 11:57:01 -0700 Subject: [PATCH] repository: do not delete config on atomic backends during upgrade rollback (#22136) In UpgradeRepo, if upgradeRepository fails, the error handler attempts contingency rollback by re-uploading the original config file. However, the contingency path unconditionally executed repo.be.Remove(ctx, h) before calling repo.be.Save. On backends supporting atomic replacement (such as local filesystems), a failed upgrade (e.g. out-of-disk-space or permissions error while writing the temporary file) leaves the original, valid version 1 configuration file completely intact on disk. Unconditionally removing the config file during rollback actively deletes the working configuration. If the subsequent Save call also fails, the repository is left permanently corrupted without a config file. Condition the pre-removal on !repo.be.Properties().HasAtomicReplace, matching the forward upgrade path in upgradeRepository. Add a unit test asserting that aborted upgrades on atomic backends preserve the intact original configuration. --------- Co-authored-by: Michael Eischer --- changelog/unreleased/issue-22136 | 10 +++++++ internal/migrations/upgrade_repo_v2.go | 2 +- internal/repository/upgrade_repo.go | 11 +++++--- internal/repository/upgrade_repo_test.go | 34 ++++++++++++++++++++++-- 4 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 changelog/unreleased/issue-22136 diff --git a/changelog/unreleased/issue-22136 b/changelog/unreleased/issue-22136 new file mode 100644 index 000000000..892fa32c3 --- /dev/null +++ b/changelog/unreleased/issue-22136 @@ -0,0 +1,10 @@ +Bugfix: Improve silience of repository v2 upgrade rollback + +When upgrading a repository from version 1 to 2, an aborted migration +attempted contingency rollback by unconditionally removing the configuration +file before re-saving it. On atomic backends (such as local filesystems), +the original configuration was intact, and deleting it during a failed rollback +left the repository unopenable. Restic now conditions config pre-removal +on whether the backend lacks atomic replacement. + +https://github.com/restic/restic/pull/22136 diff --git a/internal/migrations/upgrade_repo_v2.go b/internal/migrations/upgrade_repo_v2.go index 23a7f1ff0..079058dcb 100644 --- a/internal/migrations/upgrade_repo_v2.go +++ b/internal/migrations/upgrade_repo_v2.go @@ -36,5 +36,5 @@ func (*UpgradeRepoV2) RepoCheck() bool { } func (m *UpgradeRepoV2) Apply(ctx context.Context, repo restic.Repository) error { - return repository.UpgradeRepo(ctx, repo.(*repository.Repository)) + return repository.UpgradeRepo(ctx, repo.(*repository.Repository), "") } diff --git a/internal/repository/upgrade_repo.go b/internal/repository/upgrade_repo.go index e85840323..e2419e6eb 100644 --- a/internal/repository/upgrade_repo.go +++ b/internal/repository/upgrade_repo.go @@ -53,12 +53,12 @@ func upgradeRepository(ctx context.Context, repo *Repository) error { return nil } -func UpgradeRepo(ctx context.Context, repo *Repository) error { +func UpgradeRepo(ctx context.Context, repo *Repository, tempBaseDir string) error { if repo.Config().Version != 1 { return fmt.Errorf("repository has version %v, only upgrades from version 1 are supported", repo.Config().Version) } - tempdir, err := os.MkdirTemp("", "restic-migrate-upgrade-repo-v2-") + tempdir, err := os.MkdirTemp(tempBaseDir, "restic-migrate-upgrade-repo-v2-") if err != nil { return fmt.Errorf("create temp dir failed: %w", err) } @@ -87,8 +87,11 @@ func UpgradeRepo(ctx context.Context, repo *Repository) error { BackupFilePath: backupFileName, } - // try contingency methods, reupload the original file - _ = repo.be.Remove(ctx, h) + // Reupload the original file. For atomic-replace backends, keep the + // existing config intact in case the recovery write also fails. + if !repo.be.Properties().HasAtomicReplace { + _ = repo.be.Remove(ctx, h) + } err = repo.be.Save(ctx, h, backend.NewByteReader(rawConfigFile, nil)) if err != nil { repoError.ReuploadOldConfigError = err diff --git a/internal/repository/upgrade_repo_test.go b/internal/repository/upgrade_repo_test.go index c6bc574cf..f2d4cf273 100644 --- a/internal/repository/upgrade_repo_test.go +++ b/internal/repository/upgrade_repo_test.go @@ -8,7 +8,9 @@ import ( "testing" "github.com/restic/restic/internal/backend" + "github.com/restic/restic/internal/backend/local" "github.com/restic/restic/internal/errors" + "github.com/restic/restic/internal/restic" rtest "github.com/restic/restic/internal/test" ) @@ -18,7 +20,7 @@ func TestUpgradeRepoV2(t *testing.T) { t.Fatal("test repo has wrong version") } - err := UpgradeRepo(context.Background(), repo) + err := UpgradeRepo(context.Background(), repo, "") rtest.OK(t, err) } @@ -46,6 +48,34 @@ func (be *failBackend) Save(ctx context.Context, h backend.Handle, rd backend.Re return be.Backend.Save(ctx, h, rd) } +func TestUpgradeRepoV2FailurePreservesLocalConfig(t *testing.T) { + ctx := context.Background() + be, err := local.Create(ctx, local.Config{Path: t.TempDir()}, t.Logf) + rtest.OK(t, err) + + // Both the upgrade and the recovery write fail after initialization. + repo, _ := TestRepositoryWithBackend(t, &failBackend{ + ConfigFileSavesUntilError: 1, + Backend: be, + }, 1, Options{}) + originalConfig, err := repo.LoadRaw(ctx, restic.ConfigFile, restic.ID{}) + rtest.OK(t, err) + + err = UpgradeRepo(ctx, repo, t.TempDir()) + if err == nil { + t.Fatal("expected upgrade to fail") + } + upgradeErr := err.(*upgradeRepoV2Error) + if upgradeErr.ReuploadOldConfigError == nil { + t.Fatal("expected recovery write to fail") + } + + // A failed recovery must not delete the intact original config. + preservedConfig, err := repo.LoadRaw(ctx, restic.ConfigFile, restic.ID{}) + rtest.OK(t, err) + rtest.Equals(t, originalConfig, preservedConfig) +} + func TestUpgradeRepoV2Failure(t *testing.T) { be := TestBackend(t) @@ -60,7 +90,7 @@ func TestUpgradeRepoV2Failure(t *testing.T) { t.Fatal("test repo has wrong version") } - err := UpgradeRepo(context.Background(), repo) + err := UpgradeRepo(context.Background(), repo, "") if err == nil { t.Fatal("expected error returned from Apply(), got nil") }