From 6a5db94ade8871f403e5b493273f51c98b3aaae7 Mon Sep 17 00:00:00 2001 From: "Kevin Veen-Birkenbach [aka. Frantz]" Date: Sat, 19 Jun 2021 13:10:59 +0200 Subject: [PATCH] optimized backups-cleanup --- .../files/backups-cleanup.service | 7 +++++++ .../files/backups-cleanup.timer | 9 +++++++++ roles/native-backups-cleanup/handlers/main.yml | 12 ++++++++++++ roles/native-backups-cleanup/meta/main.yml | 3 ++- roles/native-backups-cleanup/tasks/main.yml | 15 +++++++++++++++ .../backups-cleanup.py.j2} | 4 ++-- site.yml | 5 +++++ 7 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 roles/native-backups-cleanup/files/backups-cleanup.service create mode 100644 roles/native-backups-cleanup/files/backups-cleanup.timer create mode 100644 roles/native-backups-cleanup/handlers/main.yml rename roles/native-backups-cleanup/{files/backups-cleanup.py => templates/backups-cleanup.py.j2} (89%) diff --git a/roles/native-backups-cleanup/files/backups-cleanup.service b/roles/native-backups-cleanup/files/backups-cleanup.service new file mode 100644 index 00000000..286a8fee --- /dev/null +++ b/roles/native-backups-cleanup/files/backups-cleanup.service @@ -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 diff --git a/roles/native-backups-cleanup/files/backups-cleanup.timer b/roles/native-backups-cleanup/files/backups-cleanup.timer new file mode 100644 index 00000000..5265a849 --- /dev/null +++ b/roles/native-backups-cleanup/files/backups-cleanup.timer @@ -0,0 +1,9 @@ +[Unit] +Description=starts backups-cleanup.service + +[Timer] +OnCalendar=19:00 +RandomizedDelaySec=1h + +[Install] +WantedBy=timers.target diff --git a/roles/native-backups-cleanup/handlers/main.yml b/roles/native-backups-cleanup/handlers/main.yml new file mode 100644 index 00000000..aa7db473 --- /dev/null +++ b/roles/native-backups-cleanup/handlers/main.yml @@ -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 diff --git a/roles/native-backups-cleanup/meta/main.yml b/roles/native-backups-cleanup/meta/main.yml index f3b59641..651a13b8 100644 --- a/roles/native-backups-cleanup/meta/main.yml +++ b/roles/native-backups-cleanup/meta/main.yml @@ -1,2 +1,3 @@ dependencies: -- native-python-pip + - native-python-pip + - native-systemd-email diff --git a/roles/native-backups-cleanup/tasks/main.yml b/roles/native-backups-cleanup/tasks/main.yml index 5849e2a6..e135a563 100644 --- a/roles/native-backups-cleanup/tasks/main.yml +++ b/roles/native-backups-cleanup/tasks/main.yml @@ -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 diff --git a/roles/native-backups-cleanup/files/backups-cleanup.py b/roles/native-backups-cleanup/templates/backups-cleanup.py.j2 similarity index 89% rename from roles/native-backups-cleanup/files/backups-cleanup.py rename to roles/native-backups-cleanup/templates/backups-cleanup.py.j2 index 973574d2..b3a80c8c 100644 --- a/roles/native-backups-cleanup/files/backups-cleanup.py +++ b/roles/native-backups-cleanup/templates/backups-cleanup.py.j2 @@ -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): diff --git a/site.yml b/site.yml index 1ef82ce3..6c29cb9d 100644 --- a/site.yml +++ b/site.yml @@ -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