scl*_*son 9 sql sql-server powershell smo restore
从另一台服务器获取数据库备份我试图在localhost上恢复到sqlexpress.这个恢复将通过gui工作,但我有问题用PowerShell恢复它.我收到以下错误消息:
Exception calling "SqlRestore" with "1" argument(s): "Restore failed for Server
+ $smoRestore.SqlRestore <<<< ($server)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Run Code Online (Sandbox Code Playgroud)
错误消息指向此行的字符23:
$smoRestore.SqlRestore($server)
Run Code Online (Sandbox Code Playgroud)
脚本:
[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
}
Run Code Online (Sandbox Code Playgroud)
G-M*_*Mac 11
我有一个非常类似的脚本,有一些值得注意的差异:
SqlRestore,我打电话给$server.KillAllProcesses($databaseName).$smoRestore.NoRecovery = $false,而不是$true$smoRestore.FileNumber = 1,你根本没有.我认为这对应于从GUI中的备份集中检查文件.我也有类似的设置逻辑/物理文件名的代码,但$server.Information我没有使用,我从注册表中提取信息(不确定哪个是"更好").另一个不同之处在于我用$smoRestore.ReadFileList而不是$smoRestore.ReadBackupHeader.
您也可以尝试Write-Host在路径上使用一些语句,以确保它们看起来正确,如果您还没有.
希望其中一个项目符号调整可以解决您的问题.如果您想从我的脚本中获取更多信息,请告诉我.
| 归档时间: |
|
| 查看次数: |
9835 次 |
| 最近记录: |