-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSQL-RestoreDatabase.ps1
289 lines (226 loc) · 10.5 KB
/
SQL-RestoreDatabase.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<#============================================================================
Written by Christophe LAPORTE, SQL Server MVP / MCM
Blog : http://conseilit.wordpress.com
Twitter : @ConseilIT
You may alter this code for your own *non-commercial* purposes. You may
republish altered code as long as you give due credit.
THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
============================================================================#>
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
Clear-Host
$DatabasesToRestore = "c:\temp\RestoreDatabase.csv"
<#
# use the following script to create one
$DBList = @"
"source";"destination";"database"
"SourceInstance1";"DestinationInstance1"; "Database1"
"SourceInstance2";"DestinationInstance2"; "Database2"
"SourceInstance3";"DestinationInstance3"; "Database3"
"SourceInstance4";"DestinationInstance4"; "Database4"
"SourceInstance5";"DestinationInstance5"; "Database5"
"SourceInstance6";"DestinationInstance6"; "Database6"
"@
$DBList >>$DatabasesToRestore
#>
[bool]$global:Verbose = $true
[string]$global:BackupFolderRoot = "\\BackupShare\SQL"
[bool]$global:IsOlaStructure = $true
[bool]$global:ShowTSQL = $true
[bool]$global:PerformRestore = $False
[string]$global:RestoreDBprefix = "" # specify a prefix here
function GetFormattedDate(){
return (Get-Date).toString("yyyy-MM-dd HH:mm:ss")
}
#region Log
$global:OutputDirectory = ".\SQL-RestoreDatabaseOutput\"
$WriteHost = $true
function New-Log {
$Logoutputfile = $global:OutputDirectory + $timestamp +"_Summary.log" ;
if ((Test-Path -Path $global:OutputDirectory) -eq $false ) {
Write-Host "Create directory $global:OutputDirectory" ;
$dir = New-Item -type directory $global:OutputDirectory -Force ;
}
else {
if ($PurgeLogFiles -eq $true) {
Get-ChildItem -Path $global:OutputDirectory | Remove-Item
}
}
$LogMessage = (Get-Date -format "yyyy-MM-dd HH:mm:ss : ") + $(split-path $MyInvocation.PSCommandPath -Leaf) + " starting at $(get-date -format "dd/MM/yyyy HH:mm:ss")"
if ($WriteHost -eq $true) {
Write-Host $LogMessage
}
$LogMessage = $LogMessage + "`r`n"
$global:logfile = $Logoutputfile
$file = New-Item $global:logfile -type File -value $LogMessage -force
write-log ""
}
function Write-Log ( [string] $LogMessage ) {
$LogMessage = (Get-Date -format "yyyy-MM-dd HH:mm:ss : ") + $LogMessage
Add-Content -path $global:logfile -value $LogMessage
if ($WriteHost -eq $true) {
Write-Host $LogMessage
}
}
#endregion
function RestoreDatabase ( [string] $Source, [string] $destination, [string] $Database ) {
$BackupFolder = Join-Path -Path $global:BackupFolderRoot -ChildPath $Source
$BackupFolder = Join-Path -Path $BackupFolder -ChildPath $Database
Write-Log "Restoring $Database on $destination from $BackupFolder"
$Server = New-Object -TypeName Microsoft.SQLServer.Management.Smo.Server($destination)
#region Database backup
If ($global:IsOlaStructure) {
$LastFullBackup = get-childitem "$BackupFolder\FULL\*" -recurse -ErrorAction:silentlycontinue | Where-Object {$_.Extension -eq ".bak" } | Sort-Object LastWriteTime -Descending | Select-Object -First 1
}
else {
$LastFullBackup = get-childitem "$BackupFolder\*" -recurse -ErrorAction:silentlycontinue | Where-Object {$_.Extension -eq ".bak" } | Sort-Object LastWriteTime -Descending | Select-Object -First 1
}
$LastFullBackupFile = $LastFullBackup.FullName
$LastFullBackupDate = $LastFullBackup.LastWriteTime
if ($global:Verbose) {
Write-Log "Last Full Backup File : $LastFullBackupFile"
Write-Log "Last Full Backup Date : $LastFullBackupDate"
}
# if wanna a DB prefix to restore as a "new" database, update the global variable
$Database= $global:RestoreDBprefix + $Database
# Create restore object and specify the settings
$smoRestore = new-object("Microsoft.SqlServer.Management.Smo.Restore")
$smoRestore.Database = $Database
$smoRestore.NoRecovery = $true;
$smoRestore.ReplaceDatabase = $true;
$smoRestore.Action = "Database"
# Create location to restore from
$backupDevice = New-Object("Microsoft.SqlServer.Management.Smo.BackupDeviceItem") ($LastFullBackupFile, "File")
if ($smoRestore.Devices.Count -ge 1) {
$smoRestore.Devices.Clear();
}
$smoRestore.Devices.Add($backupDevice)
# retreiving the database data and log files
# the physical disk layer may be different, have to use the MOVE TO option
Write-Log "Retriving database files from backup file $LastFullBackupFile"
$tSQL = "RESTORE FILELISTONLY FROM DISK = '$LastFullBackupFile'"
if ($global:ShowTSQL) {Write-Log "T-SQL : $tSQL"}
$FileList = Invoke-SqlCmd -Query $tSQL -Serverinstance $destination
# Move each file to the destination data / log folder
$DataFileCount = 1
foreach ($file in $FileList) {
$rsfile = new-object('Microsoft.SqlServer.Management.Smo.RelocateFile')
$rsfile.LogicalFileName = $file.LogicalName
if ($file.Type -eq 'D') {
$rsfile.PhysicalFileName = $Server.DefaultFile + $Database + "_Data" + $DataFileCount + ".mdf"
$DataFileCount += 1
}
else {
$rsfile.PhysicalFileName = $Server.DefaultLog + $Database + "_Log.ldf"
}
if ($global:Verbose){
Write-Log "Moving $($file.LogicalName) : $($file.PhysicalName) ==> $($rsfile.PhysicalFileName)"
}
$smoRestore.RelocateFiles.Add($rsfile) | Out-Null
}
Write-Log ""
# Restoring the database now
Write-Log "Begin Restore $DatabaseName on $destination from $LastFullBackupFile"
$tSQL = $smoRestore.Script($server.DomainInstanceName)
if ($global:ShowTSQL) {Write-Log "T-SQL : $tSQL"}
try {
If ([bool]$global:PerformRestore){
$ActiveConnections = $server.GetActiveDBConnectionCount("$DatabaseName")
if ($ActiveConnections -gt 0) {
Write-Log "$ActiveConnections active connections on $DatabaseName"
Write-Log "Killing all of them before starting restore ..."
$server.KillAllProcesses("$DatabaseName")
}
Invoke-Sqlcmd -Query $tSQL.Item(0) -ServerInstance $server.DomainInstanceName -QueryTimeout 0
}
}
catch {
Write-Log -ForegroundColor Red $_.Exception.Message
}
finally {
Write-Log " Done !!"
}
Write-Log ""
#endregion
#region differential backup
# find the last differential backup file
If ($global:IsOlaStructure) {
$BackupFile = get-childitem "$BackupFolder\DIFF\*" -recurse -ErrorAction:silentlycontinue | Where-Object LastWriteTime -ge $LastFullBackupDate | Where-Object {$_.Extension -eq ".bak" } | Sort-Object LastWriteTime -Descending | Select-Object -First 1
}
else {
$BackupFile = get-childitem "$BackupFolder\*" -recurse -ErrorAction:silentlycontinue | Where-Object LastWriteTime -ge $LastFullBackupDate | Where-Object {$_.Extension -eq ".diff" } | Sort-Object LastWriteTime -Descending | Select-Object -First 1
}
if ($BackupFile) {
$LastFullBackupFile = $BackupFile.FullName
$LastFullBackupDate = $BackupFile.LastWriteTime
if ($global:Verbose) {
Write-Log "Last Differential Backup File : $LastFullBackupFile"
Write-Log "Last Differential Backup Date : $LastFullBackupDate"
}
$tSQL = Restore-SqlDatabase -Database $Database -BackupFile $LastFullBackupFile -ServerInstance "$Instance" -NoRecovery -Script
if ($global:ShowTSQL) {Write-Log "T-SQL : $tSQL"}
try {
If ([bool]$global:PerformRestore){
Invoke-Sqlcmd -Query $tSQL.Item(0) -ServerInstance $server.DomainInstanceName -QueryTimeout 0
}
}
catch {
Write-Log -ForegroundColor Red $_.Exception.Message
}
finally {
Write-Log " Done !!"
}
Write-Log ""
}
else {
Write-Log "No differential backup found"
}
#endregion
#region transaction log
# list all trn files newer than the full / differential backup
write-Log "Restore all transaction log"
If ($global:IsOlaStructure) {
$BackupFiles = get-childitem "$BackupFolder\LOG\*" -recurse -ErrorAction:silentlycontinue | Where-Object LastWriteTime -ge $LastFullBackupDate | Where-Object {$_.Extension -eq ".trn" } | Sort-Object LastWriteTime
}
else {
$BackupFiles = get-childitem "$BackupFolder\*" -recurse -ErrorAction:silentlycontinue | Where-Object LastWriteTime -ge $LastFullBackupDate | Where-Object {$_.Extension -eq ".trn" } | Sort-Object LastWriteTime
}
if(!($BackupFiles)) {
write-Log "No transaction log backup found"
}
else {
ForEach ($BackupFile in $BackupFiles) {
$LogBackupFile = $BackupFile.FullName
$LogBackupDate = $BackupFile.LastWriteTime
write-Log "Restoring $LogBackupFile ($LogBackupDate) "
$tSQL = Restore-SqlDatabase -Database $Database -BackupFile $LogBackupFile -ServerInstance "$Instance" -NoRecovery -RestoreAction 'Log' -Script
if ($global:ShowTSQL) {Write-Log "T-SQL : $tSQL"}
try {
If ([bool]$global:PerformRestore){
Invoke-Sqlcmd -Query $tSQL.Item(0) -ServerInstance $server.DomainInstanceName -QueryTimeout 0
}
}
catch {
Write-Log -ForegroundColor Red $_.Exception.Message
}
finally {
Write-Log " Done !"
}
}
Write-Log ""
}
#endregion
}
# main program
$timestamp = (Get-Date -format "yyyyMMddHHmmss")
New-Log
Write-Log "Getting databases to restore from $DatabasesToRestore"
$RestoreOperations = Import-Csv $DatabasesToRestore -Delimiter ";"
foreach ($RestoreOperation in $RestoreOperations) {
RestoreDatabase -Source $RestoreOperation.Source -destination $RestoreOperation.Destination -Database $RestoreOperation.Database
# add some functionality : CheckDB, change DB Compat Level, Drop auto created stats, alter index, ...
}
$LogMessage = "Ending module at $(get-date -format "dd/MM/yyyy HH:mm:ss")"
write-log $LogMessage