Last active
February 25, 2019 10:21
-
-
Save MateuszNad/8add747e78e1a2090f51a2d11716cd21 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.Synopsis | |
Test-LastBackups | |
Funkcja korzystająca z poleceń Test-DbaLastBackup, Out-DbaDataTable, Write-DbaDataTable modułu dbatools. | |
Służy do testowego przywracania kopii zapasowych oraz sprawdzania logicznej i fizycznej integralności obiektów w bazie danych. | |
Rezultaty zapisywane są do bazy danych określonej w parametrze -OutDatabase i tabeli - OutTable | |
.EXAMPLE | |
$paramTestLastBackups = @{ | |
SqlSource = 'serwer-db' | |
Databases = 'Application' | |
SqlDestination = 'serwer-sql' | |
DataDirectory = 'E:\SQLData130' | |
LogDirectory = 'F:\SQLData130' | |
OutTable = 'DBA' | |
OutDatabase = 'dbo.LastBackup' | |
} | |
$paramTestLastBackups | |
Test-LastBackups @paramTestLastBackups | |
.LINK | |
Author: Mateusz Nadobnik | |
Link: mnadobnik.pl | |
Date: 20.11.2017 | |
Version: 1.0.0.1 | |
Keywords: backup, restore, dbatools, checkdb | |
Notes: | |
Changelog: 25.02.2019 | |
#> | |
#Requires -Version 3 | |
#Requires -Modules dbatools | |
function Test-LastBackups | |
{ | |
Param( | |
#Instancja źródłowa z której zostaną pobrane informację o ostatnich kopiach zapasowych | |
[Parameter(Mandatory=$true)] | |
[string]$SqlSource, | |
#Instancja docelowa gdzie zostaną odtworzone bazy danych | |
[Parameter(Mandatory=$true)] | |
[string]$SqlDestination, | |
#Lista baz danych których kopie zapasowe mają zostać testowo odtworzone | |
[Parameter(Mandatory=$true)] | |
[array]$Databases, | |
#Scieżka na instancji docelowej dla plików mdf, ndf | |
[Parameter(Mandatory=$true)] | |
[string]$DataDirectory, | |
#Ścieżka na instancji docelowej dla plików ldf | |
[Parameter(Mandatory=$true)] | |
[string]$LogDirectory, | |
#Baza na instacni docelowej w ktorej istnieje table z parametru $OutTable. Jezeli nie istnieje to zostanie utworzona | |
[Parameter(Mandatory=$true)] | |
[string]$OutDatabase, | |
#Tabela do ktorej maja zostac zapisane rezultaty odtworzenia oraz polecenia CHECKDB() | |
[Parameter(Mandatory=$true)] | |
[string]$OutTable | |
) | |
$StartDate = $(Get-Date -Format "yyyy-MM-dd hh:mm:ss.fff") | |
$FnName = "[$StartDate][Test-LastBackups]" | |
#$ErrorActionPreference = 'Stop' | |
Write-Host "$FnName Restore $($Databases -join ',') with $SqlSource to $SqlDestination" | |
try { | |
$parameterTestDbaLastBackup = @{ | |
SqlInstance = $SqlSource | |
Database = $Databases | |
Destination = $SqlDestination | |
DataDirectory = $DataDirectory | |
LogDirectory = $LogDirectory | |
} | |
$parameterTestDbaLastBackup | |
$Result = Test-DbaLastBackup @parameterTestDbaLastBackup -Verbose -WarningVariable Warning | |
if($Warning) { | |
$warningObj = [PSCustomObject]@{ | |
SourceServer = $SqlSource.ToUpper() | |
TestServer = $SqlDestination.ToUpper() | |
Database = $Databases -join ',' | |
FileExists = '' | |
Size = '' | |
RestoreResult = "Failure;$Warning" | |
DbccResult = 'Failure' | |
RestoreStart = $StartDate | |
RestoreEnd = $(Get-Date -Format "yyyy-MM-dd hh:mm:ss.fff") | |
RestoreElapsed = '' | |
DbccStart = '' | |
DbccEnd = '' | |
DbccElapsed = '' | |
BackupDate = '' | |
BackupFiles = '' | |
} | |
$warningObj | ConvertTo-DbaDataTable | Write-DbaDataTable -SqlInstance $SqlDestination -Database $OutDatabase -Table $OutTable -AutoCreateTable | |
} | |
else { | |
$Result | ConvertTo-DbaDataTable | Write-DbaDataTable -SqlInstance $SqlDestination -Database $OutDatabase -Table $OutTable -AutoCreateTable | |
} | |
} | |
catch | |
{ | |
$errorObj = [PSCustomObject]@{ | |
SourceServer = $SqlSource.ToUpper() | |
TestServer = $SqlDestination.ToUpper() | |
Database = $Databases -join ',' | |
FileExists = '' | |
Size = '' | |
RestoreResult = "Failure;$($_.Exception.Message)" | |
DbccResult = 'Failure' | |
RestoreStart = $StartDate | |
RestoreEnd = $(Get-Date -Format "yyyy-MM-dd hh:mm:ss.fff") | |
RestoreElapsed = '' | |
DbccStart = '' | |
DbccEnd = '' | |
DbccElapsed = '' | |
BackupDate = '' | |
BackupFiles = '' | |
} | |
$errorObj | ConvertTo-DbaDataTable | Write-DbaDataTable -SqlInstance $SqlDestination -Database $OutDatabase -Table $OutTable -AutoCreateTable | |
Write-Error $_.Exception.Message | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment