powershell - 在使用 Octopus 部署之前将生产环境数据库复制到暂存区

标签 powershell continuous-integration azure-sql-database octopus-deploy system-testing

为了更好地验证我的数据库脚本的部署,我想使用生产数据库的镜像预初始化我的暂存数据库,作为我的 Octopus 部署的第一步。我正在使用 SQL Azure 和 DACFX。我很好奇是否还有其他人尝试过这个...

  • Start-AzureSqlDatabaseCopy 是用于此操作的正确 PS cmdlet 吗?
  • 这会影响我的生产环境的性能吗?
  • 是否还有其他选择可供考虑?

更新

我开发了下面的脚本,它似乎有效。但是,在数据库完成复制之前,我无法阻止脚本的完成。在某些时候 Get-AzureSqlDatabaseCopy 会抛出错误(也许 Azure 无法处理负载?)。

Import-Module 'C:\Program Files (x86)\Microsoft SDKs\Windows Azure\PowerShell\ServiceManagement\Azure\Azure.psd1'

$serverName = "..."
$sourceDbName = "..."
$targetDbName = "..."

$testdb = Get-AzureSqlDatabase -ServerName $serverName -DatabaseName $targetDbName -ErrorAction SilentlyContinue

IF (!$testdb)
{
    Write-Host "TestDB Not Found"
}
ELSE
{
    Remove-AzureSqlDatabase -ServerName $serverName -Database $testdb -Force
}

$dbCopy = Start-AzureSqlDatabaseCopy -ServerName $serverName -DatabaseName $sourceDbName -PartnerDatabase $targetDbName

WHILE ($dbCopy)
{
    Write-Progress -Activity "Copying Database" -PercentComplete [int]$dbCopy.PercentComplete
    $dbCopy = Get-AzureSqlDatabaseCopy -ServerName $serverName -DatabaseCopy $dbCopy

    # Sleep 10 seconds
    [System.Threading.Thread]::Sleep(10000);
}

Write-Host "Complete"

我仍然不相信这是正确的方法&它似乎给 Azure 带来了很多负载(由于某种原因在运行时无法登录我的门户)。任何想法将不胜感激...

最佳答案

只是想我会回复这个进展如何。我将以下脚本添加到我的 UAT(暂存)环境的 Octopus 步骤中,并且运行良好。原始脚本的主要问题是我对 Write-Progess 的调用采用了错误的参数(我只是删除了该调用,因为无论如何它在 Octopus 中都无法正常工作)。

需要注意的一件事是,我确实必须让我的触手以我的用户身份运行。我想不出让 azure 脚本在本地系统下运行的方法。

Import-Module 'C:\Program Files (x86)\Microsoft SDKs\Windows Azure\PowerShell\ServiceManagement\Azure\Azure.psd1'

$serverName = "..."
$sourceDbName = "..."
$targetDbName = "..."

$testdb = Get-AzureSqlDatabase -ServerName $serverName -DatabaseName $targetDbName -ErrorAction SilentlyContinue

IF (!$testdb)
{
    Write-Host "TestDB Not Found"
}
ELSE
{
    Remove-AzureSqlDatabase -ServerName $serverName -Database $testdb -Force
}

$dbCopy = Start-AzureSqlDatabaseCopy -ServerName $serverName -DatabaseName $sourceDbName -PartnerDatabase $targetDbName

WHILE ($dbCopy)
{
    $dbCopy = Get-AzureSqlDatabaseCopy -ServerName $serverName -DatabaseCopy $dbCopy

    # Sleep 10 seconds
    [System.Threading.Thread]::Sleep(10000);
}

Write-Host "Complete"

关于powershell - 在使用 Octopus 部署之前将生产环境数据库复制到暂存区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25089325/

相关文章:

powershell - 如何使用 REST API 销毁 TFVC 中的分支?

powershell - 忽略 ForEach-Object 循环中的错误

c# - 使用 Microsoft.SqlServer.SMO 创建一个 Alter Stored Procedure sql 脚本?

github - Github 的正确 VCS 用户名设置是什么,以便 "My Changes"正常工作?

azure-active-directory - 使用托管标识从本地 Azure 函数查询 Azure SQL 数据库

azure - 将虚拟网络中的 Azure 云服务连接到 Azure SQL 数据库

powershell - 如何从 powershell 脚本调用 sc create

continuous-integration - 为什么 Azure 门户的部署中心中没有外部 git 存储库的选项?

continuous-integration - 在 .travis.yml 中禁用 Travis CI 构建

c# - 如何使用Azure函数或逻辑应用将数据保存到sql数据库中?