2011-02-25 104 views
9

從另一臺服務器,我想恢復到SQLEXPRESS本地主機上以數據庫備份新的數據庫。這種恢復將通過gui工作,但我遇到的問題使用PowerShell進行恢復。我收到以下錯誤信息:錯誤數據庫備份恢復到與SMO和PowerShell

Exception calling "SqlRestore" with "1" argument(s): "Restore failed for Server 
+ $smoRestore.SqlRestore <<<< ($server) 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : DotNetMethodException 

錯誤信息指向該行的字符23:

 $smoRestore.SqlRestore($server) 

腳本:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null 
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null 
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null 
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null 

Import-Module PSCX 
Import-Module WebAdministration 

function GetLatestItem(){ 
    param([string]$RemotePath) 
    $returnString = Get-ChildItem $RemotePath -force -filter "*.7z" | sort @{expression={$_.LastWriteTime}; Descending=$true} | select Name -first 1 
    return $returnString.Name 
} 

function DatabaseExists(){ 
    param([Microsoft.SqlServer.Management.Smo.Server]$server,[string]$databaseName) 
    foreach($database in $server.Databases){ 
     if($database.Name -eq $databaseName){ 
      $true 
     } 
    } 
    $false 
} 

$LocalFile = "C:\backups\backupname.bak.7z" 
$LocalFilePath = "C:\backups\" 

Expand-Archive $Localfile $LocalFilePath  

# Most of the restore information was found at http://www.sqlmusings.com/2009/06/01/how-to-restore-sql-server-databases-using-smo-and-powershell/ 
$backupFile = $LocalFilePath + [IO.Path]::GetFileNameWithoutExtension($LocalFile) 
[Microsoft.SqlServer.Management.Smo.Server]$server = New-Object ("Microsoft.SqlServer.Management.Smo.Server") ".\SQLEXPRESS" 
$backupDevice = New-Object ("Microsoft.SqlServer.Management.Smo.BackupDeviceItem") ($backupFile, "File") 
$smoRestore = New-Object Microsoft.SqlServer.Management.Smo.Restore 

$smoRestore.NoRecovery = $true; 
$smoREstore.ReplaceDatabase = $true; 
$smoRestore.Action = "Database" 
$smoRestore.PercentCompleteNotification = 10; 
$smoRestore.Devices.Add($backupDevice) 

# Get the details from the backup device for the database name and output that 
$smoRestoreDetails = $smoRestore.ReadBackupHeader($server) 
$databaseName = $smoRestoreDetails.Rows[0]["DatabaseName"] 

"Database Name from Backup Header : " + $databaseName 
$smoRestore.Database = $databaseName  

if(DatabaseExists $server $databaseName -not){ 
    $smoRestoreFile = New-Object("Microsoft.SqlServer.Management.Smo.RelocateFile") 
    $smoRestoreLog = New-Object("Microsoft.SqlServer.Management.Smo.RelocateFile") 
    $smoRestoreFile.LogicalFileName = $smoRestoreDetails.Rows[0]["DatabaseName"] 
    $smoRestoreFile.PhysicalFileName = $server.Information.MasterDBPath + "\" + $smoRestore.Database + "_Data.mdf" 
    $smoRestoreLog.LogicalFileName = $smoRestoreDetails.Rows[0]["DatabaseName"] + "_Log" 
    $smoRestoreLog.PhysicalFileName = $server.Information.MasterDBPath + "\" + $smoRestore.Database + "_Log.ldf" 
    $smoRestore.RelocateFiles.Add($smoRestoreFile) 
    $smoRestore.RelocateFiles.Add($smoRestoreLog) 
} 

$smoRestore.SqlRestore($server) 
if($error.Count -eq 0){ 
} 
else{ 
    $Error[0].exception.message 
} 
+0

你的問題代碼,幫我解決了問題!謝謝:) – Gezim 2013-01-28 20:23:45

回答

11

我有一個非常類似的腳本來你的,有一些值得注意的區別:

  • 致電SqlRestore之前,我打電話給$server.KillAllProcesses($databaseName)
  • 我有$smoRestore.NoRecovery = $false,而不是$true
  • 我有$smoRestore.FileNumber = 1,你不必在所有。我認爲這對應於從GUI中的備份集檢查文件。

我也有設置邏輯/物理文件名類似的代碼,但不是使用$server.Information,我從註冊表(不知道這是「更好」)提取信息。另一個區別是我使用$smoRestore.ReadFileList而不是$smoRestore.ReadBackupHeader

您也可以嘗試使用上你的路幾Write-Host報表,以確保他們看的權利,如果您還沒有。

希望項目符號的調整的一個解決您的問題。讓我知道,如果你想從我的腳本更多的信息。

+0

對於任何未來的讀者,添加「$ smoRestore.FileNumber = 1」是爲我做的竅門。 – CIGuy 2013-10-02 15:27:49

+6

你也可以嘗試深入研究這個例外。嘗試使用: '$錯誤[0] .exception.GetBaseException()Message' – sakadas 2015-01-26 06:22:06

1

一位同事和我都有這個問題,經過一些故障排除後,我們發現關閉SQL Server Management Studio的確有竅門。

希望別人可以跳過我們所做的所有故障排除,這個簡單的解決方案將爲他們節省幾個小時。