数据执行计划在数据库恢复时出错

时间:2015-06-08 14:31:21

标签: powershell azure azure-sql-database database-restore

我试图在Power Shell上执行一个脚本,该脚本必须在一台Azure服务器上备份数据库,然后在另一台服务器(总是Azure)中恢复它。脚本出错2点:

  1. 当我针对现有数据库执行脚本时,它在恢复之前不会删除(或终止)数据库,即使存在应该执行此操作的过程。
  2. 当我针对新创建的数据库执行脚本时,我收到此错误:
  3.   

    powershell:使用“2”参数调用“ImportBacpac”的异常:   “数据计划执行在线:1个字符:1   + powershell -ExecutionPolicy ByPass -File C:\ Untitled1.ps1 -noexit   + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~       + CategoryInfo:NotSpecified :(异常calli ...计划执行:字符串)[],RemoteException       + FullyQualifiedErrorId:NativeCommandError因消息失败发生了一个或多个错误。“在C:\ Untitled1.ps1:48 char:5   + $ Services.ImportBacpac($ Package,$ restoreToDatabaseName)   + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~       + CategoryInfo:NotSpecified:(:) [],MethodInvocationException       + FullyQualifiedErrorId:DataException

    我使用的代码如下:

    [string]$azureConnectionString = "Server=myserver.database.windows.net; User ID=myUser; Password=MyPWD; Trusted_Connection=False; Encrypt=True; Connection Timeout=30;"
    [string]$azureDatabaseName = "SourceDb"
    [string]$bacpacFileDropLocation = "path where to create the bacpac file"
    [string]$restoreToServer = 'secondserver'
    [string]$restoreToDatabaseName="DestinationDb"
    [string]$restoreToConnectionString = "Server=secondserver.database.windows.net,1433; User ID=User; Password=Pwd; Trusted_Connection=False; Encrypt=True; Connection Timeout=30;"
    [string]$SqlInstallationFolder = "C:\Program Files (x86)\Microsoft SQL Server"
    Write-Host "Backing up Azure database '$azureDatabaseName' and restoring to database '$restoreToDatabaseName'"
    
    $Watch = New-Object System.Diagnostics.StopWatch
    $Watch.Start()
    $DacAssembly = "$SqlInstallationFolder\120\DAC\bin\Microsoft.SqlServer.Dac.dll"
    $smo= "C:\Program Files (x86)\Microsoft SQL Server\120\SDK\Assemblies\Microsoft.SqlServer.Smo.dll"
    Add-Type -Path $DacAssembly
    Add-Type -Path $smo
    $Services = new-object Microsoft.SqlServer.Dac.DacServices $azureConnectionString
    
    if ($Services -eq $null)
    { 
        Write-Host "Failed to load Sql Server Dac Services"
        exit
    }
    
    $Services.ExportBacpac($bacpacFileDropLocation, $azureDatabaseName)
    
    if (test-path($bacpacFileDropLocation))
    {      
        [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null
        $SMOserver = New-Object ('Microsoft.SqlServer.Management.Smo.Server') -argumentlist $restoreToServer
        Write-Host "$SMOserver.Databases[$restoreToDatabaseName]"
        Write-Host "$restoreToDatabaseName"
        if  ($SMOserver.Databases[$restoreToDatabaseName] -ne $null) 
        {  
           # $SMOserver.Databases[$restoreToDatabaseName].Drop()  
           $SMOserver.KillDatabase($restoreToDatabaseName)
       } 
        else {Write-Host "Database not dropped"
        }
    
        $Services = new-object Microsoft.SqlServer.Dac.DacServices $restoreToConnectionString
        Write-Host $Services
        if ($Services -eq $null)
        {
            exit
        }
    
        $Package =  [Microsoft.SqlServer.Dac.BacPackage]::Load($bacpacFileDropLocation)
        $Services.ImportBacpac($Package, $restoreToDatabaseName)
        $Package.Dispose()
        $Watch.Stop();
        Write-Host "Azure backup and local restore completed in "$Watch.Elapsed.ToString()
    }
    else
    {
        Write-Host "Back up has failed!"
    }
    

    合乎逻辑的是,当我在新的Db上执行脚本时,会显示“Database not dropped”消息,但是当我针对现有Db执行它时它也会显示。也许y脚本有什么问题吗?我是新手,所以我更难调试这样的代码

0 个答案:

没有答案