optimized backups-cleanup

This commit is contained in:
Kevin Veen-Birkenbach 2021-06-19 13:10:59 +02:00
parent 85ffc968df
commit 6a5db94ade
7 changed files with 52 additions and 3 deletions

View File

@ -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

View File

@ -0,0 +1,9 @@
[Unit]
Description=starts backups-cleanup.service
[Timer]
OnCalendar=19:00
RandomizedDelaySec=1h
[Install]
WantedBy=timers.target

View File

@ -0,0 +1,12 @@
- name: "restart backups-cleanup.service"
systemd:
name: backups-cleanup.service
state: restarted
enabled: yes
daemon_reload: yes
- name: "restart backups-cleanup.timer"
systemd:
name: backups-cleanup.timer
state: restarted
enabled: yes
daemon_reload: yes

View File

@ -1,2 +1,3 @@
dependencies:
- native-python-pip
- native-systemd-email

View File

@ -1,3 +1,18 @@
- name: install psutil
pip:
name: psutil
- name: create backups-cleanup.py
template: src=backups-cleanup.py.j2 dest=/usr/local/bin/backups-cleanup.py
- name: create backups-cleanup.service
copy:
src: backups-cleanup.service
dest: "/etc/systemd/system/backups-cleanup.service"
notify: restart backups-cleanup.service
- name: create backups-cleanup.timer
copy:
src: backups-cleanup.timer
dest: "/etc/systemd/system/backups-cleanup.timer"
notify: restart backups-cleanup.timer

View File

@ -2,10 +2,10 @@
import psutil
import shutil
import os
backup_disk_path = "/media/encrypteddrive-sda/"
backup_disk_path = "{{backup_disk_path}}"
backups_folder_path = os.path.join(backup_disk_path, "Backups/")
deleted = True
while psutil.disk_usage(backup_disk_path).percent > 50 and deleted:
while psutil.disk_usage(backup_disk_path).percent > int({{backup_space_percent}}) 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):

View File

@ -39,6 +39,11 @@
become: true
roles:
- role: native-pull-primary-backups
- name: setup backups cleanup
hosts: backups_cleanup
become: true
roles:
- role: native-backups-cleanup
- name: setup homepages
hosts: homepage_hosts
become: true