53 lines
2.1 KiB
PowerShell
53 lines
2.1 KiB
PowerShell
[CmdletBinding()]
|
||
param(
|
||
[string]$SourceBackupPath = "D:\work\code\PayrollManagementSystem\db\ceshi20261026.bak",
|
||
[string]$TPlusRoot = "D:\work\app\tplus-sqlserver"
|
||
)
|
||
|
||
Set-StrictMode -Version Latest
|
||
$ErrorActionPreference = "Stop"
|
||
|
||
function Assert-OnDDrive([string]$Path) {
|
||
$full = [IO.Path]::GetFullPath($Path)
|
||
if (-not $full.StartsWith("D:\", [StringComparison]::OrdinalIgnoreCase)) {
|
||
throw "T+ 数据目录必须位于 D 盘:$full"
|
||
}
|
||
}
|
||
|
||
Assert-OnDDrive $TPlusRoot
|
||
if (-not (Test-Path -LiteralPath $SourceBackupPath -PathType Leaf)) {
|
||
throw "找不到客户备份文件:$SourceBackupPath"
|
||
}
|
||
|
||
$dataDir = Join-Path $TPlusRoot "data"
|
||
$logDir = Join-Path $TPlusRoot "log"
|
||
$backupDir = Join-Path $TPlusRoot "backup"
|
||
$scriptsDir = Join-Path $TPlusRoot "scripts"
|
||
foreach ($dir in @($dataDir, $logDir, $backupDir, $scriptsDir)) {
|
||
New-Item -ItemType Directory -Path $dir -Force | Out-Null
|
||
}
|
||
|
||
$destination = Join-Path $backupDir (Split-Path $SourceBackupPath -Leaf)
|
||
$sourceHash = (Get-FileHash -LiteralPath $SourceBackupPath -Algorithm SHA256).Hash
|
||
|
||
if (Test-Path -LiteralPath $destination -PathType Leaf) {
|
||
$destinationHash = (Get-FileHash -LiteralPath $destination -Algorithm SHA256).Hash
|
||
if ($destinationHash -ne $sourceHash) {
|
||
throw "D 盘已有同名备份,但 SHA-256 不一致:$destination"
|
||
}
|
||
Write-Host "备份已存在且校验一致:$destination"
|
||
} else {
|
||
Write-Host "正在把备份复制到 D 盘,源文件不会被修改……"
|
||
Copy-Item -LiteralPath $SourceBackupPath -Destination $destination -Force
|
||
$destinationHash = (Get-FileHash -LiteralPath $destination -Algorithm SHA256).Hash
|
||
if ($destinationHash -ne $sourceHash) {
|
||
throw "复制完成后 SHA-256 校验失败:$destination"
|
||
}
|
||
Write-Host "备份复制完成:$destination"
|
||
}
|
||
|
||
$hashFile = "$destination.sha256"
|
||
"$destinationHash $(Split-Path $destination -Leaf)" | Set-Content -LiteralPath $hashFile -Encoding utf8
|
||
Write-Host "SHA-256:$destinationHash"
|
||
Write-Host "SQL Server 数据、日志和备份目录已准备在:$TPlusRoot"
|