forked from rubrikinc/rubrik-scripts-for-powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvoke-manualsqlbackup.ps1
More file actions
144 lines (128 loc) · 4.51 KB
/
invoke-manualsqlbackup.ps1
File metadata and controls
144 lines (128 loc) · 4.51 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
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
param(
$RubrikServer
,[pscredential]$RubrikCredential
,$ServerInstance
)
#Load supporting functions
function Connect-Rubrik {
[cmdletbinding()]
Param (
[Parameter(Mandatory = $true)][System.String]$RubrikAddress,
[Parameter(Mandatory = $true)][System.Management.Automation.PSCredential]$RubrikCredential,
[Parameter(Mandatory = $false)][System.String]$RubrikApi = "/api/v1/"
)
try {
Add-Type -TypeDefinition @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object -TypeName TrustAllCertsPolicy
}
catch {}
$global:RubrikAddress = $RubrikAddress
$global:RubrikRESTHeader = @{
"Authorization" = "Basic " + [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($RubrikCredential.UserName+':'+$RubrikCredential.GetNetworkCredential().Password))
}
$global:RubrikApi = $RubrikApi
#test authenticated connection to Rubrik
try {
$result = Invoke-WebRequest -Uri ("https://$RubrikAddress$RubrikApi" + "cluster/me") -Headers $RubrikRESTHeader -Method "GET" -ErrorAction Stop
if($result.StatusCode -ne 200) {
throw "Bad status code returned from Rubrik cluster at $RubrikAddress"
}
else {
Write-Host "Connected to Rubrik cluster $RubrikAddress"
}
}
catch {
throw $_
}
}
function Invoke-RubrikCmd {
[cmdletbinding()]
Param (
[Parameter(Mandatory = $true,HelpMessage = 'REST Endpoint')]
[ValidateNotNullorEmpty()]
[System.String]$RESTEndpoint,
[Parameter(Mandatory = $true,HelpMessage = 'REST Method')]
[ValidateNotNullorEmpty()]
[System.String]$RESTMethod,
[Parameter(Mandatory = $false,HelpMessage = 'REST Content')]
[ValidateNotNullorEmpty()]
[System.Array]$RESTBody,
[Parameter(Mandatory = $false,HelpMessage = 'Limit to Number of Results')]
[ValidateNotNullorEmpty()]
[System.Int32]$Limit = 9999,
[Parameter(Mandatory = $false,HelpMessage = 'INTERNAL METHOD')]
[ValidateNotNullorEmpty()]
[System.Management.Automation.SwitchParameter]$Internal
)
#connect to Rubrik if not already connected
if( !($global:RubrikAddress) -or !($global:RubrikRESTHeader) -or !($global:RubrikApi) ) {
Connect-Rubrik
}
#construct uri
if($Internal) {
$api = "/api/internal/"
}
else {
$api = $global:RubrikAPI
}
$address = $global:RubrikAddress
$uri = "https://$address$api$RESTEndpoint"
#add limit if provided
if( $Limit ) {
if($uri.Indexof('?') -eq -1) {
$uri = $uri += "?limit=$Limit"
}
else {
$uri = $uri += "&limit=$Limit"
}
}
#execute REST operation
try {
Write-Verbose $uri
if($RESTBody){
$result = Invoke-WebRequest -Uri $uri -Headers $global:RubrikRESTHeader -Method $RESTMethod -Body (ConvertTo-Json -InputObject $RESTBody) -ErrorAction Stop
} else {
$result = Invoke-WebRequest -Uri $uri -Headers $global:RubrikRESTHeader -Method $RESTMethod -ErrorAction Stop
}
if($result.Content){
$content = ConvertFrom-Json -InputObject $result.Content -ErrorAction Stop
} else {
$content = $result.StatusCode
}
}
catch {
throw $_
}
#check for partial data
if( $content.hasMore ) {
Write-Warning "A limited amount of results have been returned. Try using -Limit 9999 to return more data"
}
return $content
}#End supporting functions
#Parse ServerInstance
if($ServerInstance -contains '\'){
$HostName = ($ServerInstance -split '\')[0]
$InstanceName = ($ServerInstance -split '\')[1]
} else {
$HostName = $ServerInstance
$InstanceName = 'MSSQLSERVER'
}
#Connect to the Rubrik Cluster
Connect-Rubrik -RubrikAddress $RubrikServer -RubrikCredential $RubrikCredential
$dbs = (Invoke-RubrikCmd -RESTEndpoint 'mssql/db' -RESTMethod GET).data |
Where-Object {$_.instanceName -eq $InstanceName -and $_.rootProperties.rootName -eq $HostName -and $_.isrelic -ne 'TRUE' -and $_.isLiveMount -ne 'TRUE'}
$requests = @()
$dbs | Select-Object name,recoveryModel,effectiveSLADomainName,id | Sort-Object name |
Out-GridView -Title $("$ServerInstance databases - Select DBs to backup") -PassThru |
ForEach-Object {$requests += Invoke-RubrikCmd -RESTEndpoint "mssql/db/$($_.id)/snapshot" -RESTMethod POST -RESTBody '{"slaID":"INHERIT","forceFull":"false"}'}
$requests | Out-Host