213 lines
7.3 KiB
Python
213 lines
7.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Бэкап конфигураций: собирает bind-монтирования контейнеров (без данных)."""
|
|
import os
|
|
import subprocess
|
|
import tarfile
|
|
import time
|
|
|
|
DEST = "/mnt/backups/configs"
|
|
SKIP = ("/mnt/media", "/data", "/music", "/photos", "/downloads", "/library",
|
|
"/calibre-library", "/cwa-book-ingest")
|
|
MAX_FILE = 20 * 1024 * 1024
|
|
|
|
|
|
def sh(cmd, timeout=60):
|
|
r = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=timeout)
|
|
return (r.stdout or "").strip()
|
|
|
|
|
|
|
|
CT_TARGETS = [
|
|
("106", "/opt/smart-lxc/homeassistant/config", "homeassistant"),
|
|
("106", "/opt/ha-strip", "ha-strip"),
|
|
("140", "/opt/gitea/gitea/conf", "gitea-conf"),
|
|
("140", "/opt/gitea/docker-compose.yml", "gitea-compose"),
|
|
("134", "/opt/prometheus/rules", "prometheus-rules"),
|
|
("134", "/opt/prometheus/prometheus.yml", "prometheus-yml"),
|
|
("134", "/opt/alertmanager", "alertmanager"),
|
|
("107", "/opt/network-lxc/mosquitto/config", "mosquitto"),
|
|
("107", "/opt/network-lxc/uptime/data", "uptime-kuma"),
|
|
("107", "/opt/network-lxc/adguard/conf", "adguard"),
|
|
("108", "/opt/automation-lxc/n8n", "n8n-ct"),
|
|
("111", "/opt/books-bot", "books-bot-ct"),
|
|
("111", "/opt/catalog/data", "catalog-ct"),
|
|
("110", "/opt/lifestyle-lxc/grocy/config", "grocy"),
|
|
("110", "/opt/lifestyle-lxc/vikunja/db", "vikunja-db"),
|
|
("110", "/opt/lifestyle-lxc/homebox/data", "homebox"),
|
|
]
|
|
|
|
|
|
def ct_size_mb(ct, path):
|
|
out = sh(f"pct exec {ct} -- du -sm {path} 2>/dev/null | cut -f1")
|
|
try:
|
|
return int(out or 0)
|
|
except Exception:
|
|
return 0
|
|
|
|
|
|
def add_ct(tar, ct, path, name):
|
|
"""Пакует каталог или файл внутри CT и кладёт в общий архив."""
|
|
if not path.endswith(".yml") and not path.endswith(".yaml"):
|
|
pass
|
|
if path.endswith((".yml", ".yaml", ".conf")):
|
|
r = subprocess.run(["pct", "exec", ct, "--", "cat", path],
|
|
capture_output=True, timeout=120)
|
|
if r.returncode != 0 or not r.stdout:
|
|
return 0
|
|
tmpf = f"/tmp/ct-{name}.txt"
|
|
open(tmpf, "wb").write(r.stdout)
|
|
try:
|
|
tar.add(tmpf, arcname=os.path.join("ct", name))
|
|
return 1
|
|
finally:
|
|
try:
|
|
os.remove(tmpf)
|
|
except Exception:
|
|
pass
|
|
if ct_size_mb(ct, path) > 60:
|
|
return 0
|
|
tmp = f"/tmp/ct-{name}.tgz"
|
|
r = subprocess.run(["pct", "exec", ct, "--", "tar", "czf", "-", "-C", path, "."],
|
|
capture_output=True, timeout=300)
|
|
if r.returncode != 0 or not r.stdout:
|
|
return 0
|
|
open(tmp, "wb").write(r.stdout)
|
|
try:
|
|
tar.add(tmp, arcname=os.path.join("ct", name + ".tgz"))
|
|
return 1
|
|
finally:
|
|
try:
|
|
os.remove(tmp)
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
def containers():
|
|
out = []
|
|
for line in sh("pct list | tail -n +2").splitlines():
|
|
parts = line.split()
|
|
if len(parts) < 3:
|
|
continue
|
|
ct = parts[0]
|
|
names = sh(f"pct exec {ct} -- docker ps --format '{{{{.Names}}}}'").split()
|
|
for n in names:
|
|
out.append((ct, n))
|
|
for n in sh("docker ps --format '{{.Names}}'").split():
|
|
out.append(("0", n))
|
|
return out
|
|
|
|
|
|
def mounts(ct, name):
|
|
"""Список монтирований: работает и на хосте, и внутри CT."""
|
|
if ct == "0":
|
|
raw = sh(f"docker inspect {name} --format '{{{{range .Mounts}}}}{{{{.Source}}}}|{{{{end}}}}'")
|
|
else:
|
|
raw = sh(f"pct exec {ct} -- docker inspect {name} "
|
|
"--format '{{range .Mounts}}{{.Source}}|{{end}}'")
|
|
return [{"Source": x.strip()} for x in raw.split("|") if x.strip()]
|
|
|
|
|
|
def add_dir(tar, src, arc):
|
|
if not os.path.isdir(src):
|
|
return 0
|
|
if any(src.startswith(p) for p in SKIP):
|
|
return 0
|
|
n = 0
|
|
for root, dirs, files in os.walk(src):
|
|
dirs[:] = [d for d in dirs if not d.startswith(".")][:20]
|
|
for f in files[:200]:
|
|
p = os.path.join(root, f)
|
|
try:
|
|
if os.path.getsize(p) > MAX_FILE:
|
|
continue
|
|
rel = os.path.relpath(p, src)
|
|
tar.add(p, arcname=os.path.join(arc, rel))
|
|
n += 1
|
|
except Exception:
|
|
continue
|
|
if n > 800:
|
|
break
|
|
return n
|
|
|
|
|
|
|
|
DATA_HINTS = ("pgdata", "postgres", "library", "media", "downloads", "photos",
|
|
"music", "cache", "uploads", "backups", "ingest", "thumbnails")
|
|
CONFIG_HINTS = ("config", "conf", "etc", "data", "settings", "app.ini", ".yml", ".yaml")
|
|
|
|
|
|
def auto_discover(tar, seen, count, total):
|
|
"""Идёт по всем CT, смотрит монтирования контейнеров и берёт конфиги."""
|
|
for ct, name in containers():
|
|
if ct == "0":
|
|
continue
|
|
raw = sh(f"pct exec {ct} -- docker ps --format '{{{{.Names}}}}'")
|
|
for cname in raw.split():
|
|
try:
|
|
mounts = mounts(ct, cname)
|
|
except Exception:
|
|
continue
|
|
for m in mounts:
|
|
src = (m.get("Source") or "").strip()
|
|
if not src.startswith("/opt") or src in seen:
|
|
continue
|
|
low = src.lower()
|
|
if any(h in low for h in DATA_HINTS) and "config" not in low:
|
|
continue
|
|
if not any(h in low for h in CONFIG_HINTS):
|
|
continue
|
|
seen.add(src)
|
|
n = add_ct(tar, ct, src, f"{cname}-{os.path.basename(src)}")
|
|
if n:
|
|
count += 1
|
|
total += 1
|
|
return count, total
|
|
|
|
|
|
def main():
|
|
os.makedirs(DEST, exist_ok=True)
|
|
stamp = time.strftime("%Y%m%d-%H%M")
|
|
path = os.path.join(DEST, f"configs-{stamp}.tar.gz")
|
|
total, count = 0, 0
|
|
import glob
|
|
with tarfile.open(path, "w:gz") as tar:
|
|
seen = set()
|
|
for ct, cname in containers():
|
|
for m in mounts(ct, cname):
|
|
src = (m.get("Source") or "").strip()
|
|
if not src.startswith("/opt") or src in seen:
|
|
continue
|
|
low = src.lower()
|
|
if any(h in low for h in DATA_HINTS) and "config" not in low:
|
|
continue
|
|
if not any(h in low for h in CONFIG_HINTS):
|
|
continue
|
|
seen.add(src)
|
|
base = os.path.basename(src.rstrip("/")) or "root"
|
|
if ct == "0":
|
|
n = add_dir(tar, src, os.path.join(cname, base))
|
|
else:
|
|
n = add_ct(tar, ct, src, f"{cname}-{base}")
|
|
if n:
|
|
count += 1
|
|
total += n
|
|
|
|
for ct, ctpath, name in CT_TARGETS:
|
|
n = add_ct(tar, ct, ctpath, name)
|
|
if n:
|
|
count += 1
|
|
total += 1
|
|
|
|
size = os.path.getsize(path)
|
|
# ротация
|
|
for f in sorted(os.listdir(DEST)):
|
|
full = os.path.join(DEST, f)
|
|
if f.startswith("configs-") and os.path.getmtime(full) < time.time() - 30 * 86400:
|
|
os.remove(full)
|
|
print(f"{time.strftime('%Y-%m-%dT%H:%M:%S')} configs: dirs={count} files={total} "
|
|
f"size={size} -> {path}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|