2021-06-19 11:27:06 +02:00
import psutil
2021-06-19 11:45:12 +02:00
import shutil
2021-06-19 11:27:06 +02:00
import os
2023-04-16 12:37:31 +02:00
import argparse
# Validating arguments
parser . add_argument ( ' --maximum-backup-size-percent ' , type = int , dest = ' maximum_backup_size_percent ' , required = True , choices = range ( 0 , 100 ) , help = " The directory from which the data should be encrypted. " )
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 ( )
2021-06-19 12:38:05 +02:00
deleted = True
2023-04-16 12:37:31 +02:00
while psutil . disk_usage ( args . backups_folder_path ) . percent > args . maximum_backup_size_percent and deleted :
2021-06-19 12:38:05 +02:00
deleted = False
2023-04-16 12:37:31 +02:00
print ( " %d %% of disk %s are used. Freeing space... " % ( psutil . disk_usage ( args . backups_folder_path ) . percent , args . backups_folder_path ) )
for primary_directory in os . listdir ( args . backups_folder_path ) :
primary_directory = os . path . join ( args . backups_folder_path , primary_directory )
2021-06-19 11:27:06 +02:00
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! " )
2023-04-16 12:37:31 +02:00
print ( " Cleaning up finished: %d %% of disk %s are used. " % ( psutil . disk_usage ( args . backups_folder_path ) . percent , args . backups_folder_path ) )