forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNew-DbaLogShippingPrimarySecondary.ps1
156 lines (120 loc) · 6.24 KB
/
New-DbaLogShippingPrimarySecondary.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
function New-DbaLogShippingPrimarySecondary {
<#
.SYNOPSIS
New-DbaLogShippingPrimarySecondary adds an entry for a secondary database.
.DESCRIPTION
New-DbaLogShippingPrimarySecondary adds an entry for a secondary database.
This is executed on the primary server.
.PARAMETER SqlInstance
SQL Server instance. You must have sysadmin access and server version must be SQL Server version 2000 or greater.
.PARAMETER SqlCredential
Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted. To use:
$scred = Get-Credential, then pass $scred object to the -SqlCredential parameter.
To connect as a different Windows user, run PowerShell as that user.
.PARAMETER PrimaryDatabase
Is the name of the database on the primary server.
.PARAMETER SecondaryDatabase
Is the name of the secondary database.
.PARAMETER SecondaryServer
Is the name of the secondary server.
.PARAMETER SecondarySqlCredential
Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted. To use:
$scred = Get-Credential, then pass $scred object to the -SecondarySqlCredential parameter.
.PARAMETER WhatIf
Shows what would happen if the command were to run. No actions are actually performed.
.PARAMETER Confirm
Prompts you for confirmation before executing any changing operations within the command.
.PARAMETER EnableException
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
.NOTES
Author: Sander Stad (@sqlstad, sqlstad.nl)
Tags: Log shipping, primary database, secondary database
Website: https://dbatools.io
Copyright: (C) Chrissy LeMaire, clemaire@gmail.com
License: GNU GPL v3 https://opensource.org/licenses/GPL-3.0
.LINK
.EXAMPLE
New-DbaLogShippingPrimarySecondary -SqlInstance sql1 -PrimaryDatabase DB1 -SecondaryServer sql2 -SecondaryDatabase DB1_DR
#>
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = "Low")]
param (
[parameter(Mandatory = $true)]
[Alias("ServerInstance", "SqlServer")]
[object]$SqlInstance,
[System.Management.Automation.PSCredential]
$SqlCredential,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[object]$PrimaryDatabase,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[object]$SecondaryDatabase,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[object]$SecondaryServer,
[System.Management.Automation.PSCredential]
$SecondarySqlCredential,
[switch][Alias('Silent')]$EnableException
)
# Try connecting to the instance
Write-Message -Message "Attempting to connect to $SqlInstance" -Level Verbose
try {
$ServerPrimary = Connect-SqlInstance -SqlInstance $SqlInstance -SqlCredential $SqlCredential
}
catch {
Stop-Function -Message "Could not connect to Sql Server instance" -Target $SqlInstance -Continue
}
# Try connecting to the instance
Write-Message -Message "Attempting to connect to $SecondaryServer" -Level Verbose
try {
$ServerSecondary = Connect-SqlInstance -SqlInstance $SecondaryServer -SqlCredential $SecondarySqlCredential
}
catch {
Stop-Function -Message "Could not connect to Sql Server instance" -Target $SecondaryServer -Continue
}
# Check if the database is present on the source sql server
if ($ServerPrimary.Databases.Name -notcontains $PrimaryDatabase) {
Stop-Function -Message "Database $PrimaryDatabase is not available on instance $SqlInstance" -ErrorRecord $_ -Target $SqlInstance -Continue
}
# Check if the database is present on the destination sql server
if ($ServerSecondary.Databases.Name -notcontains $SecondaryDatabase) {
Stop-Function -Message "Database $SecondaryDatabase is not available on instance $SecondaryServer" -ErrorRecord $_ -Target $SecondaryServer -Continue
}
$Query = "SELECT primary_database FROM msdb.dbo.log_shipping_primary_databases WHERE primary_database = '$PrimaryDatabase'"
try {
Write-Message -Message "Executing query:`n$Query" -Level Verbose
$Result = $ServerPrimary.Query($Query)
if ($Result.Count -eq 0 -or $Result[0] -ne $PrimaryDatabase) {
Stop-Function -Message "Database $PrimaryDatabase does not exist as log shipping primary.`nPlease run New-DbaLogShippingPrimaryDatabase first." -ErrorRecord $_ -Target $SqlInstance -Continue
}
}
catch {
Stop-Function -Message "Error executing the query.`n$($_.Exception.Message)`n$Query" -ErrorRecord $_ -Target $SqlInstance -Continue
}
# Set the query for the log shipping primary and secondary
$Query = "EXEC master.sys.sp_add_log_shipping_primary_secondary
@primary_database = N'$PrimaryDatabase'
,@secondary_server = N'$SecondaryServer'
,@secondary_database = N'$SecondaryDatabase' "
if ($ServerPrimary.Version.Major -gt 9) {
$Query += ",@overwrite = 1;"
}
else {
$Query += ";"
}
# Execute the query to add the log shipping primary
if ($PSCmdlet.ShouldProcess($SqlInstance, ("Configuring logshipping connecting the primary database $PrimaryDatabase to secondary database $SecondaryDatabase on $SqlInstance"))) {
try {
Write-Message -Message "Configuring logshipping connecting the primary database $PrimaryDatabase to secondary database $SecondaryDatabase on $SqlInstance." -Level Output
Write-Message -Message "Executing query:`n$Query" -Level Verbose
$ServerPrimary.Query($Query)
}
catch {
Write-Message -Message "$($_.Exception.InnerException.InnerException.InnerException.InnerException.Message)" -Level Warning
Stop-Function -Message "Error executing the query.`n$($_.Exception.Message)`n$Query" -ErrorRecord $_ -Target $SqlInstance -Continue
}
}
Write-Message -Message "Finished configuring of primary database $PrimaryDatabase to secondary database $SecondaryDatabase." -Level Output
}