mirror of
https://github.com/kevinveenbirkenbach/docker-volume-backup.git
synced 2026-02-07 13:24:08 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 21b4d237d3 | |||
| ec051b4c2b | |||
| ed78f69b3b | |||
| a69074c302 | |||
| 0b4696f649 |
10
CHANGELOG.md
10
CHANGELOG.md
@@ -1,3 +1,13 @@
|
||||
## [1.7.0] - 2026-02-07
|
||||
|
||||
* 🚀 Backup jobs now support all valid Docker Compose file names – case-insensitive and hassle-free.
|
||||
|
||||
|
||||
## [1.6.0] - 2026-02-06
|
||||
|
||||
* Compose handling is now fully delegated to the Infinito.Nexus compose wrapper or plain docker compose, removing all custom env and file detection to ensure a single, consistent source of truth.
|
||||
|
||||
|
||||
## [1.5.0] - 2026-01-31
|
||||
|
||||
* * Make `databases.csv` optional: missing or empty files now emit warnings and no longer break backups
|
||||
|
||||
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "backup-docker-to-local"
|
||||
version = "1.5.0"
|
||||
version = "1.7.0"
|
||||
description = "Backup Docker volumes to local with rsync and optional DB dumps."
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.9"
|
||||
|
||||
@@ -97,7 +97,7 @@ dump_debug() {
|
||||
docker -H "${DIND_HOST}" rm -f "${tmpc}" >/dev/null 2>&1 || true
|
||||
|
||||
log "DEBUG: artifacts written:"
|
||||
ls -la "${ARTIFACTS_DIR}" | sed 's/^/ /' || true
|
||||
find "${ARTIFACTS_DIR}" -maxdepth 1 -mindepth 1 -print | sed 's/^/ /' || true
|
||||
}
|
||||
|
||||
cleanup() {
|
||||
|
||||
@@ -7,85 +7,58 @@ from pathlib import Path
|
||||
from typing import List, Optional
|
||||
|
||||
|
||||
def _detect_env_file(project_dir: Path) -> Optional[Path]:
|
||||
"""
|
||||
Detect Compose env file in a directory.
|
||||
Preference (same as Infinito.Nexus wrapper):
|
||||
1) <dir>/.env (file)
|
||||
2) <dir>/.env/env (file) (legacy layout)
|
||||
"""
|
||||
c1 = project_dir / ".env"
|
||||
if c1.is_file():
|
||||
return c1
|
||||
|
||||
c2 = project_dir / ".env" / "env"
|
||||
if c2.is_file():
|
||||
return c2
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def _detect_compose_files(project_dir: Path) -> List[Path]:
|
||||
"""
|
||||
Detect Compose file stack in a directory (same as Infinito.Nexus wrapper).
|
||||
Always requires docker-compose.yml.
|
||||
Optionals:
|
||||
- docker-compose.override.yml
|
||||
- docker-compose.ca.override.yml
|
||||
"""
|
||||
base = project_dir / "docker-compose.yml"
|
||||
if not base.is_file():
|
||||
raise FileNotFoundError(f"Missing docker-compose.yml in: {project_dir}")
|
||||
|
||||
files = [base]
|
||||
|
||||
override = project_dir / "docker-compose.override.yml"
|
||||
if override.is_file():
|
||||
files.append(override)
|
||||
|
||||
ca_override = project_dir / "docker-compose.ca.override.yml"
|
||||
if ca_override.is_file():
|
||||
files.append(ca_override)
|
||||
|
||||
return files
|
||||
|
||||
|
||||
def _compose_wrapper_path() -> Optional[str]:
|
||||
"""
|
||||
Prefer the Infinito.Nexus compose wrapper if present.
|
||||
Equivalent to: `which compose`
|
||||
"""
|
||||
return shutil.which("compose")
|
||||
|
||||
|
||||
def _build_compose_cmd(project_dir: str, passthrough: List[str]) -> List[str]:
|
||||
"""
|
||||
Build the compose command for this project directory.
|
||||
|
||||
Behavior:
|
||||
- If `compose` wrapper exists: use it with --chdir (so it resolves -f/--env-file itself)
|
||||
- Else: use `docker compose` and replicate wrapper's file/env detection.
|
||||
Policy:
|
||||
- If `compose` wrapper exists (Infinito.Nexus): use it and delegate ALL logic to it.
|
||||
- Else: use plain `docker compose` with --chdir.
|
||||
- NO custom compose file/env detection in this project.
|
||||
"""
|
||||
pdir = Path(project_dir).resolve()
|
||||
|
||||
wrapper = _compose_wrapper_path()
|
||||
wrapper = shutil.which("compose")
|
||||
if wrapper:
|
||||
# Wrapper defaults project name to basename of --chdir.
|
||||
# "--" ensures wrapper stops parsing its own args.
|
||||
return [wrapper, "--chdir", str(pdir), "--", *passthrough]
|
||||
|
||||
# Fallback: pure docker compose, but mirror wrapper behavior.
|
||||
files = _detect_compose_files(pdir)
|
||||
env_file = _detect_env_file(pdir)
|
||||
docker = shutil.which("docker")
|
||||
if docker:
|
||||
return [docker, "compose", "--chdir", str(pdir), *passthrough]
|
||||
|
||||
cmd: List[str] = ["docker", "compose"]
|
||||
for f in files:
|
||||
cmd += ["-f", str(f)]
|
||||
if env_file:
|
||||
cmd += ["--env-file", str(env_file)]
|
||||
raise RuntimeError("Neither 'compose' nor 'docker' found in PATH")
|
||||
|
||||
cmd += passthrough
|
||||
return cmd
|
||||
|
||||
def _find_compose_file(project_dir: str) -> Optional[Path]:
|
||||
"""
|
||||
Detect a compose file in `project_dir` (case-insensitive).
|
||||
|
||||
Supported names:
|
||||
- compose.yml / compose.yaml
|
||||
- docker-compose.yml / docker-compose.yaml
|
||||
"""
|
||||
pdir = Path(project_dir)
|
||||
if not pdir.is_dir():
|
||||
return None
|
||||
|
||||
# Map lowercase filename -> actual Path (preserves original casing)
|
||||
by_lower = {p.name.lower(): p for p in pdir.iterdir() if p.is_file()}
|
||||
|
||||
# Preferred order (policy decision)
|
||||
candidates = [
|
||||
"docker-compose.yml",
|
||||
"docker-compose.yaml",
|
||||
"compose.yml",
|
||||
"compose.yaml",
|
||||
]
|
||||
|
||||
for name in candidates:
|
||||
found = by_lower.get(name)
|
||||
if found is not None:
|
||||
return found
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def hard_restart_docker_services(dir_path: str) -> None:
|
||||
@@ -102,7 +75,8 @@ def hard_restart_docker_services(dir_path: str) -> None:
|
||||
|
||||
|
||||
def handle_docker_compose_services(
|
||||
parent_directory: str, hard_restart_required: list[str]
|
||||
parent_directory: str,
|
||||
hard_restart_required: list[str],
|
||||
) -> None:
|
||||
for entry in os.scandir(parent_directory):
|
||||
if not entry.is_dir():
|
||||
@@ -110,11 +84,12 @@ def handle_docker_compose_services(
|
||||
|
||||
dir_path = entry.path
|
||||
name = os.path.basename(dir_path)
|
||||
compose_file = os.path.join(dir_path, "docker-compose.yml")
|
||||
|
||||
print(f"Checking directory: {dir_path}", flush=True)
|
||||
if not os.path.isfile(compose_file):
|
||||
print("No docker-compose.yml found. Skipping.", flush=True)
|
||||
|
||||
compose_file = _find_compose_file(dir_path)
|
||||
if compose_file is None:
|
||||
print("No supported compose file found. Skipping.", flush=True)
|
||||
continue
|
||||
|
||||
if name in hard_restart_required:
|
||||
|
||||
@@ -23,6 +23,7 @@ def _setup_compose_dir(
|
||||
tmp_path: Path,
|
||||
name: str = "mailu",
|
||||
*,
|
||||
compose_name: str = "docker-compose.yml",
|
||||
with_override: bool = False,
|
||||
with_ca_override: bool = False,
|
||||
env_layout: str | None = None, # None | ".env" | ".env/env"
|
||||
@@ -30,7 +31,7 @@ def _setup_compose_dir(
|
||||
d = tmp_path / name
|
||||
d.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
_touch(d / "docker-compose.yml")
|
||||
_touch(d / compose_name)
|
||||
|
||||
if with_override:
|
||||
_touch(d / "docker-compose.override.yml")
|
||||
@@ -53,62 +54,53 @@ class TestCompose(unittest.TestCase):
|
||||
|
||||
cls.compose_mod = mod
|
||||
|
||||
def test_detect_env_file_prefers_dotenv_over_legacy(self) -> None:
|
||||
def test_find_compose_file_supports_all_valid_names_case_insensitive(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
tmp_path = Path(td)
|
||||
d = _setup_compose_dir(tmp_path, env_layout=".env/env")
|
||||
# Also create .env file -> should be preferred
|
||||
_touch(d / ".env")
|
||||
|
||||
env_file = self.compose_mod._detect_env_file(d)
|
||||
self.assertEqual(env_file, d / ".env")
|
||||
variants = [
|
||||
"compose.yml",
|
||||
"compose.yaml",
|
||||
"docker-compose.yml",
|
||||
"docker-compose.yaml",
|
||||
"docker-compose.yAml",
|
||||
]
|
||||
|
||||
def test_detect_env_file_uses_legacy_if_no_dotenv(self) -> None:
|
||||
for i, name in enumerate(variants):
|
||||
d = _setup_compose_dir(
|
||||
tmp_path,
|
||||
name=f"project{i}",
|
||||
compose_name=name,
|
||||
)
|
||||
found = self.compose_mod._find_compose_file(str(d))
|
||||
self.assertIsNotNone(found)
|
||||
self.assertEqual(found.name, name)
|
||||
|
||||
def test_find_compose_file_returns_none_when_missing(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
tmp_path = Path(td)
|
||||
d = _setup_compose_dir(tmp_path, env_layout=".env/env")
|
||||
d = tmp_path / "empty"
|
||||
d.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
env_file = self.compose_mod._detect_env_file(d)
|
||||
self.assertEqual(env_file, d / ".env" / "env")
|
||||
found = self.compose_mod._find_compose_file(str(d))
|
||||
self.assertIsNone(found)
|
||||
|
||||
def test_detect_compose_files_requires_base(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
tmp_path = Path(td)
|
||||
d = tmp_path / "stack"
|
||||
d.mkdir()
|
||||
|
||||
with self.assertRaises(FileNotFoundError):
|
||||
self.compose_mod._detect_compose_files(d)
|
||||
|
||||
def test_detect_compose_files_includes_optional_overrides(self) -> None:
|
||||
def test_build_cmd_uses_wrapper_when_present(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
tmp_path = Path(td)
|
||||
d = _setup_compose_dir(
|
||||
tmp_path,
|
||||
with_override=True,
|
||||
with_ca_override=True,
|
||||
env_layout=".env",
|
||||
)
|
||||
|
||||
files = self.compose_mod._detect_compose_files(d)
|
||||
self.assertEqual(
|
||||
files,
|
||||
[
|
||||
d / "docker-compose.yml",
|
||||
d / "docker-compose.override.yml",
|
||||
d / "docker-compose.ca.override.yml",
|
||||
],
|
||||
)
|
||||
def fake_which(name: str):
|
||||
if name == "compose":
|
||||
return "/usr/local/bin/compose"
|
||||
return None
|
||||
|
||||
def test_build_cmd_uses_wrapper_when_present(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
tmp_path = Path(td)
|
||||
d = _setup_compose_dir(
|
||||
tmp_path, with_override=True, with_ca_override=True, env_layout=".env"
|
||||
)
|
||||
|
||||
with patch.object(
|
||||
self.compose_mod.shutil, "which", lambda name: "/usr/local/bin/compose"
|
||||
):
|
||||
with patch.object(self.compose_mod.shutil, "which", fake_which):
|
||||
cmd = self.compose_mod._build_compose_cmd(str(d), ["up", "-d"])
|
||||
|
||||
self.assertEqual(
|
||||
@@ -123,7 +115,7 @@ class TestCompose(unittest.TestCase):
|
||||
],
|
||||
)
|
||||
|
||||
def test_build_cmd_fallback_docker_compose_with_all_files_and_env(self) -> None:
|
||||
def test_build_cmd_fallback_uses_plain_docker_compose_chdir(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
tmp_path = Path(td)
|
||||
d = _setup_compose_dir(
|
||||
@@ -133,22 +125,23 @@ class TestCompose(unittest.TestCase):
|
||||
env_layout=".env",
|
||||
)
|
||||
|
||||
with patch.object(self.compose_mod.shutil, "which", lambda name: None):
|
||||
def fake_which(name: str):
|
||||
if name == "compose":
|
||||
return None
|
||||
if name == "docker":
|
||||
return "/usr/bin/docker"
|
||||
return None
|
||||
|
||||
with patch.object(self.compose_mod.shutil, "which", fake_which):
|
||||
cmd = self.compose_mod._build_compose_cmd(
|
||||
str(d), ["up", "-d", "--force-recreate"]
|
||||
)
|
||||
|
||||
expected: List[str] = [
|
||||
"docker",
|
||||
"/usr/bin/docker",
|
||||
"compose",
|
||||
"-f",
|
||||
str((d / "docker-compose.yml").resolve()),
|
||||
"-f",
|
||||
str((d / "docker-compose.override.yml").resolve()),
|
||||
"-f",
|
||||
str((d / "docker-compose.ca.override.yml").resolve()),
|
||||
"--env-file",
|
||||
str((d / ".env").resolve()),
|
||||
"--chdir",
|
||||
str(d.resolve()),
|
||||
"up",
|
||||
"-d",
|
||||
"--force-recreate",
|
||||
@@ -160,9 +153,12 @@ class TestCompose(unittest.TestCase):
|
||||
tmp_path = Path(td)
|
||||
d = _setup_compose_dir(tmp_path, name="mailu", env_layout=".env")
|
||||
|
||||
with patch.object(
|
||||
self.compose_mod.shutil, "which", lambda name: "/usr/local/bin/compose"
|
||||
):
|
||||
def fake_which(name: str):
|
||||
if name == "compose":
|
||||
return "/usr/local/bin/compose"
|
||||
return None
|
||||
|
||||
with patch.object(self.compose_mod.shutil, "which", fake_which):
|
||||
calls = []
|
||||
|
||||
def fake_run(cmd, check: bool):
|
||||
@@ -210,7 +206,14 @@ class TestCompose(unittest.TestCase):
|
||||
env_layout=".env/env",
|
||||
)
|
||||
|
||||
with patch.object(self.compose_mod.shutil, "which", lambda name: None):
|
||||
def fake_which(name: str):
|
||||
if name == "compose":
|
||||
return None
|
||||
if name == "docker":
|
||||
return "/usr/bin/docker"
|
||||
return None
|
||||
|
||||
with patch.object(self.compose_mod.shutil, "which", fake_which):
|
||||
calls = []
|
||||
|
||||
def fake_run(cmd, check: bool):
|
||||
@@ -220,19 +223,32 @@ class TestCompose(unittest.TestCase):
|
||||
with patch.object(self.compose_mod.subprocess, "run", fake_run):
|
||||
self.compose_mod.hard_restart_docker_services(str(d))
|
||||
|
||||
down_cmd = calls[0][0]
|
||||
up_cmd = calls[1][0]
|
||||
|
||||
self.assertTrue(calls[0][1] is True)
|
||||
self.assertTrue(calls[1][1] is True)
|
||||
|
||||
self.assertEqual(down_cmd[0:2], ["docker", "compose"])
|
||||
self.assertEqual(down_cmd[-1], "down")
|
||||
self.assertIn("--env-file", down_cmd)
|
||||
|
||||
self.assertEqual(up_cmd[0:2], ["docker", "compose"])
|
||||
self.assertTrue(up_cmd[-2:] == ["up", "-d"] or up_cmd[-3:] == ["up", "-d"])
|
||||
self.assertIn("--env-file", up_cmd)
|
||||
self.assertEqual(
|
||||
calls,
|
||||
[
|
||||
(
|
||||
[
|
||||
"/usr/bin/docker",
|
||||
"compose",
|
||||
"--chdir",
|
||||
str(d.resolve()),
|
||||
"down",
|
||||
],
|
||||
True,
|
||||
),
|
||||
(
|
||||
[
|
||||
"/usr/bin/docker",
|
||||
"compose",
|
||||
"--chdir",
|
||||
str(d.resolve()),
|
||||
"up",
|
||||
"-d",
|
||||
],
|
||||
True,
|
||||
),
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user