【问题标题】:How to restore schema on the Azure SQL Server Databases using PowerShell script?如何使用 PowerShell 脚本还原 Azure SQL Server 数据库上的架构?
【发布时间】:2020-05-06 05:19:39
【问题描述】:

我已经创建了一个 sql 服务器,在该服务器上部署了弹性池,并且能够按照此链接 https://dba.stackexchange.com/a/257739/197860 在该弹性池中创建多个数据库。现在我上传这个问题的目的是..我只想恢复模式,然后再将数据恢复到那些 使用 PowerShell 脚本创建 azure sql 数据库。谁能帮我解决这个问题?

【问题讨论】:

  • 您要从什么数据库架构中恢复?

标签: sql-server powershell azure-sql-database schema restore


【解决方案1】:

还原 bacpac 会在 Azure SQL 数据库上创建一个新数据库。下面是用于恢复它的 PowerShell 脚本。

# Restore/Import bacpac to database with an S3 performance level
$importRequest = New-AzSqlDatabaseImport -ResourceGroupName $resourceGroupName `
    -ServerName $serverName `
    -DatabaseName $databaseName `
    -DatabaseMaxSizeBytes "262144000" `
    -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 restore/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

恢复后,您可以将数据库分配给弹性池。

# Move the database to the pool
$firstDatabase = Set-AzSqlDatabase -ResourceGroupName $resourceGroupName `
    -ServerName $serverName `
    -DatabaseName $firstDatabaseName `
    -ElasticPoolName $PoolName

【讨论】:

    【解决方案2】:

    如果您想将本地 SQL 服务器的架构恢复到弹性池中的 Azure SQL 数据库,您可以使用工具Data Migration Assistant(DMA)

    DMA 支持仅迁移架构:

    如果要在 Azure SQL 数据库之间恢复架构,可以使用 SSMS Generate and Publish Scripts Wizard。您可以使用生成和发布脚本向导创建用于在 SQL Server 数据库引擎或 Azure SQL 数据库实例之间传输数据库的脚本。

    右键数据库--生成脚本:

    然后您可以使用 PowerShell 来运行 SQL 脚本。

    参考:

    1. How to execute .sql file using powershell?
    2. How to execute SQL Script using windows powershell(using invoke-sqlcmd or any if)

    我有一个问题,因为您需要将架构和以后的数据恢复到那些创建的 Azure SQL 数据库,为什么不导入备份文件(如 .bacpac 文件)直接使用 PowerShell 在该 Azure SQL 服务器中创建数据库?

    请参考这些教程:

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-28
      • 2021-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多