forked from MikeFal/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestore-SqlDBWithMove.ps1
51 lines (47 loc) · 1.79 KB
/
Restore-SqlDBWithMove.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
function Restore-SqlDbWithMove{
[cmdletbinding()]
param([string]$ServerInstance = 'localhost'
,[string]$BackupFile
,[string]$NewDataPath
,[string]$NewLogPath = $NewDataPath
,[string]$OutputPath = 'NoPath'
)
#Validate paths
<#
If(-not (Test-Path $BackupFile)){
Write-Error "Invalid Backup File: $BackupFile"
return
}
If(-not (Test-Path $NewDataPath)){
Write-Error "Invalid Data Path: $NewDataPath"
return
}
If(-not (Test-Path $NewLogPath)){
Write-Error "Invalid Log Path: $NewLogPath"
return
}
If(-not (Test-Path $OutputPath) -and $OutputPath -ne 'NoPath'){
Write-Error "Invalid Output Path: $OutputPath"
return
}
#>
#initialize internal variables
$relocate = @()
$dbname = (Invoke-Sqlcmd -ServerInstance $ServerInstance -Database tempdb -Query "RESTORE HEADERONLY FROM DISK='$BackupFile';").DatabaseName
$dbfiles = Invoke-Sqlcmd -ServerInstance $ServerInstance -Database tempdb -Query "RESTORE FILELISTONLY FROM DISK='$BackupFile';"
foreach($dbfile in $dbfiles){
$DbFileName = $dbfile.PhysicalName | Split-Path -Leaf
if($dbfile.Type -eq 'L'){
$newfile = Join-Path -Path $NewLogPath -ChildPath $DbFileName
} else {
$newfile = Join-Path -Path $NewDataPath -ChildPath $DbFileName
}
$relocate += New-Object Microsoft.SqlServer.Management.Smo.RelocateFile ($dbfile.LogicalName,$newfile)
}
If($OutputPath -ne 'NoPath'){
$OutputFile = Join-Path $OutputPath -ChildPath "$dbname`_restore.sql"
Restore-SqlDatabase -ServerInstance $ServerInstance -Database $dbname -RelocateFile $relocate -BackupFile "$BackupFile" -RestoreAction Database -Script | Out-File $OutputFile
} else {
Restore-SqlDatabase -ServerInstance $ServerInstance -Database $dbname -RelocateFile $relocate -BackupFile "$BackupFile" -RestoreAction Database
}
}