如何使用脚本将 Azure SQL 托管实例数据库导出到 Azure 存储?

问题描述 投票:0回答:1

我想将一个SQL托管实例的多个TDE加密数据库一一导出到Azure的存储中。我可以使用SSMS的GUI进行操作。但如果要手工将数百个数据库一一完成,会非常耗时。相反,如果可能的话,我想使用脚本来完成此操作。我无法使用 T-SQL 执行此操作,因为数据库是 TDE 加密的。所以我不能使用这里的解决方案:https://stackoverflow.com/a/60368759/2402577

为此,chatgpt 指导我在 powershell 中使用

Start-AzSqlInstanceDatabaseExport

示例:

# Select Azure Subscription
Select-AzSubscription -SubscriptionId "..."

# Define variables
$resourceGroup = "my-rg"
$instanceName = "my-prodmssql"
$databaseName = "AllotmentTest"
$storageAccountName = "alpertestbackup"
$containerName = "sql"
$bacpacName = "AllotmentTest.bacpac"

# Get storage account key
$storageAccountKey = (Get-AzStorageAccountKey -ResourceGroupName $resourceGroup -AccountName $storageAccountName).Value[0]
$secureStorageAccountKey = ConvertTo-SecureString -String $storageAccountKey -AsPlainText -Force

# Export database to BACPAC file in Azure Storage
Start-AzSqlInstanceDatabaseExport -ResourceGroupName $resourceGroup `
                                  -ManagedInstanceName $instanceName `
                                  -Name $databaseName `
                                  -StorageKeyType "StorageAccessKey" `
                                  -StorageKey $secureStorageAccountKey `
                                  -StorageUri "https://$storageAccountName.blob.core.windows.net/$containerName/$bacpacName"

我收到以下错误消息:

Start-AzSqlInstanceDatabaseExport : The term 'Start-AzSqlInstanceDatabaseExport' is not recognized as the name of a cmd
let, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that
 the path is correct and try again.
At line:20 char:1
+ Start-AzSqlInstanceDatabaseExport -ResourceGroupName $resourceGroup `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Start-AzSqlInstanceDatabaseExport:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

有没有办法使用脚本而不是使用 SSMS 的 GUI 来解决这个问题?


这里 https://learn.microsoft.com/en-us/azure/azure-sql/database/database-export?view=azuresql#sqlpackage-utility

SQLPackage
推荐。

我们建议在大多数生产环境中使用 SQLPackage 实用程序来实现规模和性能。

但是我找不到任何与

SQLPackage
用法相关的示例/教程。

azure azure-sql-managed-instance sqlpackage
1个回答
0
投票

ChatGPT 喜欢发明不存在的 cmdlet、库和类。正确的 PowerShell cmdlet 是

New-AzSqlDatabaseExport

这仅适用于 Azure SQL

这是单个数据库使用的示例:

# Variables
$resourceGroupName = "YourResourceGroup"
$serverName = "YourSqlServerName"
$databaseName = "YourDatabaseName"
$storageAccountName = "YourStorageAccountName"
$storageContainerName = "YourContainerName"
$bacpacFilename = "YourDatabase.bacpac"
$storageKey = "YourStorageAccountKey"

# Get the storage account context
$storageContext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageKey

# Export the database to a .bacpac file in the storage account
New-AzSqlDatabaseExport -ResourceGroupName $resourceGroupName `
                        -ServerName $serverName `
                        -DatabaseName $databaseName `
                        -StorageKeytype "StorageAccessKey" `
                        -StorageKey $storageKey `
                        -StorageUri "$($storageContext.BlobEndPoint)$storageContainerName/$bacpacFilename"

https://learn.microsoft.com/en-us/powershell/module/az.sql/new-azsqldatabaseexport?view=azps-11.5.0

要对 ALL 数据库执行此操作,您需要将数据库名称列出到数组中并迭代它们,如下所示。

# Variables
$resourceGroupName = "YourResourceGroup"
$serverName = "YourSqlServerName"
$storageAccountName = "YourStorageAccountName"
$storageContainerName = "YourContainerName"
$storageKey = "YourStorageAccountKey"

# Get the storage account context
$storageContext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageKey

# Get all database names from the SQL Server
$databases = Get-AzSqlDatabase -ResourceGroupName $resourceGroupName -ServerName $serverName

# Iterate through each database and export to .bacpac file
foreach ($db in $databases) {
    $databaseName = $db.DatabaseName
    $bacpacFilename = "$databaseName.bacpac"
    
    New-AzSqlDatabaseExport -ResourceGroupName $resourceGroupName `
                            -ServerName $serverName `
                            -DatabaseName $databaseName `
                            -StorageKeytype "StorageAccessKey" `
                            -StorageKey $storageKey `
                            -StorageUri "$($storageContext.BlobEndPoint)$storageContainerName/$bacpacFilename"
    
    Write-Output "Exported $databaseName database to $bacpacFilename"
}
© www.soinside.com 2019 - 2024. All rights reserved.