AWS RDS SQL Server - Restore from S3 Bucket

Octopus.Script exported 2020-08-18 by octobob belongs to ‘AWS’ category.

Will restore a database backup from an S3 bucket

Parameters

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

SQL Server

rdsSqlRestoreBackupSqlServer =

The SQL Server to perform the work on

SQL Login

rdsSqlRestoreBackupSqlLoginUserWhoHasCreateUserRights =

The login of the user who has permissions to create a database.

Leave blank for integrated security

SQL Password

rdsSqlRestoreBackupSqlLoginPasswordWhoHasRights =

The password of the user who has permissions to create SQL Logins

Leave blank for integrated security

Database Name

rdsSqlRestoreBackupDatabaseName =

The name of the database to restore to

S3 Bucket Name

rdsSqlRestoreBackupS3Bucket =

The name of the bucket (including any sub directories).

Backup File Name and Extension

rdsSqlRestoreBackupFileName =

The name of the back up file (including the extension).

Script body

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

Write-Host "SqlLoginWhoHasRights $rdsSqlRestoreBackupSqlLoginUserWhoHasCreateUserRights"
Write-Host "CreateSqlServer $rdsSqlRestoreBackupSqlServer"
Write-Host "CreateDatabaseName $rdsSqlRestoreBackupDatabaseName"
Write-Host "Backup S3 Bucket $rdsSqlRestoreBackupS3Bucket"
Write-Host "Backup File Name $rdsSqlRestoreBackupFileName"

if ([string]::IsNullOrWhiteSpace($rdsSqlRestoreBackupSqlLoginUserWhoHasCreateUserRights) -eq $true){
	Write-Host "No username found, using integrated security"
    $connectionString = "Server=$rdsSqlRestoreBackupSqlServer;Database=msdb;integrated security=true;"
}
else {
	Write-Host "Username found, using SQL Authentication"
    $connectionString = "Server=$rdsSqlRestoreBackupSqlServer;Database=msdb;User ID=$rdsSqlRestoreBackupSqlLoginUserWhoHasCreateUserRights;Password=$rdsSqlRestoreBackupSqlLoginPasswordWhoHasRights;"
}

$sqlConnection = New-Object System.Data.SqlClient.SqlConnection
$sqlConnection.ConnectionString = $connectionString

$command = New-Object System.Data.SqlClient.SqlCommand("dbo.rds_restore_database", $sqlConnection)
$command.CommandType = [System.Data.CommandType]'StoredProcedure'

$backupDestParamValue = "arn:aws:s3:::$rdsSqlRestoreBackupS3Bucket/$rdsSqlRestoreBackupFileName"
$command.Parameters.AddWithValue("s3_arn_to_restore_from", $backupDestParamValue)
$command.Parameters.AddWithValue("with_norecovery", 0)
$command.Parameters.AddWithValue("restore_db_name", $rdsSqlRestoreBackupDatabaseName)

$taskStatusCommand = New-Object System.Data.SqlClient.SqlCommand("dbo.rds_task_status", $sqlConnection)
$taskStatusCommand.CommandType = [System.Data.CommandType]'StoredProcedure'
$taskStatusCommand.Parameters.AddWithValue("db_name", $rdsSqlRestoreBackupDatabaseName)

$taskStatusAdapter = New-Object System.Data.SqlClient.SqlDataAdapter $taskStatusCommand

Write-Host "Opening the connection to $rdsSqlRestoreBackupSqlServer"
$sqlConnection.Open()
    
Write-Host "Executing backup"
$command.ExecuteNonQuery()

Write-Host "Closing the connection to $rdsSqlRestoreBackupSqlServer"
$sqlConnection.Close()

Write-Host "Getting status of backup"
$backupIsActive = $true

