-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure-sql-automated-migration.ps1
117 lines (92 loc) · 4.6 KB
/
azure-sql-automated-migration.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Notes
# ------------------------------------------------------------------------------------------
# This script connects to a local instance, enumerates the databases and migrates them
# to Azure. This script automates the process of extracting a bacpac, uploading the
# bacpac to azure blob storage, and restoring the bacpac to an Azure SQL database.
#
# This script presently makes a few assumptions that are easy to addres.
# 1. The databases you want to migrate are on the local instance.
# 2. You want to migrate all the databases on the instance.
# 3. You want to migrage all the databases to the same azure resoure group.
# ------------------------------------------------------------------------------------------
#local variables
$sourceServer = "localhost"
$tempLocation = "c:\Projects"
#Define the path to the sqlpackage.exe
$cmd = "C:\Program Files (x86)\Microsoft SQL Server\130\DAC\bin\sqlpackage.exe"
#azure variables
$resourceGroupName = "[resource group]"
$storageAccount = "[storage account]"
$storageContainer = "[storage container]"
$StorageKey = "[storage key]"
#base path for SqlPS databases.
$root = "SQLSERVER:\SQL\$sourceServer\DEFAULT\Databases"
Set-Location $root
$subscriptionId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
Set-AzureRmContext -SubscriptionId $subscriptionId
#Enumerate the databases on the server
foreach ($Item in Get-ChildItem)
{
#Retreive the size of the database so we know how to size the SQL Azure DB.
$dbName = $Item.Name
Set-Location "$root\$name"
$dbSize = $Item.Size
Write-Host $dbName
#Call function to extract the database
ExtractBacpac -server $sourceServer -database $dbName -size $dbSize -account $storageAccount -key $storageKey -container $storageContainer
#define the URI needed for the restore step
$uri = "http://$storageAccount.blob.core.windows.net/$storageContainer/$dbName.bacpac"
#call function to restore the database
MigrateDatabase -storageUri $uri -key $storageKey -resourceGroup $resourceGroupName -database $dbName -size $dbSize
}
function ExtractBacpac
{
param([string]$server, [string]$database, [int]$size, [string]$account, [string]$key, [string]$container)
Set-Location c:
$fileName = "$database.bacpac"
$localPath = "$tempLocation\$fileName"
$params = "/Action:Export /ssn:" + $server + " /sdn:" + $database + " /tf:" + $localPath
$p = $params.Split(" ")
#shell out the export command for the bacpac
& "$cmd" $p
#Set storage context and upload blob file
$context = New-AzureStorageContext -StorageAccountName $account -StorageAccountKey $key
Set-AzureStorageBlobContent -File $localpath -Container $container -Context $context
}
function MigrateDatabase
{
param([string]$storageUri, [string]$key, [string]$resourceGroup, [string]$database, [int]$size)
Write-Host $size
$serverName = "[destination server]"
$credential = Get-Credential
#set the service objective baseline based on the size of the database
If ($size -gt 250)
{
$edition = "Premium"
$objective = "p1"
}
else
{
$edition = "Standard"
$objective = "s0"
}
$importRequest = New-AzureRmSqlDatabaseImport –ResourceGroupName $resourceGroup `
–ServerName $serverName `
–DatabaseName $database `
–StorageKeyType "StorageAccessKey" `
–StorageKey $key `
-StorageUri $storageUri `
–AdministratorLogin $credential.UserName `
–AdministratorLoginPassword $credential.Password `
–Edition $edition `
–ServiceObjectiveName $objective `
-DatabaseMaxSizeBytes $size
Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink $importRequest.OperationStatusLink
# The DatabaseImport command is async so check status of the export every 10 seconds
do
{
$status = Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink $importRequest.OperationStatusLink
Write-Host $status.StatusMessage
Start-Sleep -s 10
} while($status.Status -eq "InProgress")
}