mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-29 15:06:26 +02:00
Renamed to backup and cleanup roles
This commit is contained in:
14
roles/cleanup-backups-service/README.md
Normal file
14
roles/cleanup-backups-service/README.md
Normal file
@@ -0,0 +1,14 @@
|
||||
# role cleanup-backups-timer
|
||||
|
||||
Cleans up old backups
|
||||
|
||||
## Additional software
|
||||
|
||||
It may be neccessary to install gcc seperat to use psutil
|
||||
|
||||
```bash
|
||||
sudo pacman -S gcc
|
||||
```
|
||||
|
||||
## further information
|
||||
- https://stackoverflow.com/questions/48929553/get-hard-disk-size-in-python
|
57
roles/cleanup-backups-service/files/backups-cleanup.py
Normal file
57
roles/cleanup-backups-service/files/backups-cleanup.py
Normal file
@@ -0,0 +1,57 @@
|
||||
import psutil
|
||||
import shutil
|
||||
import os
|
||||
import argparse
|
||||
import subprocess
|
||||
|
||||
# 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():
|
||||
print("%d %% of disk %s are used" % (psutil.disk_usage(args.backups_folder_path).percent,args.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
|
||||
|
||||
for host_backup_directory_name in os.listdir(args.backups_folder_path):
|
||||
host_backup_directory_path = os.path.join(args.backups_folder_path, host_backup_directory_name)
|
||||
for application_directory in os.listdir(host_backup_directory_path):
|
||||
|
||||
# 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)
|
||||
|
||||
print_used_disc_space()
|
||||
for version in versions:
|
||||
version_path=os.path.join(versions_directory, version)
|
||||
print("Checking directory %s ..." % (version_path))
|
||||
if version == versions[-1]:
|
||||
print("Directory %s contains the last version of the backup. Skipped." % (version_path))
|
||||
continue
|
||||
|
||||
if is_directory_used_by_another_process(version_path):
|
||||
print("Directory %s is used by another process. Skipped." % (version_path))
|
||||
continue
|
||||
|
||||
old_disc_usage_percent=psutil.disk_usage(args.backups_folder_path).percent
|
||||
if old_disc_usage_percent > args.maximum_backup_size_percent:
|
||||
print("Deleting %s to free space." % (version_path))
|
||||
shutil.rmtree(version_path)
|
||||
new_disc_usage_percent=psutil.disk_usage(args.backups_folder_path).percent
|
||||
difference_percent=old_disc_usage_percent-new_disc_usage_percent
|
||||
print("{:6.2f} %% of drive freed".format(difference_percent))
|
||||
continue
|
||||
|
||||
print_used_disc_space()
|
||||
print("Cleaning up finished.")
|
6
roles/cleanup-backups-service/handlers/main.yml
Normal file
6
roles/cleanup-backups-service/handlers/main.yml
Normal file
@@ -0,0 +1,6 @@
|
||||
- name: "reload backups-cleanup.service"
|
||||
systemd:
|
||||
name: backups-cleanup.service
|
||||
state: reloaded
|
||||
enabled: yes
|
||||
daemon_reload: yes
|
3
roles/cleanup-backups-service/meta/main.yml
Normal file
3
roles/cleanup-backups-service/meta/main.yml
Normal file
@@ -0,0 +1,3 @@
|
||||
dependencies:
|
||||
- python-pip
|
||||
- systemd_notifier
|
23
roles/cleanup-backups-service/tasks/main.yml
Normal file
23
roles/cleanup-backups-service/tasks/main.yml
Normal file
@@ -0,0 +1,23 @@
|
||||
- name: install lsof and python-psutil
|
||||
community.general.pacman:
|
||||
name:
|
||||
- lsof
|
||||
- python-psutil
|
||||
state: present
|
||||
|
||||
- name: "create {{docker_backups_cleanup}}"
|
||||
file:
|
||||
path: "{{docker_backups_cleanup}}"
|
||||
state: directory
|
||||
mode: 0755
|
||||
|
||||
- name: create backups-cleanup.py
|
||||
copy:
|
||||
src: "backups-cleanup.py"
|
||||
dest: "{{docker_backups_cleanup}}backups-cleanup.py"
|
||||
|
||||
- name: create backups-cleanup.service
|
||||
template:
|
||||
src: "backups-cleanup.service.j2"
|
||||
dest: "/etc/systemd/system/backups-cleanup.service"
|
||||
notify: reload backups-cleanup.service
|
@@ -0,0 +1,7 @@
|
||||
[Unit]
|
||||
Description=delete old backups
|
||||
OnFailure=systemd-notifier@%n.service
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=/usr/bin/python {{docker_backups_cleanup}}backups-cleanup.py --backups-folder-path {{backups_folder_path}} --maximum-backup-size-percent {{size_percent_maximum_backup}}
|
1
roles/cleanup-backups-service/vars/main.yml
Normal file
1
roles/cleanup-backups-service/vars/main.yml
Normal file
@@ -0,0 +1 @@
|
||||
docker_backups_cleanup: "{{path_administrator_scripts}}backups-cleanup/"
|
Reference in New Issue
Block a user