mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-11 12:46:49 +00:00
fix: backups and snapshots keep their order when the clock goes back
Backup retention kept the last --keep backups by name, and a name is the panel's local time. Where the clock goes back an hour in autumn (TIME_ZONE=Europe/Berlin), a backup at 02:10 CET forty minutes after one at 02:50 CEST sorted first, and --keep 1 deleted the new one. Retention now keeps the newest by when they were made (the directory's mtime); the names stay as they were. Snapshot times from libvirt (epoch seconds) became naive local times, which the snapshot list sorts newest first: in the same hour the older snapshot came first. They are now aware UTC times; the page still shows them in the panel's time zone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
parent
0d20d07b29
commit
25e49c2aa2
4 changed files with 26 additions and 7 deletions
|
|
@ -46,7 +46,11 @@ class Command(BaseCommand):
|
|||
raise
|
||||
# only after a good backup: a failing one never costs an older one;
|
||||
# the partial ones are what a killed run left behind
|
||||
for old in [*sorted(backups.glob(PREFIX + "*"))[:-keep], *backups.glob(f".{PREFIX}*")]:
|
||||
# the newest by when they were made, not by name: a name is the local time, which goes back
|
||||
# an hour in autumn (and was the panel's zone or UTC as an installation's TZ changed);
|
||||
# by name among those of the same time, which a coarse file system clock gives
|
||||
made = sorted(backups.glob(PREFIX + "*"), key=lambda path: (path.stat().st_mtime_ns, path.name))
|
||||
for old in [*made[:-keep], *backups.glob(f".{PREFIX}*")]:
|
||||
shutil.rmtree(old)
|
||||
return backups / name
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
readable only by the panel's user, the newest --keep of them kept."""
|
||||
|
||||
import fcntl
|
||||
from datetime import datetime
|
||||
import os
|
||||
import shutil
|
||||
import sqlite3
|
||||
|
|
@ -85,12 +86,24 @@ class BackupTests(SimpleTestCase):
|
|||
self.assertEqual(oct(copy.stat().st_mode & 0o777), "0o600")
|
||||
|
||||
def test_only_the_newest_backups_and_no_partial_one_are_kept(self):
|
||||
for name in ("webvirtcloud-backup-20260101-000000", "webvirtcloud-backup-20260102-000000",
|
||||
".webvirtcloud-backup-20260103-000000"):
|
||||
for made, name in ((1, "webvirtcloud-backup-20260101-000000"), (2, "webvirtcloud-backup-20260102-000000"),
|
||||
(3, ".webvirtcloud-backup-20260103-000000")):
|
||||
(self.backups / name).mkdir(parents=True)
|
||||
os.utime(self.backups / name, (made, made))
|
||||
backup = self.backup("--keep", "2")
|
||||
self.assertEqual(self.kept(), ["webvirtcloud-backup-20260102-000000", backup.name])
|
||||
|
||||
def test_the_newest_are_kept_by_when_they_were_made_not_by_name(self):
|
||||
# 02:50 CEST, then 02:10 CET forty minutes later as the clock went back; or an
|
||||
# older backup named in another time zone
|
||||
older = self.backups / "webvirtcloud-backup-20261025-025000"
|
||||
older.mkdir(parents=True)
|
||||
os.utime(older, (1, 1))
|
||||
with patch("appsettings.management.commands.backup.datetime") as clock:
|
||||
clock.now.return_value = datetime(2026, 10, 25, 2, 10)
|
||||
newest = self.backup("--keep", "1")
|
||||
self.assertEqual(self.kept(), [newest.name])
|
||||
|
||||
def test_a_failed_backup_leaves_nothing_and_deletes_no_older_one(self):
|
||||
(self.backups / "webvirtcloud-backup-20260101-000000").mkdir(parents=True)
|
||||
with patch.object(Command, "copy", side_effect=OSError("No space left on device")), self.assertRaises(OSError):
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ from libvirt import (
|
|||
from libvirt_qemu import qemuAgentCommand
|
||||
|
||||
from collections import OrderedDict
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timezone
|
||||
from xml.etree import ElementTree
|
||||
|
||||
from lxml import etree
|
||||
|
|
@ -2136,7 +2136,8 @@ class wvmInstance(wvmConnect):
|
|||
detaches.setdefault(dev, path)
|
||||
snapshots.append(
|
||||
{
|
||||
"date": datetime.fromtimestamp(int(tree.findtext("creationTime"))),
|
||||
# aware: sorted in UTC, shown in the panel's time zone
|
||||
"date": datetime.fromtimestamp(int(tree.findtext("creationTime")), timezone.utc),
|
||||
"name": snap.getName(),
|
||||
"description": tree.findtext("description") or "",
|
||||
"memory": tree.find("memory[@snapshot='external']") is not None,
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ cases are in vrtManager/tests/test_live_snapshots.py."""
|
|||
|
||||
import json
|
||||
import unittest
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timezone
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import libvirt
|
||||
|
|
@ -110,7 +110,8 @@ class SnapshotListTestCase(unittest.TestCase):
|
|||
[entry] = p.get_snapshot(EXTERNAL)
|
||||
self.assertEqual(entry["name"], "one")
|
||||
self.assertEqual(entry["description"], "d & e")
|
||||
self.assertEqual(entry["date"], datetime.fromtimestamp(1700000000))
|
||||
# aware: two snapshots an hour apart around an autumn clock change stay in order
|
||||
self.assertEqual(entry["date"], datetime(2023, 11, 14, 22, 13, 20, tzinfo=timezone.utc))
|
||||
self.assertTrue(entry["memory"])
|
||||
self.assertTrue(entry["current"])
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue