From 85ffc968dfbe3d229d430eddecd614172ded3889 Mon Sep 17 00:00:00 2001 From: "Kevin Veen-Birkenbach [aka. Frantz]" Date: Sat, 19 Jun 2021 12:38:05 +0200 Subject: [PATCH] Updated backup script --- .../files/backups-cleanup.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/roles/native-backups-cleanup/files/backups-cleanup.py b/roles/native-backups-cleanup/files/backups-cleanup.py index 649c9e8a..973574d2 100644 --- a/roles/native-backups-cleanup/files/backups-cleanup.py +++ b/roles/native-backups-cleanup/files/backups-cleanup.py @@ -4,8 +4,9 @@ import shutil import os backup_disk_path = "/media/encrypteddrive-sda/" backups_folder_path = os.path.join(backup_disk_path, "Backups/") - -while psutil.disk_usage(backup_disk_path).percent > 50: +deleted = True +while psutil.disk_usage(backup_disk_path).percent > 50 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) @@ -14,7 +15,11 @@ while psutil.disk_usage(backup_disk_path).percent > 50: diffs_directory = os.path.join(application_directory, "diffs/") diffs = os.listdir(diffs_directory) diffs.sort(reverse=False) - delete_diff = diffs_directory + diffs[0] - print("Deleting %s..." % (delete_diff)) - shutil.rmtree(delete_diff) + if len(diffs) >= 1: + delete_diff = diffs_directory + diffs[0] + print("Deleting %s..." % (delete_diff)) + shutil.rmtree(delete_diff) + deleted = True +if not deleted: + print("All diffs had been deleted!") print("Cleaning up finished: %d %% of disk %s are used." % (psutil.disk_usage(backup_disk_path).percent,backup_disk_path))