Skip to content

Instantly share code, notes, and snippets.

Created October 24, 2016 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/270e842d84bf8a64def618843356e4d9 to your computer and use it in GitHub Desktop.
Save anonymous/270e842d84bf8a64def618843356e4d9 to your computer and use it in GitHub Desktop.
Sample script performing a database restore from a newly taken backup
#The first line perform checks to DB online or not.
# If DB is accessible (not online and not in Recovery/ReadOnly mode) it's $true else $false
$testFODB2 = & {Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
$testFO2 = New-Object Microsoft.SqlServer.Management.Smo.Server ("$DRSlave")
if(($testFO2.Databases["$DBname"].IsAccessible -eq $true) -and ($testFO2.Databases["$DBname"].ReadOnly -eq $false)) {
$true}
else { $false }
}
# Now we perfom the query if $true
if($testFODB2){
# First backup the primary database
Invoke-Sqlcmd -InputFile "$DRbicdir\__03_DR_Database_Backup.sql" -ServerInstance "$DRMaster" -ErrorAction Stop
#Copy the backup file to the secondary server/database
Get-ChildItem $DRbicdir -Filter "*_Full.bak" | ForEach-Object{Copy-Item $_.FullName $DRbicdir2} `
#Then restore the database.
| ForEach-Object {Invoke-Sqlcmd -InputFile "$DRbicdir2\__03_DR_Restore_Database.sql" -ServerInstance "$DRSlave" -ErrorAction Stop}
Write-Host "[FIX SUCCEED] $bic database on $DRSlave successfuly set as secondary database in the failover" -ForegroundColor Green
}
else{
Write-Host "[TEST SUCCEED] $DBname is already in StandBy/ReadOnly or NO Recovery mode" -ForegroundColor Green
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment