Refactored code

This commit is contained in:
Kevin Veen-Birkenbach 2024-01-12 20:08:30 +01:00
parent 67f54c2a16
commit eac64089f8

View File

@ -10,8 +10,8 @@ parser.add_argument('--maximum-backup-size-percent', type=int, dest='maximum_bac
parser.add_argument('--backups-folder-path',type=str,dest='backups_folder_path',required=True, help="The folder in which the backups are stored")
args = parser.parse_args()
def print_used_disc_space():
print("%d %% of disk %s are used" % (psutil.disk_usage(args.backups_folder_path).percent,args.backups_folder_path))
def print_used_disc_space(backups_folder_path):
print("%d %% of disk %s are used" % (psutil.disk_usage(backups_folder_path).percent,backups_folder_path))
def is_directory_used_by_another_process(directory_path):
command= "lsof " + directory_path
@ -22,8 +22,23 @@ def is_directory_used_by_another_process(directory_path):
return False
return True
for host_backup_directory_name in os.listdir(args.backups_folder_path):
host_backup_directory_path = os.path.join(args.backups_folder_path, host_backup_directory_name)
def isSmallerThenMaximumBackupSize(maximum_backup_size_percent,backups_folder_path):
current_disc_usage_percent=psutil.disk_usage(backups_folder_path).percent
return current_disc_usage_percent > maximum_backup_size_percent
def isDirectoryDeletable(version, versions_directory, version_path):
print("Checking directory %s ..." % (version_path))
if version == versions[-1]:
print("Directory %s contains the last version of the backup. Skipped." % (version_path))
return False
if is_directory_used_by_another_process(version_path):
print("Directory %s is used by another process. Skipped." % (version_path))
return False
backups_folder_path=args.backups_folder_path
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
@ -32,26 +47,19 @@ for host_backup_directory_name in os.listdir(args.backups_folder_path):
versions = os.listdir(versions_directory)
versions.sort(reverse=False)
print_used_disc_space()
print_used_disc_space(backups_folder_path)
for version in versions:
version_path=os.path.join(versions_directory, version)
print("Checking directory %s ..." % (version_path))
if version == versions[-1]:
print("Directory %s contains the last version of the backup. Skipped." % (version_path))
if not isDirectoryDeletable(version, versions_directory, version_path):
continue
if is_directory_used_by_another_process(version_path):
print("Directory %s is used by another process. Skipped." % (version_path))
continue
old_disc_usage_percent=psutil.disk_usage(args.backups_folder_path).percent
if old_disc_usage_percent > args.maximum_backup_size_percent:
if isSmallerThenMaximumBackupSize(maximum_backup_size_percent,backups_folder_path):
print("Deleting %s to free space." % (version_path))
shutil.rmtree(version_path)
new_disc_usage_percent=psutil.disk_usage(args.backups_folder_path).percent
new_disc_usage_percent=psutil.disk_usage(backups_folder_path).percent
difference_percent=old_disc_usage_percent-new_disc_usage_percent
print("{:6.2f} %% of drive freed".format(difference_percent))
continue
print_used_disc_space()
print_used_disc_space(backups_folder_path)
print("Cleaning up finished.")