简体   繁体   中英

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 . 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. 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) .

DMA supports oly migrate the schema:

在此处输入图片说明

If you want restore the schema between Azure SQL database, you could use SSMS Generate and Publish Scripts Wizard . 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.

Right click database--Generate Script:

在此处输入图片说明

在此处输入图片说明

Then you could using PowerShell to run the SQL script.

Reference:

  1. How to execute .sql file using powershell?
  2. How to execute SQL Script using windows powershell(using invoke-sqlcmd or any 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?

Please reference these tutorial:

Hope this helps.

Restoring a bacpac creates a new database on Azure SQL Database. Below you the PowerShell script to restore it.

# 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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