다른 서버에서 데이터베이스 백업을 사용하고 있습니다. localhost의 sqlexpress로 복원하려고합니다. 이 복원은 GUI를 통해 작동하지만 PowerShell로 복원 할 때 문제가 있습니다. 이 라인의 문자 23smo 및 powershell을 사용하여 데이터베이스 백업을 새 데이터베이스로 복원하는 중 오류가 발생했습니다.
Exception calling "SqlRestore" with "1" argument(s): "Restore failed for Server
+ $smoRestore.SqlRestore <<<< ($server)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
오류 메시지 포인트 : 나는 다음과 같은 오류 메시지가
$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
}
귀하의 질문 코드는 제가 문제를 해결하는 데 도움이되었습니다! 고마워요 :) – Gezim