-
Notifications
You must be signed in to change notification settings - Fork 3
Rotate logs and backup database on deployed gateways #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2d3993a
Rotate logs and backup databases on Windows VMs
steventux e6448d8
Archive PACS files
steventux 129005e
Retain 5 db backups
steventux 1a595e8
Validate action passed to script
steventux a2dc741
Document maintenance script
steventux 9df8ec2
Check that service is Running when starting
steventux 7e23a82
Explicitly invoke Python when calling db backup
steventux 0ecb9e4
Check for storage directory and files before archiving
steventux 772f7a5
Fix scheduled times
steventux 801ce7c
Rotate maintenance log before performing action
steventux f789240
Rename backup function to backup_and_reset
steventux File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,379 @@ | ||
| # Gateway Maintenance Script Documentation | ||
|
|
||
| ## Overview | ||
|
|
||
| The `maintenance.ps1` script performs routine maintenance tasks for the NHS Rubie Gateway installation. It provides automated operations for: | ||
|
|
||
| * MWL database backup | ||
| * PACS database backup | ||
| * PACS storage archiving | ||
| * Service log rotation | ||
|
|
||
| The script ensures that Gateway services are safely stopped before maintenance activities are performed and restarted afterwards. | ||
|
|
||
| --- | ||
|
|
||
| # Script Location | ||
|
|
||
| ```text | ||
| <InstallPath>\current\scripts\powershell\maintenance.ps1 | ||
| ``` | ||
|
|
||
| Default installation path: | ||
|
|
||
| ```text | ||
| C:\Program Files\NHS\ManageBreastScreeningGateway | ||
| ``` | ||
|
|
||
| Scheduled tasks are configured in the Windows Task Scheduler via the `deploy.ps1` script. | ||
| The `deploy.ps1` script is located at: | ||
|
|
||
| ```text | ||
| <InstallPath>\current\scripts\powershell\deploy.ps1 | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| # Parameters | ||
|
|
||
| | Parameter | Description | Default | | ||
| | ----------------- | ------------------------------------------------------- | --------------------------------------------------- | | ||
| | `BaseInstallPath` | Root installation directory of the Gateway application. | `C:\Program Files\NHS\ManageBreastScreeningGateway` | | ||
| | `Action` | Maintenance operation to execute. | None | | ||
|
|
||
| ### Supported Actions | ||
|
|
||
| | Action | Description | | ||
| | -------------------- | --------------------------------------------------------------------- | | ||
| | `BackupMWLDatabase` | Creates a backup of the MWL database. | | ||
| | `BackupPACSDatabase` | Creates a backup of the PACS database and archives stored PACS files. | | ||
| | `RotateLogs` | Rotates Gateway service log files. | | ||
|
|
||
| ### Example | ||
|
|
||
| ```powershell | ||
| .\maintenance.ps1 -Action BackupMWLDatabase | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| # Logging | ||
|
|
||
| Maintenance activity is recorded in: | ||
|
|
||
| ```text | ||
| <InstallPath>\logs\maintenance.log | ||
| ``` | ||
|
|
||
| Each log entry includes: | ||
|
|
||
| * Timestamp | ||
| * Log level | ||
| * Message | ||
|
|
||
| Example: | ||
|
|
||
| ```text | ||
| [2026-01-01 02:00:00.123] [INFO] Starting database backup... | ||
| ``` | ||
|
|
||
| ### Log Levels | ||
|
|
||
| | Level | Meaning | | ||
| | ------- | ------------------------------------- | | ||
| | INFO | Informational messages | | ||
| | SUCCESS | Successful completion of an operation | | ||
| | WARNING | Non-critical issue | | ||
| | ERROR | Operation failure | | ||
|
|
||
| --- | ||
|
|
||
| # Gateway Service Management | ||
|
|
||
| The script automatically discovers services matching: | ||
|
|
||
| ```text | ||
| Gateway-* | ||
| ``` | ||
|
|
||
| Examples: | ||
|
|
||
| ```text | ||
| Gateway-MWL | ||
| Gateway-PACS | ||
| Gateway-Relay | ||
| Gateway-Listener | ||
| ``` | ||
|
|
||
| ## Service Stop Process | ||
|
|
||
| Before maintenance operations begin: | ||
|
|
||
| 1. Each running Gateway service is stopped. | ||
| 2. Service state is monitored until it reaches `Stopped`. | ||
| 3. If a service fails to stop within the configured timeout, an exception is raised and processing stops. | ||
|
|
||
| ## Service Start Process | ||
|
|
||
| After maintenance operations complete: | ||
|
|
||
| 1. Each Gateway service is started. | ||
| 2. Service state is monitored until it reaches `Running`. | ||
| 3. Successful startup is logged. | ||
|
|
||
| --- | ||
|
|
||
| # Database Backup Operations | ||
|
|
||
| ## Common Backup Configuration | ||
|
|
||
| The script sets the following environment variables before invoking the Python backup utility: | ||
|
|
||
| | Variable | Value | | ||
| | ------------- | -------------------------------------- | | ||
| | `PYTHONPATH` | `<InstallPath>\current\scripts\python` | | ||
| | `BACKUP_PATH` | `<InstallPath>\data\backups` | | ||
| | `MAX_BACKUPS` | `5` | | ||
|
|
||
| The backup process is executed using: | ||
|
|
||
| ```powershell | ||
| ..\python\database.py | ||
| ``` | ||
|
|
||
| Only the most recent five backups are retained. | ||
|
|
||
| --- | ||
|
|
||
| ## MWL Database Backup | ||
|
|
||
| Action: | ||
|
|
||
| ```powershell | ||
| BackupMWLDatabase | ||
| ``` | ||
|
|
||
| Database configuration: | ||
|
|
||
| | Setting | Value | | ||
| | ------------- | -------------------------------- | | ||
| | Database File | `<InstallPath>\data\worklist.db` | | ||
| | Table Name | `worklist_items` | | ||
|
|
||
| Process: | ||
|
|
||
| 1. Stop Gateway services. | ||
| 2. Backup MWL database. | ||
| 3. Restart Gateway services. | ||
|
|
||
| --- | ||
|
|
||
| ## PACS Database Backup | ||
|
|
||
| Action: | ||
|
|
||
| ```powershell | ||
| BackupPACSDatabase | ||
| ``` | ||
|
|
||
| Database configuration: | ||
|
|
||
| | Setting | Value | | ||
| | ------------- | ---------------------------- | | ||
| | Database File | `<InstallPath>\data\pacs.db` | | ||
| | Table Name | `stored_instances` | | ||
|
|
||
| Process: | ||
|
|
||
| 1. Stop Gateway services. | ||
| 2. Backup PACS database. | ||
| 3. Archive PACS storage files. | ||
| 4. Restart Gateway services. | ||
|
|
||
| --- | ||
|
|
||
| # PACS Archive Process | ||
|
|
||
| After a successful PACS database backup: | ||
|
|
||
| ## Source Directory | ||
|
|
||
| ```text | ||
| <InstallPath>\data\storage | ||
| ``` | ||
|
|
||
| ## Archive Location | ||
|
|
||
| ```text | ||
| <InstallPath>\data\storage.zip | ||
| ``` | ||
|
|
||
| ## Process | ||
|
|
||
| 1. All files within the PACS storage directory are compressed into a ZIP archive. | ||
| 2. Archive is written to `storage.zip`. | ||
| 3. Original storage contents are deleted. | ||
| 4. Archive operation is logged. | ||
|
|
||
| ### Important | ||
|
|
||
| The archive operation removes all files from the PACS storage directory after successful compression. | ||
|
|
||
| --- | ||
|
|
||
| # Log Rotation | ||
|
|
||
| Action: | ||
|
|
||
| ```powershell | ||
| RotateLogs | ||
| ``` | ||
|
|
||
| The script rotates log files for each Gateway service. | ||
|
|
||
| Example: | ||
|
|
||
| ```text | ||
| Gateway-MWL.log | ||
| ``` | ||
|
|
||
| becomes: | ||
|
|
||
| ```text | ||
| Gateway-MWL.log.1 | ||
| ``` | ||
|
|
||
| Existing rotations are shifted: | ||
|
|
||
| ```text | ||
| Gateway-MWL.log.1 -> Gateway-MWL.log.2 | ||
| Gateway-MWL.log.2 -> Gateway-MWL.log.3 | ||
| ... | ||
| Gateway-MWL.log.5 removed | ||
| ``` | ||
|
|
||
| ## Retention Policy | ||
|
|
||
| * Maximum retained log generations: 5 | ||
| * Oldest rotation is deleted before creating a new rotation | ||
|
|
||
| Process: | ||
|
|
||
| 1. Stop Gateway services. | ||
| 2. Rotate all service logs. | ||
| 3. Restart Gateway services. | ||
|
|
||
| --- | ||
|
|
||
| # Scheduled Tasks | ||
|
|
||
| The script registers three Windows Scheduled Tasks. | ||
|
|
||
| ## MWL Database Maintenance | ||
|
|
||
| | Setting | Value | | ||
| | --------- | ------------------------- | | ||
| | Task Name | `Gateway-MWL-Maintenance` | | ||
| | Schedule | Weekly | | ||
| | Day | Sunday | | ||
| | Time | 02:00 | | ||
| | User | SYSTEM | | ||
| | Run Level | Highest | | ||
|
|
||
| Executed command: | ||
|
|
||
| ```powershell | ||
| powershell.exe -ExecutionPolicy Bypass -File maintenance.ps1 -Action BackupMWLDatabase | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## PACS Database Maintenance | ||
|
|
||
| | Setting | Value | | ||
| | --------- | -------------------------- | | ||
| | Task Name | `Gateway-PACS-Maintenance` | | ||
| | Schedule | Weekly | | ||
| | Day | Sunday | | ||
| | Time | 02:15 | | ||
| | User | SYSTEM | | ||
| | Run Level | Highest | | ||
|
|
||
| Executed command: | ||
|
|
||
| ```powershell | ||
| powershell.exe -ExecutionPolicy Bypass -File maintenance.ps1 -Action BackupPACSDatabase | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Log Rotation Maintenance | ||
|
|
||
| | Setting | Value | | ||
| | --------- | -------------------------- | | ||
| | Task Name | `Gateway-Logs-Maintenance` | | ||
| | Schedule | Daily | | ||
| | Time | 02:30 | | ||
| | User | SYSTEM | | ||
| | Run Level | Highest | | ||
|
|
||
| Executed command: | ||
|
|
||
| ```powershell | ||
| powershell.exe -ExecutionPolicy Bypass -File maintenance.ps1 -Action RotateLogs | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| # Failure Handling | ||
|
|
||
| The script terminates when: | ||
|
|
||
| * A Gateway service cannot be stopped within the configured timeout. | ||
| * The database backup utility returns a non-zero exit code. | ||
| * An unhandled PowerShell exception occurs. | ||
|
|
||
| Errors are written to: | ||
|
|
||
| ```text | ||
| <InstallPath>\logs\maintenance.log | ||
| ``` | ||
|
|
||
| and displayed on the console. | ||
|
|
||
| --- | ||
|
|
||
| # Maintenance Workflow Summary | ||
|
|
||
| ## MWL Backup | ||
|
|
||
| ```text | ||
| Stop Services | ||
| ↓ | ||
| Backup MWL Database | ||
| ↓ | ||
| Start Services | ||
| ``` | ||
|
|
||
| ## PACS Backup | ||
|
|
||
| ```text | ||
| Stop Services | ||
| ↓ | ||
| Backup PACS Database | ||
| ↓ | ||
| Archive PACS Files | ||
| ↓ | ||
| Start Services | ||
| ``` | ||
|
|
||
| ## Log Rotation | ||
|
|
||
| ```text | ||
| Stop Services | ||
| ↓ | ||
| Rotate Logs | ||
| ↓ | ||
| Start Services | ||
| ``` | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
without wanting to get into some sort of log rotation inception... do we need to clear out maintenance.log sometimes as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
24b402f Added maintenance log rotation before rotating service logs.