"""Utility script to persist the Restic repository password securely.""" from __future__ import annotations import argparse import os import stat from pathlib import Path from backup_health import DEFAULT_PASSWORD_FILE def write_password(password: str, target: Path) -> None: """Write the password to the target file with restrictive permissions.""" target.parent.mkdir(parents=True, exist_ok=True) target.write_text(password + "\n", encoding="utf-8") os.chmod(target, stat.S_IRUSR | stat.S_IWUSR) def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser(description="Store Restic password for diagnostics.") parser.add_argument( "--password", required=True, help="Password to store in the default Restic password file.", ) parser.add_argument( "--path", default=str(DEFAULT_PASSWORD_FILE), help=f"Target password file path (default: {DEFAULT_PASSWORD_FILE})", ) return parser.parse_args() def main() -> None: args = parse_args() password = args.password.strip() if not password: raise SystemExit("Refusing to store empty password.") target = Path(args.path).expanduser() write_password(password, target) print(f"Restic password stored in {target}") if __name__ == "__main__": main()