2021-06-19 11:27:06 +02:00
|
|
|
# @see https://stackoverflow.com/questions/48929553/get-hard-disk-size-in-python
|
|
|
|
import psutil
|
2021-06-19 11:45:12 +02:00
|
|
|
import shutil
|
2021-06-19 11:27:06 +02:00
|
|
|
import os
|
2021-06-19 13:10:59 +02:00
|
|
|
backup_disk_path = "{{backup_disk_path}}"
|
2021-06-19 11:27:06 +02:00
|
|
|
backups_folder_path = os.path.join(backup_disk_path, "Backups/")
|
2021-06-19 12:38:05 +02:00
|
|
|
deleted = True
|
2023-04-16 09:59:54 +02:00
|
|
|
while psutil.disk_usage(backup_disk_path).percent > int({{size_percent_maximum_backup}}) and deleted:
|
2021-06-19 12:38:05 +02:00
|
|
|
deleted = False
|
2021-06-19 11:27:06 +02:00
|
|
|
print("%d %% of disk %s are used. Freeing space..." % (psutil.disk_usage(backup_disk_path).percent,backup_disk_path))
|
|
|
|
for primary_directory in os.listdir(backups_folder_path):
|
|
|
|
primary_directory = os.path.join(backups_folder_path, primary_directory)
|
|
|
|
for application_directory in os.listdir(primary_directory):
|
|
|
|
application_directory = os.path.join(primary_directory, application_directory)
|
2022-03-28 18:22:03 +02:00
|
|
|
versions_directory = os.path.join(application_directory, "versions/")
|
|
|
|
versions = os.listdir(versions_directory)
|
|
|
|
versions.sort(reverse=False)
|
|
|
|
if len(versions) >= 1:
|
|
|
|
delete_diff = versions_directory + versions[0]
|
2021-06-19 12:38:05 +02:00
|
|
|
print("Deleting %s..." % (delete_diff))
|
|
|
|
shutil.rmtree(delete_diff)
|
|
|
|
deleted = True
|
|
|
|
if not deleted:
|
2022-03-28 18:22:03 +02:00
|
|
|
print("All versions had been deleted!")
|
2021-06-19 11:45:12 +02:00
|
|
|
print("Cleaning up finished: %d %% of disk %s are used." % (psutil.disk_usage(backup_disk_path).percent,backup_disk_path))
|