2026-07-31 13:01:43 +02:00
|
|
|
"""Back up every Docker volume of a host into a timestamped generation."""
|
|
|
|
|
|
2025-12-26 18:13:26 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-07-31 13:01:43 +02:00
|
|
|
from contextlib import ExitStack
|
2025-12-26 18:13:26 +01:00
|
|
|
from datetime import datetime
|
2026-08-18 01:47:38 +02:00
|
|
|
from pathlib import Path
|
2025-12-26 18:13:26 +01:00
|
|
|
|
|
|
|
|
from .cli import parse_args
|
|
|
|
|
from .compose import handle_docker_compose_services
|
|
|
|
|
from .docker import (
|
|
|
|
|
change_containers_status,
|
|
|
|
|
containers_using_volume,
|
|
|
|
|
docker_volume_names,
|
2026-07-11 08:23:11 +02:00
|
|
|
filter_stoppable,
|
2025-12-26 18:13:26 +01:00
|
|
|
)
|
2026-08-18 01:47:38 +02:00
|
|
|
from .dumps import VolumeOutcome, backup_dumps_for_volume, load_databases_df
|
2026-07-31 13:01:43 +02:00
|
|
|
from .layout import (
|
|
|
|
|
create_version_directory,
|
|
|
|
|
create_volume_directory,
|
|
|
|
|
get_machine_id,
|
|
|
|
|
stamp_directory,
|
2026-08-18 01:47:38 +02:00
|
|
|
write_manifest,
|
2026-07-31 13:01:43 +02:00
|
|
|
)
|
|
|
|
|
from .policy import requires_stop, volume_is_fully_ignored
|
2026-08-17 05:26:28 +02:00
|
|
|
from .snapshot import snapshot_source, volume_snapshot
|
|
|
|
|
from .volume import backup_volume, inspect_backing
|
2025-12-26 18:13:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def main() -> int:
|
|
|
|
|
args = parse_args()
|
|
|
|
|
|
|
|
|
|
machine_id = get_machine_id()
|
2026-08-17 04:35:50 +02:00
|
|
|
# Local wall clock on purpose: generations sort by this name, and UTC would
|
|
|
|
|
# order new ones before the existing ones wherever the offset is positive.
|
|
|
|
|
backup_time = datetime.now().strftime("%Y%m%d%H%M%S") # noqa: DTZ005
|
2025-12-26 18:13:26 +01:00
|
|
|
|
2026-08-18 01:47:38 +02:00
|
|
|
versions_dir = str(Path(args.backups_dir) / machine_id / args.repo_name)
|
2025-12-26 18:13:26 +01:00
|
|
|
version_dir = create_version_directory(versions_dir, backup_time)
|
|
|
|
|
|
2026-08-17 13:54:39 +02:00
|
|
|
databases_df = None if args.only_files else load_databases_df(args.databases_csv)
|
2025-12-26 18:13:26 +01:00
|
|
|
|
|
|
|
|
print("💾 Start volume backups...", flush=True)
|
|
|
|
|
|
2026-08-18 01:47:38 +02:00
|
|
|
outcomes: dict[str, VolumeOutcome] = {}
|
|
|
|
|
|
2026-07-31 13:01:43 +02:00
|
|
|
with ExitStack() as stack:
|
|
|
|
|
resolve_source = None
|
|
|
|
|
if args.snapshot:
|
|
|
|
|
resolve_source = stack.enter_context(
|
|
|
|
|
volume_snapshot(args.snapshot, args.snapshot_subject, backup_time)
|
2025-12-29 11:39:57 +01:00
|
|
|
)
|
|
|
|
|
|
2026-07-31 13:01:43 +02:00
|
|
|
for volume_name in docker_volume_names():
|
|
|
|
|
print(f"Start backup routine for volume: {volume_name}", flush=True)
|
2026-08-02 09:10:20 +02:00
|
|
|
|
|
|
|
|
if volume_name in args.volumes_no_backup_required:
|
|
|
|
|
print(
|
|
|
|
|
f"Skipping volume '{volume_name}' entirely (declared no-backup).",
|
|
|
|
|
flush=True,
|
|
|
|
|
)
|
|
|
|
|
continue
|
|
|
|
|
|
2026-07-31 13:01:43 +02:00
|
|
|
containers = containers_using_volume(volume_name)
|
2025-12-29 11:39:57 +01:00
|
|
|
|
2026-07-31 13:01:43 +02:00
|
|
|
if volume_is_fully_ignored(containers, args.images_no_backup_required):
|
|
|
|
|
print(
|
|
|
|
|
f"Skipping volume '{volume_name}' entirely (all linked containers are ignored).",
|
|
|
|
|
flush=True,
|
|
|
|
|
)
|
|
|
|
|
continue
|
2025-12-26 18:13:26 +01:00
|
|
|
|
2026-07-31 13:01:43 +02:00
|
|
|
vol_dir = create_volume_directory(version_dir, volume_name)
|
2025-12-29 08:28:23 +01:00
|
|
|
|
2026-08-18 01:47:38 +02:00
|
|
|
outcome = VolumeOutcome(database=False, dumped=False)
|
2026-08-17 13:54:39 +02:00
|
|
|
if not args.only_files:
|
2026-08-18 01:47:38 +02:00
|
|
|
outcome = backup_dumps_for_volume(
|
2026-08-17 13:54:39 +02:00
|
|
|
containers=containers,
|
|
|
|
|
vol_dir=vol_dir,
|
|
|
|
|
databases_df=databases_df,
|
|
|
|
|
database_containers=args.database_containers,
|
|
|
|
|
)
|
2026-08-18 01:47:38 +02:00
|
|
|
outcomes[volume_name] = outcome
|
2025-12-26 18:13:26 +01:00
|
|
|
|
2026-08-18 01:47:38 +02:00
|
|
|
if args.only_sql and outcome.database:
|
|
|
|
|
if not outcome.dumped:
|
2026-08-17 04:35:50 +02:00
|
|
|
print(
|
2026-08-17 13:54:39 +02:00
|
|
|
f"WARNING: only-sql requested but no DB dump was produced for DB volume '{volume_name}'. "
|
2026-08-17 04:35:50 +02:00
|
|
|
"Falling back to file backup.",
|
|
|
|
|
flush=True,
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
continue
|
2026-07-31 13:01:43 +02:00
|
|
|
|
2026-08-17 05:26:28 +02:00
|
|
|
backing = inspect_backing(volume_name)
|
|
|
|
|
live_source = backing.source
|
2026-07-31 13:01:43 +02:00
|
|
|
|
2026-08-17 04:35:50 +02:00
|
|
|
def copy(
|
|
|
|
|
*,
|
|
|
|
|
authoritative: bool,
|
|
|
|
|
source: str = live_source,
|
|
|
|
|
volume: str = volume_name,
|
|
|
|
|
target: str = vol_dir,
|
|
|
|
|
) -> None:
|
2026-07-31 13:01:43 +02:00
|
|
|
backup_volume(
|
|
|
|
|
versions_dir,
|
2026-08-17 04:35:50 +02:00
|
|
|
volume,
|
|
|
|
|
target,
|
2026-07-31 13:01:43 +02:00
|
|
|
authoritative=authoritative,
|
|
|
|
|
source=source,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if resolve_source is not None:
|
2026-08-17 05:26:28 +02:00
|
|
|
source, reason = snapshot_source(
|
|
|
|
|
resolve_source, backing, args.snapshot_subject
|
|
|
|
|
)
|
|
|
|
|
if source is not None:
|
|
|
|
|
copy(authoritative=True, source=source)
|
2026-07-31 16:23:25 +02:00
|
|
|
else:
|
|
|
|
|
print(
|
|
|
|
|
f"WARNING: volume '{volume_name}' is not in the snapshot "
|
2026-08-17 05:26:28 +02:00
|
|
|
f"({reason}); copying it live instead.",
|
2026-07-31 16:23:25 +02:00
|
|
|
flush=True,
|
|
|
|
|
)
|
|
|
|
|
copy(authoritative=False)
|
2026-07-31 13:01:43 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
copy(authoritative=False)
|
|
|
|
|
if requires_stop(containers, args.images_no_stop_required):
|
|
|
|
|
stoppable = filter_stoppable(containers)
|
|
|
|
|
change_containers_status(stoppable, "stop")
|
|
|
|
|
copy(authoritative=True)
|
|
|
|
|
if not args.shutdown:
|
|
|
|
|
change_containers_status(stoppable, "start")
|
2025-12-26 18:13:26 +01:00
|
|
|
|
2026-08-18 01:47:38 +02:00
|
|
|
write_manifest(version_dir, outcomes)
|
2025-12-26 18:13:26 +01:00
|
|
|
stamp_directory(version_dir)
|
|
|
|
|
print("Finished volume backups.", flush=True)
|
|
|
|
|
|
|
|
|
|
print("Handling Docker Compose services...", flush=True)
|
2026-07-12 18:19:20 +02:00
|
|
|
handle_docker_compose_services(args.compose_dir, args.hard_restart_projects)
|
2025-12-26 18:13:26 +01:00
|
|
|
|
|
|
|
|
return 0
|