-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql-operations.ps1
397 lines (329 loc) · 14 KB
/
sql-operations.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
<###[Parameters]################################>
param(
[parameter(Mandatory=$true, HelpMessage="-Server parameter is required")]
[string]$Server,
[parameter(Mandatory=$true, HelpMessage="-Database parameter is required")]
[string]$Database,
[int]$QueryTimeout = 30,
<# RunScript #>
[switch]$Exec,
<#[ValidateScript({Test-Path $_})]#>
[string]$ScriptFile,
<# Apply Permissions #>
[switch]$ApplyPermissions,
<# Backup #>
[switch]$BackupDB,
[string]$backupFilename,
<# CopyDB #>
[switch]$CopyDB,
[switch]$UseLastBackup,
[switch]$RecoveryModelSimple,
[string]$SourceServer,
[string]$SourceDatabase,
<# Replication #>
[switch]$CreateMergeReplicationPublisher,
[switch]$DropMergeReplicationPublisher,
[switch]$CreateMergeReplicationSubscriber,
[switch]$DropMergeReplicationSubscriber,
[switch]$CreateTransReplicationPublisher,
[switch]$DropTransReplicationPublisher,
[switch]$CreateTransReplicationSubscriber,
[switch]$DropTransReplicationSubscriber,
[string]$Publisher,
[string[]]$Subscribers,
[int]$StartTime
)
<###[Variables]#################################>
$Localhost = $env:computername
$LogFile = "$PSScriptRoot\logs\$(Get-Date -format "yyyy-MM-dd").log"
$script:ModulePath = "$PSScriptRoot\modules"
$script:SqlOpsPath = "$PSScriptRoot\sql-commands"
$script:UserScriptPath = "$PSScriptRoot\user-scripts"
<###[Load Modules]##############################>
Import-Module sqlps
Import-Module $script:ModulePath\utilities.ps1
Import-Module $script:ModulePath\sql.ps1
<###[Set Paths]#################################>
$script:SQLBackupPath = Get-BackupPath -Server $Server
$script:SQLDataPath = Get-DataPath -Server $Server
$script:SQLLogPath = Get-LogPath -Server $Server
$script:RemoteBackupPath = $null
<###[Functions]#################################>
Function Test-Parameters
{
if (($script:Exec) -and !($script:ScriptFile))
{
Write-Error -Message "-ScriptFile parameter not set."
exit
}
if (($script:BackupDB) -and !($script:backupFilename))
{
Write-Error -Message "-backupFilename parameter not set."
exit
}
if (($script:CopyDB))
{
if (!$script:SourceServer)
{
Write-Error -Message "-SourceServer parameter is required."
exit
}
if (!$script:SourceDatabase)
{
Write-Error -Message "-SourceDatabase parameter is required."
exit
}
if ($script:Localhost -eq $script:SourceServer)
{
Write-Error -Message "-SourceServer must be a remote server to copy from."
exit
}
if (!$script:SQLBackupPath)
{
Write-Error -Message "Unable to get local backup path."
exit
}
if (!$script:SQLDataPath)
{
Write-Error -Message "Unable to get local data(MDF) file path."
exit
}
if (!$script:SQLLogPath)
{
Write-Error -Message "Unable to get local log(LDF) file path."
exit
}
$script:RemoteBackupPath = Get-RemoteBackupPath -Server $script:SourceServer -Database "master" -InputFilePath $script:SqlOpsPath
if (!$script:RemoteBackupPath)
{
Write-Error "Unable to get remote backup path."
exit
}
}
if (($script:CreateMergeReplicationPublisher))
{
if (!($script:StartTime))
{
Write-Error -Message "-StartTime parameter is required."
exit
}
if (!($script:Subscribers))
{
Write-Error -Message "-Subscribers parameter is required."
exit
}
}
if (($script:DropMergeReplicationPublisher) -and !($script:Publisher))
{
Write-Error -Message "-Publisher parameter is required."
exit
}
if (($script:DropTransReplicationPublisher) -and !($script:Publisher))
{
Write-Error -Message "-Publisher parameter is required."
exit
}
if (($script:CreateMergeReplicationSubscriber))
{
if (!($script:Publisher))
{
Write-Error -Message "-Publisher parameter is required."
exit
}
if (!($script:StartTime))
{
Write-Error -Message "-StartTime parameter is required."
exit
}
}
if (($script:DropMergeReplicationSubscriber) -and !($script:Publisher))
{
Write-Error -Message "-Publisher parameter is required."
exit
}
if (($script:DropTransReplicationSubscriber) -and !($script:Publisher))
{
Write-Error -Message "-Publisher parameter is required."
exit
}
}
Function Show-Script-Header
{
Write-Verbose "=============================================================================="
Write-Verbose " Powershell SQL Automation"
Write-Verbose "=============================================================================="
Write-Verbose "SQL Server : $($script:Server)"
Write-Verbose "Database : $($script:Database)"
Write-Verbose "Query Timeout : $($script:QueryTimeout)"
Write-Verbose "Data file location : $($script:SQLDataPath)"
Write-Verbose "Log file location : $($script:SQLLogPath)"
Write-Verbose "Backup file location : $($script:SQLBackupPath)"
if ($script:ApplyPermissions)
{
Write-Verbose "Apply Permissions : $($script:ApplyPermissions)"
}
if ($script:BackupDB)
{
Write-Verbose "Backup DB : $($script:BackupDB)"
Write-Verbose "Backup Filename : $($script:backupFilename)"
}
if ($script:CopyDB)
{
Write-Verbose "Copy DB : $($script:CopyDB)"
Write-Verbose "Use last backup : $($script:UseLastBackup)"
Write-Verbose "Source Server : $($script:SourceServer)"
Write-Verbose "Source Database : $($script:SourceDatabase)"
}
if ($script:Exec)
{
Write-Verbose "Action : Exec"
Write-Verbose "Script File : $($script:ScriptFile)"
}
if ($script:DropMergeReplicationPublisher)
{
Write-Verbose "Drop merge replication (Publisher) : $($script:DropMergeReplicationPublisher)"
Write-Verbose "Publisher : $($script:Publisher)"
Write-Verbose "Subscribers : $($script:Subscribers)"
}
if ($script:DropMergeReplicationSubscriber)
{
Write-Verbose "Drop merge replication (Subscriber) : $($script:DropMergeReplicationSubscriber)"
Write-Verbose "Publisher : $($script:Publisher)"
}
if ($script:CreateMergeReplicationPublisher)
{
Write-Verbose "Create merge replication (Publisher) : $($script:CreateMergeReplicationPublisher)"
Write-Verbose "Publisher : $($script:Publisher)"
}
if ($script:CreateMergeReplicationSubscriber)
{
Write-Verbose "Create merge replication (Subscriber) : $($script:CreateMergeReplicationSubscriber)"
Write-Verbose "Publisher : $($script:Publisher)"
Write-Verbose "Start Time : $($script:StartTime)"
}
if ($script:DropTransReplicationPublisher)
{
Write-Verbose "Drop trans replication (Publisher) : $($script:DropTransReplicationPublisher)"
Write-Verbose "Publisher : $($script:Publisher)"
Write-Verbose "Subscribers : $($script:Subscribers)"
}
if ($script:DropTransReplicationSubscriber)
{
Write-Verbose "Drop trans replication (Subscriber) : $($script:DropTransReplicationSubscriber)"
Write-Verbose "Publisher : $($script:Publisher)"
}
if ($script:CreateTransReplicationPublisher)
{
Write-Verbose "Create trans replication (Publisher) : $($script:CreateTransReplicationPublisher)"
Write-Verbose "Publisher : $($script:Publisher)"
}
Write-Verbose "---------------------------------------------------`n"
}
Function Start-Main
{
Show-Script-Header
if ($script:BackupDB)
{
TimedCommandBlock "Backup database ($($Database))..." { Start-Backup -Server $script:Server -Database $script:Database -InputFilePath $script:SqlOpsPath -BackupPath $script:SQLBackupPath -BackupFilename $backupFilename }
}
if ($script:CopyDB)
{
$BackupLocationUNCPath = "\\$($script:SourceServer)\$($script:RemoteBackupPath.Path.Replace(':', '$'))\$SourceDatabase"
if ($UseLastBackup)
{
$LastFullBackup = Get-NewestFile -Path $BackupLocationUNCPath -Filter "*.bak"
$BackupFilename = $LastFullBackup.Name
}
else
{
$BackupFilename = "$($script:SourceDatabase).bak"
TimedCommandBlock "Backup database ($($SourceDatabase))..." { Start-Backup -Server $script:SourceServer -Database $script:SourceDatabase -InputFilePath $script:SqlOpsPath -BackupPath $script:SQLBackupPath -BackupFilename $BackupFilename }
TimedCommandBlock "Wait for backup to finish writing to disk..." { timeout 30 /nobreak }
}
if (!$BackupFilename)
{
Write-Error "Unable to get last backup file."
exit
}
TimedCommandBlock "Copying database backup ($($BackupFilename))..." { robocopy "$BackupLocationUNCPath" "$SQLBackupPath\$Database" "$BackupFilename" /z /np | Out-Default }
TimedCommandBlock "Restore DB backup..." { Start-Restore -Server $script:Server -Database $script:Database -InputFilePath $script:SqlOpsPath -DataPath $script:SQLDataPath -LogPath $script:SQLLogPath -BackupPath "$script:SQLBackupPath\$Database" -BackupFileName "$BackupFilename" }
TimedCommandBlock "Set Permissions on $($Database)..." { Set-Permissions -Server $script:Server -Database $script:Database -InputFilePath $script:SqlOpsPath }
TimedCommandBlock "Removing file ($($BackupFilename))..." { Remove-Item "$SQLBackupPath\$Database\$BackupFilename" }
if ($RecoveryModelSimple)
{
TimedCommandBlock "Setting Recovery model to Simple ($($Database))..." { Set-RecoveryModel -Server $script:Server -Database $script:Database }
}
}
if ($script:DropMergeReplicationSubscriber)
{
TimedCommandBlock "Drop subscription for [$($script:Database)] from [$($script:Server)] to [$($script:Publisher)]..." { Start-DropMergeReplicationSubscriber -Server $script:Server -Database $script:Database -InputFilePath $script:SqlOpsPath -Publisher $script:Publisher }
}
if ($script:DropMergeReplicationPublisher)
{
$subs = Get-MergeReplicationSubscribers -Server $script:Server -Database $script:Database
foreach($sub in $subs)
{
TimedCommandBlock "Drop subscriber replication from [$($sub.subscriber_server)]..." { Start-DropMergeReplicationSubscriber -Server $sub.subscriber_server -Database $script:Database -InputFilePath $script:SqlOpsPath -Publisher $script:Server }
}
TimedCommandBlock "Drop publisher replication..." { Start-DropMergeReplicationPublisher -Server $script:Server -Database $script:Database -InputFilePath $script:SqlOpsPath }
}
if ($script:DropTransReplicationSubscriber)
{
TimedCommandBlock "Drop subscription for [$($script:Database)] from [$($script:Server)] to [$($script:Publisher)]..." { Start-DropTransReplicationSubscriber -Server $script:Server -Database $script:Database -InputFilePath $script:SqlOpsPath -Publisher $script:Publisher }
}
if ($script:DropTransReplicationPublisher)
{
$subs = Get-TransReplicationSubscribers -Server $script:Server -Database $script:Database
foreach($sub in $subs)
{
TimedCommandBlock "Drop subscription from publisher..." { Start-DropTransReplicationPublisherSubscription -Server $script:Server -Database $script:Database -InputFilePath $script:SqlOpsPath -Subscriber $sub.subscriber_server }
}
TimedCommandBlock "Drop publication..." { Start-DropTransReplicationPublisher -Server $script:Server -Database $script:Database -InputFilePath $script:SqlOpsPath }
foreach($sub in $subs)
{
TimedCommandBlock "Drop subscription from [$($sub.subscriber_server)]..." { Start-DropTransReplicationSubscriber -Server $sub.subscriber_server -Database $script:Database -InputFilePath $script:SqlOpsPath -Publisher $script:Server }
}
}
if ($script:Exec)
{
$ScriptFilePath = "$($script:UserScriptPath)\$($script:ScriptFile)"
TimedCommandBlock "Executing script $($ScriptFilePath)..." { Invoke-SqlcmdFile -Server $script:Server -Database $script:Database -InputFilePath $ScriptFilePath -Timeout $script:QueryTimeout }
}
if ($script:CreateMergeReplicationPublisher)
{
$SQLReplDataPath = Get-DefaultReplicationPath -Server $script:Server -Database master
TimedCommandBlock "Deleting previous snapshots..." { Remove-Item "$SQLReplDataPath\unc\$($script:Server)_$($script:Database)_$($script:Database)MERGE\*" -recurse }
TimedCommandBlock "Create replication publication..." { Start-CreateMergeReplicationPublisher -Server $script:Server -Database $script:Database -InputFilePath $script:SqlOpsPath -Subscribers $script:Subscribers -Minutes $script:StartTime }
Write-Warning "[!] Please check that the snapshot agent job has finished before setting up the subscribers."
}
if ($script:CreateMergeReplicationSubscriber)
{
$defaultReplPath = Get-DefaultReplicationPath -Server $script:Publisher -Database master
$SQLReplDataPath = "$($defaultReplPath)\unc\$($Publisher)_$($Database)_$($Database)MERGE"
$SnapshotLocationPath = "\\$($script:Publisher)\$($SQLReplDataPath.Replace(':', '$'))"
TimedCommandBlock "Copy Snapshot from ($($Publisher))..." { robocopy "$SnapshotLocationPath" "$SQLReplDataPath" /mir /NP | Out-Default }
TimedCommandBlock "Create subscription on subscriber for ($($script:Database))..." { Start-CreateMergeReplicationSubscriber -Server $script:Server -Database $script:Database -InputFilePath $script:SqlOpsPath -Publisher $script:Publisher -Minutes $script:StartTime }
TimedCommandBlock "Set Permissions on ($($script:Database))..." { Set-Permissions -Server $script:Server -Database $script:Database -InputFilePath $script:SqlOpsPath }
}
if ($script:CreateTransReplicationPublisher)
{
$defaultReplPath = Get-DefaultReplicationPath -Server $script:Server -Database master
$SQLReplDataPath = "$($defaultReplPath)";
TimedCommandBlock "Deleting previous snapshots..." { Remove-Item "$SQLReplDataPath\unc\$($script:Server)_$($script:Database)_$($script:Database)_Transactional\*" -recurse }
TimedCommandBlock "Create transactional replication publication for [$($script:Database)]..." { Start-CreateTransReplicationPublisher -Server $script:Server -Database $script:Database -InputFilePath $script:SqlOpsPath -Subscribers $script:Subscribers }
Write-Warning "[!] Please check that the snapshot agent job has finished before setting up the subscribers."
}
if ($script:CreateTransReplicationSubscriber)
{
}
if ($script:ApplyPermissions)
{
TimedCommandBlock "Set Permissions on DB..." { Set-Permissions -Server $script:Server -Database $script:Database -InputFilePath $script:SqlOpsPath }
}
}
<###[Main Script]###############################>
Test-Parameters
Start-Log $LogFile
Start-Main
Stop-Log