将.mdf数据库拆分为多个文件

时间:2013-07-12 15:26:23

标签: sql-server database powershell split backup

我需要使用powershell将数据库.bak拆分为多个文件。怎么做得最好? 有什么例子吗?

1 个答案:

答案 0 :(得分:1)

这样的事可能有用:

$filename = "C:\path\to\your.bak"

$chunksize = 20480
$totalsize = (Get-Item $filename).Length

$stream = New-Object IO.FileStream($filename, [IO.FileMode]::Open)
$reader = New-Object IO.BinaryReader($stream)

$chunk = New-Object byte[] $chunksize
$size = $chunksize
$i = 0
do {
  $rest = $totalsize - ($i * $chunksize)
  $i++
  if ($chunksize -gt $rest) {
    $size  = $rest
    $chunk = New-Object byte[] $size
  }
  $reader.Read($chunk, 0, $size) | Out-Null
  [IO.File]::WriteAllBytes(({0}.{1:d3}" -f ($filename, $i)), $chunk)
} until ($chunksize -gt $rest)

$reader.Close()
$stream.Close()