import os import subprocess import socket from pathlib import Path, PurePosixPath is_windows = os.name == "nt" def read_data_sources(hostname: str, login: str) -> list[Path]: file = Path(f"./data_sources_{hostname}_{login}") 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(): login = os.getlogin() hostname = socket.gethostname() data_sources = read_data_sources(hostname, login) container_name = "borgmatic" ssh_auth_sock = os.getenv("SSH_AUTH_SOCK") data_path = Path.cwd() / "data" config_d_path = data_path / "borgmatic.d" config_path = data_path / "borgmatic" credentials_path = data_path / "credentials" history_file = data_path / ".bash_history" history_file.touch() 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", f"{history_file}:/root/.bash_history", "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"] 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", "SSH_KEY_NAME", "-e", f"HOST_LOGIN={login}", "--security-opt=label=disable" ] + volume_args + [image_name] print(args) subprocess.run(args) if __name__ == "__main__": main()