checks
This commit is contained in:
@@ -7,31 +7,39 @@ import yaml
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pathlib import Path
|
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):
|
def run_cmd(cmd, capture_output=False):
|
||||||
result = subprocess.run(cmd, shell=True, text=True, capture_output=capture_output)
|
result = subprocess.run(cmd, shell=True, text=True, capture_output=capture_output)
|
||||||
if result.returncode != 0:
|
if result.returncode != 0:
|
||||||
print(f"[!] Fehler bei Befehl: {cmd}")
|
print(f"[!] Error while running command: {cmd}")
|
||||||
print(result.stderr)
|
print(result.stderr)
|
||||||
raise RuntimeError("Befehl fehlgeschlagen")
|
raise RuntimeError("Command failed with return code:", result.returncode)
|
||||||
return result.stdout if capture_output else None
|
return result.stdout if capture_output else None
|
||||||
|
|
||||||
def get_project_name():
|
def get_project_name():
|
||||||
return Path(os.getcwd()).name
|
return Path(os.getcwd()).name
|
||||||
|
|
||||||
def get_volumes():
|
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 = run_cmd("docker compose config", capture_output=True)
|
||||||
output = yaml.safe_load(output)
|
output = yaml.safe_load(output)
|
||||||
volumes = []
|
volumes = []
|
||||||
|
|
||||||
for logical_name, attrs in output.get("volumes", {}).items():
|
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)
|
volumes.append(actual_name)
|
||||||
|
|
||||||
return volumes
|
return volumes
|
||||||
|
|
||||||
def archive_project(output_dir):
|
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:
|
with tarfile.open(output_dir / "project_files.tar.gz", "w:gz") as tar:
|
||||||
for item in Path(".").iterdir():
|
for item in Path(".").iterdir():
|
||||||
if item.name in [output_dir.name, ".git", "__pycache__"]:
|
if item.name in [output_dir.name, ".git", "__pycache__"]:
|
||||||
@@ -39,7 +47,7 @@ def archive_project(output_dir):
|
|||||||
tar.add(item, arcname=item.name)
|
tar.add(item, arcname=item.name)
|
||||||
|
|
||||||
def archive_volume(volume_name, backup_path):
|
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"
|
archive_file = backup_path / f"{volume_name}.tar.gz"
|
||||||
cmd = (
|
cmd = (
|
||||||
f"docker run --rm "
|
f"docker run --rm "
|
||||||
@@ -51,6 +59,14 @@ def archive_volume(volume_name, backup_path):
|
|||||||
return archive_file
|
return archive_file
|
||||||
|
|
||||||
def main():
|
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()
|
project_name = get_project_name()
|
||||||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||||
final_archive_name = f"{project_name}_backup_{timestamp}.tar.gz"
|
final_archive_name = f"{project_name}_backup_{timestamp}.tar.gz"
|
||||||
@@ -72,12 +88,12 @@ def main():
|
|||||||
for vol in volumes:
|
for vol in volumes:
|
||||||
archive_volume(vol, tmp_path)
|
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:
|
with tarfile.open(final_archive_path, "w:gz") as tar:
|
||||||
for file in tmp_path.iterdir():
|
for file in tmp_path.iterdir():
|
||||||
tar.add(file, arcname=file.name)
|
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__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user