PowerShell script exchange
Post Reply
fergalmoore
Lurker
Posts: 1
Liked: never
Joined: Mar 29, 2018 11:32 am
Full Name: Fergal Moore
Contact:

Restoring latest SQL backup via PS

Post by fergalmoore »

Hi All

I am new to restoring via Powershell, but have a new requirement to automate a number of SQL database restores for mirroring servers and smoke testing.

I have got the powershell working but I can't seem to recover the most recent backup available.The backup seems to recover the oldest (or first) sql backup available.

Does any one have experience , or know the command to restore the most recent SQL database available? I have tried setting the -TOPOINTINTIME with a variable but I am not able to get it to recover then (it fails)

My powershell as follows:

Code: Select all

Add-PSSnapin veeampssnapin
$restoreBacus = Get-VBRApplicationRestorePoint -SQL -Name "SQLDBVM08"
$locationBacus = Get-VBRSqlDatabase -ApplicationRestorePoint $restoreBacus[0] -Name "BACUS_PROD"
$setcreds = Add-VBRCredentials -user "UTK\UTK.SQLADMIN" -password "apasswordyes1!"
$guestcreds = Set-VBRCredentials -Credential $setcreds
Get-VBRSQLDatabaseRestoreInterval -Database $locationAbacus
Start-VBRSQLDatabaseRestore -Database $locationBacus -Servername "GPE-Jerex" -DatabaseName "BACUS_RESTORE" -GuestCredentials $setcreds -Force
veremin
Product Manager
Posts: 20271
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Restoring latest SQL backup via PS

Post by veremin »

Try to sort restore points and select the latest one:

Code: Select all

$restoreBacus = Get-VBRApplicationRestorePoint -SQL -Name "SQLDBVM08" | Sort-Object creationtime -Descending | Select-Object -First 1
Thanks.
Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests