Backup Azure Database To Azure Storage

Octopus.Script exported 2015-03-25 by trailmax belongs to ‘Azure’ category.

Create a backup of SQL Azure Database and store it in Azure Blob Storage. Requires Azure PowerShell to be installed on Tentacle machine.

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Database Server Name

DatabaseServerName

Azure SQL Server name, i.e. “d340fe8ok”. Not a full name, just the machine name

Database Name

DatabaseName

Name of the database to be backed-up

Database Password

DatabasePassword

SQL Server Password to access the database

Database Username

DatabaseUsername

SQL Server Username to access the database

Storage Name

StorageName

Name of the storage account where the backup should go

Storage Key

StorageKey

Access key to the storage account

Storage Container Name

StorageContainerName

Name of the container where the backup should go

Script body

Steps based on this template will execute the following PowerShell script.

# Check if Windows Azure Powershell is avaiable 
try{ 
    Import-Module Azure -ErrorAction Stop
}catch{
    throw "Windows Azure Powershell not found! Please make sure to install them from http://www.windowsazure.com/en-us/downloads/#cmd-line-tools" 
}


$dateTime = get-date -Format u
$blobName = "Deployment-Backup/$DatabaseName/$dateTime.bacpac"
Write-Host "Using blobName: $blobName"

# Create Database Connection
$securedPassword = ConvertTo-SecureString -String $DatabasePassword -asPlainText -Force
$serverCredential = new-object System.Management.Automation.PSCredential($DatabaseUsername, $securedPassword) 
$databaseContext = New-AzureSqlDatabaseServerContext -ServerName $DatabaseServerName -Credential $serverCredential

# Create Storage Connection
$storageContext = New-AzureStorageContext -StorageAccountName $StorageName -StorageAccountKey $StorageKey

# Initiate the Export
$operationStatus = Start-AzureSqlDatabaseExport -StorageContext $storageContext -SqlConnectionContext $databaseContext -BlobName $blobName -DatabaseName $DatabaseName -StorageContainerName $StorageContainerName

# Wait for the operation to finish
do{
    $status = Get-AzureSqlDatabaseImportExportStatus -Request $operationStatus    
    Start-Sleep -s 3
    $progress =$status.Status.ToString()
    Write-Host "Waiting for database export completion. Operation status: $progress" 
}until ($status.Status -eq "Completed")
Write-Host "Database export is complete"

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "633e7b90-7025-45d4-b16f-f92d6cf25c9e",
  "Name": "Backup Azure Database To Azure Storage",
  "Description": "Create a backup of SQL Azure Database and store it in Azure Blob Storage. Requires Azure PowerShell to be installed on Tentacle machine.",
  "Version": 7,
  "ExportedAt": "2015-03-25T08:48:19.055+00:00",
  "ActionType": "Octopus.Script",
  "Author": "trailmax",
  "Parameters": [
    {
      "Name": "DatabaseServerName",
      "Label": "Database Server Name",
      "HelpText": "Azure SQL Server name, i.e. \"d340fe8ok\". Not a full name, just the machine name",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "DatabaseName",
      "Label": "Database Name",
      "HelpText": "Name of the database to be backed-up",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "DatabasePassword",
      "Label": "Database Password",
      "HelpText": "SQL Server Password to access the database",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "Sensitive"
      }
    },
    {
      "Name": "DatabaseUsername",
      "Label": "Database Username",
      "HelpText": "SQL Server Username to access the database",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "StorageName",
      "Label": "Storage Name",
      "HelpText": "Name of the storage account where the backup should go",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "StorageKey",
      "Label": "Storage Key",
      "HelpText": "Access key to the storage account",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "Sensitive"
      }
    },
    {
      "Name": "StorageContainerName",
      "Label": "Storage Container Name",
      "HelpText": "Name of the container where the backup should go",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptBody": "# Check if Windows Azure Powershell is avaiable \ntry{ \n    Import-Module Azure -ErrorAction Stop\n}catch{\n    throw \"Windows Azure Powershell not found! Please make sure to install them from http://www.windowsazure.com/en-us/downloads/#cmd-line-tools\" \n}\n\n\n$dateTime = get-date -Format u\n$blobName = \"Deployment-Backup/$DatabaseName/$dateTime.bacpac\"\nWrite-Host \"Using blobName: $blobName\"\n\n# Create Database Connection\n$securedPassword = ConvertTo-SecureString -String $DatabasePassword -asPlainText -Force\n$serverCredential = new-object System.Management.Automation.PSCredential($DatabaseUsername, $securedPassword) \n$databaseContext = New-AzureSqlDatabaseServerContext -ServerName $DatabaseServerName -Credential $serverCredential\n\n# Create Storage Connection\n$storageContext = New-AzureStorageContext -StorageAccountName $StorageName -StorageAccountKey $StorageKey\n\n# Initiate the Export\n$operationStatus = Start-AzureSqlDatabaseExport -StorageContext $storageContext -SqlConnectionContext $databaseContext -BlobName $blobName -DatabaseName $DatabaseName -StorageContainerName $StorageContainerName\n\n# Wait for the operation to finish\ndo{\n    $status = Get-AzureSqlDatabaseImportExportStatus -Request $operationStatus    \n    Start-Sleep -s 3\n    $progress =$status.Status.ToString()\n    Write-Host \"Waiting for database export completion. Operation status: $progress\" \n}until ($status.Status -eq \"Completed\")\nWrite-Host \"Database export is complete\"",
    "Octopus.Action.Script.Syntax": "PowerShell"
  },
  "Category": "Azure",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/backup-azure-database-to-azure-storage.json",
  "Website": "/step-templates/633e7b90-7025-45d4-b16f-f92d6cf25c9e",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADNQTFRF////AHjXf7vrv931QJrh7/f8EIDaIIncMJHfYKvmz+b3n8zw3+76j8Ttr9XycLPpUKLkkKvYFAAABGZJREFUeNrsnNmCqjoQRc1MEiD8/9cer7Yt2KBJZQC8ez07sKlKTQlcLgAAAAAAAAAAAAAAA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AMcsl8okXcdKrl4sPsLmQVy/Sj90ucPRc/d/Bxxj+dXSpCayen32D+hLi16MsIV8gfCXrYp6ySsiJKRUF0XXiLpVbFU+fNv4r7mOwhFsX4ZdwpSi1DYs2jb6ebZ9788cblTzMrYhu7sf/17IFdtuviJ2ioHA6pMHkoH4CLUeMBU7iGkxuM/YgcdderF9ibRdc7O982F1HpYhjfWUe+x5a6pjop9iNLfoePvlsdZdTSMwfxSmTY20Q0eHnUNzga1edeNmmqbg18aMVR1L9vwSXHF9TfIWBxpKLs2hj3eQeBC0USvp2HHF3eIkRdhFOd6ER8AAAAAAAAAAAAAAAAAAAAAAAAAAAAA/I/4J8AAo/80BciBec4AAAAASUVORK5CYII=",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Wednesday, March 25, 2015