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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
| # $HeadURL: https://svn.barfoo.org/svn/scripts/netapp/snapvault-archive-mssql.ps1 $
# $Author: christian.th.heim $
# $Revision: 326 $
# $Date: 2012-12-27 21:20:00 +0100 (Do, 27 Dez 2012) $
Add-PSSNapin -Name "SqlServerCmdletSnapin100"
$DB_NAMES = $args
$ARCHIVE = "daily"
$OUTPATH="C:ScriptsTEMP"
$OUTFILE="$( $OUTPATH )snapvault-archive-mssql"
$SMTP_HOST = "smtprelay.barfoo.org"
$SMTP_SENDER = "christian.heim@barfoo.org"
$SMTP_RECEIVER = "christian.heim@barfoo.org"
$WAIT = 15
function clean-tempdir() {
param( [string]$outputpath, [string]$outputfile )
# Sanatize the environment, check if OUTPATH exists, otherwise create it.
# Check if OUTFILE is still there from a previous run, otherwise delete it.
if ( !(Test-Path $outputpath) ) {
New-Item $outputpath -type directory
}
if ( Test-Path $outputfile ) {
Remove-Item $outputfile
}
}
function get-db-mountpoint() {
# Lists all necessary mountpoints for a specified SQL database
param( [string] $dbname )
$mountpoints = Invoke-Sqlcmd `
-Database master -Query "SELECT DB_NAME(database_id) DatabaseName, physical_name FROM sys.master_files WHERE DB_NAME(database_id) LIKE '$( $dbname )%'"
# Remove junk from the end of the string
# Output looks something like this: F:sql_dbdb_data1msdb.ldf
# Parsed output looks like this: F:sql_dbdb_data1
foreach ( $db in $mountpoints ) {
$str = $db.physical_name
$pos = $str.LastIndexOf('')
$str = $str.Substring(0, $pos)
$pos = $str.LastIndexOf('')
$str = $str.Substring(0, $pos)
$str
}
}
function sdcli-version-list() {
# Get the sdcli version. Depending on this, adjust the SDCLI_WAIT (as 6.3.1R1
# takes about a minute to complete a snapvault archive) and adjust the $ARCHIVE
param( [string] $opt )
$sdcli_version = (Get-Command 'C:Program FilesNetAppSnapDrivesdcli.exe').FileVersionInfo.FileVersion
$sdcli_version = $sdcli_version -replace " ",""
switch -wildcard ($sdcli_version) {
"6.3*"
{
$SDCLI_WAIT = 10
$ARCHIVE_SEARCH = "$($ARCHIVE)"
}
"6.4*"
{
$SDCLI_WAIT = 120
$ARCHIVE_SEARCH = "$($ARCHIVE).0"
}
}
switch ($opt) {
"sdcli_wait" { return $SDCLI_WAIT }
"archive_search" { return $ARCHIVE_SEARCH }
}
}
function sdcli-snap-list() {
# Get the snapshot for the selected mountpoint. Searches for a pattern, and
# returns only the first one (the newest)
param( [string] $mountpoint, [string] $search )
if ( $search ) {
# Output of sdcli snap list -d $volume looks like this:
# sqlsnap__vcsrv_10-22-2012_14.05.16 1% ( 1%) 0% ( 0%) Okt 22 14:05 Consistent snapshot copy
# Thus, we need to emulate awk and just print the first row of this output
$str = &"C:Program FilesNetAppSnapDrivesdcli.exe" snap list -d $mountpoint |
Select-String $search | Select -First 1 | Out-String | %{ $_.Split(' ')[0]; }
} else {
$str = &"C:Program FilesNetAppSnapDrivesdcli.exe" snap list -d $mountpoint |
Select -First 1 | Out-String | %{ $_.Split(' ')[0]; }
}
# Sadly the above returns the snapshot name beginning with a newline,
# thus screwing the output. So we need to replace all carriage returns!
$str = $str -replace "`t|`n|`r",""
$str
}
function sdcli-vol-list() {
# Get the controller and volume for a mountpoint. Needed for systems, where
# more than one LUN are provisioned in the same volume, thus we would end
# up calling snapvault archive more than once for the same volume, thus
# running into an error, as the archive is already done.
param ( [string] $mountpoint, [string] $outfile )
If ( ! ( Test-Path "$( $outfile ).dsk" ) ) {
&"C:Program FilesNetAppSnapDrivesdcli.exe" disk list |
Out-File "$( $outfile ).dsk"
}
$hash = "{0:D4}" -f (1..9999 | Get-Random)
$hashfile = "$( $outfile )-$( $hash ).dsk"
# Sadly, Select-String requires an escaped backslash (escaped by a backslash)
# so we need to run a replace ..
$mountpoint = $mountpoint -replace "\","``"
# Output from disk list looks like this:
# Storage System: fas6210a
# Storage System Path: /vol/vol001_vcsrv_lun03/sv/vcsrv_lun03
# Type: lun
# Disk serial number: bfd4K8Cp7lvM
# Backed by Snapshot Copy:
# Shared: No
# BootOrSystem Disk: No
# SCSI port: 2
# Bus: 0
# Target: 1
# LUN: 1
# Readonly: No
# Size: 5122 MB
# Snapmirror Source: No
# Snapvault Primary: Yes
# Disk Partition Style: Master Boot Record (MBR)
# Clone Split Restore status: Normal
# DiskID: 1
# Volume Name: \?Volume{68e8100e-8565-11e1-b4e9-0025b502004f}
# Mount points: F:sql_dbdb_data1
Get-Content "$( $outfile ).dsk" | Select-String "$( $mountpoint )" `
-Context 19,0 | Out-File $hashfile
# Get the Storage System and the Path from the output of sdcli disk list.
$storage_volume = Get-Content $hashfile |
Select-String "Storage System Path:" | Out-String |
%{ $_.Split(' ')[12]; }
$storage_volume = $storage_volume -replace "`t|`n|`r","" |
%{ $_.Split('/')[2]; }
return $storage_volume
}
function sdcli-fas-list() {
# Get the controller and volume for a mountpoint. Needed for systems, where
# more than one LUN are provisioned in the same volume, thus we would end
# up calling snapvault archive more than once for the same volume, thus
# running into an error, as the archive is already done.
param ( [string] $mountpoint, [string] $outfile )
If ( ! ( Test-Path "$( $outfile ).dsk" ) ) {
&"C:Program FilesNetAppSnapDrivesdcli.exe" disk list |
Out-File "$( $outfile ).dsk"
}
$hash = "{0:D4}" -f (1..9999 | Get-Random)
$hashfile = "$( $outfile )-$( $hash ).dsk"
# Output from disk list looks like this:
# Storage System: fas6210a
# Storage System Path: /vol/vol001_vcsrv_lun03/sv/vcsrv_lun03
# Type: lun
# Disk serial number: bfd4K8Cp7lvM
# Backed by Snapshot Copy:
# Shared: No
# BootOrSystem Disk: No
# SCSI port: 2
# Bus: 0
# Target: 1
# LUN: 1
# Readonly: No
# Size: 5122 MB
# Snapmirror Source: No
# Snapvault Primary: Yes
# Disk Partition Style: Master Boot Record (MBR)
# Clone Split Restore status: Normal
# DiskID: 1
# Volume Name: \?Volume{68e8100e-8565-11e1-b4e9-0025b502004f}
# Mount points: F:sql_dbdb_data1
# Sadly, Select-String requires an escaped backslash (escaped by a backslash)
# so we need to run a replace ..
$mountpoint = $mountpoint -replace "\","``"
Get-Content "$( $outfile ).dsk" | Select-String "$( $mountpoint )" -Context 19,0 |
Out-File $hashfile
# Get the Storage System and the Path from the output of sdcli disk list.
$storage_controller = Get-Content $hashfile | `
Select-String "Storage System:" | Out-String | %{ $_.Split(' ')[15]; }
$storage_controller = $storage_controller -replace "`t|`n|`r",""
return $storage_controller
}
function sdcli-sv-archive() {
# Starts the SnapVault archive to the SnapVault secondary.
# Needs the archive class (specified on the controller by viewing snapvault snap sched),
# the mountpoint, the snapshot name - all which are needed to trigger the snapvault
# archive.
param( [string] $archive, [string] $mountpoint, [string] $snapname, [string] $outfile )
&"C:Program FilesNetAppSnapDrivesdcli.exe" snapvault archive -a $archive `
-DS $mountpoint $snapname
}
function sdcli-sv-snaplist() {
# Retrieves the snapshot list on SV_SEC and only returns the last snapshot
# that is already on the destination.
param ( [string] $mountpoint, [string] $search )
if ( $search ) {
$str = &"C:Program FilesNetAppSnapDrivesdcli.exe" snapvault snap_list `
-d $mountpoint | Select-String $search | Select -First 1 | Out-String |
%{ $_.Split(' ')[16]; }
} else {
$str = &"C:Program FilesNetAppSnapDrivesdcli.exe" snapvault snap_list `
-d $mountpoint | Select -First 1 | Out-String |
%{ $_.Split(' ')[16]; }
}
# Sadly the above returns the snapshot name beginning with a newline,
# thus screwing the output. So we need to replace all carriage returns!
$str = $str -replace "`t|`n|`r",""
$str
}
function sdcli-sv-snaprename() {
# Since the snapvault archive names the snapshot like the archive class
# (in most cases 'daily'), we need to rename the snapshot on the SV_SEC
# to it's original name. However we need to wait until the transport
# to the SV_SEC is finished, in order to rename the snapshot.
param( [string] $archive, [string] $mountpoint, [string] $snapshot )
&"C:Program FilesNetAppSnapDrivesdcli.exe" snapvault snapshot_rename `
-o $archive -n $snapshot -d $mountpoint
}
function Write-CustomOut ($Details){
$LogDate = Get-Date -Format T
Write-Output "$($LogDate) $Details"
}
# Call the cleanup.
clean-tempdir $OUTPATH "$( $OUTFILE ).csv"
clean-tempdir $OUTPATH "$( $OUTFILE ).tmp"
clean-tempdir $OUTPATH "$( $OUTFILE )*.dsk"
# Generate a csv-file containing all mountpoints and associated snapshot names
Add-Content "$( $OUTFILE ).csv" "Controller;Volume;Mountpoint;Snapshot"
foreach ($db in $DB_NAMES) {
$mountpoints = get-db-mountpoint $db
if ( ! ($mountpoints) ) {
$body = "Didn't get a vaild database mountpoint from database $( $db )`n`nDebug output:`nOutput from `$mountpoints: $( $mountpoints )"
Send-MailMessage -from $SMTP_SENDER -to $SMTP_RECEIVER `
-subject "SnapVault archive failed on $( $env:COMPUTERNAME )" `
-body $body `
-priority High -smtpServer $SMTP_HOST
}
foreach ( $mountpoint in $mountpoints ) {
$snapname = sdcli-snap-list $mountpoint "sql"
$controller = sdcli-fas-list $mountpoint $OUTFILE
$volume = sdcli-vol-list $mountpoint $OUTFILE
Add-Content "$( $OUTFILE ).csv" "$( $controller );$( $volume );$( $mountpoint );$( $snapname )"
}
}
# Call the sdcli-version-list function, to adjust SDCLI_WAIT and $ARCHIVE_SEARCH
$ARCHIVE_SEARCH = sdcli-version-list archive_search
$SDCLI_WAIT = sdcli-version-list sdcli_wait
Write-CustomOut "ARCHIVE_SEARCH: $($ARCHIVE_SEARCH)"
Write-CustomOut "SDCLI_WAIT: $($SDCLI_WAIT)"
if ( $ARCHIVE_SEARCH -notmatch $ARCHIVE ) {
Write-CustomOut "`$ARCHIVE_SEARCH doesn't match `$ARCHIVE. This breaks the script, so the script"
Write-CustomOut "aborts at this point. Please look into it and fix it!"
exit 128
}
if ( $SDCLI_WAIT -lt 10 ) {
Write-CustomOut "`$SDCLI_WAIT is less than 10. Currently 10 is the parameter for SnapDrive 6.3.1R1"
Write-CustomOut "and anything below doesn't make sense ... The script aborts at this point."
exit 128
}
# Read the csv-file and sort it, and eliminate duplicate entries.
# Separate the triggering of the snapvault and snapvault rename into two
# separate loops, otherwise the first loop would have to wait each time
# until the snapshot is on the SV_SEC.
Write-CustomOut "Starting SnapVault backup"
Import-CSV "$( $OUTFILE ).csv" -Delimiter ";" | Sort-Object Controller,Volume | Get-Unique -asstring | foreach {
$controller = $_.Controller
$volume = $_.Volume
$mountpoint = $_.Mountpoint
$snap = $_.Snapshot
Write-CustomOut " - Creating SnapVault transfer task for $( $mountpoint ) - $( $snap )"
if ( !( $snapvaulted | Select-String "$( $controller )-$( $volume )" -q ) ) {
# Check if there is already an unrenamed snapshot on the destination and
# abort, otherwise more stuff goes kablooey.
$svsnap = sdcli-sv-snaplist $mountpoint "$($ARCHIVE_SEARCH)" | Out-String
Write-CustomOut $svsnap
if ( $svsnap -Match "$($ARCHIVE_SEARCH)" ) {
Write-CustomOut " failed ($($ARCHIVE_SEARCH) already present - failed rename?)"
$body = " - Affected controller: $( $controller )`n - Affected volume: $( $volume )`n - Affected snapshot: $( $snap )`n - Affected mountpoint: $( $mountpoint )"
Send-MailMessage -from $SMTP_SENDER -to $SMTP_RECEIVER `
-subject "SnapVault job $( $env:COMPUTERNAME ):$( $volume ) - failed: $($ARCHIVE_SEARCH) present" `
-body $body `
-priority High -smtpServer $SMTP_HOST
} else {
# Call the sdcli-sv-archive function, copying the snapshot to our SV_SEC.
$command = sdcli-sv-archive $ARCHIVE $mountpoint $snap | Out-String
Write-CustomOut $command
if ( $command -NotMatch "The operation completed successfully" ) {
Write-CustomOut " failed (transfer)"
$body = " - Affected controller: $( $controller )`n - Affected volume: $( $volume )`n - Affected snapshot: $( $snap )`n - Affected mountpoint: $( $mountpoint )`n`nDebug output:`nOutput from `$command: $( $command )"
Send-MailMessage -from $SMTP_SENDER -to $SMTP_RECEIVER `
-subject "SnapVault job $( $env:COMPUTERNAME ):$( $volume ) - transfer failed (snapvault update)" `
-body $body `
-priority High -smtpServer $SMTP_HOST
} else {
Write-CustomOut " started (transfer)"
$body = " - Affected controller: $( $controller )`n - Affected volume: $( $volume )`n - Affected snapshot: $( $snap )`n - Affected mountpoint: $( $mountpoint )`n`nDebug output:`nOutput from `$command: $( $command )"
Send-MailMessage -from $SMTP_SENDER -to $SMTP_RECEIVER `
-subject "SnapVault job $( $env:COMPUTERNAME ):$( $volume ) - transfer started" `
-body $body `
-smtpServer $SMTP_HOST
}
}
$snapvaulted += "$( $controller )-$( $volume )`n"
Write-CustomOut " finished(volume)"
}
Write-CustomOut " finished(loop-volume)"
}
# Now, wait in this loop until the snapshot is on the SV_SEC, and then rename
# the snapshot to it's original name - making it more recognizable than 'daily'
Write-CustomOut "Starting SnapVault renames"
Import-CSV "$( $OUTFILE ).csv" -Delimiter ";" | Sort-Object Controller,Volume | Get-Unique -asstring | foreach {
$controller = $_.Controller
$volume = $_.Volume
$mountpoint = $_.Mountpoint
$snap = $_.Snapshot
Write-CustomOut " - Creating SnapVault rename task for $( $mountpoint ) - $( $snap )"
if ( !( $snapvaulted_ren | Select-String "$( $controller )-$( $volume )" -q ) ) {
# Since the snapvault archive names the destination snapshot like the archive
# class (which is bullshit), wait till the snapshot is on the destination and
# then rename the snapvault snapshot.
Write-CustomOut " checking for snapshot $($ARCHIVE_SEARCH)"
$svsnap = sdcli-sv-snaplist $mountpoint "$($ARCHIVE_SEARCH)"
Write-CustomOut $svsnap
while ( $svsnap -ne "$($ARCHIVE_SEARCH)" ) {
Write-CustomOut " waiting for snapshot $($ARCHIVE_SEARCH))"
sleep $SDCLI_WAIT
$svsnap = sdcli-sv-snaplist $mountpoint "$($ARCHIVE_SEARCH)"
Write-CustomOut $svsnap
$count++
}
if ( $count -gt $WAIT ) {
$body = " - Affected controller: $( $controller )`n - Affected volume: $( $volume )`n - Affected snapshot: $( $snap )`n - Affected mountpoint: $( $mountpoint )`n`nDebug output:`nOutput from `$command: $( $command )"
Send-MailMessage -from $SMTP_SENDER -to $SMTP_RECEIVER `
-subject "SnapVault job $( $env:COMPUTERNAME ):$( $volume ) - transfer skipped" `
-body $body `
-priority High -smtpServer $SMTP_HOST
} else {
Write-CustomOut " snapshot $($ARCHIVE_SEARCH) present"
$command = sdcli-sv-snaprename "$($ARCHIVE_SEARCH)" $mountpoint $snap | Out-String
Write-CustomOut $command
if ( $command -NotMatch "The operation completed successfully" ) {
$body = " - Affected controller: $( $controller )`n - Affected volume: $( $volume )`n - Affected snapshot: $( $snap )`n - Affected mountpoint: $( $mountpoint )`n`nDebug output:`nOutput from `$command: $( $command )"
Send-MailMessage -from $SMTP_SENDER -to $SMTP_RECEIVER `
-subject "SnapVault job $( $env:COMPUTERNAME ):$( $volume ) - transfer failed (snapvault rename)" `
-body $body `
-priority High -smtpServer $SMTP_HOST
} else {
$body = " - Affected controller: $( $controller )`n - Affected volume: $( $volume )`n - Affected snapshot: $( $snap )`n - Affected mountpoint: $( $mountpoint )`n`nDebug output:`nOutput from `$command: $( $command )"
Send-MailMessage -from $SMTP_SENDER -to $SMTP_RECEIVER `
-subject "SnapVault job $( $env:COMPUTERNAME ):$( $volume ) - transfer completed" `
-body $body `
-smtpServer $SMTP_HOST
}
}
$snapvaulted_ren += "$( $controller )-$( $volume )`n"
Write-CustomOut " finished(volume)"
}
Write-CustomOut " finished(loop-volume)"
}
Write-CustomOut "all done!"
Write-CustomOut ""
|