我是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 _"文件被下载到本地计算机我使用这个scipt将文件合并到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中恢复数据库时,我得到一个错误,基本上说该文件已损坏且无法恢复。
我现在几天都在苦苦挣扎,似乎没有理解我的头脑。
一切似乎都在起作用,但有些东西正在给我带来这些错误。有没有人有任何想法?
提前致谢 / d
答案 0 :(得分:0)
我建议你看一下Background Intelligent Transfer Service,你应该能够整个下载整个文件,以避免使任何事情变得复杂。 (还支持启动/停止传输等。)