Files
claudetools/projects/dataforth-dos/testdatadb-fix/backup-db.ps1
Mike Swanson 470638ff86 sync: Dataforth sync fixes, TestDataDB stability, and client scripts
Dataforth DOS:
- TestDataDB: singleton DB connection fix (crash prevention), WAL mode,
  WinSW service config, backup script, uncaught exception handlers
- Sync-FromNAS.ps1: Get-NASFileList temp file approach to avoid SSH
  stdout deadlock, *> $null output suppression, 8.3 filename filter
  for PUSH phase, backslash-escaped SCP paths, rename-to-.synced
- import.js: INSERT OR REPLACE for re-tested devices
- Full import run: 1,028,275 -> 1,632,793 records, indexes added
- Deploy script for sync fixes to AD2

Client scripts (temp/):
- BG Builders: Lesley account check, MFA phone update
- Lonestar Electrical: Kyla/Russ Google Workspace setup, 2FA bypass
- AD2 diagnostics and NAS connectivity tests

PENDING: Investigate why newest test_date is Jan 19 despite daily tests

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 06:08:31 -07:00

62 lines
1.9 KiB
PowerShell

# backup-db.ps1
# Backs up the TestDataDB SQLite database with 7-day retention.
# Intended to run as a scheduled task via install-backup-task.ps1.
$ErrorActionPreference = 'Stop'
$SourceDb = 'C:\Shares\testdatadb\database\testdata.db'
$BackupDir = 'C:\Shares\testdatadb\backups'
$LogFile = 'C:\Shares\testdatadb\logs\backup.log'
$Retention = 7
function Write-Log {
param([string]$Message)
$timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
$entry = "[$timestamp] $Message"
Add-Content -Path $LogFile -Value $entry
Write-Host $entry
}
# Ensure directories exist
foreach ($dir in @($BackupDir, (Split-Path $LogFile -Parent))) {
if (-not (Test-Path $dir)) {
New-Item -ItemType Directory -Path $dir -Force | Out-Null
}
}
# Verify source database exists
if (-not (Test-Path $SourceDb)) {
Write-Log "[ERROR] Source database not found: $SourceDb"
exit 1
}
# Create dated backup
$datestamp = Get-Date -Format 'yyyy-MM-dd'
$backupFile = Join-Path $BackupDir "testdata-$datestamp.db"
try {
Copy-Item -Path $SourceDb -Destination $backupFile -Force
$sizeKb = [math]::Round((Get-Item $backupFile).Length / 1024, 1)
Write-Log "[OK] Backup created: $backupFile ($sizeKb KB)"
} catch {
Write-Log "[ERROR] Backup failed: $_"
exit 1
}
# Prune old backups beyond retention period
$cutoff = (Get-Date).AddDays(-$Retention)
$oldBackups = Get-ChildItem -Path $BackupDir -Filter 'testdata-*.db' |
Where-Object { $_.LastWriteTime -lt $cutoff }
foreach ($old in $oldBackups) {
try {
Remove-Item -Path $old.FullName -Force
Write-Log "[OK] Deleted old backup: $($old.Name)"
} catch {
Write-Log "[WARNING] Could not delete old backup: $($old.Name) - $_"
}
}
$remaining = (Get-ChildItem -Path $BackupDir -Filter 'testdata-*.db').Count
Write-Log "[INFO] Backup complete. $remaining backup(s) on disk."