From a597c5a47401580c56e9e231466feeada0a69c58 Mon Sep 17 00:00:00 2001 From: Jean28518 Date: Sat, 27 Apr 2024 10:24:36 +0200 Subject: [PATCH] Change backup running evaluation #85 --- src/lac/unix/unix_scripts/maintenance/do_backup.sh | 6 +----- src/lac/unix/unix_scripts/service.py | 3 ++- src/lac/unix/unix_scripts/unix.py | 6 +++--- src/lac/unix/unix_scripts/utils.py | 7 +++++++ 4 files changed, 13 insertions(+), 9 deletions(-) create mode 100644 src/lac/unix/unix_scripts/utils.py diff --git a/src/lac/unix/unix_scripts/maintenance/do_backup.sh b/src/lac/unix/unix_scripts/maintenance/do_backup.sh index f9cf6de..9a12902 100644 --- a/src/lac/unix/unix_scripts/maintenance/do_backup.sh +++ b/src/lac/unix/unix_scripts/maintenance/do_backup.sh @@ -1,8 +1,6 @@ #!/bin/bash source ../unix.conf -touch backup_running - # Dump all databases # --default-character-set=utf8mb4: For emojis and similar, otherwise its broken mysqldump -u root --all-databases --default-character-set=utf8mb4 > /mysql_all_databases.sql @@ -69,6 +67,4 @@ borg prune -v --keep-within=$MAXIMUM_AGE_IN_DAYS $BORG_REPOSITORY borg list --short $BORG_REPOSITORY > ../history/borg_list -borg info $BORG_REPOSITORY > ../history/borg_info - -rm backup_running \ No newline at end of file +borg info $BORG_REPOSITORY > ../history/borg_info \ No newline at end of file diff --git a/src/lac/unix/unix_scripts/service.py b/src/lac/unix/unix_scripts/service.py index 1c68116..68d638a 100644 --- a/src/lac/unix/unix_scripts/service.py +++ b/src/lac/unix/unix_scripts/service.py @@ -4,6 +4,7 @@ import time from datetime import datetime import subprocess +import utils # If cron is not running as root, exit if os.geteuid() != 0: @@ -59,7 +60,7 @@ def ensure_fingerprint_is_trusted(): date = time.strftime("%Y-%m-%d") # If current time is higher than backup time, run backup - if time.strftime("%H:%M") > backup_time and not os.path.isfile("maintenance/backup_running") and not os.path.isfile(f"history/borg_errors_{date}.log"): + if time.strftime("%H:%M") > backup_time and not utils.is_backup_running() and not os.path.isfile(f"history/borg_errors_{date}.log"): print("Running backup") # Run do_backup.sh script with cwd in the maintenance directory p = subprocess.Popen(["bash", "do_backup.sh"], cwd="maintenance") diff --git a/src/lac/unix/unix_scripts/unix.py b/src/lac/unix/unix_scripts/unix.py index 13a4c17..3aa0b90 100644 --- a/src/lac/unix/unix_scripts/unix.py +++ b/src/lac/unix/unix_scripts/unix.py @@ -9,6 +9,7 @@ from app_dashboard.models import DashboardEntry from django.urls import reverse import requests +import unix.unix_scripts.utils as utils # Change current directory to the directory of this script os.chdir(os.path.dirname(os.path.realpath(__file__))) @@ -127,8 +128,7 @@ def get_borg_information_for_dashboard(): if not rv["last_backup"]["success"]: rv["backup_status"] = "last_backup_failed" - # If file "backup_running" exists, set backup status to "running" - if os.path.isfile("maintenance/backup_running"): + if utils.is_backup_running(): rv["backup_status"] = "running" if os.path.isfile("maintenance/recovery_running"): @@ -177,7 +177,7 @@ def set_trusted_fingerprint(fingerprint): def retry_backup(): read_config_file() # If the backup is currently running, exit - if os.path.isfile("maintenance/backup_running"): + if utils.is_backup_running(): return # If the backup is deactivated, exit if os.path.isfile("maintenance/backup_disabled"): diff --git a/src/lac/unix/unix_scripts/utils.py b/src/lac/unix/unix_scripts/utils.py new file mode 100644 index 0000000..c7e7b24 --- /dev/null +++ b/src/lac/unix/unix_scripts/utils.py @@ -0,0 +1,7 @@ +# To this file unix.py (django) and also service.py (not django) can access +import subprocess + + +def is_backup_running(): + # Check if a process with the name backup.sh in it is running + return "backup.sh" in subprocess.getoutput("ps -aux") \ No newline at end of file