Implemented free disc space command

This commit is contained in:
Kevin Veen-Birkenbach 2023-04-16 13:24:41 +02:00
parent 3b980dba66
commit 4f0bb25bc9
9 changed files with 112 additions and 1 deletions

View File

@ -19,3 +19,4 @@ on_calendar_deploy_mailu_certificates: "*-*-* 12,00:50:00"
# Space Variables # Space Variables
size_percent_maximum_backup: 75 size_percent_maximum_backup: 75
size_percent_disc_space_warning: 85 size_percent_disc_space_warning: 85
size_percent_free_disc_space: 90

View File

@ -0,0 +1,4 @@
# free-disc-space
Frees disc space
## More information
- https://askubuntu.com/questions/380238/how-to-clean-tmp

View File

@ -0,0 +1,12 @@
- name: "reload free-disc-space.service"
systemd:
name: free-disc-space.service
state: reloaded
enabled: yes
daemon_reload: yes
- name: "restart free-disc-space.timer"
systemd:
name: free-disc-space.timer
state: restarted
enabled: yes
daemon_reload: yes

View File

@ -0,0 +1,2 @@
dependencies:
- native-systemd-email

View File

@ -0,0 +1,22 @@
- name: "create {{free_disc_space_folder}"
file:
path: "{{free_disc_space_folder}"
state: directory
mode: 0755
- name: create free-disc-space.sh
template:
src: free-disc-space.sh.j2
dest: "{{free_disc_space_folder}free-disc-space.sh"
- name: create free-disc-space.service
template:
src: free-disc-space.service.j2
dest: /etc/systemd/system/free-disc-space.service
notify: reload free-disc-space.service
- name: create free-disc-space.timer
template:
src: free-disc-space.timer.j2
dest: /etc/systemd/system/free-disc-space.timer
notify: restart free-disc-space.timer

View File

@ -0,0 +1,7 @@
[Unit]
Description=freeing disc space
OnFailure=systemd-email@%n.service
[Service]
Type=oneshot
ExecStart=/bin/bash {{free_disc_space_folder}}free-disc-space.sh {{size_percent_free_disc_space}}

View File

@ -0,0 +1,52 @@
#!/bin/sh
# @param $1 mimimum free disc space
# @param $2 --force to for execution indepentend on how much disc space is free
execute_free_disc_space=0
minimum_percent_free_disc_space="$1"
force_freeing=false
echo "Checking free disc space..."
df
if [ $# -gt 0 ] && [ "$2" == "--force" ]; then
echo "Forcing disc space freeing."
force_freeing=true
fi
for disc_use_percent in $(df --output=pcent | sed 1d)
do
disc_use_percent_number=$(echo "$disc_use_percent" | sed "s/%//")
if [ "$disc_use_percent_number" -gt "$minimum_percent_free_disc_space" ]; then
echo "WARNING: $disc_use_percent_number exceeds the limit of {{size_percent_disc_space_warning}}%."
execute_free_disc_space+=1;
fi
done
if [ "$disc_use_percent_number" -gt "$minimum_percent_free_disc_space" ] || [ "$force_freeing" = true ]; then
echo "cleaning up /tmp" &&
find /tmp -type f -atime +10 -delete || exit 1
{% if backups_folder_path is defined and size_percent_maximum_backup is defined %}
echo "cleaning up backups" &&
python /home/administrator/scripts/backups-cleanup/backups-cleanup.py --backups-folder-path {{backups_folder_path}} --maximum-backup-size-percent {{size_percent_maximum_backup}} || exit 2
{% endif %}
if pacman -Qs $package > /dev/null ; then
echo "cleaning up docker" &&
docker system prune -f || exit 3
nextcloud_application_container="nextcloud-application-1"
if [ "$(docker ps -a -q -f name=$nextcloud_application_container)" ] ; then
docker exec -it -u www-data $nextcloud_application_container /var/www/html/occ files:cleanup || exit 4
docker exec -it -u www-data $nextcloud_application_container /var/www/html/occ trashbin:cleanup --all-users || exit 5
docker exec -it -u www-data $nextcloud_application_container /var/www/html/occ versions:cleanup || exit 6
fi
fi
echo "cleaning pacman cache" &&
yes | pacman -Sc || exit 7
echo "cleanup finished."
elif
echo "Sufficiend disc space available."
echo "To force the freeing of disc space pass the parameter --force."
fi
exit 0

View File

@ -0,0 +1,10 @@
[Unit]
Description=starts free-disc-space.service
[Timer]
OnCalendar={{on_calendar_disc_space_check}}
RandomizedDelaySec={{randomized_delay_sec}}
Persistent=true
[Install]
WantedBy=timers.target

View File

@ -0,0 +1 @@
free_disc_space_folder: "/home/administrator/scripts/free-disc-space/"