mirror of
https://github.com/kevinveenbirkenbach/docker-volume-backup.git
synced 2026-07-17 06:05:13 +00:00
fix(backup,restore): harden the branch fixes and prove them with tests
Backup: a container that vanishes between the docker ps listing and the swarm-task inspect (--rm one-shots, task-history GC) no longer aborts the whole backup run; it counts as not stoppable and is skipped. Restore: the postgres replay streams the dump through a spooled temp file instead of buffering it three times in memory (multi-GB dumps OOMed the restore mid-replay), and the superuser-only line filter is COPY-aware: data rows inside COPY ... FROM stdin blocks pass through untouched, so a row that happens to start with COMMENT ON EXTENSION or ALTER DEFAULT PRIVILEGES is no longer silently dropped. The e2e runner talks to the DinD daemon through docker exec instead of a host-published tcp://127.0.0.1:2375: port publishing is unreachable from sandboxed runners and from hosts with broken loopback publishing, and the unencrypted root API port disappears from the host. The debug tmp dump shrinks to tar plus docker cp against the DinD container itself. New coverage: an e2e reproducing the swarm flake end to end (service task on the volume, nothing whitelisted: the backup must succeed, the very same task container must keep running, and the service must never replace a task), unit tests for the COPY-aware filter, the swarm-task probe including the vanished-container path, filter_stoppable ordering, and the one-session FOREIGN_KEY_CHECKS drop assembly. Full suite: 35 unit, 9 integration, 30 e2e green. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from .shell import execute_shell_command
|
||||
from .shell import BackupException, execute_shell_command
|
||||
|
||||
|
||||
def get_image_info(container: str) -> str:
|
||||
@@ -27,11 +27,16 @@ def containers_using_volume(volume_name: str) -> list[str]:
|
||||
def is_swarm_task(container: str) -> bool:
|
||||
"""Swarm-managed task containers must never be stopped or started
|
||||
manually: the orchestrator replaces the stopped task and a later
|
||||
`docker start` fails on the detached overlay network."""
|
||||
out = execute_shell_command(
|
||||
"docker inspect --format "
|
||||
f"'{{{{index .Config.Labels \"com.docker.swarm.task.id\"}}}}' {container}"
|
||||
)
|
||||
`docker start` fails on the detached overlay network. A container that
|
||||
vanished between listing and inspect (--rm one-shots, task-history GC)
|
||||
counts as not stoppable instead of aborting the whole backup run."""
|
||||
try:
|
||||
out = execute_shell_command(
|
||||
"docker inspect --format "
|
||||
f"'{{{{index .Config.Labels \"com.docker.swarm.task.id\"}}}}' {container}"
|
||||
)
|
||||
except BackupException:
|
||||
return True
|
||||
return bool(out and out[0].strip())
|
||||
|
||||
|
||||
|
||||
@@ -1,9 +1,41 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
from collections.abc import Iterable, Iterator
|
||||
|
||||
from ..run import docker_exec
|
||||
|
||||
_SUPERUSER_ONLY_PREFIXES = (b"COMMENT ON EXTENSION", b"ALTER DEFAULT PRIVILEGES")
|
||||
|
||||
|
||||
def filter_superuser_only_lines(lines: Iterable[bytes]) -> Iterator[bytes]:
|
||||
"""Drop superuser-only statements an app-level psql replay cannot run.
|
||||
|
||||
Args:
|
||||
lines: dump lines including their trailing newlines.
|
||||
|
||||
Yields:
|
||||
Every line except top-level statements starting with a superuser-only
|
||||
prefix. Lines inside COPY ... FROM stdin data blocks are passed
|
||||
through untouched: a data row may legally start with the same bytes,
|
||||
and dropping it would silently corrupt the restored table.
|
||||
"""
|
||||
in_copy = False
|
||||
for line in lines:
|
||||
if in_copy:
|
||||
yield line
|
||||
if line.rstrip(b"\r\n") == b"\\.":
|
||||
in_copy = False
|
||||
continue
|
||||
if line.startswith(b"COPY ") and line.rstrip(b"\r\n").endswith(b"FROM stdin;"):
|
||||
in_copy = True
|
||||
yield line
|
||||
continue
|
||||
if line.startswith(_SUPERUSER_ONLY_PREFIXES):
|
||||
continue
|
||||
yield line
|
||||
|
||||
|
||||
def restore_postgres_sql(
|
||||
*,
|
||||
@@ -68,19 +100,18 @@ END $$;
|
||||
docker_env=docker_env,
|
||||
)
|
||||
|
||||
with open(sql_path, "rb") as f:
|
||||
raw_sql = f.read()
|
||||
# COMMENT ON EXTENSION and ALTER DEFAULT PRIVILEGES are superuser-only;
|
||||
# app-level restores must skip them or ON_ERROR_STOP aborts the replay.
|
||||
superuser_only = (b"COMMENT ON EXTENSION", b"ALTER DEFAULT PRIVILEGES")
|
||||
sql = b"\n".join(
|
||||
line for line in raw_sql.splitlines() if not line.startswith(superuser_only)
|
||||
)
|
||||
docker_exec(
|
||||
container,
|
||||
["psql", "-v", "ON_ERROR_STOP=1", "-U", user, "-d", db_name],
|
||||
stdin=sql,
|
||||
docker_env=docker_env,
|
||||
)
|
||||
# Filter into a spooled temp file instead of building the whole dump in
|
||||
# memory: production dumps reach many GB and the previous read/splitlines/
|
||||
# join needed roughly three times the dump size in RSS.
|
||||
with open(sql_path, "rb") as src, tempfile.TemporaryFile() as filtered:
|
||||
for line in filter_superuser_only_lines(src):
|
||||
filtered.write(line)
|
||||
filtered.seek(0)
|
||||
docker_exec(
|
||||
container,
|
||||
["psql", "-v", "ON_ERROR_STOP=1", "-U", user, "-d", db_name],
|
||||
stdin=filtered,
|
||||
docker_env=docker_env,
|
||||
)
|
||||
|
||||
print(f"PostgreSQL restore complete for db '{db_name}'.")
|
||||
|
||||
Reference in New Issue
Block a user