[PR #6088] [MERGED] fix(backup): check free space before writing the automatic backup #7056

Closed
opened 2026-10-23 04:16:08 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/6088
Author: @SonaGat3
Created: 9/12/2026
Status: ✅ Merged
Merged: 9/12/2026
Merged by: @undefined

Base: staging ← Head: fix/6087-automatic-backup-free-space


📝 Commits (5)

  • dfa7307 fix(backup): check free space before writing the automatic backup
  • 244871c Merge remote-tracking branch 'origin/staging' into fix/6087-automatic-backup-free-space
  • 6547cc3 fix(backup): log a failed free-space read before the automatic backup
  • da6cbed test: exercise automatic backup disk-space refusal and recovery
  • 3acccc2 Merge branch 'staging' into fix/6087-automatic-backup-free-space

📊 Changes

6 files changed (+174 additions, -2 deletions)

View changed files

📝 CHANGELOG.md (+2 -0)
📝 docs/data/backup-and-restore.md (+4 -0)
📝 packages/server/src/routes/backup.routes.ts (+37 -2)
📝 packages/server/src/services/backup/automatic-backup-retention.ts (+13 -0)
➕ scripts/regressions/automatic-backup-free-space.regression.ts (+98 -0)
📝 scripts/regressions/automatic-backup-retention.regression.ts (+20 -0)

📄 Description

Important

Contributions target staging. Only SpicyMarinara may promote this repository's staging branch or a same-repository hotfix/* branch to main.
Outside and first-time contributors also require an approving review from SpicyMarinara.

Linked issue

Closes #6087

Why this change

  • The automatic backup writes a full copy of the data folder into backups/, on the same disk as the live storage/ tables, without checking that the space exists. The previous archive is kept until the new one is complete, so peak usage is two full archives plus the retained history.
  • When the write fails with ENOSPC the failure is recorded but lastBackupAt is not advanced, so the backup stays due and the hourly timer retries the whole write, filling the disk again every hour while any other write on that disk can fail.
  • No user report yet; found while reviewing the backup code. The change is preventive and deliberately small.

What changed

  • automaticBackupFreeSpaceError(freeBytes, archiveBytes) in automatic-backup-retention.ts: pure decision plus the message shown in Settings, with a fixed 256 MiB headroom on top of the archive size.
  • writeFullBackupArchive takes an optional beforeWrite(archiveBytes) hook, called after every source is collected and before the output file is opened. The archive is stored, not compressed, so the sum of the collected file sizes is the archive size apart from headers. Download Backup does not pass the hook and is unchanged.
  • writeAutomaticBackup passes the hook: fs.promises.statfs on backups/, compare, throw the message if it does not fit. The existing catch stores it as lastError, which the Automatic Backups control already displays. If statfs itself fails, the check is skipped and the backup proceeds as today, so the guard can never block a backup on its own.
  • Retention, rotation order, the 9999 limit and the UI are untouched.
  • Regression: three pure cases for the message (fits exactly, one byte short, empty disk) and a source assertion that the automatic backup consults statfs before writing.
  • Docs: one paragraph in docs/data/backup-and-restore.md; CHANGELOG entry under [Unreleased].

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

  • pnpm check: passed (exit code 0).
  • node scripts/run-regressions.mjs --filter automatic-backup-retention: passed, including the new cases.
  • fs.promises.statfs checked on Windows 10 against the data drive: it reports the same free space as the OS (253.5 GiB), so the guard works on Windows as well as Linux/Termux.
  • Isolated server built from this branch (DATA_DIR, TEMP, MARINARA_ENV_FILE pointing to a scratch folder, port 9123): enabling Automatic Backups (daily, keep 2) produced marinara-automatic-backup.zip (29.2 MiB) within a second, with lastBackupAt set and lastError: null, so the happy path is unchanged when space is plentiful.
  • The refusal path is covered by the pure regression cases; I did not fill a real disk to trigger ENOSPC.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

UI evidence (if applicable)

Not applicable: no UI change. The message reuses the existing lastError slot next to the Automatic Backups control.

Summary by CodeRabbit

  • New Features

    • Automatic backups now check available disk space before creating an archive.
    • Runs that would exceed available space are skipped with a clear message in Settings and retried during the next check after space is freed.
    • The previous archive is retained until the new one finishes, requiring space for an additional full archive.
  • Documentation

    • Updated backup and restore guidance with automatic backup disk-space requirements.
  • Tests

    • Added coverage for insufficient-space handling and backup validation.

Maintainer sweep validation

Added automatic-backup-free-space.regression.ts and ran it against an isolated real Fastify backup route: zero available space reports the error without changing the previous archive or leaving a partial ZIP; available space then permits a complete, readable ZIP and normal retention. The check mocks the free-space query rather than filling the host disk. Translated documentation is covered by #6097.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/6088 **Author:** [@SonaGat3](https://github.com/SonaGat3) **Created:** 9/12/2026 **Status:** ✅ Merged **Merged:** 9/12/2026 **Merged by:** [@undefined](undefined) **Base:** `staging` ← **Head:** `fix/6087-automatic-backup-free-space` --- ### 📝 Commits (5) - [`dfa7307`](https://github.com/Pasta-Devs/Marinara-Engine/commit/dfa7307be3992f8e3bb5347eed822a2490601f8d) fix(backup): check free space before writing the automatic backup - [`244871c`](https://github.com/Pasta-Devs/Marinara-Engine/commit/244871c91d39f6799f4546e8c2174bf6e3cb6c9e) Merge remote-tracking branch 'origin/staging' into fix/6087-automatic-backup-free-space - [`6547cc3`](https://github.com/Pasta-Devs/Marinara-Engine/commit/6547cc386bf12c87837cb9ddb3cf58bceeadf789) fix(backup): log a failed free-space read before the automatic backup - [`da6cbed`](https://github.com/Pasta-Devs/Marinara-Engine/commit/da6cbed4c16e832575f68cb8bd6ad9e53a72cbc2) test: exercise automatic backup disk-space refusal and recovery - [`3acccc2`](https://github.com/Pasta-Devs/Marinara-Engine/commit/3acccc21cde85e1718e26af4620cf9d654e07c6c) Merge branch 'staging' into fix/6087-automatic-backup-free-space ### 📊 Changes **6 files changed** (+174 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+2 -0) 📝 `docs/data/backup-and-restore.md` (+4 -0) 📝 `packages/server/src/routes/backup.routes.ts` (+37 -2) 📝 `packages/server/src/services/backup/automatic-backup-retention.ts` (+13 -0) ➕ `scripts/regressions/automatic-backup-free-space.regression.ts` (+98 -0) 📝 `scripts/regressions/automatic-backup-retention.regression.ts` (+20 -0) </details> ### 📄 Description > [!IMPORTANT] > Contributions target `staging`. Only `SpicyMarinara` may promote this repository's `staging` branch or a same-repository `hotfix/*` branch to `main`. > Outside and first-time contributors also require an approving review from `SpicyMarinara`. ## Linked issue Closes #6087 ## Why this change - The automatic backup writes a full copy of the data folder into `backups/`, on the same disk as the live `storage/` tables, without checking that the space exists. The previous archive is kept until the new one is complete, so peak usage is two full archives plus the retained history. - When the write fails with ENOSPC the failure is recorded but `lastBackupAt` is not advanced, so the backup stays due and the hourly timer retries the whole write, filling the disk again every hour while any other write on that disk can fail. - No user report yet; found while reviewing the backup code. The change is preventive and deliberately small. ## What changed - `automaticBackupFreeSpaceError(freeBytes, archiveBytes)` in `automatic-backup-retention.ts`: pure decision plus the message shown in Settings, with a fixed 256 MiB headroom on top of the archive size. - `writeFullBackupArchive` takes an optional `beforeWrite(archiveBytes)` hook, called after every source is collected and before the output file is opened. The archive is stored, not compressed, so the sum of the collected file sizes is the archive size apart from headers. Download Backup does not pass the hook and is unchanged. - `writeAutomaticBackup` passes the hook: `fs.promises.statfs` on `backups/`, compare, throw the message if it does not fit. The existing catch stores it as `lastError`, which the Automatic Backups control already displays. If `statfs` itself fails, the check is skipped and the backup proceeds as today, so the guard can never block a backup on its own. - Retention, rotation order, the 9999 limit and the UI are untouched. - Regression: three pure cases for the message (fits exactly, one byte short, empty disk) and a source assertion that the automatic backup consults `statfs` before writing. - Docs: one paragraph in `docs/data/backup-and-restore.md`; CHANGELOG entry under `[Unreleased]`. ## Validation - [x] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [ ] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes - `pnpm check`: passed (exit code 0). - `node scripts/run-regressions.mjs --filter automatic-backup-retention`: passed, including the new cases. - `fs.promises.statfs` checked on Windows 10 against the data drive: it reports the same free space as the OS (253.5 GiB), so the guard works on Windows as well as Linux/Termux. - Isolated server built from this branch (`DATA_DIR`, `TEMP`, `MARINARA_ENV_FILE` pointing to a scratch folder, port 9123): enabling Automatic Backups (daily, keep 2) produced `marinara-automatic-backup.zip` (29.2 MiB) within a second, with `lastBackupAt` set and `lastError: null`, so the happy path is unchanged when space is plentiful. - The refusal path is covered by the pure regression cases; I did not fill a real disk to trigger ENOSPC. ## Docs and release impact - [ ] No docs changes needed - [x] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) ## UI evidence (if applicable) Not applicable: no UI change. The message reuses the existing `lastError` slot next to the Automatic Backups control. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Automatic backups now check available disk space before creating an archive. * Runs that would exceed available space are skipped with a clear message in Settings and retried during the next check after space is freed. * The previous archive is retained until the new one finishes, requiring space for an additional full archive. * **Documentation** * Updated backup and restore guidance with automatic backup disk-space requirements. * **Tests** * Added coverage for insufficient-space handling and backup validation. <!-- end of auto-generated comment: release notes by coderabbit.ai --> ### Maintainer sweep validation Added `automatic-backup-free-space.regression.ts` and ran it against an isolated real Fastify backup route: zero available space reports the error without changing the previous archive or leaving a partial ZIP; available space then permits a complete, readable ZIP and normal retention. The check mocks the free-space query rather than filling the host disk. Translated documentation is covered by #6097. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-23 04:16:08 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#7056
No description provided.