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.

107 lines
3.1 KiB

  1. from argparse import ArgumentParser
  2. from pathlib import Path
  3. import toml
  4. from openwrt_backup.retrieve import make_backup_on_remote, retrieve_backup_on_remote
  5. from openwrt_backup.utils import (
  6. client_from_config,
  7. exec_remote,
  8. get_new_backup_filepath,
  9. )
  10. from openwrt_backup.config import CONFIG_DATA # noqa
  11. from openwrt_backup.prune import get_all_but_last_n
  12. def do_prune(dest_path: Path, n: int, dry, hostname=None):
  13. if dry:
  14. to_delete = get_all_but_last_n(dest_path, n - 1, hostname)
  15. else:
  16. to_delete = get_all_but_last_n(dest_path, n, hostname)
  17. for f in to_delete:
  18. if dry:
  19. print(f"Deleting: {f}")
  20. else:
  21. f.unlink()
  22. def do_backup(
  23. config_hostname,
  24. ssh_config_root: Path,
  25. backup_path: Path,
  26. tmp_path: Path,
  27. time_format,
  28. dry,
  29. ):
  30. client = client_from_config(config_hostname, ssh_config_root)
  31. stdout, stderr = exec_remote(client, "echo $HOSTNAME")
  32. hostname = stdout.read().decode().rstrip()
  33. dest = get_new_backup_filepath(backup_path, time_format, hostname)
  34. if dry:
  35. print(f"Making backup {tmp_path / 'backup_latest.openwrt_backup.tar.gz'}")
  36. print(
  37. f"Copying remote {tmp_path / 'backup_latest.openwrt_backup.tar.gz'} to {dest}"
  38. )
  39. else:
  40. backup_on_remote = make_backup_on_remote(client, tmp_path)
  41. retrieve_backup_on_remote(client, backup_on_remote, dest)
  42. return hostname
  43. def main():
  44. global CONFIG_DATA
  45. parser = ArgumentParser()
  46. parser.add_argument(
  47. "-d",
  48. "--dry",
  49. action="store_true",
  50. help="Don't do anything but show what would happen",
  51. )
  52. parser.add_argument(
  53. "-c", "--config", help="config path for backup script", default=None
  54. )
  55. subparsers = parser.add_subparsers(dest="action", required=True)
  56. p_run = subparsers.add_parser(
  57. "run",
  58. help=f"Make backup then prune all but the {CONFIG_DATA['backup']['keep']} most recent",
  59. )
  60. p_backup = subparsers.add_parser(
  61. "backup", help="Same as run but do not delete any backups"
  62. )
  63. p_dump_config = subparsers.add_parser( # noqa
  64. "dump-config", help="Show config and exit"
  65. ) # noqa
  66. p_run.add_argument(
  67. "name", help="Hostname in ssh_config file for openwrt device we need to backup"
  68. )
  69. p_backup.add_argument(
  70. "name", help="Hostname in ssh_config file for openwrt device we need to backup"
  71. )
  72. args = parser.parse_args()
  73. if args.config:
  74. with open(args.config) as f:
  75. CONFIG_DATA = toml.load(f)
  76. if args.action == "dump-config":
  77. print(toml.dumps(CONFIG_DATA))
  78. exit(0)
  79. hostname = do_backup(
  80. args.name,
  81. Path(CONFIG_DATA["ssh"]["root"]).expanduser(),
  82. Path(CONFIG_DATA["backup"]["path"]).expanduser(),
  83. Path(CONFIG_DATA["router"]["temp"]),
  84. CONFIG_DATA["backup"]["time_format"],
  85. args.dry,
  86. )
  87. if args.action != "backup":
  88. do_prune(
  89. Path(CONFIG_DATA["backup"]["path"]).expanduser(),
  90. CONFIG_DATA["backup"]["keep"],
  91. args.dry,
  92. hostname,
  93. )