From 5935502b36c5513d131df8409c35b67ea50086d2 Mon Sep 17 00:00:00 2001 From: Cameron Battagler Date: Fri, 17 Mar 2017 15:35:16 -0500 Subject: [PATCH] Added instructions for adding the Blob Storage container, added better information messages, added edits suggested in pull request #199 --- .../AutoExport.ps1 | 33 +++++++++---------- .../README.md | 5 +++ 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/samples/manage/azure-automation-automated-export/AutoExport.ps1 b/samples/manage/azure-automation-automated-export/AutoExport.ps1 index 08ea71e0..14b0cea2 100644 --- a/samples/manage/azure-automation-automated-export/AutoExport.ps1 +++ b/samples/manage/azure-automation-automated-export/AutoExport.ps1 @@ -39,7 +39,6 @@ $waitInMinutes = 30; # Connection Asset Name for Authenticating (Keep as AzureClassicRunAsConnection if you created the default RunAs accounts) $connectionAssetName = "AzureClassicRunAsConnection"; - $storageKeyVariableName = "STORAGEKEYVARIABLENAME"; $storageAccountName = "STORAGEACCOUNTNAME"; $storageContainerName = "STORAGECONTAINERNAME"; @@ -144,9 +143,9 @@ function StartExport($dbObj) $currentTime = Get-Date -format "_yyyy-MM-dd_HH:mm.ss"; $blobName = $dbObj.DatabaseName + "_ExportBlob" + $currentTime; # Use the stored credential to create a server credential to use to login to the server. - $servercredential = $global:serverCredentialsDictionary[$dbObj.ServerName]; + $serverCredential = $global:serverCredentialsDictionary[$dbObj.ServerName]; # Set up a SQL connection context to use when exporting. - $ctx = New-AzureSqlDatabaseServerContext -ServerName $dbObj.ServerName -Credential $servercredential; + $ctx = New-AzureSqlDatabaseServerContext -ServerName $dbObj.ServerName -Credential $serverCredential; # Get the storage key to setup the storage context. $storageKey = Get-AutomationVariable -Name $global:storageKeyVariableName; # Get the storage context. @@ -171,7 +170,7 @@ function StartExport($dbObj) } # Set the state to Exporting. $dbObj.DatabaseState = ([DatabaseState]::Exporting); - LogMessage ("Exporting " + $dbObj.DatabaseCopyName); + LogMessage ("Exporting " + $dbObj.DatabaseCopyName + " with RequestID: " + $dbObj.Export.RequestGuid); $dbObj.OperationStartTime = Get-Date; } @@ -192,7 +191,7 @@ function CheckExport($dbObj) { # If the status is "Failed" and we have more retries left, try to export the database copy again. LogMessage ("The last export failed on database " + $dbObj.DatabaseName + ", going back to ToExport state to try again"); - LogMessage $check + LogMessage $check.ErrorMessage $dbObj.DatabaseState = ([DatabaseState]::ToExport); $dbObj.RetryCount++; return; @@ -229,7 +228,7 @@ function ExportProcess $dbsToCopy = $global:dbs | Where-Object DatabaseState -eq ([DatabaseState]::ToCopy); for($i = 0; $i -lt $dbsToCopy.Count; $i++) { - LogMessage $dbsToCopy[$i]; + LogMessage "Database Name: $($dbsToCopy[$i].DatabaseName) State: $($dbsToCopy[$i].DatabaseState) Retry Count: $($dbsToCopy[$i].RetryCount)"; StartCopy($dbsToCopy[$i]); } @@ -244,7 +243,7 @@ function ExportProcess $dbsToExport = $global:dbs | Where-Object DatabaseState -eq ([DatabaseState]::ToExport); for($i = 0; $i -lt $dbsToExport.Count; $i++) { - LogMessage $dbsToExport[$i]; + LogMessage "Database Name: $($dbsToExport[$i].DatabaseName) State: $($dbsToExport[$i].DatabaseState) Retry Count: $($dbsToExport[$i].RetryCount)"; StartExport($dbsToExport[$i]); } @@ -259,7 +258,7 @@ function ExportProcess $dbsToDrop = $global:dbs | Where-Object DatabaseState -eq ([DatabaseState]::ToDrop); for($i = 0; $i -lt $dbsToDrop.Count; $i++) { - LogMessage $dbsToDrop[$i]; + LogMessage "Database Name: $($dbsToDrop[$i].DatabaseName) State: $($dbsToDrop[$i].DatabaseState) Retry Count: $($dbsToDrop[$i].RetryCount)"; StartDrop($dbsToDrop[$i]); } @@ -273,23 +272,23 @@ function ExportProcess # Authenticate to Azure with certificate Write-Verbose "Get connection asset: $connectionAssetName" -Verbose; -$Conn = Get-AutomationConnection -Name $connectionAssetName; -if ($Conn -eq $null) +$automationConnection = Get-AutomationConnection -Name $connectionAssetName; +if ($automationConnection -eq $null) { throw "Could not retrieve connection asset: $connectionAssetName. Assure that this asset exists in the Automation account."; } -$CertificateAssetName = $Conn.CertificateAssetName; -Write-Verbose "Getting the certificate: $CertificateAssetName" -Verbose; -$AzureCert = Get-AutomationCertificate -Name $CertificateAssetName; -if ($AzureCert -eq $null) +$certificateAssetName = $automationConnection.CertificateAssetName; +Write-Verbose "Getting the certificate: $certificateAssetName" -Verbose; +$automationCertificate = Get-AutomationCertificate -Name $certificateAssetName; +if ($automationCertificate -eq $null) { - throw "Could not retrieve certificate asset: $CertificateAssetName. Assure that this asset exists in the Automation account."; + throw "Could not retrieve certificate asset: $certificateAssetName. Assure that this asset exists in the Automation account."; } Write-Verbose "Authenticating to Azure with certificate." -Verbose; -Set-AzureSubscription -SubscriptionName $Conn.SubscriptionName -SubscriptionId $Conn.SubscriptionID -Certificate $AzureCert; -Select-AzureSubscription -SubscriptionId $Conn.SubscriptionID; +Set-AzureSubscription -SubscriptionName $automationConnection.SubscriptionName -SubscriptionId $automationConnection.SubscriptionID -Certificate $automationCertificate; +Select-AzureSubscription -SubscriptionId $automationConnection.SubscriptionID; $currentIndex = 0; for($currentRun = 0; $currentRun -lt ([math]::Ceiling($databaseServerPairs.Length/$batchingLimit)); $currentRun++) diff --git a/samples/manage/azure-automation-automated-export/README.md b/samples/manage/azure-automation-automated-export/README.md index 9e53c2b7..ac3fcce7 100644 --- a/samples/manage/azure-automation-automated-export/README.md +++ b/samples/manage/azure-automation-automated-export/README.md @@ -16,6 +16,11 @@ Provides the scripts and lists the steps to set up automatically exporting your - Create your Automation Credential for each of your SQL Azure servers you intend to export by [following the instructions here](https://docs.microsoft.com/en-us/azure/automation/automation-credentials#creating-a-new-credential-asset). 3. Create the Azure Storage Account to hold your bacpac files - Create the Storage Account by [following the instructions here](https://docs.microsoft.com/en-us/azure/storage/storage-create-storage-account#create-a-storage-account). + - Create the Blob Storage Container + - Go to your Storage Account + - Click the Blobs tile + - Click the Add Container button + - Name the container, keep the access type as Private, and click the Create button - Copy your Storage Account access keys by [following the instructions here](https://docs.microsoft.com/en-us/azure/storage/storage-create-storage-account#view-and-copy-storage-access-keys). - Create an Azure Automation string Variable asset for your Storage Account access key by [following the instructions here](https://docs.microsoft.com/en-us/azure/automation/automation-variables#creating-an-automation-variable). 4. Set Up Log Analytics (OMS) and Alerts (optional for alerting)