start.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_path = Path.cwd() / "data" / "borgmatic.d"
  21. credentials_path = Path.cwd() / "data" / "credentials"
  22. storage_box_env = credentials_path / "storage_box_env"
  23. ssh_config_path = Path.home() / ".ssh"
  24. volumes = [
  25. f"{config_path}:/etc/borgmatic.d/",
  26. f"{credentials_path}:/credentials/",
  27. f"{ssh_config_path}:/root/.ssh",
  28. "borg_config:/root/.config/borg",
  29. "borg_cache:/root/.cache/borg",
  30. "borgmatic_state:/root/.local/state/borgmatic",
  31. ]
  32. if ssh_auth_sock:
  33. volumes += [f"{ssh_auth_sock}:{ssh_auth_sock}:Z"]
  34. if is_windows:
  35. ssh_key_name = "theenglishway-windows"
  36. else:
  37. ssh_key_name = "theenglishway.pub"
  38. volumes += [
  39. f"{vol}:{to_source_path(vol)}:ro" for vol in data_sources
  40. ]
  41. volume_args = [a for vol in volumes for a in ["-v", vol]]
  42. image_name = "ghcr.io/borgmatic-collective/borgmatic"
  43. args = [
  44. "podman",
  45. "run",
  46. "-h",
  47. hostname,
  48. "--detach",
  49. "--name",
  50. container_name,
  51. "-e",
  52. "SSH_AUTH_SOCK",
  53. "-e",
  54. "TZ=Europe/Paris",
  55. "-e",
  56. f"SSH_KEY_NAME={ssh_key_name}",
  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()