mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-29 15:06:26 +02:00
optimized backups-cleanup
This commit is contained in:
@@ -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 = "/media/encrypteddrive-sda/"
|
||||
backups_folder_path = os.path.join(backup_disk_path, "Backups/")
|
||||
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)
|
||||
for application_directory in os.listdir(primary_directory):
|
||||
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)
|
||||
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))
|
@@ -0,0 +1,7 @@
|
||||
[Unit]
|
||||
Description=delete old backups
|
||||
OnFailure=systemd-email@%n.service
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=/usr/bin/python /usr/local/bin/backups-cleanup.py
|
9
roles/native-backups-cleanup/files/backups-cleanup.timer
Normal file
9
roles/native-backups-cleanup/files/backups-cleanup.timer
Normal file
@@ -0,0 +1,9 @@
|
||||
[Unit]
|
||||
Description=starts backups-cleanup.service
|
||||
|
||||
[Timer]
|
||||
OnCalendar=19:00
|
||||
RandomizedDelaySec=1h
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
Reference in New Issue
Block a user