Skip to content

Commit

Permalink
Change backup running evaluation #85
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean28518 committed Apr 27, 2024
1 parent deac526 commit a597c5a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
6 changes: 1 addition & 5 deletions src/lac/unix/unix_scripts/maintenance/do_backup.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
borg info $BORG_REPOSITORY > ../history/borg_info
3 changes: 2 additions & 1 deletion src/lac/unix/unix_scripts/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions src/lac/unix/unix_scripts/unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)))
Expand Down Expand Up @@ -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"):
Expand Down Expand Up @@ -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"):
Expand Down
7 changes: 7 additions & 0 deletions src/lac/unix/unix_scripts/utils.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit a597c5a

Please sign in to comment.