简体   繁体   English

如何使用 PowerShell 在 Azure SQL 数据库上将 ComputeModel 属性设置为无服务器?

[英]How to set the ComputeModel property to Serverless on an Azure SQL Database using PowerShell?

I'm restoring an Azure SQL Database (Serverless) from a deleted database backup using Get-AzSqlDeletedDatabaseBackup and Restore-AzSqlDatabase PowerShell commandlets.我正在使用 Get-AzSqlDeletedDatabaseBackup 和 Restore-AzSqlDatabase Z3D265B4E1EEEF0DDF17881FA003B 命令从已删除的数据库备份中恢复 Azure SQL 数据库(无服务器)。 The restore works, but the tags and ComputeModel are not restored with the database.恢复工作,但标签和 ComputeModel 没有与数据库一起恢复。

I've tried using Set-AzSqlDatabase:我试过使用 Set-AzSqlDatabase:

Set-AzSqlDatabase -ResourceGroupName $resourcegroupname -DatabaseName $databasename -ServerName $servername -ComputeModel "Serverless" -AutoPauseDelayInMinutes 45

Update: I tried the following code and the Kind is set prior to using the Set-AzResource cmdlet, but it doesn't stick更新:我尝试了以下代码,并在使用 Set-AzResource cmdlet 之前设置了种类,但它没有坚持下去

$resource = Get-AzResource -ResourceGroupName $resourcegroupname -ResourceType "Microsoft.Sql/servers/databases" -Name "$servername/$databasename"

Write-Host "Setting ComputeModel to Serverless..."
$resource.Kind = "v12.0,user,vcore,serverless"
$resource  
# resource.Kind is successfully set on the $resource object

Write-Host "Set-AzResource..."
$resource | Set-AzResource -Force

Anyone have any ideas?有人有想法么?

Thank you.谢谢你。

Cheers,干杯,

Andy安迪

The Get-AzSqlDeletedDatabaseBackup and Restore-AzSqlDatabase PowerShell cmdlets are doesn't contain a property to get the ComputeModel . Get-AzSqlDeletedDatabaseBackupRestore-AzSqlDatabase PowerShell cmdlet 不包含获取ComputeModel的属性。

While Restore and Delete Backup database we don't require the ComputeModel properties.还原删除备份数据库时,我们不需要ComputeModel属性。 while setting database we need to require the ComputeModel .在设置数据库时,我们需要ComputeModel

If you want to get the compute model for the Azure SQL Database you can use, Get-AzResource command to fetch the specific information.如果要获取 Azure SQL 数据库的计算 model ,可以使用Get-AzResource命令获取具体信息。

Thanks @joy wang SO Solution we can get the serverless Azure SQL Database.感谢@joy wang SO 解决方案,我们可以获得无服务器 Azure SQL 数据库。

在此处输入图像描述

Thanks to @holger and @Delliganesh Sevanesan for the help, I was able implement a solution that restores the most recent deleted database (SQL Database), adds some resource tags, and sets the ComputeModel to serverless.感谢@holger 和@Delliganesh Sevanesan 的帮助,我能够实现一个解决方案,恢复最近删除的数据库(SQL 数据库),添加一些资源标签,并将 ComputeModel 设置为无服务器。

Here's the code:这是代码:

<# 
Purpose: Restore the most recently deleted Azure Sql Database

Dependencies:
    Az.Sql PowerShell module
#>

# Set variables first
$resourcegroupname = 'myresourcegroup'
$servername = 'myservername'
$databasename = 'mydatabasename'

[hashtable]$tags = @{ 
    application = "testing"
}

try {
    $deleteddatabases = Get-AzSqlDeletedDatabaseBackup -ResourceGroupName $resourcegroupname -ServerName $servername -DatabaseName $databasename
} catch {
    Write-Error "Error getting database backups [Get-AzSqlDeletedDatabaseBackup]: " + $_.Exception.Message
    exit(1)
}

# Get most recent backup in case there are multiple copies
# Assumes index and order in foreach is the same - proven in test
Write-Host "Database backups:"
$index = 0
$MostRecentBackupIndex = 0
$MostRecentBackupDate = (Get-date).AddDays(-2) # initialize variable with date from two days ago
foreach ($db in $deleteddatabases) {
    if ($db.CreationDate -ge $MostRecentBackupDate) {
        $MostRecentBackupIndex = $index
        $MostRecentBackupDate = $db.CreationDate
        Write-Host "Most Recent Database: $($db.DatabaseName) : Created: $($db.CreationDate) : DeleteDate: $($db.DeletionDate)"
    }
    $index++
}

$deleteddatabase = $deleteddatabases[$MostRecentBackupIndex]

Write-Host "----------------------------------------------------------------------------------"
Write-Host "Restoring: $($deleteddatabase.DatabaseName) from: $($deleteddatabase.CreationDate) backup"
Write-Host "----------------------------------------------------------------------------------"

Write-Host "Deleted database info ResourceId: "
Write-Host $deleteddatabase.ResourceId

try {
    Restore-AzSqlDatabase -FromDeletedDatabaseBackup `
        -DeletionDate $deleteddatabase.DeletionDate `
        -ResourceGroupName $resourcegroupname `
        -ServerName $servername `
        -TargetDatabaseName $databasename `
        -ResourceId $deleteddatabase.ResourceID `
        -Edition $deleteddatabase.Edition `
        -Vcore 2 `
        -ComputeGeneration "Gen5" 
} catch {
    Write-Error "Error restoring database [Restore-AzSqlDatabase]: " + $_.Exception.Message
    exit(1)
}

# Wait a few minutes to allow restore to complete before applying the tags
Start-Sleep -Seconds 180

Write-Host "Applying tags to database..."
try {
    $resource = Get-AzResource -ResourceGroupName $resourcegroupname -ResourceType "Microsoft.Sql/servers/databases" -Name "$servername/$databasename"
    New-AzTag -ResourceId $resource.Id -Tag $tags
} catch {
    Write-Error "Error adding tags to database [Get-AzResource, New-AzTag]: " + $_.Exception.Message
    exit(1)
}

Write-Host "Setting ComputeModel to Serverless..."
try {
    # Important - must include -AutoPauseDelayInMinutes 60, -MinVcore, and MaxVcore parameters (thanks holger)
    Set-AzSqlDatabase -ResourceGroupName $resourcegroupname -DatabaseName $databasename -ServerName $servername -ComputeModel Serverless -AutoPauseDelayInMinutes 60 -MinVcore 1 -MaxVcore 2
} catch {
    Write-Error "Error setting serverless mode [Set-AzSqlDatabase]: " + $_.Exception.Message
    exit(1)
}

Write-Host "Database restore complete."

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

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