forked from rubrikinc/rubrik-scripts-for-powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql-export-instance.ps1
More file actions
70 lines (57 loc) · 2.73 KB
/
sql-export-instance.ps1
File metadata and controls
70 lines (57 loc) · 2.73 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
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
# Script will compare instances to see what database can be exported, and submit the requests
# .\sql-export-instance.ps1 -srcHost [hostname in rubrik] -srcInst MSSQLSERVER -dstHost [hostname in rubrik] -dstInst MSSQLSERVER
Param(
[String]$srcHost,
[String]$srcInst,
[String]$dstHost,
[String]$dstInst
)
Write-Host "Exporting databases available from $($srcHost):$($srcInst) to $($dstHost):$($dstInst)"
Write-Host -nonewline " * Is this what you want to do? (Y to continue, any other key to abort)"
$confirm= read-host
if ( $confirm -ne "Y" ) { exit }
$fromInstance = Get-RubrikDatabase -Host $srcHost -Instance $srcInst
$toInstance = Get-RubrikDatabase -Host $dstHost -Instance $dstInst
# Figure out what's on the target already, and what can be exported to it from the source
$dbsAtTarget = @()
$dbsNotAtTarget = @()
foreach($sourceInstance in $fromInstance){
foreach ($targetInstance in $toInstance){
$targetInstanceId = $targetInstance.instanceId
if ( ( $sourceInstance.name -eq $targetInstance.name ) ) {
$dbsAtTarget += $targetInstance.name
}
else {
if ( $dbsNotAtTarget -notcontains $sourceInstance.name ) {
$dbsNotAtTarget += $sourceInstance.name
}
}
}
}
# This is the list of what can be exported
$dbsNotAtTarget = $dbsNotAtTarget |Where-Object { $dbsAtTarget -notcontains $_ }
# Make sure we have the destination SQL Instance ID
Write-Host "Exporting to $($targetInstanceId)"
# Now we can iterate through the requested exports and take action
foreach ($sourceInstance in $fromInstance){
if ( $dbsNotAtTarget -notcontains $sourceInstance.name ) {
continue
}
# Getting the database details on the last recovery point
$details = Get-RubrikDatabase -id $sourceInstance.id
# Let's convert the latest restore point timestamp to Epoch (UTC) with Milliseconds for the Export Request
$timestampMs = (Get-Date (Get-Date -Date ([DateTime]::Parse($details.latestRecoveryPoint)).ToUniversalTime()) -UFormat %s) + "000"
Write-Host "* Exporting $($sourceInstance.name) ($($sourceInstance.id)"
Write-Host " Latest Recovery Point is $($details.latestRecoveryPoint) - $($timestampMs)"
$finishRecovery = "false"
$maxDataStreams = 4
$targetDatabaseName = $sourceInstance.name
# Send the call
$exportCall = Export-RubrikDatabase -id $sourceInstance.id `
-targetInstanceId $targetInstanceId `
-targetDatabaseName $targetDatabaseName `
-maxDataStreams $maxDataStreams `
-timestampMs $timestampMs `
-finishRecovery `
-confirm:$false
}