|
| 1 | +import logging |
| 2 | +import os |
| 3 | +import sqlite3 |
| 4 | +from datetime import datetime |
| 5 | + |
| 6 | +logging.basicConfig( |
| 7 | + level=os.getenv("LOG_LEVEL", "INFO").upper(), |
| 8 | + format=os.getenv("LOG_FORMAT", "%(asctime)s - %(name)s - %(levelname)s - %(message)s"), |
| 9 | +) |
| 10 | +logger = logging.getLogger(__name__) |
| 11 | + |
| 12 | + |
| 13 | +def backup_database(db_path: str, backup_dir: str) -> str: |
| 14 | + """ |
| 15 | + Backup a SQLite database to a timestamped file in backup_dir. |
| 16 | +
|
| 17 | + Returns the path of the backup file. |
| 18 | + """ |
| 19 | + os.makedirs(backup_dir, exist_ok=True) |
| 20 | + timestamp = datetime.now().strftime("%Y%m%d-%H%M%S") |
| 21 | + db_filename = os.path.basename(db_path) |
| 22 | + backup_path = os.path.join(backup_dir, f"{timestamp}.{db_filename}.backup") |
| 23 | + with sqlite3.connect(db_path) as conn: |
| 24 | + with sqlite3.connect(backup_path) as backup_conn: |
| 25 | + conn.backup(backup_conn) |
| 26 | + return backup_path |
| 27 | + |
| 28 | + |
| 29 | +def backup_databases(): |
| 30 | + """ |
| 31 | + Backup all configured databases. |
| 32 | +
|
| 33 | + Environment variables: |
| 34 | + PACS_DB_PATH: Path to the PACS SQLite database |
| 35 | + MWL_DB_PATH: Path to the MWL SQLite database |
| 36 | + BACKUP_PATH: Directory for backups (default: ./backups) |
| 37 | + """ |
| 38 | + pacs_db_path = os.getenv("PACS_DB_PATH") |
| 39 | + mwl_db_path = os.getenv("MWL_DB_PATH") |
| 40 | + backup_path = os.getenv("BACKUP_PATH", "./backups") |
| 41 | + |
| 42 | + if not pacs_db_path and not mwl_db_path: |
| 43 | + logger.warning("No database paths configured (PACS_DB_PATH or MWL_DB_PATH), skipping backup") |
| 44 | + return |
| 45 | + |
| 46 | + success = True |
| 47 | + |
| 48 | + if pacs_db_path: |
| 49 | + try: |
| 50 | + pacs_backup_path = backup_database(pacs_db_path, backup_path) |
| 51 | + except Exception as e: |
| 52 | + logging.error(f"PACS backup failed: {e}") |
| 53 | + success = False |
| 54 | + else: |
| 55 | + logging.info("PACS_DB_PATH not set, skipping PACS backup") |
| 56 | + |
| 57 | + if mwl_db_path: |
| 58 | + try: |
| 59 | + mwl_backup_path = backup_database(mwl_db_path, backup_path) |
| 60 | + except Exception as e: |
| 61 | + logging.error(f"MWL backup failed: {e}") |
| 62 | + success = False |
| 63 | + else: |
| 64 | + logging.info("MWL_DB_PATH not set, skipping MWL backup") |
| 65 | + |
| 66 | + if success: |
| 67 | + logger.info("All database backups completed successfully. Backup files:") |
| 68 | + if pacs_db_path: |
| 69 | + logger.info(f" PACS backup: {pacs_backup_path}") |
| 70 | + if mwl_db_path: |
| 71 | + logger.info(f" MWL backup: {mwl_backup_path}") |
| 72 | + |
| 73 | + |
| 74 | +def reset_worklist_database() -> int: |
| 75 | + """ |
| 76 | + Backs up and clears the MWL database.. |
| 77 | +
|
| 78 | + Environment variables: |
| 79 | + MWL_DB_PATH: Path to the MWL SQLite database (default: /var/lib/pacs/worklist.db) |
| 80 | + BACKUP_PATH: Directory for database backups (default: /var/lib/pacs/backups) |
| 81 | + """ |
| 82 | + mwl_db_path = os.getenv("MWL_DB_PATH", "/var/lib/pacs/worklist.db") |
| 83 | + backup_path = os.getenv("BACKUP_PATH", "/var/lib/pacs/backups") |
| 84 | + count = 0 |
| 85 | + |
| 86 | + try: |
| 87 | + path = backup_database(mwl_db_path, backup_path) |
| 88 | + logger.info(f"Backup complete: {path}") |
| 89 | + except Exception as e: |
| 90 | + logger.error(f"MWL backup failed: {e}", exc_info=True) |
| 91 | + |
| 92 | + try: |
| 93 | + with sqlite3.connect(mwl_db_path) as conn: |
| 94 | + cursor = conn.execute("DELETE FROM worklist_items") |
| 95 | + conn.commit() |
| 96 | + count = cursor.rowcount |
| 97 | + logger.info(f"MWL reset complete: {count} items deleted") |
| 98 | + except Exception as e: |
| 99 | + logger.error(f"MWL clear failed: {e}", exc_info=True) |
| 100 | + |
| 101 | + return count |
0 commit comments