forked from rubrikinc/rubrik-scripts-for-powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql-refresh-example.ps1
More file actions
42 lines (31 loc) · 1.94 KB
/
sql-refresh-example.ps1
File metadata and controls
42 lines (31 loc) · 1.94 KB
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
#Load Rubrik Module
import-module Rubrik
#set credential to log in to Rubrik
$cred = Get-Credential
#Connect to Rubrik server
Connect-Rubrik -Server '<Rubrik IP>' -Credential $cred -Verbose
#Export all dbs to another instance, no file relocation
$SourceServer = 'SERVERA'
$TargetServer = 'SERVERB'
$db = Get-RubrikDatabase -Hostname $SourceServer -Instance 'MSSQLSERVER' | Where-Object {@('master','model','msdb') -notcontains $_.Name -and $_.isRelic -ne 'True'}
$TargetInstanceID = (Get-RubrikDatabase -Hostname $TargetServer -Instance 'MSSQLSERVER' -Database 'master').instanceId
#If you need to drop databases before Export/Restore
$db.name | ForEach-Object {Invoke-Sqlcmd -ServerInstance $TargetServer -Database tempdb -Query 'DROP DATABASE $_;'}
#go ahead and run the export after a meta data refresh
New-RubrikHost $SourceServer
$requests = $db | Export-RubrikDatabase -RecoveryDateTime (Get-Date '2017-06-26T12:00:00') -TargetInstanceId $TargetInstanceID -WhatIf
#If the above -WhatIf output is good, replace -WhatIf with -Confirm:$false
$reqcount = ($requests | Where-Object {$_.status -ne 'SUCCEEDED'} | Measure-Object).Count
if($reqcount -eq 0){
$db.name | ForEach-Object {Invoke-Sqlcmd -ServerInstance $TargetServer -Database tempdb -InputFile C:\SQLFiles\postexport.sql}
}
#To create live mounts, use this instead of the Export-RubrikDatabase
#only support in Rubrik 4.0
$db | New-RubrikDatabaseMount -RecoveryDateTime (Get-Date '2017-06-26T12:00:00') -TargetInstanceId $TargetInstanceID -MountedDatabaseName "$_`LM" -WhatIf
#To Execute post export/mount scripts
#capture outupt of Export-RubrikDatabase call, these are there the async requests
#you will want to check to see if all the requests are 'SUCCEEDED'
$reqcount = ($requests | Where-Object {$_.status -ne 'SUCCEEDED'} | Measure-Object).Count
if($reqcount -eq 0){
$db.name | ForEach-Object {Invoke-Sqlcmd -ServerInstance $TargetServer -Database tempdb -InputFile C:\SQLFiles\postexport.sql}
}