While ($backupIsActive)
{
	Write-Host "Opening the connection to $rdsSqlRestoreBackupSqlServer"
	$sqlConnection.Open()
    
    $taskStatusDataSet = New-Object System.Data.DataSet
	$taskStatusAdapter.Fill($taskStatusDataSet)
    $taskStatus = $taskStatusDataSet.Tables[0].Rows[0]["lifecycle"]
    $taskComplete = $taskStatusDataSet.Tables[0].Rows[0]["% complete"]
    
    Write-Host "The task is $taskComplete% complete."
    $backupIsActive = $taskStatus -eq "CREATED" -or $taskStatus -eq "IN_PROGRESS"
    
    Write-Host "Closing the connection to $rdsSqlRestoreBackupSqlServer"
	$sqlConnection.Close()
    
    Start-Sleep -Seconds 5
}

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": "55848421-44b9-403c-b1f0-ba8a84b1f177",
  "Name": "AWS RDS SQL Server - Restore from S3 Bucket",
  "Description": "Will restore a database backup from an S3 bucket",
  "Version": 2,
  "ExportedAt": "2020-08-18T13:27:31.393Z",
  "ActionType": "Octopus.Script",
  "Author": "octobob",
  "Packages": [],
  "Parameters": [
    {
      "Id": "3e45bb88-3632-4115-a0d5-54680615f0ca",
      "Name": "rdsSqlRestoreBackupSqlServer",
      "Label": "SQL Server",
      "HelpText": "The SQL Server to perform the work on",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "880a4e73-64fe-4a36-b4c3-a281b64e3c23",
      "Name": "rdsSqlRestoreBackupSqlLoginUserWhoHasCreateUserRights",
      "Label": "SQL Login",
      "HelpText": "The login of the user who has permissions to create a database.\n\nLeave blank for integrated security",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "6689fbe5-f47a-4800-945d-df50fc19c7b0",
      "Name": "rdsSqlRestoreBackupSqlLoginPasswordWhoHasRights",
      "Label": "SQL Password",
      "HelpText": "The password of the user who has permissions to create SQL Logins\n\nLeave blank for integrated security",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "Sensitive"
      }
    },
    {
      "Id": "1a358cca-5cd1-41ae-b763-fcaf2c7350f9",
      "Name": "rdsSqlRestoreBackupDatabaseName",
      "Label": "Database Name",
      "HelpText": "The name of the database to restore to",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "c5e2fa69-1b42-4427-91d2-10e1a13af744",
      "Name": "rdsSqlRestoreBackupS3Bucket",
      "Label": "S3 Bucket Name",
      "HelpText": "The name of the bucket (including any sub directories).",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "ea9d9eb8-9c0f-4c69-a6d4-4d00e43383af",
      "Name": "rdsSqlRestoreBackupFileName",
      "Label": "Backup File Name and Extension",
      "HelpText": "The name of the back up file (including the extension).",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptBody": "Write-Host \"SqlLoginWhoHasRights $rdsSqlRestoreBackupSqlLoginUserWhoHasCreateUserRights\"\nWrite-Host \"CreateSqlServer $rdsSqlRestoreBackupSqlServer\"\nWrite-Host \"CreateDatabaseName $rdsSqlRestoreBackupDatabaseName\"\nWrite-Host \"Backup S3 Bucket $rdsSqlRestoreBackupS3Bucket\"\nWrite-Host \"Backup File Name $rdsSqlRestoreBackupFileName\"\n\nif ([string]::IsNullOrWhiteSpace($rdsSqlRestoreBackupSqlLoginUserWhoHasCreateUserRights) -eq $true){\n\tWrite-Host \"No username found, using integrated security\"\n    $connectionString = \"Server=$rdsSqlRestoreBackupSqlServer;Database=msdb;integrated security=true;\"\n}\nelse {\n\tWrite-Host \"Username found, using SQL Authentication\"\n    $connectionString = \"Server=$rdsSqlRestoreBackupSqlServer;Database=msdb;User ID=$rdsSqlRestoreBackupSqlLoginUserWhoHasCreateUserRights;Password=$rdsSqlRestoreBackupSqlLoginPasswordWhoHasRights;\"\n}\n\n$sqlConnection = New-Object System.Data.SqlClient.SqlConnection\n$sqlConnection.ConnectionString = $connectionString\n\n$command = New-Object System.Data.SqlClient.SqlCommand(\"dbo.rds_restore_database\", $sqlConnection)\n$command.CommandType = [System.Data.CommandType]'StoredProcedure'\n\n$backupDestParamValue = \"arn:aws:s3:::$rdsSqlRestoreBackupS3Bucket/$rdsSqlRestoreBackupFileName\"\n$command.Parameters.AddWithValue(\"s3_arn_to_restore_from\", $backupDestParamValue)\n$command.Parameters.AddWithValue(\"with_norecovery\", 0)\n$command.Parameters.AddWithValue(\"restore_db_name\", $rdsSqlRestoreBackupDatabaseName)\n\n$taskStatusCommand = New-Object System.Data.SqlClient.SqlCommand(\"dbo.rds_task_status\", $sqlConnection)\n$taskStatusCommand.CommandType = [System.Data.CommandType]'StoredProcedure'\n$taskStatusCommand.Parameters.AddWithValue(\"db_name\", $rdsSqlRestoreBackupDatabaseName)\n\n$taskStatusAdapter = New-Object System.Data.SqlClient.SqlDataAdapter $taskStatusCommand\n\nWrite-Host \"Opening the connection to $rdsSqlRestoreBackupSqlServer\"\n$sqlConnection.Open()\n    \nWrite-Host \"Executing backup\"\n$command.ExecuteNonQuery()\n\nWrite-Host \"Closing the connection to $rdsSqlRestoreBackupSqlServer\"\n$sqlConnection.Close()\n\nWrite-Host \"Getting status of backup\"\n$backupIsActive = $true\n\nWhile ($backupIsActive)\n{\n\tWrite-Host \"Opening the connection to $rdsSqlRestoreBackupSqlServer\"\n\t$sqlConnection.Open()\n    \n    $taskStatusDataSet = New-Object System.Data.DataSet\n\t$taskStatusAdapter.Fill($taskStatusDataSet)\n    $taskStatus = $taskStatusDataSet.Tables[0].Rows[0][\"lifecycle\"]\n    $taskComplete = $taskStatusDataSet.Tables[0].Rows[0][\"% complete\"]\n    \n    Write-Host \"The task is $taskComplete% complete.\"\n    $backupIsActive = $taskStatus -eq \"CREATED\" -or $taskStatus -eq \"IN_PROGRESS\"\n    \n    Write-Host \"Closing the connection to $rdsSqlRestoreBackupSqlServer\"\n\t$sqlConnection.Close()\n    \n    Start-Sleep -Seconds 5\n}"
  },
  "Category": "AWS",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/aws-rds-restore-s3.json",
  "Website": "/step-templates/55848421-44b9-403c-b1f0-ba8a84b1f177",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADNQTFRF////9o0R/eLD/Nu0/erS95Qg+bhr95sv/vHh+r96/vjw+bFc/NSl+KI++82W+saI+KpNeDqM1wAAA41JREFUeNrsnG2XazAURiuo0Cr//9feliIvR3DvXJFZe3+a6XpW5+xWEpyY2w0AAAAAAAAAAAAAAAAAAADgf1J0bda/9N70q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TNRidhlE0rYAAAAAAAAAAAAAAAAAAACAZPkjwAAMDi+bsnPP/wAAAABJRU5ErkJggg==",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Tuesday, August 18, 2020