-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathSQL-ConfigureDatabaseMirroring.ps1
397 lines (309 loc) · 14.6 KB
/
SQL-ConfigureDatabaseMirroring.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
###########################################################################################
# Scenario: Configure Database mirroring
#
# How to use this powershell script:
# - Launch SQL Server PowerShell ( Start -> Run -> sqlps.exe)
# - Copy the following powershell script and save to file (Ex c:\DBMirrroringSetup.ps1)
# - in powershell window, type in script file path ( Ex: c:\DBMirrroringSetup.ps1) to run the script
###########################################################################################
# Backup Database
function BackupDatabase
{
Param([string]$servername, [string]$dbName, [string]$backupfiledir, [Microsoft.SqlServer.Management.Smo.BackupActionType]$actionType)
$server = GetServer($servername)
# construct a unique backup file name
$backupfilepath = $backupfiledir + "\" + $dbName + "-" + $actionType.ToString() + "-" +[DateTime]::Now.ToString('s').Replace(":","-") + ".bak"
$backup = new-object ('Microsoft.SqlServer.Management.Smo.Backup')
$backup.Action = $actionType
$backup.Database = $dbName
$backup.Devices.AddDevice($backupfilepath, [Microsoft.SqlServer.Management.Smo.DeviceType]::File)
$backup.SqlBackup($server)
write-host "Backup completed successfully"
write-host "Server:",$server.Name
write-host "Database:$dbName"
write-host "Backup File:$backupfilepath"
$backupfilepath;
}
# Restore Database
function RestoreDatabase
{
Param([string]$servername, [string]$dbName, [string]$backupDataFile)
$server = GetServer($servername)
$targetDBFilePath = $server.MasterDBPath + "\" + $dbName + "-Data-" + [DateTime]::Now.ToString('s').Replace(":","-") + ".mdf"
$targetLogFilePath = $server.MasterDBLogPath + "\" + $dbName + "-Log-" + [DateTime]::Now.ToString('s').Replace(":","-") + ".ldf"
$restore = new-object ('Microsoft.SqlServer.Management.Smo.Restore')
$restore.Database = $dbName
$restore.Devices.AddDevice($backupDataFile, [Microsoft.SqlServer.Management.Smo.DeviceType]::File)
$relocateDataFile = new-object ('Microsoft.SqlServer.Management.Smo.RelocateFile')($dbName, $targetDBFilePath)
$logFileName = $dbName + "_Log"
$relocateLogFile = new-object ('Microsoft.SqlServer.Management.Smo.RelocateFile')($logFileName, $targetLogFilePath)
$restore.RelocateFiles.Add($relocateDataFile)
$restore.RelocateFiles.Add($relocateLogFile)
$restore.ReplaceDatabase = $True
$restore.NoRecovery = $True
$restore.SqlRestore($server)
write-host "Restore completed successfully"
write-host "Server:", $server.Name
write-host "Database:$dbName"
}
# Create Endpoints for Database mirroring and grant permissions
function CreateDBMirroringEndPoint
{
Param([string]$servername, [Microsoft.SqlServer.Management.Smo.ServerMirroringRole]$mirroringRole)
$server = GetServer($servername)
$tcpPort = GetNextAvailableTCPPort $servername
$endPointName = "Database_Mirroring_" + [DateTime]::Now.ToString('s').Replace(":","-")
$endpoint = new-object ('Microsoft.SqlServer.Management.Smo.EndPoint')($server, $endPointName)
$endpoint.ProtocolType = [Microsoft.SqlServer.Management.Smo.ProtocolType]::Tcp
$endpoint.EndpointType = [Microsoft.SqlServer.Management.Smo.EndpointType]::DatabaseMirroring
$endpoint.Protocol.Tcp.ListenerPort = $tcpPort
$endpoint.Payload.DatabaseMirroring.ServerMirroringRole = $mirroringRole
$endpoint.Create()
$endpoint.Start()
# TCP:Server:port
$fullyQualifiedName = "TCP://" + $server.NetName + ":" + $tcpPort
$fullyQualifiedName;
}
# Get DB Mirroring Endpoint ( if configured already)
# Note: we can have only one DB mirroring end point per sql instance
function GetFullyQualifiedMirroringEndpoint
{
Param([string]$serverInstance, [Microsoft.SqlServer.Management.Smo.ServerMirroringRole]$mirroringRole)
$fullyQualifiedMirroringEndPointName = ""
$EndPointList = GetEndPointList $serverInstance
$server = GetServer $serverInstance
if($EndPointList -eq $null)
{
$fullyQualifiedMirroringEndPointName = CreateDBMirroringEndPoint $serverInstance $mirroringRole
}
else
{
foreach($endPoint in $EndPointList)
{
$fullyQualifiedMirroringEndPointName = "TCP://" + $server.NetName + ":" + $endPoint.Properties["ListenerPort"].Value
break
}
}
write-host "Server Name:$serverInstance"
write-host "Mirroring Role:$mirroringRole"
write-host "EndPointName:$fullyQualifiedMirroringEndPointName"
$fullyQualifiedMirroringEndPointName;
}
# Get EndPoint List
function GetEndPointList
{
Param([string]$servername)
$server = GetServer($servername)
$PSPath = $server.PsPath + "\EndPoints"
$EndPointList = @()
$AllEndPoints = dir $PSPath
foreach($endpoint in $AllEndPoints)
{
$EndPointList += $endpoint.Protocol.Tcp
}
$EndPointList;
}
# Get Next available port
function GetNextAvailableTCPPort
{
Param([string]$serverInstance)
$measure = GetEndPointList $serverInstance | measure-object ListenerPort -max
if($measure.Maximum -eq $null)
{
$maxPort = 5000
}
else
{
$maxPort = $measure.Maximum
}
#choose a random port that is greater than the current max port
$maxPort + (new-object random).Next(1,500)
}
# Get Server object
function GetServer
{
Param([string]$serverInstance)
$array = $serverInstance.Split("\")
if([String]::IsNullOrEmpty($serverInstance))
{
write-error "Server instance name is not valid"
return
}
if($array.Length -eq 1)
{
$machineName = $array[0]
$instanceName = "DEFAULT"
}
else
{
$machineName = $array[0]
$instanceName = $array[1]
}
$PSPath = "\SQL\" + $machineName + "\" + $instanceName
$server = get-item $PSPath
CheckForErrors
$server;
}
# Set Recovery Model for given database
function SetRecoveryModel
{
Param($serverInstance, $dbName, [Microsoft.SqlServer.Management.Smo.RecoveryModel]$recoveryModel)
write-host "Setting", $recoveryModel, "Recovery model for database:", $dbName
$server = GetServer($serverInstance)
$PSPath = $server.PsPath + "\Databases\" + $dbName
$db = get-item $PSPath
$db.RecoveryModel = $recoveryModel
$db.Alter()
write-host "[SetRecoveryModel:] OK"
CheckForErrors
}
# Set Partner, Witness
function SetMirroringPartner
{
Param($serverInstance, $dbName, $fqName, [bool]$isPartner)
$server = GetServer($serverInstance)
$PSPath = $server.PsPath + "\Databases\" + $dbName
$db = get-item $PSPath
if($isPartner -eq $True)
{
$db.MirroringPartner = $fqName
}
else
{
$db.MirroringWitness = $fqName
}
$db.Alter()
}
# Reports Errors
function CheckForErrors
{
$errorsReported = $False
if($Error.Count -ne 0)
{
write-host "******************************"
write-host "Errors:", $Error.Count
write-host "******************************"
foreach($err in $Error)
{
$errorsReported = $True
if( $err.Exception.InnerException -ne $null)
{
write-host $err.Exception.InnerException.ToString()
}
else
{
write-host $err.Exception.ToString()
}
write-host "----------------------------------------------"
}
throw
}
}
# Perform initial validation checks to make sure that all input parameters are valid
function PerformValidation
{
Param($primary , $mirror, $witness, $shareName, $dbName)
#Clear any errors
$Error.Clear()
write-host "Performing Validation checks..."
$primaryServer = GetServer $primary
$PSPath = $primaryServer.PsPath + "\Databases\" + $dbName
$primaryDatabase = get-item $PSPath
write-host "Checking if Database:$dbName on Primary:$primary is not mirrored..."
if($primaryDatabase.MirroringStatus -ne [Microsoft.SqlServer.Management.Smo.MirroringStatus]::None)
{
$errorMessage = "Cannot setup mirroring on database due to its current MirroringState:" + $primaryDatabase.MirroringStatus
throw $errorMessage
}
write-host "[$dbName on Primary:$primary is not mirrored Check:] OK"
if($primaryDatabase.Status -ne [Microsoft.SqlServer.Management.Smo.DatabaseStatus]::Normal)
{
$errorMessage = "Cannot setup mirroring on database due to its current Status:" + $primaryDatabase.Status
throw $errorMessage
}
write-host "Checking if Database:$dbName does not exist on Mirror:$mirror..."
$mirrorServer = GetServer $mirror
$PSPath = $mirrorServer.PsPath + "\Databases\"
$mirrorDatabase= get-childitem $PSPath | where {$_.Name -eq $dbName}
if($mirrorDatabase -ne $null)
{
$dbMeasures = $mirrorDatabase | measure-object
if($dbMeasures.Count -ne 0)
{
$errorMessage = "Database:" + $dbName + " already exists on mirror server:" + $mirror
throw $errorMessage
}
}
write-host "[$dbName does not exist on Mirror:$mirror Check:] OK"
write-host "Checking if Witness Server exists..."
$witnessServer = GetServer $witness
write-host "[Witness Server Existence Check:] OK"
write-host "Checking if File Share:$ShareName exists..."
if([System.IO.Directory]::Exists($shareName) -ne $True)
{
$errorMessage = "Share:" + $shareName + " does not exists"
throw $errorMessage
}
write-host "[File Share Existence Check:] OK"
CheckForErrors
}
# Configure Database mirroring; if input params are not passed into this function,
# those values are read from user entered text from console
function ConfigureDatabaseMirroring
{
Param([string]$primary = $(Read-Host "Primary SQL Instance(like server\instance)") ,
[string]$mirror = $(Read-Host "Mirror SQL Instance(like server\instance)") ,
[string]$witness = $(Read-Host "Witness SQL Instance(like server\instance)") ,
[string]$shareName = $(Read-Host "Share Path(unc path like \\server\share)") ,
[string]$dbName = $(Read-Host "Database Name")
)
write-host
write-host "============================================================="
write-host " 1: Performing Initial checks; validating input parameters"
write-host "============================================================="
PerformValidation $primary $mirror $witness $shareName $dbName
write-host
write-host "============================================================="
write-host " 2: Set Recovery Model as FULL on primary database"
write-host "============================================================="
$fullRecoveryModelType = [Microsoft.SqlServer.Management.Smo.RecoveryModel]::Full
SetRecoveryModel $primary $dbName $fullRecoveryModelType
write-host
write-host "============================================================="
write-host " 3: Perform Full Database backup from Primary instance"
write-host "============================================================="
$backupActionType = [Microsoft.SqlServer.Management.Smo.BackupActionType]::Database
$primaryBackupDataFile = BackupDatabase $primary $dbName $shareName $backupActionType
write-host
write-host "============================================================="
write-host " 4: Restore Database backup on Mirror"
write-host "============================================================="
RestoreDatabase $mirror $dbName $primaryBackupDataFile
write-host
write-host "============================================================="
write-host " 5: Create endpoints for database mirroring"
write-host "============================================================="
$mirroringRole = [Microsoft.SqlServer.Management.Smo.ServerMirroringRole]::Partner
$primaryFQName = GetFullyQualifiedMirroringEndpoint $primary $mirroringRole
$mirrorFQName = GetFullyQualifiedMirroringEndpoint $mirror $mirroringRole
$mirroringRole = [Microsoft.SqlServer.Management.Smo.ServerMirroringRole]::Witness
$witnessFQName = GetFullyQualifiedMirroringEndpoint $witness $mirroringRole
write-host
write-host "============================================================="
write-host " 6: Set Primary, Mirror, Witness states in database"
write-host "============================================================="
write-host "Connecting to Mirror and set Primary as partner ..."
SetMirroringPartner $mirror $dbName $primaryFQName $True
write-host "Connecting to Primary, set partner as mirror ..."
SetMirroringPartner $primary $dbName $mirrorFQName $True
write-host "Connecting to Primary, set partner as witness ..."
SetMirroringPartner $primary $dbName $witnessFQName $False
write-host
write-host "============================================================="
write-host " Database:$dbName mirrored successfully."
write-host "============================================================="
}
################################################
# Configure Database mirroring
################################################
ConfigureDatabaseMirroring