简体   繁体   English

如何使用 PowerShell 脚本还原 Azure SQL Server 数据库上的架构?

[英]How to restore schema on the Azure SQL Server Databases using PowerShell script?

I have created a sql server, deployed elastic pool on that server and am able to create multiple databases in that elastic pool as per this link https://dba.stackexchange.com/a/257739/197860 .我已经创建了一个 sql 服务器,在该服务器上部署了弹性池,并且能够按照此链接https://dba.stackexchange.com/a/257739/197860在该弹性池中创建多个数据库。 Now my purpose of uploading this question is..I want to restore only schema and then later data on to those created azure sql databases using PowerShell Script.现在我上传这个问题的目的是..我只想恢复架构,然后使用 PowerShell 脚本将数据恢复到那些创建的 azure sql 数据库。 Can anyone help me with this please?任何人都可以帮我解决这个问题吗?

If you want restore the schema from local SQL server to Azure SQL database in the Elastic Pool, you could use the tool Data Migration Assistant(DMA) .如果您想将架构从本地 SQL 服务器还原到弹性池中的 Azure SQL 数据库,您可以使用工具数据迁移助手(DMA)

DMA supports oly migrate the schema: DMA 支持 oly 迁移模式:

在此处输入图片说明

If you want restore the schema between Azure SQL database, you could use SSMS Generate and Publish Scripts Wizard .如果要还原 Azure SQL 数据库之间的架构,可以使用 SSMS 生成和发布脚本向导 You can use the Generate and Publish Scripts Wizard to create scripts for transferring a database between instances of the SQL Server Database Engine or Azure SQL Database.可以使用生成和发布脚本向导创建脚本,以便在 SQL Server 数据库引擎或 Azure SQL 数据库的实例之间传输数据库。

Right click database--Generate Script:右键单击数据库--生成脚本:

在此处输入图片说明

在此处输入图片说明

Then you could using PowerShell to run the SQL script.然后你可以使用 PowerShell 来运行 SQL 脚本。

Reference:参考:

  1. How to execute .sql file using powershell? 如何使用powershell执行.sql文件?
  2. How to execute SQL Script using windows powershell(using invoke-sqlcmd or any if) 如何使用 windows powershell 执行 SQL 脚本(使用 invoke-sqlcmd 或任何 if)

I have a question, since you need to restore the schema and then later data to those created Azure SQL databases, why don't you import the backup file like .bacpac files to create database in that Azure SQL server with PowerShell directly?我有一个问题,既然您需要将架构和稍后的数据恢复到那些创建的 Azure SQL 数据库,为什么不导入备份文件(如 .bacpac 文件)以直接使用 PowerShell 在该 Azure SQL 服务器中创建数据库?

Please reference these tutorial:请参考这些教程:

Hope this helps.希望这可以帮助。

Restoring a bacpac creates a new database on Azure SQL Database.还原 bacpac 会在 Azure SQL 数据库上创建一个新数据库。 Below you the PowerShell script to restore it.下面是 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

Once restored you can assign the database to an elastic pool.恢复后,您可以将数据库分配给弹性池。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM