Updated backup script

This commit is contained in:
Kevin Veen-Birkenbach 2021-06-19 12:38:05 +02:00
parent 2691d5d3fc
commit 85ffc968df

View File

@ -4,8 +4,9 @@ import shutil
import os import os
backup_disk_path = "/media/encrypteddrive-sda/" backup_disk_path = "/media/encrypteddrive-sda/"
backups_folder_path = os.path.join(backup_disk_path, "Backups/") backups_folder_path = os.path.join(backup_disk_path, "Backups/")
deleted = True
while psutil.disk_usage(backup_disk_path).percent > 50: 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)) 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): for primary_directory in os.listdir(backups_folder_path):
primary_directory = os.path.join(backups_folder_path, primary_directory) 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_directory = os.path.join(application_directory, "diffs/")
diffs = os.listdir(diffs_directory) diffs = os.listdir(diffs_directory)
diffs.sort(reverse=False) diffs.sort(reverse=False)
delete_diff = diffs_directory + diffs[0] if len(diffs) >= 1:
print("Deleting %s..." % (delete_diff)) delete_diff = diffs_directory + diffs[0]
shutil.rmtree(delete_diff) 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)) print("Cleaning up finished: %d %% of disk %s are used." % (psutil.disk_usage(backup_disk_path).percent,backup_disk_path))