param( [Parameter(Mandatory = $true)][string]$BackupFile, [Parameter(Mandatory = $true)][string]$ConfirmDatabaseName, [ValidateSet('production', 'demo')][string]$Mode = 'production', [string]$Confirmation = '' ) . (Join-Path $PSScriptRoot 'sense-common.ps1') try { $root = Get-SensePackageRoot $state = Initialize-SenseRuntime -PackageRoot $root -Mode $Mode -AllowOccupiedPort $backup = [IO.Path]::GetFullPath($BackupFile) if (-not (Test-Path -LiteralPath $backup -PathType Leaf)) { throw "Backup file not found: $backup" } if ($ConfirmDatabaseName -cne $state.Database.Database) { throw 'Restore confirmation does not exactly match the configured database name.' } $pgRestore = Get-SensePostgresTool -Name 'pg_restore' Write-Warning "Restoring will replace objects in database '$ConfirmDatabaseName'. Stop Sense before continuing." $answer = if ([string]::IsNullOrWhiteSpace($Confirmation)) { Read-Host "Type RESTORE-$ConfirmDatabaseName to continue" } else { $Confirmation } if ($answer -cne "RESTORE-$ConfirmDatabaseName") { throw 'Restore cancelled.' } Invoke-SensePostgresTool -Tool $pgRestore -Database $state.Database -Arguments @('--dbname', $state.Database.Sanitized, '--clean', '--if-exists', '--no-owner', '--exit-on-error', $backup) Write-Host 'Sense database restore completed. Run migrate-sense.bat before starting Sense.' exit 0 } catch { Write-Error $_.Exception.Message exit 1 }