forked from rubrikinc/rubrik-scripts-for-powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExport-RubrikDatabasesJob.ps1
More file actions
178 lines (148 loc) · 6.94 KB
/
Export-RubrikDatabasesJob.ps1
File metadata and controls
178 lines (148 loc) · 6.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
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
<#
.SYNOPSIS
Will export databases from instance of SQL registered in Rubrik to another instance registered in Rubrik
.DESCRIPTION
Will export databases from instance of SQL registered in Rubrik to another instance registered in Rubrik
Will read a JSON file to get input and out information. For an example, use Export-RubrikDatabasesJobFile.json
The Databaess section of the JSON is an array that can be copied many times.
.PARAMETER JobFile
JSON file that will provide values for the script to work
.INPUTS
None
.OUTPUTS
None
.EXAMPLE
.\Export-RubrikDatabaessJob -JobFile .\Export-RubrikDatabasesJobFile.json
.LINK
None
.NOTES
Name: Export Rubrik Databases Job
Created: 2/6/2018
Author: Chris Lumnah
Execution Process:
1. Before running this script, you need to create a credential file so that you can securly log into the Rubrik
Cluster. To do so run the below command via Powershell
$Credential = Get-Credential
$Credential | Export-CliXml -Path .\rubrik.Cred"
The above will ask for a user name and password and them store them in an encrypted xml file.
2. Modify the JSON file to include the appropriate values.
RubrikCluster
Server: IP Address to the Rubrk Cluster
Credential: Should contain the full path and file name to the credential file created in step 1
s
Databases - Repeatable array
Name: Database name to be exported
SourceServerInstance: Source SQL Server Instance
TargetServerInstance: Target SQL Server Instance
Files - Repeatable array
LogicalName: Represents the logical name of a database file in SQL
Path: Represents the physical path to a data or log file in SQL
FileName: Physical file name of the data or log file in SQL
3. Execute this script via the example above.
#>
param(
$JobFile = ".\Export-RubrikDatabasesJobFile.json"
)
Import-Module Rubrik
if (Test-Path -Path $JobFile)
{
$JobFile = (Get-Content $JobFile) -join "`n" | ConvertFrom-Json
#Currently there is no way to check for an existing connection to a Rubrik Cluster. This attempts to do that and only
#connects if an existing connection is not present.
try
{
Get-RubrikVersion | Out-Null
}
catch
{
#04/05/2018 - Chris Lumnah - Instead of using an encrypted text file, I am now using the more standard
#CLiXml method
#$Password = Get-Content $JobFIle.RubrikCluster.CredentialFilePassword | ConvertTo-SecureString
$Credential = Import-CliXml -Path $JobFIle.RubrikCluster.Credential
# Connect-Rubrik -Server $JobFile.RubrikCluster.Server `
# -Username $JobFile.RubrikCluster.Username `
# -Password $Password | Out-Null
Connect-Rubrik -Server $JobFile.RubrikCluster.Server -Credential $Credential
}
foreach ($Database in $JobFile.Databases)
{
$RubrikDatabase = Get-RubrikDatabase -Name $Database.Name -ServerInstance $Database.SourceServerInstance
$TargetFiles = @()
foreach ($DatabaseFile in $JobFile.Databases.Files)
{
$TargetFiles += @{logicalName=$DatabaseFile.logicalName;exportPath=$DatabaseFile.Path;newFileName=$DatabaseFile.FileName}
}
#We look for the existance of the database on the target instance. If it exists, we must drop the database before we can
#proceed with exportation of the database to the target instance.
$Query = "SELECT 1 FROM sys.databases WHERE name = '" + $Database.Name + "'"
$Results = Invoke-Sqlcmd -ServerInstance $Database.TargetServerInstance -Query $Query
IF ($Results.Column1 -eq 1)
{
$Query = "ALTER DATABASE [" + $Database.Name + "] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;"
$Results = Invoke-Sqlcmd -ServerInstance $Database.TargetServerInstance -Query $Query
$Query = "DROP DATABASE [" + $Database.Name + "]"
$Results = Invoke-Sqlcmd -ServerInstance $Database.TargetServerInstance -Query $Query
#Refresh Rubik so it does not think the database still exists
New-RubrikHost -Name $Database.TargetServerInstance -Confirm:$false | Out-Null
}
$TargetInstance = (Get-RubrikSQLInstance -ServerInstance $Database.TargetServerInstance)
Export-RubrikDatabase -Id $RubrikDatabase.id `
-TargetInstanceId $TargetInstance.id `
-TargetDatabaseName $Database.Name `
-recoveryDateTime (Get-date (Get-RubrikDatabase -id $RubrikDatabase.ID).latestRecoveryPoint) `
-FinishRecovery `
-TargetFilePaths $TargetFiles `
-Confirm:$false
}
}
<#
In case the JSON file is deleted, you can use the below as an example to recreate the file.
{
"RubrikCluster":
{
"Server": "172.21.8.31",
"Credential":"C:\\Users\\chrislumnah\\OneDrive\\Documents\\WindowsPowerShell\\Credentials\\RangerLab-AD.credential",
"Username - NO LONGER USED": "admin",
"CredentialFilePassword - NO LONGER USED":"C:\\Users\\chris\\OneDrive\\Documents\\WindowsPowerShell\\Credential.txt"
},
"Databases":
[
{
"Name": "AdventureWorks2016",
"SourceServerInstance": "cl-sql2016n1.rangers.lab",
"TargetServerInstance": "cl-sql2016n2.rangers.lab",
"Files":
[
{
"LogicalName":"AdventureWorks2016_Data",
"Path":"E:\\Microsoft SQL Server\\MSSQL13.MSSQLSERVER\\MSSQL\\DATA\\",
"FileName":"AdventureWorks2016_Data.mdf"
},
{
"LogicalName":"AdventureWorks2016_Log",
"Path":"E:\\Microsoft SQL Server\\MSSQL13.MSSQLSERVER\\MSSQL\\DATA\\",
"FileName":"AdventureWorks2016_Log.ldf"
}
]
},
{
"Name": "AdventureWorksDW2016",
"SourceServerInstance": "cl-sql2016n1.rangers.lab",
"TargetServerInstance": "cl-sql2016n2.rangers.lab",
"Files":
[
{
"LogicalName":"AdventureWorksDW2016_Data",
"Path":"E:\\Microsoft SQL Server\\MSSQL13.MSSQLSERVER\\MSSQL\\DATA\\",
"FileName":"AdventureWorksDW2016_Data.mdf"
},
{
"LogicalName":"AdventureWorksDW2016_Log",
"Path":"E:\\Microsoft SQL Server\\MSSQL13.MSSQLSERVER\\MSSQL\\DATA\\",
"FileName":"AdventureWorksDW2016_Log.ldf"
}
]
}
]
}
#>