From c1bbbcd0dcc7aa0fe7a181c67b5e08306184611e Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sun, 1 May 2022 20:16:49 +0200 Subject: [PATCH] migrate: Allow migrations to request a check run This is currently only used by upgrade_repo_v2. --- cmd/restic/cmd_migrate.go | 14 ++++++++++++++ internal/migrations/interface.go | 5 +++++ internal/migrations/s3_layout.go | 4 ++++ internal/migrations/upgrade_repo_v2.go | 3 +++ 4 files changed, 26 insertions(+) diff --git a/cmd/restic/cmd_migrate.go b/cmd/restic/cmd_migrate.go index f82439715..10d78b0ca 100644 --- a/cmd/restic/cmd_migrate.go +++ b/cmd/restic/cmd_migrate.go @@ -84,6 +84,20 @@ func applyMigrations(opts MigrateOptions, gopts GlobalOptions, repo restic.Repos Warnf("check for migration %v failed, continuing anyway\n", m.Name()) } + repoCheckOpts := m.RepoCheckOptions() + if repoCheckOpts != nil { + Printf("checking repository integrity...\n") + + checkOptions := CheckOptions{} + checkGopts := gopts + // the repository is already locked + checkGopts.NoLock = true + err = runCheck(checkOptions, checkGopts, []string{}) + if err != nil { + return err + } + } + Printf("applying migration %v...\n", m.Name()) if err = m.Apply(ctx, repo); err != nil { Warnf("migration %v failed: %v\n", m.Name(), err) diff --git a/internal/migrations/interface.go b/internal/migrations/interface.go index 9d9eedba1..eb0a8e60c 100644 --- a/internal/migrations/interface.go +++ b/internal/migrations/interface.go @@ -6,11 +6,16 @@ import ( "github.com/restic/restic/internal/restic" ) +type RepositoryCheckOptions struct { +} + // Migration implements a data migration. type Migration interface { // Check returns true if the migration can be applied to a repo. Check(context.Context, restic.Repository) (bool, error) + RepoCheckOptions() *RepositoryCheckOptions + // Apply runs the migration. Apply(context.Context, restic.Repository) error diff --git a/internal/migrations/s3_layout.go b/internal/migrations/s3_layout.go index 877b44c84..b64c3b073 100644 --- a/internal/migrations/s3_layout.go +++ b/internal/migrations/s3_layout.go @@ -37,6 +37,10 @@ func (m *S3Layout) Check(ctx context.Context, repo restic.Repository) (bool, err return true, nil } +func (m *S3Layout) RepoCheckOptions() *RepositoryCheckOptions { + return nil +} + func retry(max int, fail func(err error), f func() error) error { var err error for i := 0; i < max; i++ { diff --git a/internal/migrations/upgrade_repo_v2.go b/internal/migrations/upgrade_repo_v2.go index 02806e468..86abeaeff 100644 --- a/internal/migrations/upgrade_repo_v2.go +++ b/internal/migrations/upgrade_repo_v2.go @@ -50,6 +50,9 @@ func (*UpgradeRepoV2) Check(ctx context.Context, repo restic.Repository) (bool, return isV1, nil } +func (*UpgradeRepoV2) RepoCheckOptions() *RepositoryCheckOptions { + return &RepositoryCheckOptions{} +} func (*UpgradeRepoV2) upgrade(ctx context.Context, repo restic.Repository) error { h := restic.Handle{Type: restic.ConfigFile}