Skip to content

Instantly share code, notes, and snippets.

@Stuart-Moore
Last active November 24, 2017 10:11
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 Stuart-Moore/00cbc4c3cf31583cae99c36f0494bf94 to your computer and use it in GitHub Desktop.
Save Stuart-Moore/00cbc4c3cf31583cae99c36f0494bf94 to your computer and use it in GitHub Desktop.
Restore db to different point in times
Get-DbaBackupInformation -sqlinstance localhost\sqlexpress2016 -Path C:\dbatools\RestoreTimeDiffDemo -ExportPath C:\temp\bk.xml
$BackupSelection = New-Object System.Collections.ArrayList
$startDate = Get-Date '24/11/2017 09:22'
$BaseDirectory = 'c:\restores\'
for ($i=0; $i -lt 35; $i+=7){
$BackupInfo = Get-DbaBackupInformation -Import -Path c:\temp\bk.xml
$BackupInfo = $BackupInfo | Select-DbaBackupInformation -RestoreTime $startDate.AddMinutes($i)
$DbName = $BackupInfo[0].Database
$RestorePath = ([io.path]::Combine($BaseDirectory,$DbName,$startDate.AddMinutes($i).ToString('hhmmddMMyyyy')))
$NewDbName = $DbName+"_"+$startDate.AddMinutes($i).ToString('hhmmddMMyyyy')
$null = $BackupInfo | Format-DbaBackupInformation -DataFileDirectory $RestorePath -ReplaceDatabaseName $NewDbName
$BackupSelection.Add($BackupInfo) > null
}
$null = $BackupSelection | Test-DbaBackupInformation -SqlInstance localhost\sqlexpress2016 -Withreplace
$RestoreOutput = $BackupSelection | Invoke-DbaAdvancedRestore -SqlInstance localhost\sqlexpress2016 -WithReplace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment