forked from MikeFal/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackupScript.ps1
29 lines (21 loc) · 1.18 KB
/
BackupScript.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
Import-module SQLPS -DisableNameChecking -Force
$backupfile = 'C:\TEMP\OPA2.0\OPA2.0\OPA2.0Analysis\SQL\OPAmanager.bak'
$restoreserver = 'localhost'
$restoredatabase = 'OPA'
$restorefiles = @()
$files = Invoke-Sqlcmd -ServerInstance $restoreserver -Database tempdb -Query "RESTORE FILELISTONLY FROM DISK='$backupfile'"
$newdata = 'C:\DBFiles\Data'
$newlog = 'C:\DBFiles\Log'
$restore = new-object 'Microsoft.SqlServer.Management.Smo.Restore';
$restore.Database = $restoredatabase
$restore.Devices.Add((new-object Microsoft.SqlServer.Management.Smo.BackupDeviceItem $backupfile, 'File'))
foreach($file in $files){
if($file.Type -eq 'L'){
$newpath = Join-Path $newlog $file.PhysicalName.Substring($file.PhysicalName.LastIndexOf('\')+1)
} else {
$newpath = Join-Path $newdata $file.PhysicalName.Substring($file.PhysicalName.LastIndexOf('\')+1)
}
$restore.RelocateFiles.Add((New-Object Microsoft.SqlServer.Management.Smo.RelocateFile ($file.LogicalName,$newpath)))
}
#$restore.Script($restoreserver)
Restore-SqlDatabase -ServerInstance $restoreserver -Database $restoredatabase -BackupFile $backupfile -RelocateFile $restorefiles -RestoreAction Database -Script