2025-12-26 18:13:26 +01:00
from __future__ import annotations
import argparse
2025-12-28 22:12:31 +01:00
2025-12-26 18:13:26 +01:00
def parse_args () -> argparse . Namespace :
p = argparse . ArgumentParser ( description = "Backup Docker volumes." )
p . add_argument (
"--compose-dir" ,
type = str ,
required = True ,
help = "Path to the parent directory containing docker-compose setups" ,
)
p . add_argument (
2026-07-12 18:19:20 +02:00
"--hard-restart-projects" ,
2026-07-12 14:53:13 +02:00
nargs = "*" ,
default = [],
help = "Compose dir names that require 'docker-compose down && up -d' (default: none; pass e.g. 'mailu' under compose where the DB cannot be backed up hot)" ,
2025-12-26 18:13:26 +01:00
)
p . add_argument (
"--repo-name" ,
2026-08-17 13:54:39 +02:00
required = True ,
help = "Backup repo folder name under <backups-dir>/<machine-id>/" ,
2025-12-26 18:13:26 +01:00
)
p . add_argument (
"--databases-csv" ,
2026-08-17 13:54:39 +02:00
help = "Path to databases.csv; required unless --only-files is given" ,
2025-12-26 18:13:26 +01:00
)
p . add_argument (
"--backups-dir" ,
2026-07-12 14:53:13 +02:00
required = True ,
help = "Backup root directory (e.g. /var/lib/backup/)" ,
2025-12-26 18:13:26 +01:00
)
2026-07-31 13:01:43 +02:00
p . add_argument (
"--snapshot" ,
choices = [ "btrfs" , "zfs" ],
help = "Capture every volume from one atomic filesystem snapshot instead of copying the live tree. Containers are not stopped, and the copy is a single pass. Requires --snapshot-subject. Omit to keep the live two-pass copy." ,
)
p . add_argument (
"--snapshot-subject" ,
help = "Btrfs subvolume or zfs dataset mountpoint holding the docker volumes, e.g. /var/lib/docker. Required with --snapshot." ,
)
2025-12-26 18:13:26 +01:00
p . add_argument (
"--database-containers" ,
nargs = "+" ,
2026-07-15 03:34:53 +02:00
default = [],
2025-12-26 18:13:26 +01:00
help = "Container names treated as special instances for database backups" ,
)
p . add_argument (
"--images-no-stop-required" ,
nargs = "+" ,
2026-07-15 03:34:53 +02:00
default = [],
2026-07-12 18:19:20 +02:00
help = "Exact image references (repo:tag, incl. any registry prefix) whose containers must not be stopped during file backup" ,
2025-12-26 18:13:26 +01:00
)
p . add_argument (
"--images-no-backup-required" ,
nargs = "+" ,
default = [],
2026-07-12 18:19:20 +02:00
help = "Exact image references (repo:tag, incl. any registry prefix) for which no backup should be performed" ,
2025-12-26 18:13:26 +01:00
)
2026-08-02 09:10:20 +02:00
p . add_argument (
"--volumes-no-backup-required" ,
nargs = "+" ,
default = [],
help = "Exact volume names that are never backed up, whatever containers use them. For derived trees a restore cannot reproduce, above all a nested docker data root" ,
)
2025-12-26 18:13:26 +01:00
p . add_argument (
"--shutdown" ,
action = "store_true" ,
help = "Do not restart containers after backup" ,
)
2026-01-10 18:40:22 +01:00
2026-08-17 13:54:39 +02:00
scope = p . add_mutually_exclusive_group ()
scope . add_argument (
"--only-sql" ,
2025-12-26 18:13:26 +01:00
action = "store_true" ,
2025-12-29 08:28:23 +01:00
help = (
"Create database dumps only for DB volumes. "
"File backups are skipped for DB volumes if a dump succeeds, "
"but non-DB volumes are still backed up. "
"If a DB dump cannot be produced, baudolo falls back to a file backup."
),
2025-12-26 18:13:26 +01:00
)
2026-08-17 13:54:39 +02:00
scope . add_argument (
"--only-files" ,
action = "store_true" ,
help = (
"Take no database dumps at all and back up every volume as files. "
"For hosts that hold no database credentials. A database's files "
"are only consistent if its containers are stopped for the second "
"pass, so keep its image off --images-no-stop-required."
),
)
2026-07-31 13:01:43 +02:00
args = p . parse_args ()
2026-08-17 13:54:39 +02:00
if not args . only_files and not args . databases_csv :
p . error ( "--databases-csv is required unless --only-files is given" )
2026-07-31 13:01:43 +02:00
if bool ( args . snapshot ) != bool ( args . snapshot_subject ):
p . error ( "--snapshot and --snapshot-subject must be given together" )
if args . snapshot and args . shutdown :
2026-08-02 09:10:55 +02:00
p . error (
"--shutdown is meaningless with --snapshot: containers are never stopped"
)
2026-07-31 16:23:25 +02:00
if args . snapshot and args . hard_restart_projects :
p . error (
"--hard-restart-projects is meaningless with --snapshot: the flag exists "
"for stacks whose database cannot be backed up hot, which a snapshot solves"
)
2026-07-31 13:01:43 +02:00
return args