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.

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