This commit is contained in:
2025-07-20 13:17:20 +02:00
parent 57806534ce
commit 27ee19a263

View File

@@ -7,31 +7,39 @@ import yaml
from datetime import datetime
from pathlib import Path
def is_valid_compose_project():
result = subprocess.run(
["docker", "compose", "config"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
return result.returncode == 0
def run_cmd(cmd, capture_output=False):
result = subprocess.run(cmd, shell=True, text=True, capture_output=capture_output)
if result.returncode != 0:
print(f"[!] Fehler bei Befehl: {cmd}")
print(f"[!] Error while running command: {cmd}")
print(result.stderr)
raise RuntimeError("Befehl fehlgeschlagen")
raise RuntimeError("Command failed with return code:", result.returncode)
return result.stdout if capture_output else None
def get_project_name():
return Path(os.getcwd()).name
def get_volumes():
print("[*] Ermittele Volumes mit `docker compose config`...")
print("[*] Determine Volumes with `docker compose config`...")
output = run_cmd("docker compose config", capture_output=True)
output = yaml.safe_load(output)
volumes = []
for logical_name, attrs in output.get("volumes", {}).items():
actual_name = attrs.get("name", logical_name) # fallback if "name" not set
actual_name = attrs.get("name", logical_name)
volumes.append(actual_name)
return volumes
def archive_project(output_dir):
print("[*] Archiviere Projektordner...")
print("[*] Archiving Project folder")
with tarfile.open(output_dir / "project_files.tar.gz", "w:gz") as tar:
for item in Path(".").iterdir():
if item.name in [output_dir.name, ".git", "__pycache__"]:
@@ -39,7 +47,7 @@ def archive_project(output_dir):
tar.add(item, arcname=item.name)
def archive_volume(volume_name, backup_path):
print(f"[*] Archiviere Volume: {volume_name}")
print(f"[*] Archiving Volume: {volume_name}")
archive_file = backup_path / f"{volume_name}.tar.gz"
cmd = (
f"docker run --rm "
@@ -51,6 +59,14 @@ def archive_volume(volume_name, backup_path):
return archive_file
def main():
if is_valid_compose_project():
print("[*] Valid Compose Project Found")
else:
print("[*] No Valid Compose Project Found")
return
return
project_name = get_project_name()
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
final_archive_name = f"{project_name}_backup_{timestamp}.tar.gz"
@@ -72,12 +88,12 @@ def main():
for vol in volumes:
archive_volume(vol, tmp_path)
print(f"[*] Erstelle Gesamtarchiv: {final_archive_path}")
print(f"[*] Creating full archive: {final_archive_path}")
with tarfile.open(final_archive_path, "w:gz") as tar:
for file in tmp_path.iterdir():
tar.add(file, arcname=file.name)
print(f"[✔] Backup abgeschlossen: {final_archive_path}")
print(f"[✔] Backup completed successfully. Archive saved as: {final_archive_path}")
if __name__ == "__main__":
main()