diff --git a/roles/native-backups-cleanup/files/backups-cleanup.py b/roles/native-backups-cleanup/files/backups-cleanup.py new file mode 100644 index 00000000..b04e21a6 --- /dev/null +++ b/roles/native-backups-cleanup/files/backups-cleanup.py @@ -0,0 +1,44 @@ +# @see https://stackoverflow.com/questions/48929553/get-hard-disk-size-in-python +# import shutil +import psutil +import os +import time +backup_disk_path = "/media/encrypteddrive-sda/" +backups_folder_path = os.path.join(backup_disk_path, "Backups/") + + +# Helper class for statistics +# class Disk_Statistics(object): +# def __init__(self): +# self.total = 0 +# self.used = 0 +# self.free = 0 +# self.update_disc_usage() +# +# def update_disc_usage(self): +# old_used = self.used +# old_free = self.free +# self.total, self.used, self.free = shutil.disk_usage(backup_disk_path) +# if [(old_used == self.used) or (old_free == self.free)]: +# raise Exception("The values didn't change. \n Used: %d/%d \n Free: %d/%d" % (old_used,self.used, old_free,self.free)) +# +# def get_used_space_percent(self): +# return (self.used/self.total)*100 +# +# +# disk_statistics = Disk_Statistics() + +while psutil.disk_usage(backup_disk_path).percent > 52: + 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): + time.sleep(5) + application_directory = os.path.join(primary_directory, application_directory) + 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 and free %s space..." % (delete_diff)) + #shutil.rmtree(delete_diff) + #disk_statistics.update_disc_usage() diff --git a/roles/native-backups-cleanup/meta/main.yml b/roles/native-backups-cleanup/meta/main.yml new file mode 100644 index 00000000..f3b59641 --- /dev/null +++ b/roles/native-backups-cleanup/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: +- native-python-pip diff --git a/roles/native-backups-cleanup/readme.md b/roles/native-backups-cleanup/readme.md new file mode 100644 index 00000000..4ce6bd03 --- /dev/null +++ b/roles/native-backups-cleanup/readme.md @@ -0,0 +1,3 @@ +# role native-backups-cleanup + +Cleans up old backups diff --git a/roles/native-backups-cleanup/tasks/main.yml b/roles/native-backups-cleanup/tasks/main.yml new file mode 100644 index 00000000..5849e2a6 --- /dev/null +++ b/roles/native-backups-cleanup/tasks/main.yml @@ -0,0 +1,3 @@ +- name: install psutil + pip: + name: psutil diff --git a/roles/native-python-pip/tasks/main.yml b/roles/native-python-pip/tasks/main.yml new file mode 100644 index 00000000..015772c1 --- /dev/null +++ b/roles/native-python-pip/tasks/main.yml @@ -0,0 +1,5 @@ +--- +- name: python pip install + pacman: + name: python-pip + state: present