Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ _install-uv:
echo "uv already installed"; \
fi

# ---------------------------------------------------------------------------
# Database backup
# ---------------------------------------------------------------------------
backup-db: # Backup sqlite databases @Operations
PYTHONPATH=scripts/python uv run python -m backup_database

# ---------------------------------------------------------------------------
# Testing
# ---------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions scripts/bat/backup_database.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:: Batch file to run the backup_database.py script
@echo off
set %rootdir=%1
python "%rootdir%\scripts\python\backup_database.py"
5 changes: 5 additions & 0 deletions scripts/bat/schtasks.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
:: This batch file creates a scheduled task to run the backup_database.bat script daily at midnight.
@echo off
set thisdir=%~dp0
set rootdir="%thisdir%..\.."
schtasks /create /tn BackupDatabaseTask /tr "%thisdir%backup_database.bat %rootdir%" /sc daily /st 00:00
73 changes: 73 additions & 0 deletions scripts/python/backup_database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from datetime import datetime
import dotenv
import sqlite3
import os
import sys

dotenv.load_dotenv()


def backup_database(db_path, backup_path):
"""Backup the database at db_path."""

conn = None

if not os.path.exists(db_path):
print(f"Database not found: {db_path}")
return False

if not os.path.exists(backup_path):
os.makedirs(backup_path)

try:
date_and_time = datetime.now().strftime("%Y%m%d-%H%M%S")
db_filename = os.path.basename(db_path)
backup_path = f"{backup_path}/{date_and_time}.{db_filename}.backup"

conn = sqlite3.connect(db_path)
with sqlite3.connect(backup_path) as backup_conn:
conn.backup(backup_conn)
print(f"Database backed up successfully to: {backup_path}")
return True
except Exception as e:
print(f"Error during backup: {e}")
return False
finally:
if conn:
conn.close()


def main():
"""Main entry point."""
try:
pacs_db_path = os.getenv("PACS_DB_PATH")
mwl_db_path = os.getenv("MWL_DB_PATH")
backup_path = os.getenv("BACKUP_PATH", "./backups")

print("Starting database backup...")
print("PACS_DB_PATH:", pacs_db_path, "MWL_DB_PATH:", mwl_db_path)
success = True

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be the start and end of all computer programs


if pacs_db_path:
print(f"Backing up PACS database: {pacs_db_path} to {backup_path}")
success = success and backup_database(pacs_db_path, backup_path)
else:
print("PACS_DB_PATH not set, skipping PACS database backup")

if mwl_db_path:
print(f"Backing up MWL database: {mwl_db_path} to {backup_path}")
success = success and backup_database(mwl_db_path, backup_path)
else:
print("MWL_DB_PATH not set, skipping MWL database backup")

sys.exit(0 if success else 1)
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
import traceback

traceback.print_exc()
sys.exit(1)


if __name__ == "__main__":
main()
File renamed without changes.
Loading