From f703dc936c3cc1388586dbcc8f7cb84e2d700b27 Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Tue, 25 May 2021 23:59:20 -0500 Subject: [PATCH] Added functional do_backup function to create a backup of router in backup_path --- openwrt_backup/__init__.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/openwrt_backup/__init__.py b/openwrt_backup/__init__.py index ef4598a..132d7ee 100644 --- a/openwrt_backup/__init__.py +++ b/openwrt_backup/__init__.py @@ -1,4 +1,29 @@ from argparse import ArgumentParser +from pathlib import Path + +from openwrt_backup.retrieve import make_backup_on_remote, retrieve_backup_on_remote +from openwrt_backup.utils import ( + client_from_config, + exec_remote, + get_new_backup_filepath, +) +from openwrt_backup.config import CONFIG_DATA + + +def do_backup( + config_hostname, + ssh_config_root: Path, + backup_path: Path, + tmp_path: Path, + time_format, + dry, +): + client = client_from_config(config_hostname, ssh_config_root) + stdout, stderr = exec_remote(client, "echo $HOSTNAME") + hostname = stdout.read().decode().rstrip() + dest = get_new_backup_filepath(backup_path, time_format, hostname) + backup_on_remote = make_backup_on_remote(client, tmp_path) + retrieve_backup_on_remote(client, backup_on_remote, dest) def main(): @@ -12,3 +37,11 @@ def main(): "name", help="hostname in ssh_config file for openwrt device we need to backup" ) args = parser.parse_args() + do_backup( + args.name, + Path(CONFIG_DATA["ssh"]["root"]).expanduser(), + Path(CONFIG_DATA["backup"]["path"]).expanduser(), + Path(CONFIG_DATA["router"]["temp"]), + CONFIG_DATA["backup"]["time_format"], + args.dry, + )