You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.7 KiB

  1. from argparse import ArgumentParser
  2. from pathlib import Path
  3. from openwrt_backup.retrieve import make_backup_on_remote, retrieve_backup_on_remote
  4. from openwrt_backup.utils import (
  5. client_from_config,
  6. exec_remote,
  7. get_new_backup_filepath,
  8. )
  9. from openwrt_backup.config import CONFIG_DATA
  10. def do_backup(
  11. config_hostname,
  12. ssh_config_root: Path,
  13. backup_path: Path,
  14. tmp_path: Path,
  15. time_format,
  16. dry,
  17. ):
  18. client = client_from_config(config_hostname, ssh_config_root)
  19. stdout, stderr = exec_remote(client, "echo $HOSTNAME")
  20. hostname = stdout.read().decode().rstrip()
  21. dest = get_new_backup_filepath(backup_path, time_format, hostname)
  22. if dry:
  23. print(f"Making backup {tmp_path / 'backup_latest.openwrt_backup.tar.gz'}")
  24. print(
  25. f"Copying remote {tmp_path / 'backup_latest.openwrt_backup.tar.gz'} to {dest}"
  26. )
  27. else:
  28. backup_on_remote = make_backup_on_remote(client, tmp_path)
  29. retrieve_backup_on_remote(client, backup_on_remote, dest)
  30. def main():
  31. parser = ArgumentParser()
  32. parser.add_argument("-d", "--dry", action="store_true", help="Don't do anything")
  33. parser.add_argument("--dump-config", action="store_true", help="Show config")
  34. parser.add_argument(
  35. "-c" "--config", help="config path for backup script", default=None
  36. )
  37. parser.add_argument(
  38. "name", help="hostname in ssh_config file for openwrt device we need to backup"
  39. )
  40. args = parser.parse_args()
  41. do_backup(
  42. args.name,
  43. Path(CONFIG_DATA["ssh"]["root"]).expanduser(),
  44. Path(CONFIG_DATA["backup"]["path"]).expanduser(),
  45. Path(CONFIG_DATA["router"]["temp"]),
  46. CONFIG_DATA["backup"]["time_format"],
  47. args.dry,
  48. )