Delen via


PowerShell gebruiken om een BACPAC-bestand te importeren in een database in SQL Database

Van toepassing op:Azure SQL Database

In dit Voorbeeld van een Azure PowerShell-script wordt een database uit een BACPAC-bestand geïmporteerd in een nieuwe database in SQL Database.

Als u geen Azure-abonnement hebt, kunt u een gratis Azure-account maken voordat u begint.

Opmerking

In dit artikel wordt de Azure Az PowerShell-module gebruikt. Dit is de aanbevolen PowerShell-module voor interactie met Azure. Zie Azure PowerShell-installeren om aan de slag te gaan met de Az PowerShell-module. Om te leren hoe u naar de Az PowerShell-module kunt migreren, zie Migrate Azure PowerShell from AzureRM to Az.

Azure Cloud Shell gebruiken

Azure host Azure Cloud Shell, een interactieve shell-omgeving die u via uw browser kunt gebruiken. U kunt Bash of PowerShell met Cloud Shell gebruiken om te werken met Azure-services. U kunt de vooraf geïnstalleerde Cloud Shell-opdrachten gebruiken om de code in dit artikel uit te voeren zonder dat u iets hoeft te installeren in uw lokale omgeving.

Om Azure Cloud Shell op te starten:

Optie Voorbeeld/koppeling
Selecteer Nu proberen in de rechterbovenhoek van een codeblok. Als u Try It selecteert, wordt de code niet automatisch gekopieerd naar Cloud Shell. Schermopname met een voorbeeld van Try It for Azure Cloud Shell.
Ga naar https://shell.azure.com, of selecteer de knop Cloud Shell starten om Cloud Shell in uw browser te openen. Schermopname van het starten van Cloud Shell in een nieuw venster.
Klik op de knop Cloud Shell in het menu in de balk rechtsboven in de Azure-portal. Schermopname van de knop Cloud Shell in Azure Portal

Om de code in dit artikel in Azure Cloud Shell uit te voeren:

  1. Start Cloud Shell.

  2. Selecteer de knop Kopiëren op een codeblok om de code te kopiëren.

  3. Plak de code in de Cloud Shell-sessie door Ctrl+Shift+V- in Windows en Linux te selecteren of door Cmd+Shift+V- in macOS te selecteren.

  4. Selecteer Voer in om de code uit te voeren.

Als u PowerShell lokaal wilt installeren en gebruiken, is voor deze zelfstudie Az PowerShell 1.4.0 of hoger vereist. Zie Azure PowerShell-module installerenals u een upgrade wilt uitvoeren. Als u PowerShell lokaal uitvoert, moet u ook Connect-AzAccount uitvoeren om een verbinding met Azure te maken.

Voorbeeldscript

# Connect-AzAccount
# The SubscriptionId in which to create these objects
$SubscriptionId = ''
# Set the resource group name and location for your server
$resourceGroupName = "myResourceGroup-$(Get-Random)"
$location = "westeurope"
# Set an admin login and password for your server
$adminSqlLogin = "SqlAdmin"
$password = "ChangeYourAdminPassword1"
# Set server name - the logical server name has to be unique in the system
$serverName = "server-$(Get-Random)"
# The sample database name
$databaseName = "myImportedDatabase"
# The storage account name and storage container name
$storageAccountName = "sqlimport$(Get-Random)"
$storageContainerName = "importcontainer$(Get-Random)"
# BACPAC file name
$bacpacFilename = "sample.bacpac"
# The ip address range that you want to allow to access your server
$startip = "0.0.0.0"
$endip = "0.0.0.0"

# Set subscription 
Set-AzContext -SubscriptionId $subscriptionId 

# Create a resource group
$resourcegroup = New-AzResourceGroup -Name $resourceGroupName -Location $location

