Rename runner function

This commit is contained in:
Orsiris de Jong 2023-12-28 13:49:15 +01:00
parent 42769607d1
commit f56875bd3c
4 changed files with 51 additions and 47 deletions

View file

@ -53,7 +53,30 @@ This program is distributed under the GNU General Public License and comes with
This is free software, and you are welcome to redistribute it under certain conditions; Please type --license for more info.""",
)
parser.add_argument(
"-c",
"--config-file",
dest="config_file",
type=str,
default=None,
required=False,
help="Path to alternative configuration file (defaults to current dir/npbackup.conf)",
)
parser.add_argument(
"--repo-name",
dest="repo_name",
type=str,
default="default",
required=False,
help="Name of the repository to work with. Defaults to 'default'",
)
parser.add_argument("-b", "--backup", action="store_true", help="Run a backup")
parser.add_argument(
"-f", "--force",
action="store_true",
default=False,
help="Force running a backup regardless of existing backups age",
)
parser.add_argument(
"-r",
"--restore",
@ -129,30 +152,7 @@ This is free software, and you are welcome to redistribute it under certain cond
parser.add_argument(
"--has-recent-backup", action="store_true", help="Check if a recent backup exists"
)
parser.add_argument(
"-f", "--force",
action="store_true",
default=False,
help="Force running a backup regardless of existing backups",
)
parser.add_argument(
"-c",
"--config-file",
dest="config_file",
type=str,
default=None,
required=False,
help="Path to alternative configuration file",
)
parser.add_argument(
"--repo-name",
dest="repo_name",
type=str,
default="default",
required=False,
help="Name of the repository to work with. Defaults to 'default'",
"--has-recent-snapshot", action="store_true", help="Check if a recent snapshot exists"
)
parser.add_argument(
"--restore-include",
@ -162,11 +162,11 @@ This is free software, and you are welcome to redistribute it under certain cond
help="Restore only paths within include path",
)
parser.add_argument(
"--restore-from-snapshot",
"--snapshot",
type=str,
default="latest",
required=False,
help="Choose which snapshot to restore from. Defaults to latest",
help="Choose which snapshot to use. Defaults to latest",
)
parser.add_argument(
"-v", "--verbose", action="store_true", help="Show verbose output"
@ -313,20 +313,24 @@ This is free software, and you are welcome to redistribute it under certain cond
cli_args["op_args"] = {
"command": args.raw
}
elif args.has_recent_snapshot:
cli_args["operation"] = "has_recent_snapshot"
locking_operations = ["backup", "repair", "forget", "prune", "raw", "unlock"]
# Program entry
if cli_args["operation"] in locking_operations:
try:
with pidfile.PIDFile(PID_FILE):
entrypoint(**cli_args)
except pidfile.AlreadyRunningError:
logger.critical("Backup process already running. Will not continue.")
# EXIT_CODE 21 = current backup process already running
sys.exit(21)
if cli_args["operation"]:
locking_operations = ["backup", "repair", "forget", "prune", "raw", "unlock"]
# Program entry
if cli_args["operation"] in locking_operations:
try:
with pidfile.PIDFile(PID_FILE):
entrypoint(**cli_args)
except pidfile.AlreadyRunningError:
logger.critical("Backup process already running. Will not continue.")
# EXIT_CODE 21 = current backup process already running
sys.exit(21)
else:
entrypoint(**cli_args)
else:
entrypoint(**cli_args)
logger.warning("No operation has been requested")

View file

@ -315,7 +315,7 @@ class NPBackupRunner:
def wrapper(self, *args, **kwargs):
required_permissions = {
"backup": ["backup", "restore", "full"],
"check_recent_backups": ["backup", "restore", "full"],
"has_recent_snapshot": ["backup", "restore", "full"],
"list": ["backup", "restore", "full"],
"ls": ["backup", "restore", "full"],
"find": ["backup", "restore", "full"],
@ -375,7 +375,7 @@ class NPBackupRunner:
return fn(self, *args, **kwargs)
except Exception as exc:
# pylint: disable=E1101 (no-member)
self.write_logs(f"Function {fn.__name__} failed with: {exc}")
self.write_logs(f"Function {fn.__name__} failed with: {exc}", level="error")
logger.debug("Trace:", exc_info=True)
return False
@ -636,7 +636,7 @@ class NPBackupRunner:
@is_ready
@apply_config_to_restic_runner
@catch_exceptions
def check_recent_backups(self) -> bool:
def has_recent_snapshot(self) -> bool:
"""
Checks for backups in timespan
Returns True or False if found or not
@ -650,7 +650,7 @@ class NPBackupRunner:
level="info",
)
self.restic_runner.verbose = False
result, backup_tz = self.restic_runner.has_snapshot_timedelta(
result, backup_tz = self.restic_runner.has_recent_snapshot(
self.minimum_backup_age
)
self.restic_runner.verbose = self.verbose
@ -776,7 +776,7 @@ class NPBackupRunner:
level="critical",
)
return False
if self.check_recent_backups() and not force:
if self.has_recent_snapshot() and not force:
self.write_logs("No backup necessary.", level="info")
return True
self.restic_runner.verbose = self.verbose

View file

@ -467,7 +467,7 @@ def _main_gui(viewer_mode: bool):
snapshots = gui_thread_runner(
repo_config, "list", __gui_msg=gui_msg, __autoclose=True, __compact=True
)
current_state, backup_tz = ResticRunner._has_snapshot_timedelta(
current_state, backup_tz = ResticRunner._has_recent_snapshot(
snapshots, repo_config.g("repo_opts.minimum_backup_age")
)
snapshot_list = []

View file

@ -850,7 +850,7 @@ class ResticRunner:
return False, output
@staticmethod
def _has_snapshot_timedelta(
def _has_recent_snapshot(
snapshot_list: List, delta: int = None
) -> Tuple[bool, Optional[datetime]]:
"""
@ -882,7 +882,7 @@ class ResticRunner:
return True, backup_ts
return None, backup_ts
def has_snapshot_timedelta(
def has_recent_snapshot(
self, delta: int = None
) -> Tuple[bool, Optional[datetime]]:
"""
@ -904,7 +904,7 @@ class ResticRunner:
snapshots = self.snapshots()
if self.last_command_status is False:
return None, None
return self._has_snapshot_timedelta(snapshots, delta)
return self._has_recent_snapshot(snapshots, delta)
except IndexError as exc:
self.write_logs(f"snapshot information missing: {exc}", level="error")
logger.debug("Trace", exc_info=True)