{ "metadata": { "kernelspec": { "name": "powershell", "display_name": "PowerShell", "language": "powershell" }, "language_info": { "name": "powershell", "codemirror_mode": "shell", "mimetype": "text/x-sh", "file_extension": ".ps1" } }, "nbformat_minor": 2, "nbformat": 4, "cells": [ { "cell_type": "markdown", "source": [ "# Migrate SQL Server Database to Azure SQL DB\n", "\n", "## Overview\n", "\n", "Use this notebook when attempting to migrate a SQL Server database to Azure SQL Database. The process is different than using an Azure SQL Virtual Machine or Azure SQL Managed Instance. Refer to this [Microsoft docs](https://docs.microsoft.com/en-us/azure/dms/howto-sql-server-to-azure-sql-powershell) article for the method described.\n", "\n", "## Variables\n", "\n", "Use the table below as reference when configuring this notebook for execution.\n", "\n", "| Line | Variable | Description | Example |\n", "| --- | --- | --- | --- |\n", "| 1 | Subscription | Azure subscription name or ID | guid |\n", "| 2 | dmsName | Name of the Data Migration Service | MyDms |\n", "| 3 | dmsResourceGroup | Exact name of resource group to target or create | _MyAz\\_RG_ |\n", "| 4 | loc | Specifies the location of the service | EastUS2 |\n", "| 5 | vNetName | Specify the name of the vnet Azure resource to use | vnet1 |\n", "| 6 | Subnet | Name of the Azure subnet resource to use | subnet1 |\n", "| 7-8 | dmsSku | This parameter corresponds to DMS Sku name. The currently supported sku is listed. | GeneralPurpose\\_4vCores |\n", "| 9 | TargetSqlServer | Target SQL server to migrate to | \"sqllazuretarget.database.windows.net\" |\n", "| 10 | SourceDb | Name of database to migrate from, the _source_ | AdventureWorks2016 |\n", "| 11 | TargetDb | Name of database to migrate to, in case it differs from the source, the _target_ | AdventureWorks2016 |\n", "\n", "Lastly, specify the table names explicitly as a PowerShell list of names:\n", "\n", "> `$Tables = @(`\"HumanResources.Department\",  \"HumanResources.Employee\",  \"HumanResources.EmployeeDepartmentHistory\")" ], "metadata": { "azdata_cell_guid": "f706da59-22c3-4317-bf41-c00dde794097" } }, { "cell_type": "code", "source": [ "$Subscription = \"\"\r\n", "$dmsName = \"\"\r\n", "$dmsResourceGroup = \"\"\r\n", "$loc = \"\"\r\n", "$vNetName = \"\"\r\n", "$Subnet = Subnet1\r\n", "$dmsSku = GeneralPurpose_4vCores \r\n", "$TargetSqlServer = \"\"\r\n", "$SourceDb = \"\"\r\n", "$TargetDb = \"\"\r\n", "\r\n", "# Define a list of tables to migrate\r\n", "$Tables = @()\r\n", "<# :::For example::: \r\n", "$Tables = @(\"HumanResources.Department\", `\r\n", " \"HumanResources.Employee\", `\r\n", " \"HumanResources.EmployeeDepartmentHistory\", `\r\n", " \"HumanResources.EmployeePayHistory\", `\r\n", " \"HumanResources.JobCandidate\", `\r\n", " \"HumanResources.Shift\")\r\n", "#>" ], "metadata": { "azdata_cell_guid": "2624afc0-2403-4d42-ad88-6adcfe1a5c2b", "tags": [ "parameters" ] }, "outputs": [], "execution_count": null }, { "cell_type": "markdown", "source": [ "## Connections" ], "metadata": { "azdata_cell_guid": "f0a5e313-a2a6-4e75-ab94-5d96436b8473" } }, { "cell_type": "code", "source": [ "# Configure virtual network\r\n", "$vNet = Get-AzVirtualNetwork -ResourceGroupName $dmsResourceGroup -Name $vNetName\r\n", "\r\n", "$vSubNet = Get-AzVirtualNetworkSubnetConfig -VirtualNetwork $vNet -Name $Subnet\r\n", "\r\n", "# Create a Database Connection Info object for the source connections\r\n", "$sourceConnInfo = New-AzDmsConnInfo -ServerType SQL `\r\n", " -DataSource $SourceSqlServer `\r\n", " -AuthType SqlAuthentication `\r\n", " -TrustServerCertificate:$true\r\n", "\r\n", "# target connection\r\n", "$targetConnInfo = New-AzDmsConnInfo -ServerType SQL `\r\n", " -DataSource $TargetSqlServer `\r\n", " -AuthType SqlAuthentication `\r\n", " -TrustServerCertificate:$false" ], "metadata": { "azdata_cell_guid": "dbf40b4b-af83-46c6-9db1-15f45eb24382" }, "outputs": [], "execution_count": null }, { "cell_type": "markdown", "source": [ "## Select Data by Defining Database and Table Mapping" ], "metadata": { "azdata_cell_guid": "b9113be6-0aef-41fd-afe8-e1ce51c5999b" } }, { "cell_type": "code", "source": [ "# Create a list of databases to migrate\r\n", "$dbInfo1 = New-AzDataMigrationDatabaseInfo -SourceDatabaseName $SourceDb\r\n", "$dbList = @($dbInfo1)\r\n", "\r\n", "# Create a table map \r\n", "$tableMap = New-Object 'system.collections.generic.dictionary[string,string]'\r\n", "foreach ($table in $Tables)\r\n", "{\r\n", " $tableMap.Add($table,$table)\r\n", "}\r\n", "\r\n", "# Select the data\r\n", "$selectedDbs = New-AzDmsSelectedDB -MigrateSqlServerSqlDb -Name $SourceDb `\r\n", " -TargetDatabaseName $TargetDb `\r\n", " -TableMap $tableMap" ], "metadata": { "azdata_cell_guid": "3b6551e9-4d42-441e-8fe1-f247471995df" }, "outputs": [], "execution_count": null }, { "cell_type": "markdown", "source": [ "## Create Service Project" ], "metadata": { "azdata_cell_guid": "a9abeeb9-4f45-4e0c-8b1b-20270dddece4" } }, { "cell_type": "code", "source": [ "$service = New-AzDms -ResourceGroupName $dmsResourceGroup `\r\n", " -ServiceName $dmsName `\r\n", " -Location $loc `\r\n", " -Sku $dmsSku `\r\n", " -VirtualSubnetId $vSubNet.Id\r\n", "\r\n", "# Create a project object\r\n", "$project = New-AzDataMigrationProject -ResourceGroupName $dmsResourceGroup `\r\n", " -ServiceName $service.Name `\r\n", " -ProjectName $dmsNameProject `\r\n", " -Location $loc `\r\n", " -SourceType SQL `\r\n", " -TargetType SQLDB `\r\n", " -SourceConnection $sourceConnInfo `\r\n", " -TargetConnection $targetConnInfo `\r\n", " -DatabaseInfo $dbList" ], "metadata": { "azdata_cell_guid": "77415bea-b098-4a9f-a166-9751e4261fc1" }, "outputs": [], "execution_count": null }, { "cell_type": "markdown", "source": [ "## Credentials for Source and Target" ], "metadata": { "azdata_cell_guid": "f8c27b61-d2c9-43ee-bc93-8182752edb33" } }, { "cell_type": "code", "source": [ "$secpasswd = ConvertTo-SecureString -String $sourcePassword -AsPlainText -Force\r\n", "$sourceCred = New-Object System.Management.Automation.PSCredential ($sourceUserName, $secpasswd)\r\n", "$secpasswd = ConvertTo-SecureString -String $targetPassword -AsPlainText -Force\r\n", "$targetCred = New-Object System.Management.Automation.PSCredential ($targetUserName, $secpasswd)" ], "metadata": { "azdata_cell_guid": "61aa72d1-7492-41e8-847b-89a76ca28e0f" }, "outputs": [], "execution_count": null }, { "cell_type": "markdown", "source": [ "## Create the Migration Task\r\n", "Uncomment lines to perform validation checks during the task." ], "metadata": { "azdata_cell_guid": "681feaa0-f7d8-4322-af0c-75ab6402fd6e" } }, { "cell_type": "code", "source": [ "$migTask = New-AzDataMigrationTask -TaskType MigrateSqlServerSqlDb `\r\n", " -ResourceGroupName $dmsResourceGroup `\r\n", " -ServiceName $service.Name `\r\n", " -ProjectName $project.Name `\r\n", " -TaskName $dmsNameTask `\r\n", " -SourceConnection $sourceConnInfo `\r\n", " -SourceCred $sourceCred `\r\n", " -TargetConnection $targetConnInfo `\r\n", " -TargetCred $targetCred `\r\n", " -SelectedDatabase $selectedDbs `\r\n", "# -SchemaValidation `\r\n", "# -DataIntegrityValidation `\r\n", "# -QueryAnalysisValidation `" ], "metadata": { "azdata_cell_guid": "4dc629cc-1978-4bf8-80fc-69de093ac435" }, "outputs": [], "execution_count": null }, { "cell_type": "markdown", "source": [ "## Monitor the Migration Task" ], "metadata": { "azdata_cell_guid": "9074e9e5-f5f7-4d30-a4ec-b6d3f112a600" } }, { "cell_type": "code", "source": [ "# Monitor the migration\r\n", "if (($migTask.ProjectTask.Properties.State -eq \"Running\") -or ($migTask.ProjectTask.Properties.State -eq \"Queued\"))\r\n", "{\r\n", " write-host \"migration task running\"\r\n", "}\r\n", "else \r\n", "{\r\n", " Write-Host \"migration task is $migTask.ProjectTask.Properties.State\"\r\n", "}" ], "metadata": { "azdata_cell_guid": "89af76b7-1801-4d26-b6cd-907a9b455839" }, "outputs": [], "execution_count": null }, { "cell_type": "markdown", "source": [ "## Deleting the DMS Instance\r\n", "This should remove the DMS instance from the Resource Group. Uncomment to perform the task. " ], "metadata": { "azdata_cell_guid": "ed3bf1e7-a92f-4985-b89c-85f45b8e6821" } }, { "cell_type": "code", "source": [ "#Remove-AzDms -ResourceGroupName $dmsResourceGroup -ServiceName $dmsName" ], "metadata": { "azdata_cell_guid": "245b4d4e-7559-4534-bff8-3af390ac1e3a" }, "outputs": [], "execution_count": null } ] }