Refactor systemctl services and categories due to alarm bugs

This commit restructures systemctl service definitions and category mappings.

Motivation: Alarm-related bugs revealed inconsistencies in service and role handling.

Preparation step: lays the groundwork for fixing the alarm issues by aligning categories, roles, and service templates.
This commit is contained in:
2025-08-18 13:35:43 +02:00
parent 29f50da226
commit 3a839cfe37
289 changed files with 975 additions and 948 deletions

View File

@@ -0,0 +1,27 @@
# Cleanup Backups Service
## Description
This role automates the cleanup of old backups by executing a Python script that deletes outdated backup versions based on disk usage thresholds. It ensures that backup storage does not exceed a defined usage percentage.
## Overview
Optimized for effective disk space management, this role:
- Installs required packages (e.g. [lsof](https://en.wikipedia.org/wiki/Lsof) and [psutil](https://pypi.org/project/psutil/)) using pacman.
- Creates a directory for storing cleanup scripts.
- Deploys a Python script that deletes old backup directories when disk usage is too high.
- Configures a systemd service to run the cleanup script, with notifications via [sys-ctl-alm-compose](../sys-ctl-alm-compose/README.md).
## Purpose
The primary purpose of this role is to maintain optimal backup storage by automatically removing outdated backup versions when disk usage exceeds a specified threshold.
## Features
- **Automated Cleanup:** Executes a Python script to delete old backups.
- **Threshold-Based Deletion:** Removes backups based on disk usage percentage.
- **Systemd Integration:** Configures a systemd service to run cleanup tasks.
- **Dependency Integration:** Works in conjunction with related roles for comprehensive backup management.
## Other Resources
- https://stackoverflow.com/questions/48929553/get-hard-disk-size-in-python

View File

@@ -0,0 +1,153 @@
import psutil
import shutil
import os
import argparse
import subprocess
import time
# Validating arguments
parser = argparse.ArgumentParser()
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()
def print_used_disc_space(backups_folder_path):
print("%d %% of disk %s are used" % (psutil.disk_usage(backups_folder_path).percent,backups_folder_path))
def is_directory_used_by_another_process(directory_path):
command= "lsof " + directory_path
process = subprocess.Popen([command], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output, error = process.communicate()
# @See https://stackoverflow.com/questions/29841984/non-zero-exit-code-for-lsof
if process.wait() > bool(0):
return False
return True
def isSmallerThenMaximumBackupSize(maximum_backup_size_percent,backups_folder_path):
current_disc_usage_percent=psutil.disk_usage(backups_folder_path).percent
return current_disc_usage_percent > maximum_backup_size_percent
def isDirectoryDeletable(version, versions, version_path):
print("Checking directory %s ..." % (version_path))
if version == versions[-1]:
print("Directory %s contains the last version of the backup. Skipped." % (version_path))
return False
if is_directory_used_by_another_process(version_path):
print("Directory %s is used by another process. Skipped." % (version_path))
return False
print(f"Directory {version_path} can be deleted.")
return True
def deleteVersion(version_path, backups_folder_path):
print("Deleting %s to free space." % (version_path))
current_disc_usage_percent=psutil.disk_usage(backups_folder_path).percent
shutil.rmtree(version_path)
new_disc_usage_percent=psutil.disk_usage(backups_folder_path).percent
difference_percent=current_disc_usage_percent-new_disc_usage_percent
print("{:6.2f} %% of drive freed".format(difference_percent))
def count_total_application_directories(backups_folder_path):
total_app_directories = 0
for host_backup_directory_name in os.listdir(backups_folder_path):
host_backup_directory_path = os.path.join(backups_folder_path, host_backup_directory_name)
total_app_directories += sum(os.path.isdir(os.path.join(host_backup_directory_path, d)) for d in os.listdir(host_backup_directory_path))
return total_app_directories
def count_total_version_folders(backups_folder_path):
total_version_folders = 0
for host_backup_directory_name in os.listdir(backups_folder_path):
host_backup_directory_path = os.path.join(backups_folder_path, host_backup_directory_name)
for application_directory in os.listdir(host_backup_directory_path):
versions_directory = os.path.join(host_backup_directory_path, application_directory)
total_version_folders += sum(os.path.isdir(os.path.join(versions_directory, d)) for d in os.listdir(versions_directory))
return total_version_folders
def average_version_directories_per_application(backups_folder_path):
total_app_directories = count_total_application_directories(backups_folder_path)
total_version_folders = count_total_version_folders(backups_folder_path)
if total_app_directories == 0:
return 0
average = total_version_folders / total_app_directories
return int(average)
def getAmountOfIteration(versions,average_version_directories_per_application):
version_amount=len(versions)
amount_of_iterations=(len(versions) +1) - average_version_directories_per_application
print(f"Number of existing versions: {version_amount}")
print(f"Number of average version directories per application: {average_version_directories_per_application}")
print(f"Amount of iterations: {amount_of_iterations}")
return amount_of_iterations
def deleteIteration(backups_folder_path,average_version_directories_per_application):
for host_backup_directory_name in os.listdir(backups_folder_path):
print(f"Iterating over host: {host_backup_directory_name}")
host_backup_directory_path = os.path.join(backups_folder_path, host_backup_directory_name)
for application_directory in os.listdir(host_backup_directory_path):
print(f"Iterating over backup application: {application_directory}")
# The directory which contains all backup versions of the application
versions_directory = os.path.join(host_backup_directory_path, application_directory) + "/"
versions = os.listdir(versions_directory)
versions.sort(reverse=False)
version_iteration=0
while version_iteration < getAmountOfIteration(versions,average_version_directories_per_application):
print_used_disc_space(backups_folder_path)
version = versions[version_iteration]
version_path=os.path.join(versions_directory, version)
if isDirectoryDeletable(version, versions, version_path):
deleteVersion(version_path, backups_folder_path)
version_iteration += 1
def check_time_left(start_time, time_limit):
"""
Checks if there is time left within the given time limit.
Prints the start time, the current time, and the remaining time.
:param start_time: The start time of the process.
:param time_limit: The total time limit for the process.
:return: True if there is time left, False otherwise.
"""
current_time = time.time()
elapsed_time = current_time - start_time
remaining_time = time_limit - elapsed_time
# Convert times to readable format
start_time_str = time.strftime("%H:%M:%S", time.localtime(start_time))
current_time_str = time.strftime("%H:%M:%S", time.localtime(current_time))
remaining_time_str = time.strftime("%H:%M:%S", time.gmtime(remaining_time))
is_time_left = remaining_time > 0
print(f"Start time: {start_time_str}")
print(f"Current time: {current_time_str}")
if is_time_left:
print(f"Remaining time: {remaining_time_str}")
return remaining_time > 0
class TimeLimitExceededException(Exception):
"""Exception raised when the time limit for the process is exceeded."""
def __init__(self, message="Time limit exceeded, terminating the process."):
self.message = message
super().__init__(self.message)
backups_folder_path=args.backups_folder_path
maximum_backup_size_percent=args.maximum_backup_size_percent
start_time = time.time()
time_limit = 3600
itteration_counter = 1
while isSmallerThenMaximumBackupSize(maximum_backup_size_percent, backups_folder_path):
print(f"Delete Iteration: {itteration_counter}")
if not check_time_left(start_time, time_limit):
raise TimeLimitExceededException()
average_version_directories = average_version_directories_per_application(backups_folder_path)
print(f"Average version directories per application directory: {average_version_directories}")
deleteIteration(backups_folder_path, average_version_directories)
itteration_counter += 1
print_used_disc_space(backups_folder_path)
print("Cleaning up finished.")

View File

@@ -0,0 +1,5 @@
- name: "reload sys-ctl-cln-backups service"
systemd:
name: sys-ctl-cln-backups{{ SYS_SERVICE_SUFFIX }}
enabled: yes
daemon_reload: yes

View File

@@ -0,0 +1,22 @@
galaxy_info:
author: "Kevin Veen-Birkenbach"
description: "Automates the cleanup of old backups by executing a Python script that deletes outdated backup versions when disk usage exceeds a specified threshold."
license: "Infinito.Nexus NonCommercial License"
license_url: "https://s.infinito.nexus/license"
company: |
Kevin Veen-Birkenbach
Consulting & Coaching Solutions
https://www.veen.world
min_ansible_version: "2.9"
platforms:
- name: Linux
versions:
- all
galaxy_tags:
- backup
- cleanup
- disk
- automation
repository: "https://s.infinito.nexus/code"
issue_tracker_url: "https://s.infinito.nexus/issues"
documentation: "https://docs.infinito.nexus"

View File

@@ -0,0 +1,32 @@
- name: Include dependencies
include_role:
name: '{{ item }}'
loop:
- dev-python-pip
- sys-ctl-alm-compose
- sys-lock
- sys-rst-daemon
- name: install lsof and python-psutil
community.general.pacman:
name:
- lsof
- python-psutil
state: present
- name: "create {{cleanup_backups_directory}}"
file:
path: "{{cleanup_backups_directory}}"
state: directory
mode: "0755"
- name: create sys-ctl-cln-backups.py
copy:
src: "sys-ctl-cln-backups.py"
dest: "{{cleanup_backups_directory}}sys-ctl-cln-backups.py"
- name: create sys-ctl-cln-backups{{ SYS_SERVICE_SUFFIX }}
template:
src: "sys-ctl-cln-backups.service.j2"
dest: "/etc/systemd/system/sys-ctl-cln-backups{{ SYS_SERVICE_SUFFIX }}"
notify: reload sys-ctl-cln-backups service

View File

@@ -0,0 +1,5 @@
- block:
- include_tasks: 01_core.yml
- include_tasks: utils/run_once.yml
when: run_once_sys_ctl_cln_bkps is not defined

View File

@@ -0,0 +1,8 @@
[Unit]
Description=delete old backups
OnFailure=sys-ctl-alm-compose.{{ SOFTWARE_NAME }}@%n.service
[Service]
Type=oneshot
ExecStartPre=/bin/sh -c '/usr/bin/python {{ PATH_SYSTEM_LOCK_SCRIPT }} {{ SYS_SERVICE_GROUP_MANIPULATION | join(' ') }} --ignore {{SYS_SERVICE_GROUP_CLEANUP| join(' ') }} --timeout "{{ SYS_TIMEOUT_BACKUP_SERVICES }}"'
ExecStart=/bin/sh -c '/usr/bin/python {{cleanup_backups_directory}}sys-ctl-cln-backups.py --backups-folder-path {{backups_folder_path}} --maximum-backup-size-percent {{size_percent_maximum_backup}}'

View File

@@ -0,0 +1,2 @@
cleanup_backups_directory: '{{ PATH_ADMINISTRATOR_SCRIPTS }}sys-ctl-cln-backups/'