PayrollManagementSystem/db/restore_run.ps1
liuwenxin 35fea3802f fix(备份): 防止恢复脚本覆盖数据库并返回失败码
修复根目录 README 的失效文档链接,改为现有 T+ 本地恢复手册。\n恢复脚本移除 RESTORE WITH REPLACE,默认不覆盖已有数据库;成功和失败均写入结果文件,失败显式返回非零退出码,并统一释放数据库连接。\n\n影响范围:项目导航文档与两个本地 SQL Server 恢复辅助脚本。\n\nCo-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-06 15:36:39 +08:00

29 lines
1.3 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# restore_run.ps1 - 独立进程执行 RESTORE,默认不覆盖已有数据库
if (-not $env:PAYROLL_RESTORE_CONNECTION_STRING) { throw "PAYROLL_RESTORE_CONNECTION_STRING is required" }
$connStr = $env:PAYROLL_RESTORE_CONNECTION_STRING
$c = New-Object System.Data.Odbc.OdbcConnection $connStr
$cmd = $null
try {
$c.Open()
$cmd = $c.CreateCommand()
$cmd.CommandTimeout = 14400
$cmd.CommandText = @"
RESTORE DATABASE [UFTData472115_000001]
FROM DISK = '\\192.168.1.170\dbbak\ceshi20261026.bak'
WITH MOVE 'MsSql' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\UFTData472115_000001.mdf',
MOVE 'MsSql_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\UFTData472115_000001_log.ldf',
RECOVERY, STATS = 10
"@
$cmd.ExecuteNonQuery() | Out-Null
"RESTORE-DONE-OK" | Out-File 'D:\work\code\PayrollManagementSystem\db\restore_result.txt' -Encoding ascii
exit 0
} catch {
$message = $_.Exception.Message
if ($_.Exception.InnerException) { $message = $_.Exception.InnerException.Message }
("RESTORE-FAILED: " + $message) | Out-File 'D:\work\code\PayrollManagementSystem\db\restore_result.txt' -Encoding ascii
exit 1
} finally {
if ($cmd) { $cmd.Dispose() }
if ($c) { $c.Close(); $c.Dispose() }
}