Blog Post

Azure Database Support Blog
2 MIN READ

How to Migrate Azure SQL Database to Azure SQL Managed Instance

Bashar-MSFT's avatar
Bashar-MSFT
Icon for Microsoft rankMicrosoft
Mar 14, 2019
First published on MSDN on Jan 28, 2019
The latest version of SQLPackage supports migration to Azure SQL Managed Instance (MI) from multiple SQL sources such as Azure SQL Database.

Export:

Following the below steps, you can export a SQL database using the SQLPackage command-line utility. If possible, use a workstation that has access to the Azure SQL Database and the MI to avoid having to move the exported bacpac to a workstation that has access to your destination MI.

  1. Download and run the DacFramework.msi installer for Windows.

  2. Open a new Command Prompt window, and run the following command
    cd C:\Program Files\Microsoft SQL Server\150\DAC\bin

  3. Run the following command to export the SQL database:
    sqlpackage.exe /a:Export /SourceServerName:servername.database.windows.net /SourceDatabaseName:dbname /SourceUser:username /SourcePassword:password /TargetFile:C:\Users\user\Desktop\backup150.bacpac


In the above command, we will export the database “dbname” from server “servername” to a local file named “backup150.bacpac”. You will want to adjust these values to match your setup.


Import:
If the environment that contains the exported bacpac file does not have access to the destination MI, move the exported bacpac file “backup150.bacpac” to an environment that can access your Azure SQL MI.

Following the below steps, you can import the bacpac into an Azure SQL Managed Instance using the same SQLPackage utility for export. If you have already done step 1 and 2 on your current environment from the export above, please skip to step 3 to run the import.

  1. Download and run the DacFramework.msi installer for Windows.

  2. Open a new Command Prompt window, and run the following command
    cd C:\Program Files\Microsoft SQL Server\150\DAC\bin

  3. Run the following command to import into the Managed Instance
    sqlpackage.exe /a:Import /TargetServerName:ManagedInstancename.appname.database.windows.net /TargetDatabaseName:dbname /TargetUser:username /TargetPassword:password /SourceFile:C:\Users\user\Desktop\backup150.bacpac





In the above command, we will import the database “dbname” into your destination MI “ManagedInstancename” from the bacpac named “backup150.bacpac”. You will want to adjust these values to match your setup.






Please note that older DACfx versions than “18.0” do not support migration to Azure SQL Managed Instance and to ensure latest compatibility, please make sure that you are using the latest version of SQLPackage. Using older version could cause errors such as listed below:

*** Error importing database:Could not import package.
Unable to connect to master or target server ‘database name’. You must have a user with the same password in master or target server.

Or

*** Error importing database:Could not import package.
Warning SQL0: A project which specifies Microsoft Azure SQL Database v12 as the target platform may experience compatibility issues with SQL Server 2014.
Error SQL72014: .Net SqlClient Data Provider: Msg 41918, Level 16, State 1, Line 2 Specifying files and filegroups in CREATE DATABASE statement is not supported on SQL Database Managed Instance.
Error SQL72045: Script execution error.  The executed script:
CREATE DATABASE [$(DatabaseName)]
ON
PRIMARY(NAME = [$(DatabaseName)], FILENAME = N'$(DefaultDataPath)$(DefaultFilePrefix)_Primary.mdf')
LOG ON (NAME = [$(DatabaseName)_log], FILENAME = N'$(DefaultLogPath)$(DefaultFilePrefix)_Primary.ldf') COLLATE SQL_Latin1_General_CP1_CI_AS;

