mirror of
https://github.com/kevinveenbirkenbach/docker-volume-backup.git
synced 2026-08-01 12:34:50 +00:00
Backing up a live volume with rsync copies a moving target: a database
written to mid-copy lands on disk in a state no engine ever committed.
Stopping the container avoids that at the cost of downtime.
A snapshot removes both. `--snapshot {btrfs,zfs}` with `--snapshot-subject`
freezes the docker root once per run, and every volume copy is then read
from that frozen tree while the containers keep serving. A restore of such
a copy is an ordinary crash recovery, which every supported engine performs
on its own at startup.
An unsupported filesystem or an unknown snapshot kind fails loudly rather
than degrading to a live copy, since a silent fallback would return exactly
the torn backup the mode exists to prevent. `--shutdown` is rejected
alongside `--snapshot` instead of being ignored: under a snapshot no
container is ever stopped, so accepting the flag would promise downtime
semantics the run does not deliver.
Copies out of a snapshot skip rsync's --checksum verification. The source
is immutable for the lifetime of the copy, so size-and-mtime cannot race,
and dropping the second full read roughly halves the I/O per volume.
backup/app.py grew past what one module could carry and is split into
layout, policy and dumps along the lines it already had internally.
Tests: unit coverage for the new snapshot, layout, policy, volume and cli
units; e2e cases drive real btrfs, zfs and ext4 filesystems on loop devices
in a privileged container, including a MariaDB that is written to across
the snapshot and must recover from the restored copy without losing a
committed row. CI installs zfs and sets E2E_REQUIRE_FILESYSTEMS so a
missing kernel module fails the build instead of silently skipping a
filesystem.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
62 lines
2.2 KiB
Python
62 lines
2.2 KiB
Python
import unittest
|
|
from unittest.mock import patch
|
|
|
|
from baudolo.backup.policy import requires_stop
|
|
|
|
|
|
@patch("baudolo.backup.policy.is_swarm_task", return_value=False)
|
|
class TestRequiresStop(unittest.TestCase):
|
|
@patch("baudolo.backup.policy.get_image_info")
|
|
def test_requires_stop_false_when_all_images_are_whitelisted(
|
|
self, mock_get_image_info, _mock_is_swarm_task
|
|
):
|
|
mock_get_image_info.side_effect = [
|
|
"repo/mastodon:v4",
|
|
"repo/wordpress:latest",
|
|
]
|
|
containers = ["c1", "c2"]
|
|
whitelist = ["repo/mastodon:v4", "repo/wordpress:latest"]
|
|
self.assertFalse(requires_stop(containers, whitelist))
|
|
|
|
@patch("baudolo.backup.policy.get_image_info")
|
|
def test_requires_stop_true_when_any_image_is_not_whitelisted(
|
|
self, mock_get_image_info, _mock_is_swarm_task
|
|
):
|
|
mock_get_image_info.side_effect = [
|
|
"repo/mastodon:v4",
|
|
"repo/nginx:latest",
|
|
]
|
|
containers = ["c1", "c2"]
|
|
whitelist = ["repo/mastodon:v4", "repo/wordpress:latest"]
|
|
self.assertTrue(requires_stop(containers, whitelist))
|
|
|
|
@patch("baudolo.backup.policy.get_image_info")
|
|
def test_requires_stop_true_on_substring_only_match(
|
|
self, mock_get_image_info, _mock_is_swarm_task
|
|
):
|
|
mock_get_image_info.return_value = "reg:5000/repo/mastodon:v4"
|
|
self.assertTrue(requires_stop(["c1"], ["mastodon"]))
|
|
self.assertTrue(requires_stop(["c1"], ["repo/mastodon:v4"]))
|
|
|
|
@patch("baudolo.backup.policy.get_image_info")
|
|
def test_requires_stop_true_when_whitelist_empty(
|
|
self, mock_get_image_info, _mock_is_swarm_task
|
|
):
|
|
mock_get_image_info.return_value = "repo/anything:latest"
|
|
self.assertTrue(requires_stop(["c1"], []))
|
|
|
|
|
|
class TestRequiresStopSwarm(unittest.TestCase):
|
|
@patch("baudolo.backup.policy.get_image_info")
|
|
@patch("baudolo.backup.policy.is_swarm_task", return_value=True)
|
|
def test_swarm_tasks_never_require_stop(
|
|
self, _mock_is_swarm_task, mock_get_image_info
|
|
):
|
|
mock_get_image_info.return_value = "repo/not-whitelisted:latest"
|
|
self.assertFalse(requires_stop(["c1"], []))
|
|
mock_get_image_info.assert_not_called()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|