Implemented new deletion modell, which distributes the deletions over application. See also: https://chat.openai.com/share/c6c35642-276a-44db-abfd-31cb71c4cec9

This commit is contained in:
Kevin Veen-Birkenbach 2024-01-12 21:06:47 +01:00
parent 2f3d90cd07
commit 6d551b47af

View File

@ -43,26 +43,67 @@ def deleteVersion(version_path, backups_folder_path):
difference_percent=old_disc_usage_percent-new_disc_usage_percent difference_percent=old_disc_usage_percent-new_disc_usage_percent
print("{:6.2f} %% of drive freed".format(difference_percent)) print("{:6.2f} %% of drive freed".format(difference_percent))
def count_total_application_directories(backups_folder_path):
total_app_directories = 0
for host_backup_directory_name in os.listdir(backups_folder_path):
host_backup_directory_path = os.path.join(backups_folder_path, host_backup_directory_name)
total_app_directories += sum(os.path.isdir(os.path.join(host_backup_directory_path, d)) for d in os.listdir(host_backup_directory_path))
return total_app_directories
def count_total_version_folders(backups_folder_path):
total_version_folders = 0
for host_backup_directory_name in os.listdir(backups_folder_path):
host_backup_directory_path = os.path.join(backups_folder_path, host_backup_directory_name)
for application_directory in os.listdir(host_backup_directory_path):
versions_directory = os.path.join(host_backup_directory_path, application_directory)
total_version_folders += sum(os.path.isdir(os.path.join(versions_directory, d)) for d in os.listdir(versions_directory))
return total_version_folders
def average_version_directories_per_application(backups_folder_path,blur=-1):
total_app_directories = count_total_application_directories(backups_folder_path)
total_version_folders = count_total_version_folders(backups_folder_path)
if total_app_directories == 0:
return 0
average = total_version_folders / total_app_directories
return int(average) - blur
def getAmountOfIterration(versions,average_version_directories_per_application):
return len(versions) - average_version_directories_per_application
def deletInterration(backups_folder_path,average_version_directories_per_application):
for host_backup_directory_name in os.listdir(backups_folder_path):
host_backup_directory_path = os.path.join(backups_folder_path, host_backup_directory_name)
for application_directory in os.listdir(host_backup_directory_path):
# The directory which contains all backup versions of the application
versions_directory = os.path.join(host_backup_directory_path, application_directory) + "/"
versions = os.listdir(versions_directory)
versions.sort(reverse=False)
version_iterration=0
while version_iterration < getAmountOfIterration(versions,average_version_directories_per_application):
print_used_disc_space(backups_folder_path)
version = versions[version_iterration]
version_path=os.path.join(versions_directory, version)
if isDirectoryDeletable(version, versions_directory, version_path):
deleteVersion(version_path, backups_folder_path)
version_iterration += 1
backups_folder_path=args.backups_folder_path backups_folder_path=args.backups_folder_path
for host_backup_directory_name in os.listdir(backups_folder_path): maximum_backup_size_percent=args.maximum_backup_size_percent
host_backup_directory_path = os.path.join(backups_folder_path, host_backup_directory_name)
for application_directory in os.listdir(host_backup_directory_path):
# The directory which contains all backup versions of the application itteration_counter = 1
versions_directory = os.path.join(host_backup_directory_path, application_directory) + "/" while isSmallerThenMaximumBackupSize(maximum_backup_size_percent, backups_folder_path):
if itteration_counter > 200:
raise Exception("Iteration limit exceeded")
versions = os.listdir(versions_directory) print(f"Delete Iteration: {itteration_counter}")
versions.sort(reverse=False) average_version_directories = average_version_directories_per_application(backups_folder_path)
print(f"Average version directories per application directory: {average_version_directories}")
print_used_disc_space(backups_folder_path) deleteIteration(backups_folder_path, average_version_directories)
for version in versions: itteration_counter += 1
version_path=os.path.join(versions_directory, version)
if not isDirectoryDeletable(version, versions_directory, version_path):
continue
if isSmallerThenMaximumBackupSize(maximum_backup_size_percent,backups_folder_path):
deleteVersion(version_path, backups_folder_path)
continue
print_used_disc_space(backups_folder_path) print_used_disc_space(backups_folder_path)
print("Cleaning up finished.") print("Cleaning up finished.")