# Create a storage account 
$storageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroupName `
    -Name $storageAccountName `
    -Location $location `
    -SkuName "Standard_LRS"

# Create a storage container 
$storageContainer = New-AzStorageContainer -Name $storageContainerName `
    -Context $(New-AzStorageContext -StorageAccountName $storageAccountName `
        -StorageAccountKey $(Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName).Value[0])

# Download sample database from Github
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #required by Github
Invoke-WebRequest -Uri "https://github.com/Microsoft/sql-server-samples/releases/download/wide-world-importers-v1.0/WideWorldImporters-Standard.bacpac" -OutFile $bacpacfilename

# Upload sample database into storage container
Set-AzStorageBlobContent -Container $storagecontainername `
    -File $bacpacFilename `
    -Context $(New-AzStorageContext -StorageAccountName $storageAccountName `
        -StorageAccountKey $(Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName).Value[0])

# Create a new server with a system wide unique server name
$server = New-AzSqlServer -ResourceGroupName $resourceGroupName `
    -ServerName $serverName `
    -Location $location `
    -SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminSqlLogin, $(ConvertTo-SecureString -String $password -AsPlainText -Force))

# Create a server firewall rule that allows access from the specified IP range
$serverFirewallRule = New-AzSqlServerFirewallRule -ResourceGroupName $resourceGroupName `
    -ServerName $serverName `
    -FirewallRuleName "AllowedIPs" -StartIpAddress $startIp -EndIpAddress $endIp

# Import bacpac to database with an S3 performance level
$importRequest = New-AzSqlDatabaseImport -ResourceGroupName $resourceGroupName `
    -ServerName $serverName `
    -DatabaseName $databaseName `
    -DatabaseMaxSizeBytes 100GB `
    -StorageKeyType "StorageAccessKey" `
    -StorageKey $(Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -StorageAccountName $storageAccountName).Value[0] `
    -StorageUri "https://$storageaccountname.blob.core.windows.net/$storageContainerName/$bacpacFilename" `
    -Edition "Standard" `
    -ServiceObjectiveName "S3" `
    -AdministratorLogin "$adminSqlLogin" `
    -AdministratorLoginPassword $(ConvertTo-SecureString -String $password -AsPlainText -Force)

# Check import status and wait for the import to complete
$importStatus = Get-AzSqlDatabaseImportExportStatus -OperationStatusLink $importRequest.OperationStatusLink
[Console]::Write("Importing")
while ($importStatus.Status -eq "InProgress")
{
    $importStatus = Get-AzSqlDatabaseImportExportStatus -OperationStatusLink $importRequest.OperationStatusLink
    [Console]::Write(".")
    Start-Sleep -s 10
}
[Console]::WriteLine("")
$importStatus

# Scale down to S0 after import is complete
Set-AzSqlDatabase -ResourceGroupName $resourceGroupName `
    -ServerName $serverName `
    -DatabaseName $databaseName  `
    -Edition "Standard" `
    -RequestedServiceObjectiveName "S0"

# Clean up deployment 
# Remove-AzResourceGroup -ResourceGroupName $resourceGroupName

Opschoning van de implementatie

Gebruik de volgende opdracht om de resourcegroep en alle bijbehorende resources te verwijderen.

Remove-AzResourceGroup -ResourceGroupName $resourcegroupname

Uitleg van script

Dit script gebruikt de volgende commando's. Elke opdracht in de tabel linkt naar documentatie die specifiek is voor die opdracht.

Opdracht Opmerkingen
New-AzResourceGroup Hiermee maakt u een resourcegroep waarin alle resources worden opgeslagen.
New-AzSqlServer Hiermee maakt u een server die als host fungeert voor databases en elastische pools.
New-AzSqlServerFirewallRule Hiermee maakt u een firewallregel op serverniveau voor een server.
New-AzSqlDatabaseImport Hiermee importeert u een BACPAC-bestand en maakt u een nieuwe database op de server.
Remove-AzResourceGroup Verwijdert een resourcegroep, inclusief alle onderliggende resources.