Updated Mar 14, 2019
Version 2.0
  • I have a customer with a Azure SQL Database with more than 380K tables (55GB). The recommended migration process simply doesn't work for them.. after 6 days running (DTU's at 100%) the process just aborted. We need another solution like BKP/Restore from SQLAzure to SQL Managed instances to work with.

  • hiteshcpatel's avatar
    hiteshcpatel
    Copper Contributor

    Since Azure Managed Instance and Azure SQL Database are on the same platform, can't it support same backup/restore methods so it's simpler to move databases between both the deployment types?

    What happens in scenario where grew out of Azure SQL database and will need migrate out to Azure SQL Managed instance? The migration process for 100+ DBs with TB of storage is complicated with large downtime.

  • Guepester's avatar
    Guepester
    Copper Contributor

    The ability to use New-AzSqlDatabaseCopy / Create Database Y as Copy of Z would save loads of time. Copying a 1 TB production db to a Managed Instance in a Dev/Test subscription by Export/Import is not feasible. 

  • AdamAVC's avatar
    AdamAVC
    Copper Contributor

    I have a 750GB database in Azure SQL, that I need to move to managed instance.  Unfortunately after an export that took in excess of 30hours, the import failed because External Data Source objects.  Some of which I cannot remove from a production database for the duration of the export.

     

    I know that the SqlPackage.exe process has the ability to limit bacpac creation to a single object, what about an exclude parameter? Then I can generate a bacpac omitting the problem objects.

     

    Other then that, an internal Azure process to migrate from Azure SQL to Managed Instance would be perfect!  Is this in the pipeline of works?

  • AdamAVC 

    Hi,

     

    I think I have a solution that will satisfy you:

     

    SQLPACKAGE.EXE has a special parameter to specify an external model.xml file
    (https://docs.microsoft.com/en-us/sql/tools/sqlpackage/sqlpackage-import?view=sql-server-ver16)

     

    The .bacpac file, on the other hand, is actually a ZIP, which inside contains a script for creating database objects (model.xml) and data.

     

    All you need to do is open the .bacpac with 7zip, winrar or a similar program, extract the model.xml file from it and edit it removing external users, external data sources and external tables.

     

    Then use the sqlpackage.exe command more or less like this:

     

    PS C:\Program Files\Microsoft SQL Server\160\DAC\bin> .\sqlpackage.exe /a:Import '/sf:C:\Users\User\Downloads\DB.bacpac' /tsn:'ManagedInstanceServer.acbcc13fa309.database.windows.net' /tdn:'TargetDB' /tu:'ManagedInstanceUsername' /tp:ManagedInstancePassword /mfp:C:\CustomModel\model.xml

     

    Actually, the proper tool for migrating databases from Azure SQL Database to Azure Managed Instance is seriously missing.

     

    Exporting and importing a bacpac file with a 1.5TB database takes about 110h.

  • AdamAVC 

     

    If you edit the model.xml file directly in the bacpac file it has a checksum, so the import will fail. Using the /mfp parameter in sqlpackage.exe will bypass this validation

  • AdamAVC's avatar
    AdamAVC
    Copper Contributor

    The method I used was as per below, which worked.

    1. Export Data-tier Application (Advanced -> Unchecked Large tables)
    2. Import Data-tier Application into destination server
    3. Used SQL Package to export the large tables individually.
      1. SqlPackage.exe /Action:Extract /SourceDatabaseName:DBNAME /SourceServerName:SOURCE_SERVER /SourceUser:USER /SourcePassword:PASSWORD /TargetFile:F:\TABLE_NAME.dacpac /p:IgnoreExtendedProperties=True /p:ExtractAllTableData=FALSE /p:TableData=TABLE_NAME
    4. Used SQL Package to import the large table into the destination server
      1. SqlPackage.exe /a:publish /p:DropIndexesNotInSource=False /p:IgnoreDefaultSchema=True /sf:F:\TABLE_NAME.dacpac /tcs:"Data Source=SourceServerName:DEST_SERVER ;Initial Catalog=DEST_DB;User ID=UN;Password=PASSWORD;"

    This worked on a database of approximately 850GB.

  • AdamAVC's avatar
    AdamAVC
    Copper Contributor

    Thanks PiotrMazurkiewicz 

    I did try and modify the model.xml previously but left it within the .bacpac which failed to import.  I had no idea about using a model.xml external to the package, so I will give that a go!

  • ryanleestl's avatar
    ryanleestl
    Copper Contributor

    Has this ever been resolved? There currently seem to be no good path to migrate large (>50GB) databases, as all options simply take too long. We need a simple way to migrate a database from Azure SQL DB to Managed Instance, even if some offline operation is required.

  • ryanleestl's avatar
    ryanleestl
    Copper Contributor
    Thank you AdamAVC , i will look this over. Was this noticeably quicker than exporting/importing a BACPAC containing everything? Or just a solution to create recovery points in case of timeout/failure?