outsource backup folder cleanup into function in Container class
This commit is contained in:
parent
a30d7ef0de
commit
def0776066
17
backup.py
17
backup.py
|
|
@ -65,22 +65,7 @@ def backup():
|
||||||
_print()
|
_print()
|
||||||
|
|
||||||
# Clean up backup folder
|
# Clean up backup folder
|
||||||
if not utils.no_cleanup:
|
container.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}")
|
|
||||||
|
|
||||||
return backup_status
|
return backup_status
|
||||||
|
|
||||||
|
|
|
||||||
18
models.py
18
models.py
|
|
@ -331,6 +331,24 @@ class Container:
|
||||||
return 2
|
return 2
|
||||||
return 1
|
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
|
@staticmethod
|
||||||
def instantiate_containers(data: dict) -> dict:
|
def instantiate_containers(data: dict) -> dict:
|
||||||
containers = {}
|
containers = {}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue