start.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import os
  2. import subprocess
  3. import socket
  4. from pathlib import Path, PurePosixPath
  5. is_windows = os.name == "nt"
  6. def read_data_sources(file: Path) -> list[Path]:
  7. with open(file) as f:
  8. paths = f.readlines()
  9. return [Path(p_str.strip()).expanduser() for p_str in paths]
  10. def to_source_path(path: Path):
  11. mount_base = PurePosixPath("/mnt") / "source"
  12. inner_path = PurePosixPath(path)
  13. with_drive = PurePosixPath(inner_path.parts[0].replace(":", "")).joinpath(*inner_path.parts[1:])
  14. return mount_base / with_drive.relative_to(with_drive.anchor)
  15. def main():
  16. data_sources = read_data_sources(Path("./data_sources"))
  17. hostname = socket.gethostname()
  18. container_name = "borgmatic"
  19. ssh_auth_sock = os.getenv("SSH_AUTH_SOCK")
  20. config_d_path = Path.cwd() / "data" / "borgmatic.d"
  21. config_path = Path.cwd() / "data" / "borgmatic"
  22. credentials_path = Path.cwd() / "data" / "credentials"
  23. ssh_config_path = Path.home() / ".ssh"
  24. volumes = [
  25. f"{config_d_path}:/etc/borgmatic.d/",
  26. f"{config_path}:/etc/borgmatic/",
  27. f"{credentials_path}:/credentials/",
  28. f"{ssh_config_path}:/root/.ssh",
  29. "borg_config:/root/.config/borg",
  30. "borg_cache:/root/.cache/borg",
  31. "borgmatic_state:/root/.local/state/borgmatic",
  32. ]
  33. if ssh_auth_sock:
  34. volumes += [f"{ssh_auth_sock}:{ssh_auth_sock}:Z"]
  35. volumes += [
  36. f"{vol}:{to_source_path(vol)}:ro" for vol in data_sources
  37. ]
  38. volume_args = [a for vol in volumes for a in ["-v", vol]]
  39. image_name = "ghcr.io/borgmatic-collective/borgmatic"
  40. args = [
  41. "podman",
  42. "run",
  43. "-h",
  44. hostname,
  45. "--detach",
  46. "--name",
  47. container_name,
  48. "-e",
  49. "SSH_AUTH_SOCK",
  50. "-e",
  51. "TZ=Europe/Paris",
  52. "-e",
  53. "SSH_KEY_NAME",
  54. "-e",
  55. f"HOST_LOGIN={os.getlogin()}",
  56. "--security-opt=label=disable"
  57. ] + volume_args + [image_name]
  58. print(args)
  59. subprocess.run(args)
  60. if __name__ == "__main__":
  61. main()