forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormat-DbaBackupInformation.ps1
233 lines (197 loc) · 11 KB
/
Format-DbaBackupInformation.ps1
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
Function Format-DbaBackupInformation {
<#
.SYNOPSIS
Transforms the data in a dbatools backuphistory object for a restore
.DESCRIPTION
Performs various mapping on Backup History, ready restoring
Options include changing restore paths, backup paths, database name and many others
.PARAMETER BackupHistory
A dbatools backupHistory object, normally this will have been created using Select-DbaBackupInformation
.PARAMETER ReplaceDatabaseName
If a single value is provided, this will be replaced do all occurences a database name
If a Hashtable is passed in, each database name mention will be replaced as specified. If a database's name does not apper it will not be replace
DatabaseName will also be replaced where it occurs in the file paths of data and log files.
Please note, that this won't change the Logical Names of datafiles, that has to be done with a seperate Alter DB call
.PARAMETER DatabaseNamePrefix
This string will be prefixed to all restored database's name
.PARAMETER DataFileDirectory
This will move ALL restored files to this location during the restore
.PARAMETER LogFileDirectory
This will move all log files to this location, overriding DataFileDirectory
.PARAMETER DestinationFileStreamDirectory
This move the FileStream folder and contents to the new location, overriding DataFileDirectory
.PARAMETER FileNamePrefix
This string will be prefixed to all restored files (Data and Log)
.PARAMETER RebaseBackupFolder
Use this to rebase where your backups are stored.
.PARAMETER Continue
Indicates that this is a continuing restore
.PARAMETER DatabaseFilePrefix
A string that will be prefixed to every file restored
.PARAMETER DatabaseFileSuffix
A string that will be suffixed to every file restored
.PARAMETER ReplaceDbNameInFile
If set, will replace the old databasename with the new name if it occurs in the file name
.PARAMETER FileMapping
A hashtable that can be used to move specific files to a location.
$FileMapping = @{'DataFile1'='c:\restoredfiles\Datafile1.mdf';'DataFile3'='d:\DataFile3.mdf'}
And files not specified in the mapping will be restored to their original location
This Parameter is exclusive with DestinationDataDirectory
If specified, this will override any other file renaming/relocation options.
.PARAMETER EnableException
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
.NOTES
Author:Stuart Moore (@napalmgram stuart-moore.com )
DisasterRecovery, Backup, Restore
Website: https://dbatools.io
Copyright: (C) Chrissy LeMaire, clemaire@gmail.com
License: GNU GPL v3 https://opensource.org/licenses/GPL-3.0
.LINK
https://dbatools.io/Format-DbaBackupInformation
.EXAMPLE
$History | Format-DbaBackupInformation -ReplaceDatabaseName NewDb
Changes as databasename references to NewDb, both in the database name and any restore paths. Note, this will fail if the BackupHistory object contains backups for more than 1 database
.EXAMPLE
$History | Format-DbaBackupInformation -ReplaceDatabaseName @{'OldB'='NewDb';'ProdHr'='DevHr'}
Will change all occurences of original database name in the backup history (names and restore paths) using the mapping in the hashtable.
In this example any occurance of OldDb will be replaced with NewDb and ProdHr with DevPR
.EXAMPLE
$History | Format-DbaBackupInformation -DataFileDirectory 'D:\DataFiles\' -LogFileDirectory 'E:\LogFiles\
This example with change the restore path for all datafiles (everything that is not a log file) to d:\datafiles
And all Transaction Log files will be restored to E:\Logfiles
.EXAMPLE
$History | Formate-DbaBackupInformation -RebaseBackupFolder f:\backups
This example changes the location that SQL Server will look for the backups. This is useful if you've moved the backups to a different location
#>
[CmdletBinding()]
param (
[parameter(Mandatory = $true, ValueFromPipeline = $true)]
[object[]]$BackupHistory,
[object]$ReplaceDatabaseName,
[switch]$ReplaceDbNameInFile,
[string]$DataFileDirectory,
[string]$LogFileDirectory,
[string]$DestinationFileStreamDirectory,
[string]$DatabaseNamePrefix,
[string]$DatabaseFilePrefix,
[string]$DatabaseFileSuffix,
[string]$RebaseBackupFolder,
[switch]$Continue,
[hashtable]$FileMapping,
[switch]$EnableException
)
Begin {
Write-Message -Message "Starting" -Level Verbose
if ($null -ne $ReplaceDatabaseName) {
if ($ReplaceDatabaseName -is [string] -or $ReplaceDatabaseName.ToString() -ne 'System.Collections.Hashtable') {
Write-Message -Message "String passed in for DB rename" -Level Verbose
$ReplaceDatabaseNameType = 'single'
}
elseif ($ReplaceDatabaseName -is [HashTable] -or $ReplaceDatabaseName.ToString() -eq 'System.Collections.Hashtable' ) {
Write-Message -Message "Hashtable passed in for DB rename" -Level Verbose
$ReplaceDatabaseNameType = 'multi'
}
else {
Write-Message -Message "ReplacemenDatabaseName is $($ReplaceDatabaseName.Gettype().ToString()) - $ReplaceDatabaseName" -level Verbose
}
}
if ((Test-Bound -Parameter DataFileDirectory) -and $DataFileDirectory[-1] -eq '\' ) {
$DataFileDirectory = $DataFileDirectory.substring(0, $DataFileDirectory.length - 1)
}
if ((Test-Bound -Parameter DestinationFileStreamDirectory) -and $DestinationFileStreamDirectory[-1] -eq '\' ) {
$DestinationFileStreamDirectory = $DestinationFileStreamDirectory.substring(0, $DestinationFileStreamDirectory.length - 1)
}
if ((Test-Bound -Parameter LogFileDirectory) -and $LogFileDirectory[-1] -eq '\' ) {
$LogFileDirectory = $LogFileDirectory.substring(0, $LogFileDirectory.length - 1)
}
if ((Test-Bound -Parameter RebaseBackupFolder) -and $RebaseBackupFolder[-1] -eq '\' ) {
$RebaseBackupFolder = $RebaseBackupFolder.substring(0, $RebaseBackupFolder.length - 1)
}
}
Process {
ForEach ($History in $BackupHistory) {
if ("OriginalDatabase" -notin $History.PSobject.Properties.name) {
$History | Add-Member -Name 'OriginalDatabase' -Type NoteProperty -Value $History.Database
}
if ("OriginalFileList" -notin $History.PSobject.Properties.name) {
$History | Add-Member -Name 'OriginalFileList' -Type NoteProperty -Value ''
$History | ForEach-Object {$_.OriginalFileList = $_.FileList}
}
if ("OriginalFullName" -notin $History.PSobject.Properties.name) {
$History | Add-Member -Name 'OriginalFullName' -Type NoteProperty -Value $History.FullName
}
if ("IsVerified" -notin $History.PSobject.Properties.name) {
$History | Add-Member -Name 'IsVerified' -Type NoteProperty -Value $False
}
Switch ($History.Type) {
'Full' {$History.Type = 'Database'}
'Differential' {$History.Type = 'Database Differential'}
'Log' {$History.Type = 'Transaction Log'}
}
if ($ReplaceDatabaseNameType -eq 'single' -and $ReplaceDatabaseName -ne '' ) {
$History.Database = $ReplaceDatabaseName
$ReplaceMentName = $ReplaceDatabaseName
Write-Message -Message "New DbName (String) = $($History.Database)" -Level Verbose
}
elseif ($ReplaceDatabaseNameType -eq 'multi') {
if ($null -ne $ReplaceDatabaseName[$History.Database]) {
$History.Database = $ReplaceDatabaseName[$History.Database]
$ReplacementName = $ReplaceDatabaseName[$History.Database]
Write-Message -Message "New DbName (Hash) = $($History.Database)" -Level Verbose
}
}
$History.Database = $DatabaseNamePrefix + $History.Database
if ($true -ne $Continue) {
$History.FileList | ForEach-Object {
if ($null -ne $FileMapping ) {
if ($null -ne $FileMapping[$_.LogicalName]) {
$_.PhysicalName = $FileMapping[$_.LogicalName]
}
}
else {
if ($ReplaceDbNameInFile -eq $true) {
$_.PhysicalName = $_.PhysicalName -Replace $History.OriginalDatabase, $History.Database
}
Write-message -Message " 1 PhysicalName = $($_.PhysicalName) " -Level Verbose
$Pname = [System.Io.FileInfo]$_.PhysicalName
$RestoreDir = $Pname.DirectoryName
if ($_.Type -eq 'D' -or $_.FileType -eq 'D') {
if ('' -ne $DataFileDirectory) {
$RestoreDir = $DataFileDirectory
}
}
elseif ($_.Type -eq 'L' -or $_.FileType -eq 'L') {
if ('' -ne $LogFileDirectory) {
$RestoreDir = $LogFileDirectory
}
elseif ('' -ne $DataFileDirectory) {
$RestoreDir = $DataFileDirectory
}
}
elseif ($_.Type -eq 'S' -or $_.FileType -eq 'S') {
if ('' -ne $DestinationFileStreamDirectory) {
$RestoreDir = $DestinationFileStreamDirectory
}
elseif ('' -ne $DataFileDirectory) {
$RestoreDir = $DataFileDirectory
}
}
$_.PhysicalName = $RestoreDir + "\" + $DatabaseFilePrefix + $Pname.BaseName + $DatabaseFileSuffix + $pname.extension
Write-message -Message "PhysicalName = $($_.PhysicalName) " -Level Verbose
}
}
}
if ($null -ne $RebaseBackupFolder) {
$History.FullName | ForEach-Object {
$file = [System.IO.FileInfo]$_
$_ = $RebaseBackupFolder + "\" + $file.BaseName + $file.Extension
}
}
$History
}
}
End {
}
}