Scripting examples > Job creation scripts > Creating a SQL job

Creating a SQL job

The following sample script will create a simple SQL job. You will need to modify this script to fit your environment and configuration. If your source or target is a cluster, additional parameters must be added.

Be sure and expand your screen wide enough so that lines in the script file do not wrap.

 

# Sample script to create a simple SQL job

 

# Specify the variables to be used in the script

 

     # Source server and credentials

     $DtSourceName = "alpha"

     $DtSourceUserName = "domain\administrator"

     $DtSourcePassword = "password"

 

     # Target server and credentials

     $DtTargetName = "beta"

     $DtTargetUserName = "domain\administrator"

     $DtTargetPassword = "password"

 

     # Type of workload you will be protecting and type of job you will be creating

     $DtWorkloadType = "SQL"

     $DtJobType = "SQL"

 

# Import the Double-Take PowerShell module

# This may be \Service\ or \Console\ depending on your installation

Import-Module "C:\Program Files\Vision Solutions\Double-Take\Console\DoubleTake.PowerShell.dll"

 

 

# Create source and target objects

$DtSource = New-DtServer -Name $DtSourceName -UserName $DtSourceUserName -Password $DtSourcePassword

$DtTarget = New-DtServer -Name $DtTargetName -UserName $DtTargetUserName -Password $DtTargetPassword

 

# Create a workload

$DtWorkloadGUID = New-DtWorkload -ServiceHost $DtSource -WorkloadTypeName $DtWorkloadType

 

# Add what you want to protect to the workload. These lines will, by default, select all instances

$DtLogicalItem = Get-DtLogicalItem -ServiceHost $DtSource -WorkloadId $DtWorkloadGUID

$DtProtectionItems = Set-DtLogicalItemSelection -ServiceHost $DtSource -WorkloadId $DtWorkloadGuid -LogicalPath $DtLogicalItem.Path

$DtWorkload = Get-DtWorkload -ServiceHost $DtSource -WorkloadId $DtWorkloadGUID

 

# If you do not want to protect all of the instances, as the default does, comment out the

# three lines above and uncomment and use the following group of lines.

# $RootItems = Get-DtLogicalItem -ServiceHost $DtSource -WorkloadId $DtWorkloadGUID

# $RootItems | Format-List

# $RootItem = (Get-DtLogicalItem -ServiceHost $DtSource -WorkloadId $DtWorkloadGUID | Where-Object {$_.Path -eq "SQL:\"})[ 0 ]

# The following lines unselect all of the instances that were selected by default

# Get-DtLogicalItem -ServiceHost $DtSource -WorkloadId $DtWorkloadGUID -RefItem $RootItem `

#   | ForEach-Object {Set-DtLogicalItemSelection -ServiceHost $DtSource -WorkloadId $DtWorkloadGUID -LogicalPath $_.Path -Unselect}

# Select the instance that you want to protect by replacing instance_name with the name of the instance

# For example, SQL:\instance_name would be SQL:\PROD for an instance called PROD

# $DtProtectionItems = Set-DtLogicalItemSelection -ServiceHost $DtSource -WorkloadId $DtWorkloadGuid -LogicalPath "SQL:\instance_name"

# $DtWorkload = Get-DtWorkload -ServiceHost $DtSource -WorkloadId $DtWorkloadGUID

 

# Get the default job options that will be used to create the job

$DtJobOptions = Get-DtRecommendedJobOptions -ServiceHost $DtTarget -Source $DtSource -JobType $DtJobType -Workload $DtWorkload

 

# Create the job

$DtJobGuidForSQL = New-DtJob -ServiceHost $DtTarget -Source $DtSource -JobType $DtJobType -Options $DtJobOptions.JobOptions

 

# Start the job

Start-DtJob -ServiceHost $DtTarget -JobId $DtJobGuidForSQL

 

# Close the connections for the server objects

Disconnect-DtServer -ServiceHost $DtSource

Disconnect-DtServer -ServiceHost $DtTarget

If you want to hide your user credentials in your script, use the Windows PowerShell Get-Credential cmdlet. The password will not be visible because Windows stores an encrypted password. See Hiding your password in a PowerShell script for basic details on using this cmdlet. See your Windows PowerShell documentation for detailed instructions.