diff options
Diffstat (limited to 'internal/backup/backup.go')
-rw-r--r-- | internal/backup/backup.go | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/internal/backup/backup.go b/internal/backup/backup.go index 02af48455..882327517 100644 --- a/internal/backup/backup.go +++ b/internal/backup/backup.go @@ -359,9 +359,18 @@ func (mgr *Manager) Restore(ctx context.Context, req *RestoreRequest) error { return fmt.Errorf("manager: no backup steps") } - // If we can't reset the refs, perform a full restore by recreating the repo and cloning from the bundle. - if err := mgr.restoreFromBundle(ctx, repo, backup, req.AlwaysCreate); err != nil { - return fmt.Errorf("manager: restore from bundle: %w", err) + // Restore Git objects, potentially from increments. + if err := mgr.restoreFromRefs(ctx, repo, backup); err != nil { + mgr.logger.WithFields(log.Fields{ + "storage": req.Repository.GetStorageName(), + "relative_path": req.Repository.GetRelativePath(), + "backup_id": backup.ID, + }).Warn("unable to reset refs. Proceeding with a normal restore") + + // If we can't reset the refs, perform a full restore by recreating the repo and cloning from the bundle. + if err := mgr.restoreFromBundle(ctx, repo, backup, req.AlwaysCreate); err != nil { + return fmt.Errorf("manager: restore from bundle: %w", err) + } } // Restore custom hooks. The path is the same regardless of increment. @@ -369,6 +378,22 @@ func (mgr *Manager) Restore(ctx context.Context, req *RestoreRequest) error { return mgr.restoreCustomHooks(ctx, repo, latestStep.CustomHooksPath) } +func (mgr *Manager) restoreFromRefs(ctx context.Context, repo Repository, backup *Backup) error { + latestStep := backup.Steps[len(backup.Steps)-1] + refs, err := mgr.readRefs(ctx, latestStep.RefPath) + if err != nil { + return fmt.Errorf("read refs from backup: %w", err) + } + + // Explicitly reset HEAD to the default branch tracked by the manifest if available. In a + // bundle restore, this would've been done during repository creation. + if defaultBranch, ok := git.ReferenceName(backup.HeadReference).Branch(); ok { + refs = append(refs, git.NewSymbolicReference("HEAD", git.ReferenceName(defaultBranch))) + } + + return repo.ResetRefs(ctx, refs) +} + func (mgr *Manager) restoreFromBundle(ctx context.Context, repo Repository, backup *Backup, alwaysCreate bool) error { hash, err := git.ObjectHashByFormat(backup.ObjectFormat) if err != nil { |