outsource backup folder cleanup into function in Container class

This commit is contained in:
Marc Koch 2020-12-16 23:03:29 +01:00
parent a30d7ef0de
commit def0776066
Signed by: marc
GPG Key ID: AC2D4E00990A6767
2 changed files with 19 additions and 16 deletions

View File

@ -65,22 +65,7 @@ def backup():
_print()
# Clean up backup folder
if not utils.no_cleanup:
deleted_files = 0
backup_dir = os.scandir(container.backup_dir)
backup_files = [file for file in backup_dir if
file.is_file() and file.name.startswith(container.name) and file.name.endswith(".tar.gz")]
while len(backup_files) > container.number_of_backups:
del_file = min(backup_files, key=os.path.getctime)
backup_files.remove(del_file)
os.remove(del_file)
deleted_files += 1
if deleted_files == 1:
_print(F"{Fore.YELLOW}Deleted 1 old backup file.{Style.RESET_ALL}")
elif deleted_files >= 1:
_print(F"{Fore.YELLOW}Deleted {deleted_files} old backup files.{Style.RESET_ALL}")
container.cleanup()
return backup_status

View File

@ -331,6 +331,24 @@ class Container:
return 2
return 1
def cleanup(self):
if not utils.no_cleanup:
deleted_files = 0
backup_dir = os.scandir(self.backup_dir)
backup_files = [file for file in backup_dir if
file.is_file() and file.name.startswith(self.name) and file.name.endswith(".tar.gz")]
while len(backup_files) > self.number_of_backups:
del_file = min(backup_files, key=os.path.getctime)
backup_files.remove(del_file)
os.remove(del_file)
deleted_files += 1
if deleted_files == 1:
_print(F"{Fore.YELLOW}Deleted 1 old backup file.{Style.RESET_ALL}")
elif deleted_files >= 1:
_print(F"{Fore.YELLOW}Deleted {deleted_files} old backup files.{Style.RESET_ALL}")
@staticmethod
def instantiate_containers(data: dict) -> dict:
containers = {}