Solved bugs in backup procedures

This commit is contained in:
Kevin Veen-Birkenbach 2024-01-14 12:19:00 +01:00
parent 30e3ac0290
commit e9e94ba3ed
3 changed files with 57 additions and 12 deletions

View File

@ -3,6 +3,7 @@ import shutil
import os
import argparse
import subprocess
import time
# Validating arguments
parser = argparse.ArgumentParser()
@ -36,6 +37,9 @@ def isDirectoryDeletable(version, versions, 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
@ -60,7 +64,7 @@ def count_total_version_folders(backups_folder_path):
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,blur=-1):
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)
@ -68,16 +72,22 @@ def average_version_directories_per_application(backups_folder_path,blur=-1):
return 0
average = total_version_folders / total_app_directories
return int(average) - blur
return int(average)
def getAmountOfIteration(versions,average_version_directories_per_application):
return len(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) + "/"
@ -92,15 +102,48 @@ def deleteIteration(backups_folder_path,average_version_directories_per_applicat
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):
if itteration_counter > 200:
raise Exception("Iteration limit exceeded")
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)

View File

@ -2,7 +2,6 @@
# @param $1 mimimum free disc space
# @param $2 --force to for execution indepentend on how much disc space is free
execute_cleanup_disc_space=0
minimum_percent_cleanup_disc_space="$1"
force_freeing=false
echo "Checking free disc space..."
@ -16,10 +15,10 @@ do
disc_use_percent_number=$(echo "$disc_use_percent" | sed "s/%//")
if [ "$disc_use_percent_number" -gt "$minimum_percent_cleanup_disc_space" ]; then
echo "WARNING: $disc_use_percent_number exceeds the limit of {{size_percent_disc_space_warning}}%."
execute_cleanup_disc_space+=1;
force_freeing=true
fi
done
if [ "$disc_use_percent_number" -gt "$minimum_percent_cleanup_disc_space" ] || [ "$force_freeing" = true ]; then
if [ "$force_freeing" = true ]; then
echo "cleaning up /tmp" &&
find /tmp -type f -atime +10 -delete || exit 1
@ -32,7 +31,7 @@ if [ "$disc_use_percent_number" -gt "$minimum_percent_cleanup_disc_space" ] || [
echo "cleaning up docker" &&
docker system prune -f || exit 3
nextcloud_application_container="nextcloud-application-1"
nextcloud_application_container="nextcloud-application"
if [ "$(docker ps -a -q -f name=$nextcloud_application_container)" ] ; then
echo "cleaning up docker nextcloud" &&
docker exec -it -u www-data $nextcloud_application_container /var/www/html/occ files:cleanup || exit 4

View File

@ -8,6 +8,7 @@ services:
application:
image: "nextcloud:{{version_nextcloud}}-fpm-alpine"
container_name: nextcloud-application
restart: {{docker_restart_policy}}
logging:
driver: journald
@ -23,6 +24,7 @@ services:
web:
image: nginx:alpine
container_name: nextcloud-web
logging:
driver: journald
restart: {{docker_restart_policy}}
@ -40,6 +42,7 @@ services:
{% include 'templates/docker-container-networks.yml.j2' %}
cron:
container_name: nextcloud-cron
image: "nextcloud:{{version_nextcloud}}-fpm-alpine"
restart: {{docker_restart_policy}}
logging: