start.py 2.1 KB

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