[英]Split database backup file using PowerShell and merge it back
我是 PowerShell 的新手,在使用 PowerShell 拆分和重新附加文件時遇到了一些損壞文件的問題。
我有一個遠程服務器,我需要從中下載一個大小為 44GB 的 .bak 文件。 為了能夠做到這一點,我使用這個腳本將文件分成更小的(100mb)塊。
$from = "D:\largebakfile\largefile.bak"
$rootName = "D:\foldertoplacelargebakfile\part"
$ext = "PART_"
$upperBound = 100MB
$fromFile = [io.file]::OpenRead($from)
$buff = new-object byte[] $upperBound
$count = $idx = 0
try {
do {
"Reading $upperBound"
$count = $fromFile.Read($buff, 0, $buff.Length)
if ($count -gt 0) {
$to = "{0}{1}{2}" -f ($rootName, $idx, $ext)
$toFile = [io.file]::OpenWrite($to)
try {
"Writing $count to $to"
$tofile.Write($buff, 0, $count)
} finally {
$tofile.Close()
}
}
$idx ++
} while ($count -gt 0)
}
finally {
$fromFile.Close()
}
完成此操作並將“PART_”文件下載到本地計算機后,我使用此腳本將文件合並回 1 個 .bak 文件。
# replace with the location of the "PARTS" file
Set-Location "C:\Folderwithsplitfiles\Parts"
# replace with the SQL backup folder in your computer.
$outFile = "C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\Backup\newname.bak"
#The prefix for all PARTS files
$infilePrefix ="C:\Folderwithsplitfiles\Parts\PART_"
$ostream = [System.Io.File]::OpenWrite($outFile)
$chunkNum = 1
$infileName = "$infilePrefix$chunkNum"
$offset = 0
while(Test-Path $infileName) {
$bytes = [System.IO.File]::ReadAllBytes($infileName)
$ostream.Write($bytes, 0, $bytes.Count)
Write-Host "read $infileName"
$chunkNum += 1
$infileName = "$infilePrefix$chunkNum"
}
$ostream.close();
#Get-FileHash $outfile | Format-List
嘗試在 SSMS 中恢復數據庫時,我收到一條錯誤消息,主要是說文件已損壞且無法恢復。
這幾天我一直在努力解決這個問題,似乎我的頭腦不正確。
一切似乎都在工作,但有些事情導致我出現這些錯誤。 有沒有人有任何想法?
我建議您看看后台智能傳輸服務,您應該能夠將整個文件下載到一個文件中,以節省任何復雜的事情。 (也支持開始/停止傳輸等)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.