| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- import os
- import subprocess
- import socket
- from pathlib import Path, PurePosixPath
- is_windows = os.name == "nt"
- def read_data_sources(file: Path) -> list[Path]:
- with open(file) as f:
- paths = f.readlines()
- return [Path(p_str.strip()).expanduser() for p_str in paths]
- def to_source_path(path: Path):
- mount_base = PurePosixPath("/mnt") / "source"
- inner_path = PurePosixPath(path)
- with_drive = PurePosixPath(inner_path.parts[0].replace(":", "")).joinpath(*inner_path.parts[1:])
- return mount_base / with_drive.relative_to(with_drive.anchor)
- def main():
- data_sources = read_data_sources(Path("./data_sources"))
- hostname = socket.gethostname()
- container_name = "borgmatic"
- ssh_auth_sock = os.getenv("SSH_AUTH_SOCK")
- config_d_path = Path.cwd() / "data" / "borgmatic.d"
- config_path = Path.cwd() / "data" / "borgmatic"
- credentials_path = Path.cwd() / "data" / "credentials"
- storage_box_env = credentials_path / "storage_box_env"
- ssh_config_path = Path.home() / ".ssh"
- volumes = [
- f"{config_d_path}:/etc/borgmatic.d/",
- f"{config_path}:/etc/borgmatic/",
- f"{credentials_path}:/credentials/",
- f"{ssh_config_path}:/root/.ssh",
- "borg_config:/root/.config/borg",
- "borg_cache:/root/.cache/borg",
- "borgmatic_state:/root/.local/state/borgmatic",
- ]
- if ssh_auth_sock:
- volumes += [f"{ssh_auth_sock}:{ssh_auth_sock}:Z"]
- if is_windows:
- ssh_key_name = "theenglishway-windows"
- else:
- ssh_key_name = "theenglishway.pub"
- volumes += [
- f"{vol}:{to_source_path(vol)}:ro" for vol in data_sources
- ]
- volume_args = [a for vol in volumes for a in ["-v", vol]]
- image_name = "ghcr.io/borgmatic-collective/borgmatic"
- args = [
- "podman",
- "run",
- "-h",
- hostname,
- "--detach",
- "--name",
- container_name,
- "-e",
- "SSH_AUTH_SOCK",
- "-e",
- "TZ=Europe/Paris",
- "-e",
- f"SSH_KEY_NAME={ssh_key_name}",
- "--env-file",
- storage_box_env,
- "--security-opt=label=disable"
- ] + volume_args + [image_name]
- print(args)
- subprocess.run(args)
- if __name__ == "__main__":
- main()
|