Skip to content

Commit 041324c

Browse files
committed
Initialized
0 parents  commit 041324c

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

RestoreAutomation.psm1

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<#
2+
.SYNOPSIS
3+
<A brief description of the script>
4+
.DESCRIPTION
5+
<A detailed description of the script>
6+
.PARAMETER <paramName>
7+
<Description of script parameter>
8+
.EXAMPLE
9+
<An example of using the script>
10+
#>
11+
12+
#load assemblies
13+
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
14+
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
15+
16+
#processing functions
17+
function Get-Restore{
18+
Param($db,
19+
$backupfile)
20+
21+
$rs=new-object("Microsoft.SqlServer.Management.Smo.Restore")
22+
$rs.Devices.AddDevice($backupfile.FullName, "File")
23+
$rs.Database=$db
24+
$rs.NoRecovery=$true
25+
$rs.Action="Database"
26+
27+
return $rs
28+
}#Get-Restore
29+
30+
function Get-Header{
31+
Param($rs,$srv)
32+
33+
$dt=$restore.ReadBackupHeader($srv)
34+
return $dt.Rows[0]
35+
}#Get-Header
36+
37+
function Build-Restore{
38+
param([parameter(Mandatory=$true)][string] $dir,
39+
[parameter(Mandatory=$true)][string] $server,
40+
[string] $database,
41+
[string] $outputdir=([Environment]::GetFolderPath("MyDocuments")),
42+
[Switch] $Execute,
43+
[Switch] $NoRecovery)
44+
45+
$sqlout = @()
46+
$smosrv = new-object ('Microsoft.SqlServer.Management.Smo.Server') $server
47+
48+
$full = gci $dir | where {$_.name -like "*.bak"} | Sort-Object LastWriteTime -desc | Select-Object -first 1
49+
$diff = gci $dir | where {$_.name -like "*.dff"} | sort-object LastWriteTime -desc | select-object -first 1
50+
$trns = gci $dir | where {$_.name -like "*.trn"} | sort-object LastWriteTime
51+
52+
#initialize and process full backup
53+
$restore=Get-Restore $database $full
54+
$hfull=Get-Header $restore $smosrv
55+
if($database.Length -eq 0)
56+
{
57+
$database=$hfull.DatabaseName
58+
$restore.Database=$database
59+
}
60+
61+
$LSNCheck=$hfull.FirstLSN
62+
$files=$restore.ReadFileList($smosrv)
63+
foreach($file in $files){
64+
$newfile = New-Object("Microsoft.SqlServer.Management.Smo.RelocateFile") ($file.LogicalName,$file.PhysicalName)
65+
$restore.RelocateFiles.Add($newfile) | out-null
66+
}
67+
68+
$sqlout+="/****************************************************"
69+
$sqlout+="Restore Database Script Generated $(Get-Date)"
70+
$sqlout+="Database: "+$database
71+
$sqlout+="****************************************************/"
72+
$sqlout+="--FULL RESTORE"
73+
$sqlout+=$restore.Script($smosrv)
74+
75+
#process differential backups
76+
if($diff -ne $null){
77+
$restore=Get-Restore $database $diff
78+
$hdiff=Get-Header $restore $smosrv
79+
80+
if($hdiff.DifferentialBaseLSN -eq $LSNCheck){
81+
$sqlout+="--DIFF RESTORE"
82+
$sqlout+=$restore.Script($smosrv)
83+
$LSNCheck = $hdiff.LastLSN
84+
}
85+
else{
86+
$LSNCheck = $hfull.LastLSN
87+
}
88+
}
89+
90+
#process transaction log backups
91+
if($trns -ne $null){
92+
$sqlout+="--TRN LOG RESTORE"
93+
94+
foreach ($trn in $trns){
95+
$restore=Get-Restore $database $trn
96+
$htrn=Get-Header $restore $smosrv
97+
if($htrn.FirstLSN -le $LSNCheck -and $htrn.LastLSN -ge $LSNCheck){
98+
$sqlout+=$restore.Script($smosrv)
99+
$LSNCheck = $htrn.LastLSN
100+
}
101+
}
102+
}
103+
104+
#Write final recovery line if necessary
105+
if(!($NoRecovery)){
106+
$sqlout+="`r`n"
107+
$sqlout+="--COMPLETE RESTORE/ONLINE DB"
108+
$sqlout+="RESTORE DATABASE "+$database+" WITH RECOVERY"
109+
}
110+
111+
#output script file
112+
$sqlout | Out-File "$outputdir\restore_$database.sql"
113+
114+
#If called, execute script
115+
if($Execute){
116+
sqlcmd -S "$server" -E -i "$outputdir\restore_$database.sql"
117+
}
118+
} #Build-Restore

0 commit comments

Comments
 (0)