You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用同名操作复制/还原Azure SQL Database后数据异常问题排查

问题

我在弹性池中拥有两个Azure SQL Database,分别名为Web和Web2,它们关联部署槽并随槽交换移动,其中一个会作为主槽提供公网流量。槽交换后,我希望同步非公开状态的Web或Web2数据库的数据。为此编写了PowerShell脚本,流程如下:

  1. 识别主槽中的数据库
  2. 通过地理还原或数据库复制命令创建RestoredDB
  3. 删除非活动的原数据库并将RestoredDB重命名为原名称(以保持连接字符串不变)

但操作后,重命名后的数据库数据变回了原库的内容,而非预期的还原/复制的数据。请问:

  • 这是否是因为异地冗余备份(7天保留期)导致?
  • 是否必须使用唯一数据库名称而非重命名操作?

地理还原示例

$GeoBackup = Get-AzSqlDatabaseGeoBackup -ResourceGroupName "ResourceGroup01" -ServerName "Server01" -DatabaseName "Database01"
Restore-AzSqlDatabase -FromGeoBackup -ResourceGroupName "TargetResourceGroup" -ServerName "TargetServer" -TargetDatabaseName "RestoredDatabase" -ResourceId $GeoBackup.ResourceID -ElasticPoolName "MyWebPool"

复制Azure SQL DB示例

New-AzSqlDatabaseCopy -CopyDatabaseName <String> -CopyResourceGroupName <String> -CopyServerName <String> -DatabaseName db1 -ResourceGroupName MyResourceGroup -ServerName s1 -ElasticPoolName "MyWebPool"

解答

关于异地冗余备份的疑问

不是异地冗余备份导致的问题。异地冗余备份仅用于灾难恢复的备份存储,不会在你删除原库并重命名新库后自动覆盖数据。问题根源在于部署槽与数据库的关联逻辑以及重命名操作的时机:当你删除非活动槽关联的原数据库后直接重命名新库,部署槽的关联配置可能仍指向原数据库的元数据,Azure的槽同步机制会触发自动恢复操作,从本地备份恢复原名称的数据库,最终出现“数据变回原库内容”的现象。

关于重命名操作的必要性

不是必须使用唯一数据库名称,但你的操作流程存在逻辑漏洞。正确做法是先更新部署槽与数据库的关联,再执行删除和重命名,避免触发槽的自动恢复。

推荐操作流程

  1. 识别主槽中的源数据库(比如当前主槽使用的Web)
  2. 用地理还原或复制命令创建临时数据库(比如RestoredWeb2)
  3. 将非活动槽的数据库关联从原库(比如Web2)切换到临时数据库
  4. 删除原非活动数据库
  5. 将临时数据库重命名为原库名称(比如Web2)
  6. 确认槽关联已更新为重命名后的数据库

修正后的脚本逻辑参考

地理还原调整示例

# 1. 获取主槽源数据库的异地备份
$GeoBackup = Get-AzSqlDatabaseGeoBackup -ResourceGroupName "ResourceGroup01" -ServerName "Server01" -DatabaseName "Web"
# 2. 还原到临时数据库
Restore-AzSqlDatabase -FromGeoBackup -ResourceGroupName "ResourceGroup01" -ServerName "Server01" -TargetDatabaseName "RestoredWeb2" -ResourceId $GeoBackup.ResourceID -ElasticPoolName "MyWebPool"
# 3. 更新非活动槽的连接字符串为临时数据库
$slot = Get-AzWebAppSlot -ResourceGroupName "ResourceGroup01" -Name "MyWebApp" -Slot "Staging"
$slot.ConnectionStrings["DefaultConnection"] = @{
    Type = "SQLAzure"
    ConnectionString = "Server=tcp:Server01.database.windows.net,1433;Initial Catalog=RestoredWeb2;..."
}
Set-AzWebAppSlot -ResourceGroupName "ResourceGroup01" -Name "MyWebApp" -Slot "Staging" -ConnectionStrings $slot.ConnectionStrings
# 4. 删除原非活动数据库
Remove-AzSqlDatabase -ResourceGroupName "ResourceGroup01" -ServerName "Server01" -DatabaseName "Web2" -Force
# 5. 重命名临时数据库为原名称
Rename-AzSqlDatabase -ResourceGroupName "ResourceGroup01" -ServerName "Server01" -DatabaseName "RestoredWeb2" -NewName "Web2"
# 6. 再次更新槽连接字符串指向重命名后的数据库
$slot.ConnectionStrings["DefaultConnection"] = @{
    Type = "SQLAzure"
    ConnectionString = "Server=tcp:Server01.database.windows.net,1433;Initial Catalog=Web2;..."
}
Set-AzWebAppSlot -ResourceGroupName "ResourceGroup01" -Name "MyWebApp" -Slot "Staging" -ConnectionStrings $slot.ConnectionStrings

数据库复制调整示例

# 1. 复制主槽源数据库到临时库
New-AzSqlDatabaseCopy -CopyDatabaseName "CopiedWeb2" -CopyResourceGroupName "ResourceGroup01" -CopyServerName "Server01" -DatabaseName "Web" -ResourceGroupName "ResourceGroup01" -ServerName "Server01" -ElasticPoolName "MyWebPool"
# 2. 更新非活动槽的连接字符串为临时数据库
$slot = Get-AzWebAppSlot -ResourceGroupName "ResourceGroup01" -Name "MyWebApp" -Slot "Staging"
$slot.ConnectionStrings["DefaultConnection"] = @{
    Type = "SQLAzure"
    ConnectionString = "Server=tcp:Server01.database.windows.net,1433;Initial Catalog=CopiedWeb2;..."
}
Set-AzWebAppSlot -ResourceGroupName "ResourceGroup01" -Name "MyWebApp" -Slot "Staging" -ConnectionStrings $slot.ConnectionStrings
# 3. 删除原非活动数据库
Remove-AzSqlDatabase -ResourceGroupName "ResourceGroup01" -ServerName "Server01" -DatabaseName "Web2" -Force
# 4. 重命名临时数据库为原名称
Rename-AzSqlDatabase -ResourceGroupName "ResourceGroup01" -ServerName "Server01" -DatabaseName "CopiedWeb2" -NewName "Web2"
# 5. 更新槽连接字符串指向重命名后的数据库
$slot.ConnectionStrings["DefaultConnection"] = @{
    Type = "SQLAzure"
    ConnectionString = "Server=tcp:Server01.database.windows.net,1433;Initial Catalog=Web2;..."
}
Set-AzWebAppSlot -ResourceGroupName "ResourceGroup01" -Name "MyWebApp" -Slot "Staging" -ConnectionStrings $slot.ConnectionStrings

关键注意事项

  • 操作前确认非活动槽无流量,避免业务中断或数据丢失
  • 重命名前必须将所有关联应用配置(包括槽的连接字符串)临时指向临时数据库
  • 删除原数据库后可等待2-3分钟再执行重命名,确保Azure元数据同步完成

内容的提问来源于Stack Exchange,提问作者Kode

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 10:07:19