From 1e0079b84e8a845828f39ff5dc6daf3cf987f4bd Mon Sep 17 00:00:00 2001 From: buhtz Date: Sat, 24 Aug 2024 22:36:03 +0200 Subject: [PATCH] chore: Minor improvements in logging around canBackup function --- common/backintime.py | 8 +++++--- common/config.py | 48 ++++++++++++++++++++++++++++++-------------- common/snapshots.py | 24 +++++++++++----------- 3 files changed, 50 insertions(+), 30 deletions(-) diff --git a/common/backintime.py b/common/backintime.py index ed9fdab77..395a0d99c 100644 --- a/common/backintime.py +++ b/common/backintime.py @@ -509,8 +509,10 @@ def startApp(app_name='backintime'): args = argParse(None) # Name, Version, As Root, OS + msg = '' for key, val in collect_minimal_diagnostics().items(): - logger.debug(f'{key}: {val}') + msg = f'{msg}; {key}: {val}' + logger.debug(msg[2:]) # Add source path to $PATH environ if running from source if tools.runningFromSource(): @@ -610,8 +612,8 @@ def join(args, subArgs): for key in filter(lambda key: args_dict[key] is not None, args_dict) } - logger.debug(f'Used argument(s): {used_args}') - logger.debug(f'Unknown argument(s): {unknownArgs}') + + logger.debug(f'Argument(s) used: {used_args}') # Report unknown arguments but not if we run aliasParser next because we # will parse again in there. diff --git a/common/config.py b/common/config.py index 8f903d014..61d7caed9 100644 --- a/common/config.py +++ b/common/config.py @@ -409,17 +409,25 @@ def pid(self): def host(self): return socket.gethostname() - def snapshotsPath(self, profile_id = None, mode = None, tmp_mount = False): + def snapshotsPath(self, profile_id=None, mode=None, tmp_mount=False): + """Return the snapshot path (backup destination). + """ if mode is None: mode = self.snapshotsMode(profile_id) + + # If there are for this mode, then + # the path need to be mounted. if self.SNAPSHOT_MODES[mode][0] == None: - #no mount needed + # No mount needed #?Where to save snapshots in mode 'local'. This path must contain a #?folderstructure like 'backintime///';absolute path return self.profileStrValue('snapshots.path', '', profile_id) + else: - #mode need to be mounted; return mountpoint - symlink = self.snapshotsSymlink(profile_id = profile_id, tmp_mount = tmp_mount) + # Mode need to be mounted; return mountpoint + symlink = self.snapshotsSymlink( + profile_id=profile_id, tmp_mount=tmp_mount) + return os.path.join(self._LOCAL_MOUNT_ROOT, symlink) def snapshotsFullPath(self, profile_id = None): @@ -942,10 +950,10 @@ def includeV4(self, profile_id = None): return paths - def include(self, profile_id = None): + def include(self, profile_id=None): #?Include this file or folder. must be a counter starting with 1;absolute path:: #?Specify if \fIprofile.snapshots.include..value\fR is a folder (0) or a file (1).;0|1;0 - return self.profileListValue('snapshots.include', ('str:value', 'int:type'), [], profile_id) + return self.profileListValue(key='snapshots.include', type_key=('str:value', 'int:type'), default=[], profile_id=profile_id) def setInclude(self, values, profile_id = None): self.setProfileListValue('snapshots.include', ('str:value', 'int:type'), values, profile_id) @@ -1538,15 +1546,25 @@ def preparePath(self, path): path = path[: -1] return path - def isConfigured(self, profile_id = None): - """ - Checks if the program is configured - """ - return bool(self.snapshotsPath(profile_id) and self.include(profile_id)) + def isConfigured(self, profile_id=None): + """Checks if the program is configured. - def canBackup(self, profile_id = None): + It is assumed as configured if a snapshot path (backup destination) is + and include files/directories (backup source) are given. """ - Checks if snapshots_path exists + path = self.snapshotsPath(profile_id) + includes = self.include(profile_id) + + if bool(path and includes): + return True + else: + logger.debug(f'Profile ({profile_id=}) is not configured because ' + f'snapshot path is {bool(path)} and/or includes ' + f'are {bool(includes)}.', self) + return False + + def canBackup(self, profile_id=None): + """Checks if snapshots_path exists. """ if not self.isConfigured(profile_id): return False @@ -1554,11 +1572,11 @@ def canBackup(self, profile_id = None): path = self.snapshotsFullPath(profile_id) if not os.path.exists(path): + logger.warning(f'Snapshot path does not exists: {path}', self) return False if not os.path.isdir(path): - # path exists, but is no dir: something's very wrong. - logger.error("%s is not a directory"%path, self) + logger.warning(f'Snapshot path is not a directory: {path}', self) return False return True diff --git a/common/snapshots.py b/common/snapshots.py index 936caaee4..37ff2e841 100644 --- a/common/snapshots.py +++ b/common/snapshots.py @@ -845,25 +845,23 @@ def backup(self, force=False): message=message, timeout=30) + logger.warning( + 'Cannot start snapshot yet: target directory ' + 'not accessible. Will retry each second in ' + 'the next 30 seconds. Please wait.') + counter = 0 for counter in range(0, 30): - logger.debug( - 'Cannot start snapshot yet: target ' - 'directory not accessible. Waiting 1s.') time.sleep(1) if self.config.canBackup(): break - if counter != 0: - logger.info( - f'Waited {counter} seconds for target ' - 'directory to be available', self) - if not self.config.canBackup(profile_id): - logger.warning( - "Can't find snapshots folder!", self) + logger.error('Snapshots directory not ' + 'accessible. Tries stopped.', + self) # Can't find snapshots directory (is it on a # removable drive ?) self.config.PLUGIN_MANAGER.error(3) @@ -873,8 +871,10 @@ def backup(self, force=False): sid = SID(now, self.config) if sid.exists(): - logger.warning(f'Snapshot path "{sid.path()}" ' - 'already exists', self) + logger.warning( + f'Snapshot directory "{sid.path()}" ' + 'already exists', + self) # This snapshot already exists self.config.PLUGIN_MANAGER.error(4, sid)