Refactored native-backups-cleanup and native-disc-space-check. \n Removed hardcoded values and used parameters instead.

This commit is contained in:
2023-04-16 12:37:31 +02:00
parent 0591f927ec
commit da2d7823b8
7 changed files with 26 additions and 15 deletions

View File

@@ -1,25 +0,0 @@
# @see https://stackoverflow.com/questions/48929553/get-hard-disk-size-in-python
import psutil
import shutil
import os
backup_disk_path = "{{backup_disk_path}}"
backups_folder_path = os.path.join(backup_disk_path, "Backups/")
deleted = True
while psutil.disk_usage(backup_disk_path).percent > int({{size_percent_maximum_backup}}) and deleted:
deleted = False
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)
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]
print("Deleting %s..." % (delete_diff))
shutil.rmtree(delete_diff)
deleted = True
if not deleted:
print("All versions had been deleted!")
print("Cleaning up finished: %d %% of disk %s are used." % (psutil.disk_usage(backup_disk_path).percent,backup_disk_path))

View File

@@ -4,4 +4,4 @@ OnFailure=systemd-email@%n.service
[Service]
Type=oneshot
ExecStart=/usr/bin/python {{docker_backups_cleanup}}/backups-cleanup.py
ExecStart=/usr/bin/python {{docker_backups_cleanup}}/backups-cleanup.py --backups-folder-path {{backups_folder_path}} --maximum-backup-size-percent {{size_percent_maximum_backup}}