From e9201bf58bd9ec920fa6399abba76d3df593281a Mon Sep 17 00:00:00 2001 From: Qaldak Date: Mon, 20 Nov 2023 16:28:17 +0100 Subject: [PATCH] feat: add hostname to backup filename for a unique filename --- src/container/backup.py | 4 +++- tests/container/test_backup.py | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/container/backup.py b/src/container/backup.py index 42a55ad..57d1cef 100644 --- a/src/container/backup.py +++ b/src/container/backup.py @@ -2,6 +2,8 @@ from python_on_whales import docker, DockerException +from util.accessor import LocalHost + logger = logging.getLogger(__name__) @@ -13,7 +15,7 @@ def create_tar_cmd(container) -> list[str]: :return: tar command to execute """ - tar_cmd = ["tar", "-czf", f"/backup/{container.name}_volume_backup.tar.gz"] + tar_cmd = ["tar", "-czf", f"/backup/{LocalHost.get_hostname()}_{container.name}_volume_backup.tar.gz"] for volume in container.docker_volumes: tar_cmd.append(f"{volume}") diff --git a/tests/container/test_backup.py b/tests/container/test_backup.py index 0324500..7bc3a5d 100644 --- a/tests/container/test_backup.py +++ b/tests/container/test_backup.py @@ -53,7 +53,9 @@ def test_run_backup_successful(self, tmp): self.assertEqual(["INFO:container.backup:Volume backup for container 'foo_bar' successful"], log.output) -def test_create_tar_cmd(): +@patch("src.container.backup.LocalHost.get_hostname", return_value="groot") +def test_create_tar_cmd(host): container = MockContainer() tar_cmd = create_tar_cmd(container) - assert ["tar", "-czf", f"/backup/foo_bar_volume_backup.tar.gz", "/foo/data", "/foo/config", "/bar/log"] == tar_cmd + assert ["tar", "-czf", f"/backup/groot_foo_bar_volume_backup.tar.gz", "/foo/data", "/foo/config", + "/bar/log"] == tar_cmd