Skip to content

cmd: keep the database untouched when gogs restore fails to move files - #8453

Merged
unknwon merged 5 commits into
gogs:mainfrom
b84330808:fix-restore-leftover-bak
Nov 9, 2026
Merged

unknwon merged 5 commits into
gogs:mainfrom
b84330808:fix-restore-leftover-bak

Conversation

@b84330808

@b84330808 b84330808 commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Describe the pull request

gogs restore moves the current custom directory, and each data directory it restores, aside to <dir>.bak. If an earlier restore already left custom.bak behind, that rename fails: "Access is denied" on Windows, as in #7999, and "directory not empty" elsewhere. It only failed after the database had been imported, so the instance was left with the backup's database but its current custom, data and repositories.

This restores the files before the database. The current files are kept as <dir>.bak and can be moved back by hand, whereas a failed database import cannot be undone. A leftover .bak now stops the restore with a message that says which path to move away, while the database is still untouched.

Backup and restore now share one list of data directories. As a result, restore also brings back the built-in SSH server's host keys (data/ssh), which backup has been including since #7345 but restore never put back.

Link to the issue: #7999

Checklist

  • I agree to follow the Code of Conduct by submitting this pull request.
  • I have read and acknowledge the Contributing guide.
  • I have added test cases to cover the new code or have provided the test plan. (if applicable)
  • I have added an entry to CHANGELOG. (if applicable)

Test plan

With a SQLite instance:

  1. gogs backup --config custom/conf/app.ini, then gogs restore --config custom/conf/app.ini --from gogs-backup-<time>.zip. This succeeds, leaves custom.bak behind, and replaces data/ssh with the backup's host keys.
  2. Add a user, then run the same restore again.

On main, step 2 replaces the database (the new user is gone) and then fails with the error from #7999:

[FATAL] [...cmd/gogs/restore.go:122 runRestore()] Failed to backup current 'custom': rename C:\...\gogs-run\custom C:\...\gogs-run\custom.bak: Access is denied.

With this change, step 2 stops before touching the database, and the new user is still there:

[FATAL] [...cmd/gogs/restore.go:118 runRestore()] Failed to backup current 'custom': '/.../run/custom.bak' already exists, probably from an earlier restore. Move or remove it, then run the restore again

After moving custom.bak (and any data/*.bak) away, the restore succeeds.

AI usage

The follow-up maintainer commits (reordering the restore, sharing the data directory list) were written with Claude Code and reviewed by a maintainer.

Restoring moves the current custom and data directories aside to
"<dir>.bak". When an earlier restore had already left those behind, the
rename failed ("Access is denied" on Windows, "directory not empty"
elsewhere), but only after the database had been replaced, so the
instance was left half restored.

Check for the leftover ".bak" paths before importing anything and say
which one to move away.
@b84330808
b84330808 requested a review from unknwon as a code owner November 9, 2026 12:57
b84330808 and others added 3 commits November 9, 2026 21:58
Restore the custom and data directories before the database, so a
leftover ".bak" from an earlier restore stops the restore while the
database is still untouched. The current files are kept as ".bak" and
can be moved back by hand, whereas a failed database import cannot be
undone.

Backup and restore now share one list of data directories, which also
makes restore bring back the built-in SSH server's host keys that
backup has been including since gogs#7345.
@unknwon unknwon changed the title restore: stop before importing when an earlier restore left its backup cmd: keep the database untouched when gogs restore fails to move files Nov 9, 2026

@unknwon unknwon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@unknwon
unknwon merged commit ab7df7e into gogs:main Nov 9, 2026
7 checks passed
@unknwon unknwon mentioned this pull request Nov 12, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants