2012-09-24 3 views
4

온 - 프레미스 SQL Server 2012의 데이터베이스를 SQL Azure로 복사하는 연속 통합 MSBuild 스크립트를 만듭니다.BACPAC (연속 통합 용)을 통해 SQL Azure에 SQL Server 2012 데이터베이스 복사

쉬운 권리?

방법 연구의 공정한 조금 나는 다음과 같은 방법 건너 한 후에

는 :

  1. Use PowerShell to access the DAC library directly, 다음 MSBuild PowerShell extension to wrap the script를 사용합니다. PowerShell 3을 설치하고 MSBuild PowerShell 확장을 라이브러리의 최신 버전에서 다른 네임 스페이스로 apparently MS moved the DAC API과 같이 작동시키는 방법을 알아 내야합니다. PowerShell은 API에 직접 액세스 할 수 있지만 상당 부분의 상용구가 필요할 수 있습니다.

  2. Use the sample DAC Framework Client Side Tools이 코드는 Codeplex에서 다운로드 할 수있는 코드가 Hosted version이므로 다운로드해야합니다. 또한 이전 버전의 DAC를 사용하는 것처럼 DAC 3.0 클래스를 사용하도록 수정해야합니다. 그런 다음 MSBuild 스크립트의 <Exec Command="" />에서이 도구를 호출 할 수 있습니다. 보일러 플레이트가 적어지고 도로에서 충돌이 일어나면 소스를 변경할 수 있습니다.

은 어느 방법을 사용하여

처리, 프로세스가 될 수 중 하나를 온 - 프레미스 SQL 서버 2012 지역 BACPAC에

  1. 수출
  2. 업로드 BACPAC 저장
  3. 블로그에
  4. BACPAC을 SQL Azure로 가져 오기 Hosted DAC
  5. 통해 가져 오기

또는 : Client DAC

질문

