start.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. if is_windows:
  37. ssh_key_name = "theenglishway-windows"
  38. else:
  39. ssh_key_name = "theenglishway.pub"
  40. volumes += [
  41. f"{vol}:{to_source_path(vol)}:ro" for vol in data_sources
  42. ]
  43. volume_args = [a for vol in volumes for a in ["-v", vol]]
  44. image_name = "ghcr.io/borgmatic-collective/borgmatic"
  45. args = [
  46. "podman",
  47. "run",
  48. "-h",
  49. hostname,
  50. "--detach",
  51. "--name",
  52. container_name,
  53. "-e",
  54. "SSH_AUTH_SOCK",
  55. "-e",
  56. "TZ=Europe/Paris",
  57. "-e",
  58. f"SSH_KEY_NAME={ssh_key_name}",
  59. "-e",
  60. f"HOST_LOGIN={os.getlogin()}",
  61. "--env-file",
  62. storage_box_env,
  63. "--security-opt=label=disable"
  64. ] + volume_args + [image_name]
  65. print(args)
  66. subprocess.run(args)
  67. if __name__ == "__main__":
  68. main()