Redgate - SQL Clone, Create Clone

Octopus.Script exported 2019-05-16 by alex-yates belongs to ‘Redgate’ category.

Creates a database clone with Redgate SQL Clone.

Requires SQL Clone.

Version date: 16th May 2019

Parameters

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

SQL Clone Server (required)

cloneServer =

The URL for your SQL Clone server (e.g. http://sql-clone.example.com:14145)

SQL Clone User (optional)

cloneUser =

User account to access SQL Clone. (If left blank Octopus tentacle account will be used.)

SQL Clone Password (optional)

clonePassword =

User account to access SQL Clone. (If left blank Octopus tentacle account will be used.)

Image Name For Clone (required)

imageNameForClone =

The name of the database image from which to create database clone.

Template Name (optional)

templateName =

A template to modify this clone (optional).

SQL Server (required)

cloneSqlServer =

The target SQL Server to create the clone on. This SQL Server instance must have already been added to the SQL Clone Server specified above.

Clone Name (required)

cloneName =

The name of the clone, which will also be the database name.

Delete clone if exists

deleteClone =

Delete any existing clone with the same name prior to creating this clone.

Script body

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

$ErrorActionPreference = 'Stop'

# The code for this step template is largely a copy/paste job from the
# Azure DevOps Services step template which is maintained by Redgate:
# https://github.com/red-gate/SqlCloneVSTSExtension/blob/master/ImageTask/SQLCloneCloneTask.ps1
# The code was copied and adapted on 16th May 2019.

Write-Verbose "cloneServer is $cloneServer"
Write-Verbose "cloneUser is $cloneUser"
Write-Verbose "clonePassword is $clonePassword"
Write-Verbose "imageNameForClone is $imageNameForClone"
Write-Verbose "templateName is $templateName"
Write-Verbose "cloneSqlServer is $cloneSqlServer"
Write-Verbose "cloneName is $cloneName"
Write-Verbose "deleteClone is $deleteClone"

Write-Debug "Entering script SQLCloneCloneTask.ps1"

# This line is broken: Import-Module "$PSScriptRoot\Modules\RedGate.SQLClone.PowerShell.dll"

if($cloneUser){
    $password = ConvertTo-SecureString -String $clonePassword -AsPlainText -Force
    $credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $cloneUser,$password

}
Connect-SqlClone -ServerUrl $cloneServer -Credential $credential
Write-Output "Connected to SQL Clone server"

        $sqlServerParts = $cloneSqlServer.Split('\', [System.StringSplitOptions]::RemoveEmptyEntries)
        if ($sqlServerParts.Count -ge 3)
        {
            write-error 'SQL Server instance ' + $cloneSqlServer + ' has not been recognised, if specifying a named instance please use "machine\instance"'
            exit 1
        }
        $cloneSqlServerHost = $sqlServerParts[0]
        $instanceName = ''
        if ($sqlServerParts.Count -ge 2)
        {
            $instanceName = $sqlServerParts[1]
        }
        
        try
        {
            $instance = Get-SqlCloneSqlServerInstance -MachineName $cloneSqlServerHost -InstanceName $instanceName
            Write-Output "Found SQL Server instance"
        }
        catch
        {
            $instances = Get-SqlCloneSqlServerInstance
            $instanceNames = "`n"
            Foreach ($cInstance in $instances)
            {
                $instanceNames += $cInstance.Name + "`n"
            }
            $message = 'SQL Server instance "' + $cloneSqlServer + '"  has not been added to SQL Clone, available instances:' + $instanceNames
            write-error $message
            exit 1
        }
        
        try
        {
            $image = Get-SqlCloneImage -Name $imageNameForClone
            Write-Output "Found image"
        }
        catch
        {
            $images = Get-SqlCloneImage
            $imageNames = "`n"
            Foreach ($cImage in $images)
            {
                $imageNames += $cImage.Name + "`n"
            }
            $message = 'SQL Clone image "' + $imageNameForClone + '"  has not been added to SQL Clone, available images:' + $imageNames
            write-error $message
            exit 1
        }
        
        if($deleteClone)
        {
            try
            {
                $clone = Get-SqlClone -Name $cloneName -Location $instance
                Write-Output "Deleting existing clone"
                Remove-SqlClone -Clone $clone | Wait-SqlCloneOperation
            }
            catch
            {
                # Clone didn't exist so nothing to do
            }
        }
        if($templateName)
        {
            Write-Output "Creating clone with template:" + $templateName
            $image | New-SqlClone -Name $cloneName -Location $instance -Template $templateName | Wait-SqlCloneOperation
        }
        else
        {            
            Write-Output "Creating clone"
            $image | New-SqlClone -Name $cloneName -Location $instance | Wait-SqlCloneOperation            
        }
        Write-Output "Finished creating clone"        

Write-Debug "Leaving script SQLCloneCloneTask.ps1"

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": "96d88bbf-2e0a-4630-b4b6-bd179effedd7",
  "Name": "Redgate - SQL Clone, Create Clone",
  "Description": "Creates a database clone with [Redgate SQL Clone](https://www.red-gate.com/products/dba/sql-clone/index).\n\nRequires SQL Clone.\n\n*Version date: 16th May 2019*",
  "Version": 1,
  "ExportedAt": "2019-05-16T11:37:47.360Z",
  "ActionType": "Octopus.Script",
  "Author": "alex-yates",
  "Packages": [],
  "Parameters": [
    {
      "Id": "8c140a4c-65a2-4341-a604-73d14775b3a0",
      "Name": "cloneServer",
      "Label": "SQL Clone Server (required)",
      "HelpText": "The URL for your SQL Clone server (e.g. http://sql-clone.example.com:14145)",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "e5ac1d04-b8a5-440e-ba69-a5d66a53abba",
      "Name": "cloneUser",
      "Label": "SQL Clone User (optional)",
      "HelpText": "User account to access SQL Clone. (If left blank Octopus tentacle account will be used.)",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "5f6288f2-57c9-4a11-91f2-b0c2e3cb9ccd",
      "Name": "clonePassword",
      "Label": "SQL Clone Password (optional)",
      "HelpText": "User account to access SQL Clone. (If left blank Octopus tentacle account will be used.)",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "Sensitive"
      }
    },
    {
      "Id": "ee5286d3-f233-410c-92ab-36743f8743e7",
      "Name": "imageNameForClone",
      "Label": "Image Name For Clone (required)",
      "HelpText": "The name of the database image from which to create database clone.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "0edb1e1f-0c27-428b-9ff0-7c76faf1369e",
      "Name": "templateName",
      "Label": "Template Name (optional)",
      "HelpText": "A template to modify this clone (optional).",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "5e9f4d95-4776-4a0e-8245-05d645bd0997",
      "Name": "cloneSqlServer",
      "Label": "SQL Server (required)",
      "HelpText": "The target SQL Server to create the clone on. This SQL Server instance must have already been added to the SQL Clone Server specified above.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "62da0567-35f5-4c3f-ac9e-61b75a399053",
      "Name": "cloneName",
      "Label": "Clone Name (required)",
      "HelpText": "The name of the clone, which will also be the database name.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "ab892024-c4b5-46c2-9434-2ad150e3e014",
      "Name": "deleteClone",
      "Label": "Delete clone if exists",
      "HelpText": "Delete any existing clone with the same name prior to creating this clone.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "Checkbox"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptBody": "$ErrorActionPreference = 'Stop'\n\n# The code for this step template is largely a copy/paste job from the\n# Azure DevOps Services step template which is maintained by Redgate:\n# https://github.com/red-gate/SqlCloneVSTSExtension/blob/master/ImageTask/SQLCloneCloneTask.ps1\n# The code was copied and adapted on 16th May 2019.\n\nWrite-Verbose \"cloneServer is $cloneServer\"\nWrite-Verbose \"cloneUser is $cloneUser\"\nWrite-Verbose \"clonePassword is $clonePassword\"\nWrite-Verbose \"imageNameForClone is $imageNameForClone\"\nWrite-Verbose \"templateName is $templateName\"\nWrite-Verbose \"cloneSqlServer is $cloneSqlServer\"\nWrite-Verbose \"cloneName is $cloneName\"\nWrite-Verbose \"deleteClone is $deleteClone\"\n\nWrite-Debug \"Entering script SQLCloneCloneTask.ps1\"\n\n# This line is broken: Import-Module \"$PSScriptRoot\\Modules\\RedGate.SQLClone.PowerShell.dll\"\n\nif($cloneUser){\n    $password = ConvertTo-SecureString -String $clonePassword -AsPlainText -Force\n    $credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $cloneUser,$password\n\n}\nConnect-SqlClone -ServerUrl $cloneServer -Credential $credential\nWrite-Output \"Connected to SQL Clone server\"\n\n        $sqlServerParts = $cloneSqlServer.Split('\\', [System.StringSplitOptions]::RemoveEmptyEntries)\n        if ($sqlServerParts.Count -ge 3)\n        {\n            write-error 'SQL Server instance ' + $cloneSqlServer + ' has not been recognised, if specifying a named instance please use \"machine\\instance\"'\n            exit 1\n        }\n        $cloneSqlServerHost = $sqlServerParts[0]\n        $instanceName = ''\n        if ($sqlServerParts.Count -ge 2)\n        {\n            $instanceName = $sqlServerParts[1]\n        }\n        \n        try\n        {\n            $instance = Get-SqlCloneSqlServerInstance -MachineName $cloneSqlServerHost -InstanceName $instanceName\n            Write-Output \"Found SQL Server instance\"\n        }\n        catch\n        {\n            $instances = Get-SqlCloneSqlServerInstance\n            $instanceNames = \"`n\"\n            Foreach ($cInstance in $instances)\n            {\n                $instanceNames += $cInstance.Name + \"`n\"\n            }\n            $message = 'SQL Server instance \"' + $cloneSqlServer + '\"  has not been added to SQL Clone, available instances:' + $instanceNames\n            write-error $message\n            exit 1\n        }\n        \n        try\n        {\n            $image = Get-SqlCloneImage -Name $imageNameForClone\n            Write-Output \"Found image\"\n        }\n        catch\n        {\n            $images = Get-SqlCloneImage\n            $imageNames = \"`n\"\n            Foreach ($cImage in $images)\n            {\n                $imageNames += $cImage.Name + \"`n\"\n            }\n            $message = 'SQL Clone image \"' + $imageNameForClone + '\"  has not been added to SQL Clone, available images:' + $imageNames\n            write-error $message\n            exit 1\n        }\n        \n        if($deleteClone)\n        {\n            try\n            {\n                $clone = Get-SqlClone -Name $cloneName -Location $instance\n                Write-Output \"Deleting existing clone\"\n                Remove-SqlClone -Clone $clone | Wait-SqlCloneOperation\n            }\n            catch\n            {\n                # Clone didn't exist so nothing to do\n            }\n        }\n        if($templateName)\n        {\n            Write-Output \"Creating clone with template:\" + $templateName\n            $image | New-SqlClone -Name $cloneName -Location $instance -Template $templateName | Wait-SqlCloneOperation\n        }\n        else\n        {            \n            Write-Output \"Creating clone\"\n            $image | New-SqlClone -Name $cloneName -Location $instance | Wait-SqlCloneOperation            \n        }\n        Write-Output \"Finished creating clone\"        \n\nWrite-Debug \"Leaving script SQLCloneCloneTask.ps1\""
  },
  "Category": "Redgate",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/redgate-sql-clone-create-clone.json",
  "Website": "/step-templates/96d88bbf-2e0a-4630-b4b6-bd179effedd7",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAD9QTFRF9s3N7Z6e3U1N87+/+NjY0RIS2Tc3zgAA/O7u425u1SQk5Xp66Y2N/fPz76ys+eHh4F5e0AkJ/vj4/vz8////8g2GDwAABlRJREFUeNrsne2SpCoMhkEQ+RJQ+v6v9YDau+JO97a2IuxJdn7MVk3V+EzSQN6QiB7/iCEAARAAARAAARAAARAAARAAARAAARAAARAAAZAvrOeWUkSIwVprt7bwf2wIQYha3vuCQXpKjFbCNUzKoXtlQysZc05pQygvDmSkWMXnb9vuUwtAkjUKkzJAPKcEizcO+IBIOhycM94I4q1Rjn0D8bRWOmWsvwekx461Z1A8YViDbW6QnmJ3IsPvMGMacZ8NhBshu6usddrmAeGYtd2VNjBFLwcZqbqWYnGLQuOlIFSxLo9JRf1lIKNhQ5fLwuf+KhDbdHmtodeAkMwcXYsBBEAABEAABEAA5DqQdvg3QLjRyjWsrR4kyg29pYhg1ci2bpAFZzoLIywmKWuoF2QlQlgyxZuUlYNs4m2oG2SmmeONxHhjJ8XbLSAbMRufsb7dDbKIwhahyT+Vg/yON2+JVofWt3JA/iie7FvfigR5xtu8vn0mK5ULspb6Poi3KkDmDegZb0PeQ6O9AuYxhngzMusxnm1+eDyvNGtl1s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XVhudHpw4bfu4oFDc5m473fn4/+uMQLBx7MeaifIHuw0EqJYOMNo4aEZ+NGikUhCOs3K7hLyWBPMfxuCMCTCEgzwFJxwfW3A+yP4qKAuGxVyTOLT9riFh+kDGOBdFxhP+pg6lygng+DZ5hlww+ywDS8z2jc8oEsWje0eT1sw4vKytMn2S2500QZYLACFAAARAAARAAARAAKRDEujYrx+Auem1HfDtPRg6pd4mau47xHqlcThn2vqVnbz7CRYZEpJNi93DL3YmVR/rqN/VIceBNdgdSXW+Nuw5lYPjQhfWD4gM37oIQG2Sj7EHJ/6iK4h9Iu1NjbJBOk+PNA9/IQZwSdZIaNzBlKP+m+vKVrjUFAQmeOS5rxTdvBk98P9f1FKWxR0QLx3Yq7sM8+hSd04tykvbrH2MsIJipkPb39VXG2xyEWj4+zrIz1fhlufF+nCYf6blvaepimr/VU/dSf2I5OF/Fyj8yGbzpGEAABEAABEAABEAABEAABEAABEAABEAA5H8F8p8AAwD2WseknbBRCwAAAABJRU5ErkJggg==",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Thursday, May 16, 2019