mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
5.8 KiB
5.8 KiB
services, platforms, author
| services | platforms | author |
|---|---|---|
| azure automation | azure | trgrie-msft |
Setting up Auto Export in Azure Automation
Provides the scripts and lists the steps to set up automatically exporting your databases to Azure Storage with Azure Automation.
Prerequisite Set Up
- Create and set up your Azure Automation Account
- Create an Azure Automation Account by following the instructions here.
- Add Azure Automation Credential assets for your SQL Azure servers
- Create your Automation Credential for each of your SQL Azure servers you intend to export by following the instructions here.
- Create the Azure Storage Account to hold your bacpac files
- Create the Storage Account by following the instructions here.
- 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.
- Create an Azure Automation string Variable asset for your Storage Account access key by following the instructions here.
- Set Up Log Analytics (OMS) and Alerts (optional for alerting)
- If you don't have Log Analytics set up on your Azure account, follow these instructions for setting it up.
- Set Up Log Analytics Alerts
- To send yourself an email if an error occurs or one of the jobs fails, you need to set up alerts.
- Select your log analytics account that you want to use in the azure portal and click on the OMS Portal box under Management.
- Click on Log Search and enter the queries you want to alert on. These are two that are suggested:
- Category=JobStreams “Error occurred*”
- Category=JobLogs ResultType=Failed
- The first will alert on an the provided script saying an error occurred so you know if something didn't go quite right. The second alerts if the script fails entirely.
Script Set Up
Save the AutoExport.ps1 and AutoExportBlobRetention.ps1 files locally to make these edits
- In the AutoExport.ps1 script, here are the values that need to be modified:
- $databaseServerPairs: This is where you put in the names of the databases you want to export along with the name of the server they are on.
Add them in the format:
[pscustomobject]@{serverName="SAMPLESERVER1";databaseName="SAMPLEDATABASE1"}make sure to comma separate the items - $serverCredentialsDictionary: If you are backing up from multiple servers, you can setup all of the credentials here and look them up by the server’s name later.
Add a $serverCred variable in the format
$serverCred1 = Get-AutomationPSCredential -Name 'NAMEOFSERVERCREDENTIAL1';for each Azure Automation Credential you created. Increment the variable name (eg. $serverCred2 $serverCred3) for each one. Add the $serverCreds to the dictionary in the format'SAMPLESERVERNAME1'=$serverCred1; - $batchingLimit: This tells the script how many databases can be worked on at the same time (basically, the maximum number of database copies that there will be at once).
- $retryLimit: This tells the script how many times it can retry an operation.
- $waitTimeInMinutes: This tells the script how long it can wait for an operation to complete before it fails.
- $storageKeyVariableName: This is the Azure Automation string Variable name you created to store your Storage Key.
- $storageAccountName: This is the name of the storage account you are exporting to.
- $connectionAssetName: Connection Asset Name for Authenticating (Keep as AzureClassicRunAsConnection if you created the default RunAs accounts)
- $databaseServerPairs: This is where you put in the names of the databases you want to export along with the name of the server they are on.
Add them in the format:
- In AutoExportBlobRetention, here are the values that need to be modified:
- $storageKeyVariableName: This is the Azure Automation string Variable name you created to store your Storage Key.
- $storageAccountName: This is the name of your Storage Account you exported your bacpacs to.
- $storageContainerName: This is the name of the storage container where you will be monitoring the exported blobs.
- retentionInDays: This is how many days you want to keep the exported blobs stored for before deleting.
Adding the Script to Azure Automation
- Import the scripts as Azure Automation Runbooks
- Create runbooks from the scripts you editted above by following the instructions here for both scripts.
- Make sure to publish the runbook.
- Add a schedule for your Automated Export runbook
- Create a recurring schedule by following the instructions here.
- Link the schedule(s) you created to the runbooks by following the instructions here.
You should now be all set up for Automated Exports into blob storage of your selected SQL Azure databases.