의 모든 통해 온 - 프레미스 SQL 서버 2012 SQL 애저 로컬 BACPAC

  • 가져 오기 BACPAC에

    1. 수출 위의 표준 기능을 것 같다 뭔가에 대한 노력이 꽤 많은 것 같습니다 ... 그래서 전에 내가 st 휠을 재발 명하고 모든 사람들이 볼 수있는 결과를 문서화하면 내가 여기에 놓친 것이 정말 분명합니다. MS가 아직 공개하지 않은 스크립트를 미리 작성 했습니까?

      SQL Server Management Studio 2012의 GUI에는 로컬 데이터베이스를 마우스 오른쪽 단추로 클릭하고 "작업"을 클릭하고 "데이터베이스를 SQL Azure로 배포"를 클릭하는 명령이 있습니다. GUI에서 몇 번의 클릭 만한다면 명령 행에서 단일 명령이어야합니다.

  • 답변

    1

    그래서 PowerShell을 사용하기로 결정했습니다 (실제로는 스크립트가 많기 때문에) Client DAC 만 사용하십시오. 예제 here은 매우 유용했습니다.

    MSBuild Extension Pack을 사용하면 PowerShell 표준어를 래핑하는 작업을 만들 수 있습니다.

    DAC 3.0에 액세스하려면 PowerShell 3을 설치해야하며 Windows 관리 프레임 워크 3.0을 설치하는 것이 가장 좋습니다.

    TaskFactory 래퍼 다음과 같이 다음과 같이 보일 것이다 이러한 래퍼를 호출 할

    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
    
        <!-- This custom task encapsulates DAC Framework 3.0 BACPAC import and export routines, 
         which are compatible with SQL Server 2012 and later, and SQL Azure --> 
    
        <!-- Required Import to use MSBuild Extension Pack --> 
        <PropertyGroup> 
        <AssemblyFile>$(MSBuildExtensionsPath)\ExtensionPack\4.0\MSBuild.ExtensionPack.TaskFactory.PowerShell.dll</AssemblyFile> 
        <SqlServerDacDll>C:\Program Files (x86)\Microsoft SQL Server\110\DAC\bin\Microsoft.SqlServer.Dac.dll</SqlServerDacDll> 
        </PropertyGroup> 
    
        <UsingTask TaskFactory="PowershellTaskFactory" TaskName="ExportBacpac" AssemblyFile="$(AssemblyFile)"> 
        <ParameterGroup> 
         <ConnectionString Required="true" ParameterType="System.String" /> 
         <BacpacFile Required="true" ParameterType="System.String" /> 
         <DatabaseName Required="true" ParameterType="System.String" /> 
        </ParameterGroup> 
        <Task> 
         <![CDATA[ 
          #write progress to activity log 
          $log.LogMessage([Microsoft.Build.Framework.MessageImportance]"High","Starting export of database '$databasename' to '$bacpacfile' with connection string '$connectionstring' ") 
    
          # load in DAC DLL (requires config file to support .NET 4.0) 
          # change file location for a 32-bit OS 
          add-type -path "$(SqlServerDacDll)" 
    
          # make DacServices object, needs a connection string 
          $d = new-object Microsoft.SqlServer.Dac.DacServices $connectionstring 
    
          # register events, if you want 'em 
          register-objectevent -in $d -eventname Message -source "msg" -action { $log.LogMessage([Microsoft.Build.Framework.MessageImportance]"High", $Event.SourceArgs[1].Message.Message) } 
    
          # Export schema and data from database $databasename 
          $d.exportbacpac($bacpacfile, $databasename) 
    
          # clean up event 
          unregister-event -source "msg" 
    
          $log.LogMessage([Microsoft.Build.Framework.MessageImportance]"High","Completed export of database '$databasename' to '$bacpacfile'") 
         ]]> 
        </Task> 
        </UsingTask> 
    
        <UsingTask TaskFactory="PowershellTaskFactory" TaskName="ImportBacpac" AssemblyFile="$(AssemblyFile)"> 
        <ParameterGroup> 
         <ConnectionString Required="true" ParameterType="System.String" /> 
         <BacpacFile Required="true" ParameterType="System.String" /> 
         <DatabaseName Required="true" ParameterType="System.String" /><!-- Not relevant for Azure import, which uses the Bacpac file name as the database name --> 
        </ParameterGroup> 
        <Task> 
         <![CDATA[ 
          #write progress to activity log 
          $log.LogMessage([Microsoft.Build.Framework.MessageImportance]"High","Starting import of database '$databasename' from '$bacpacfile' with connection string '$connectionstring' ") 
    
          # load in DAC DLL (requires config file to support .NET 4.0) 
          # change file location for a 32-bit OS 
          add-type -path "$(SqlServerDacDll)" 
    
          # make DacServices object, needs a connection string 
          $d = new-object Microsoft.SqlServer.Dac.DacServices $connectionstring 
    
          # register events, if you want 'em 
          register-objectevent -in $d -eventname Message -source "msg" -action { $log.LogMessage([Microsoft.Build.Framework.MessageImportance]"High", $Event.SourceArgs[1].Message.Message) } 
    
          # Load bacpac from file & import to database named $databasename 
          $bp = [Microsoft.SqlServer.Dac.BacPackage]::Load($bacpacfile) 
          $d.importbacpac($bp, $databasename) 
    
          # clean up event 
          unregister-event -source "msg" 
    
          $log.LogMessage([Microsoft.Build.Framework.MessageImportance]"High","Completed import of database '$databasename' from '$bacpacfile'") 
         ]]> 
        </Task> 
        </UsingTask> 
    
    </Project> 
    

    샘플 대상 :

    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
        <Import Project="BacpacImportExport.xml"/> 
    
        <PropertyGroup> 
        <TempBacpacFile>$(ReleaseFolderPublish)\$(DestinationDBName).bacpac</TempBacpacFile> 
        </PropertyGroup> 
    
        <Target Name="CopyAndReplaceDatabaseViaBacpac"> 
        <Message Text="Clean bacpac directory"/> 
        <Exec Command="mkdir $(ReleaseFolderPublish)\" IgnoreExitCode="true"></Exec> 
        <Exec Command="del /Q $(ReleaseFolderPublish)\*.bacpac " IgnoreExitCode="true"></Exec> 
    
        <MSBuild Projects="$(MSBuildProjectFile)" Targets="ReportBuildProgress" Properties="Message=Exporting database to BACPAC from source"/> 
        <ExportBacpac 
         ConnectionString="$(SourceConnectionString)" 
         BacpacFile="$(TempBacpacFile)" 
         DatabaseName="$(SourceDBName)" 
        /> 
    
    
        <MSBuild Projects="$(MSBuildProjectFile)" Targets="ReportBuildProgress" Properties="Message=Dropping database from destination (does not fail on error)"/> 
        <MSBuild.ExtensionPack.SqlServer.SqlCmd TaskAction="Execute" Server="$(DestinationDBServer)" Database="master" 
                  LogOn="$(DestinationDBUser)" Password="$(DestinationDBPass)" 
                  CommandLineQuery="DROP DATABASE [$(DestinationDBName)];" 
                  RedirectStandardError="true" SeverityLevel="1" /> 
    
    
        <MSBuild Projects="$(MSBuildProjectFile)" Targets="ReportBuildProgress" Properties="Message=Importing database from BACPAC to destination"/> 
        <ImportBacpac 
         ConnectionString="$(DestinationConnectionString)" 
         BacpacFile="$(TempBacpacFile)" 
         DatabaseName="$(DestinationDBName)" 
        /> 
    
        </Target> 
    </Project> 
    

    이 쉽게를 통해 참조 구현 console app를 호출하여 Hosted DAC를 사용하도록 수정 될 수있다 <ImportBacpac /> 대신 <Exec Command="" />을 호출하십시오.

    개선 사항이 있으면 알려주세요.