The backup drive keeps deleted files for 30 days, then removes them (#999)

* The backup drive keeps deleted files for 30 days, then removes them

The fifth part of replacing the sync.  A backup drive never deleted anything, so it grew forever and kept every file
the media directory had ever held.  Now, like a secondary:

- A file deleted from the media directory, or the old version of one that changed, is moved to a dated folder in
  .backup-trash on the backup drive.  Folders older than backup_delete_after_days (sync.yaml; 30 by default, also
  for drives set up before this; none never deletes) are removed, before and after each backup, and the oldest go
  sooner when the drive is nearly full.
- A backup refuses a media directory that is not mounted, or has no config/wrolpi.yaml: copied with deletes, an
  empty folder would move the whole backup drive to the trash.
- The Backup Drive panel has the same "Delete after N days / Never delete them" control as the Sync panel, now one
  component, and refuses an empty number of days.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* A nearly full drive keeps the trash it just filled, and makes room for the database

Review found, in the backup drive's trash and in the secondary's sync, which share its shape:

- Below 1 GiB free, the purge after a copy removed the newest trash folder too.  Moving a file to the trash is a
  rename, which frees nothing, so on a nearly full drive a deleted file was gone at the end of the same run instead
  of 30 days later.  The newest folder is now only removed by age (0 days still deletes at once).
- The purge made room only down to 1 GiB, so a database larger than the free space failed to be copied every time
  while the trash was full.  Before the database is written (the backup's snapshot, the secondary's copy of its
  primary's), the trash now makes room for the whole database.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
lrnselfreliance 2026-11-19 16:50:21 -07:00 • committed by GitHub
parent 38885990f6
commit a5ab01198b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 345 additions and 50 deletions

View file

@ -849,3 +849,27 @@ class TestRecoveryAndSchedule:
monkeypatch.setattr(sync, "update_status", watch)
sync.run_sync(FakeRunner(media))
assert locked_when_saved == [True]
def test_purge_never_removes_the_newest_for_space(media, monkeypatch):
"""Moving a file to the trash frees nothing, so on a nearly full drive the trash this sync just filled would be
removed by the same sync. Only age removes the newest folder."""
trash = media / ".sync-trash"
for name in ("2026-11-17", "2026-11-18"):
(trash / name).mkdir(parents=True)
usage = namedtuple("usage", "total used free")
monkeypatch.setattr(sync.shutil, "disk_usage", lambda _: usage(100, 100, 0))
assert sync.purge_trash(media, 30, datetime.date(2026, 11, 18), min_free_bytes=10) == ["2026-11-17"]
assert (trash / "2026-11-18").is_dir()
assert sync.purge_trash(media, 0, datetime.date(2026, 11, 18), min_free_bytes=10) == ["2026-11-18"]
def test_room_is_made_for_the_primarys_database(media, primary_api, monkeypatch):
"""Before the API is stopped, the trash makes room for the primary's database, not only the usual 1 GiB."""
purges = []
monkeypatch.setattr(sync, "purge_trash",
lambda media_directory, days, today=None, min_free_bytes=sync.MIN_FREE_BYTES:
purges.append(min_free_bytes))
sync.run_sync(FakeRunner(media))
# The snapshot of primary_api is 1000 bytes.
assert purges[0] == 1000 + sync.MIN_FREE_BYTES