#!/usr/bin/env python3 """Запуск резервного копирования SQLite, MySQL и PostgreSQL по JSON-плану. Пример плана: {"databases": [ {"type": "sqlite", "name": "app", "path": "/data/app.db"}, {"type": "mysql", "name": "shop", "database": "shop", "user": "backup", "defaults_file": "/etc/mysql/backup.cnf"}, {"type": "postgresql", "name": "wiki", "database": "wiki", "user": "backup", "host": "db.example.com"} ]} """ from __future__ import annotations import argparse import gzip import hashlib import json import re import shutil import sqlite3 import subprocess import sys import time from pathlib import Path NAME_RE = re.compile(r"^[A-Za-z0-9._-]+$") def file_digest(path: Path, chunk_size: int = 1024 * 1024) -> str: digest = hashlib.sha256() with path.open("rb") as handle: while block := handle.read(chunk_size): digest.update(block) return digest.hexdigest() def gzip_file(path: Path) -> Path: target = path.with_name(path.name + ".gz") if target.exists(): raise RuntimeError(f"Файл уже существует: {target}") try: with path.open("rb") as source, gzip.open(target, "wb", compresslevel=6) as destination: shutil.copyfileobj(source, destination) except Exception: target.unlink(missing_ok=True) raise path.unlink() return target def check_name(value: str, label: str) -> str: if not value or not NAME_RE.fullmatch(value): raise ValueError(f"Недопустимое имя {label}: {value}") return value def backup_sqlite(entry: dict, dest: Path, stamp: str, apply: bool) -> dict: name = check_name(str(entry.get("name") or Path(entry["path"]).stem), "базы") source = Path(entry["path"]).expanduser().resolve() if not source.is_file(): raise ValueError(f"SQLite файл не найден: {source}") target = dest / f"{name}-{stamp}.sqlite" if target.exists(): raise ValueError(f"Файл уже существует: {target}") if not apply: return {"type": "sqlite", "source": str(source), "target": str(target), "status": "planned"} source_conn = sqlite3.connect(source.as_uri() + "?mode=ro", uri=True) target_conn = sqlite3.connect(target) try: source_conn.backup(target_conn) check = target_conn.execute("PRAGMA integrity_check").fetchone() if not check or check[0] != "ok": raise RuntimeError(f"integrity_check не прошёл для {name}") except Exception: target_conn.close() source_conn.close() target.unlink(missing_ok=True) raise target_conn.close() source_conn.close() return { "type": "sqlite", "source": str(source), "target": str(target), "size": target.stat().st_size, "sha256": file_digest(target), "status": "ok", } def backup_mysql(entry: dict, dest: Path, stamp: str, apply: bool) -> dict: database = check_name(str(entry.get("database", "")), "базы MySQL") name = check_name(str(entry.get("name") or database), "дампа") if not shutil.which("mysqldump"): raise RuntimeError("mysqldump не найден. Установите клиент MySQL.") target = dest / f"{name}-{stamp}.sql.gz" if target.exists(): raise ValueError(f"Файл уже существует: {target}") command = ["mysqldump"] defaults_file = entry.get("defaults_file") if defaults_file: defaults_path = Path(defaults_file).expanduser().resolve() if not defaults_path.is_file(): raise ValueError(f"Файл настроек MySQL не найден: {defaults_path}") command.append(f"--defaults-extra-file={defaults_path}") command += [ "--single-transaction", "--quick", "--routines", "--triggers", "--events", "--host", str(entry.get("host", "127.0.0.1")), "--port", str(entry.get("port", 3306)), "--user", str(entry.get("user", "backup")), "--databases", database, ] if not apply: return {"type": "mysql", "database": database, "target": str(target), "status": "planned"} temporary = dest / f"{name}-{stamp}.sql" if temporary.exists(): raise ValueError(f"Файл уже существует: {temporary}") with temporary.open("xb") as handle: result = subprocess.run(command, stdout=handle, stderr=subprocess.PIPE, check=False) if result.returncode != 0: temporary.unlink(missing_ok=True) message = result.stderr.decode("utf-8", errors="replace").strip() raise RuntimeError(f"mysqldump завершился с ошибкой: {message}") try: final = gzip_file(temporary) except Exception: temporary.unlink(missing_ok=True) raise if final != target: final.unlink(missing_ok=True) raise RuntimeError("Не удалось получить ожидаемое имя дампа MySQL") return { "type": "mysql", "database": database, "target": str(final), "size": final.stat().st_size, "sha256": file_digest(final), "status": "ok", } def backup_postgres(entry: dict, dest: Path, stamp: str, apply: bool) -> dict: database = check_name(str(entry.get("database", "")), "базы PostgreSQL") name = check_name(str(entry.get("name") or database), "дампа") if not shutil.which("pg_dump"): raise RuntimeError("pg_dump не найден. Установите клиент PostgreSQL.") target = dest / f"{name}-{stamp}.dump" if target.exists(): raise ValueError(f"Файл уже существует: {target}") command = [ "pg_dump", "--no-password", "--format=custom", "--compress=9", "--file", str(target), "--host", str(entry.get("host", "127.0.0.1")), "--port", str(entry.get("port", 5432)), "--username", str(entry.get("user", "backup")), "--dbname", database, ] if not apply: return {"type": "postgresql", "database": database, "target": str(target), "status": "planned"} result = subprocess.run(command, stderr=subprocess.PIPE, check=False) if result.returncode != 0: target.unlink(missing_ok=True) message = result.stderr.decode("utf-8", errors="replace").strip() raise RuntimeError(f"pg_dump завершился с ошибкой: {message}") if shutil.which("pg_restore"): check = subprocess.run( ["pg_restore", "--list", str(target)], stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, check=False, ) if check.returncode != 0: target.unlink(missing_ok=True) message = check.stderr.decode("utf-8", errors="replace").strip() raise RuntimeError(f"pg_restore не прочитал дамп: {message}") return { "type": "postgresql", "database": database, "target": str(target), "size": target.stat().st_size, "sha256": file_digest(target), "status": "ok", } def main() -> int: parser = argparse.ArgumentParser(description="Дампы баз по JSON-плану") parser.add_argument("--config", type=Path, required=True) parser.add_argument("--dest", type=Path, required=True) parser.add_argument("--apply", action="store_true", help="Выполнить дампы") args = parser.parse_args() config_path = args.config.expanduser().resolve() if not config_path.is_file(): parser.error(f"План не найден: {config_path}") try: payload = json.loads(config_path.read_text(encoding="utf-8")) entries = payload["databases"] if not isinstance(entries, list) or not entries: raise ValueError("поле databases пустое") except (ValueError, KeyError, json.JSONDecodeError) as error: print(f"Ошибка плана: {error}", file=sys.stderr) return 2 dest = args.dest.expanduser().resolve() if args.apply: dest.mkdir(parents=True, exist_ok=True) stamp = time.strftime("%Y%m%d-%H%M%S") results = [] failed = 0 for entry in entries: if not isinstance(entry, dict): print("Пропуск записи: ожидался объект", file=sys.stderr) failed += 1 continue kind = str(entry.get("type", "")).lower() try: if kind == "sqlite": result = backup_sqlite(entry, dest, stamp, args.apply) elif kind == "mysql": result = backup_mysql(entry, dest, stamp, args.apply) elif kind in {"postgres", "postgresql"}: result = backup_postgres(entry, dest, stamp, args.apply) else: raise ValueError(f"Неизвестный тип базы: {kind}") results.append(result) print(f"{result['status']}: {result['target']}") except Exception as error: failed += 1 print(f"Ошибка: {error}", file=sys.stderr) if args.apply and results: manifest_path = dest / f"databases-manifest-{stamp}.json" manifest_path.write_text( json.dumps({"items": results}, ensure_ascii=False, indent=2) + "\n", encoding="utf-8", ) print(f"Manifest: {manifest_path}") elif not args.apply: print("Предпросмотр. Для выполнения добавьте --apply.") print(f"Записей: {len(entries)}, успешно: {len(results)}, ошибок: {failed}") return 1 if failed else 0 if __name__ == "__main__": sys.exit(main())