Replication of production env database to staging

2019-07-17 16:31发布

问题:

In order to verify the deployment of my database scripts better, I'd like to pre-initialize my Staging database with a mirror image of the Production database as a first step in my Octopus deployment. I'm using SQL Azure and DACFX. I'm curious if anyone else has tried this...

  • Is Start-AzureSqlDatabaseCopy the right PS cmdlet to use for this operation?
  • Will this effect the performance of my production environment?
  • Are there any other options to consider?

Update

I developed the below script, which seems to work. However, I'm having trouble blocking completion of the script until the database is finished copying. At some point Get-AzureSqlDatabaseCopy will throw an error (maybe Azure can't handle the load?).

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"

I'm still not convinced this is the right way to go & it seems to put a lot of load on Azure (wasn't able to log into my portal while this was running for some reason). Any thoughts would be appreciated...

回答1:

Just thought I'd reply back with how this progress went. I added the below script to my Octopus steps for my UAT (staging) environment and it has been working out very well. The main issue with the original script was that my call to Write-Progess was taking a bad parameter (I just removed the call since it wouldn't have worked right in Octopus anyway).

One thing to note is that I did have to make my tentacle run as my user. I couldn't figure out a way to get the azure scripts to run under the local system.

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"