start.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. storage_box_env = credentials_path / "storage_box_env"
  24. ssh_config_path = Path.home() / ".ssh"
  25. volumes = [
  26. f"{config_d_path}:/etc/borgmatic.d/",
  27. f"{config_path}:/etc/borgmatic/",
  28. f"{credentials_path}:/credentials/",
  29. f"{ssh_config_path}:/root/.ssh",
  30. "borg_config:/root/.config/borg",
  31. "borg_cache:/root/.cache/borg",
  32. "borgmatic_state:/root/.local/state/borgmatic",
  33. ]
  34. if ssh_auth_sock:
  35. volumes += [f"{ssh_auth_sock}:{ssh_auth_sock}:Z"]
  36. volumes += [
  37. f"{vol}:{to_source_path(vol)}:ro" for vol in data_sources
  38. ]
  39. volume_args = [a for vol in volumes for a in ["-v", vol]]
  40. image_name = "ghcr.io/borgmatic-collective/borgmatic"
  41. args = [
  42. "podman",
  43. "run",
  44. "-h",
  45. hostname,
  46. "--detach",
  47. "--name",
  48. container_name,
  49. "-e",
  50. "SSH_AUTH_SOCK",
  51. "-e",
  52. "TZ=Europe/Paris",
  53. "-e",
  54. "SSH_KEY_NAME",
  55. "-e",
  56. f"HOST_LOGIN={os.getlogin()}",
  57. "--env-file",
  58. storage_box_env,
  59. "--security-opt=label=disable"
  60. ] + volume_args + [image_name]
  61. print(args)
  62. subprocess.run(args)
  63. if __name__ == "__main__":
  64. main()