PowerShell script exchange
Post Reply
iihaus
Influencer
Posts: 12
Liked: 1 time
Joined: Oct 19, 2023 2:05 pm
Full Name: Haus Fupepe
Contact:

SQL restore from specific backup job

Post by iihaus »

Does anyone know how to restore a sql database from a specific backup job?
We have a regular Backup job that backups up server A and a Backup Copy job that takes a copy of that regular Backup of server A. I would like to restore from the Backup Copy job but it keeps reverting to the Backup job. Any advice?

This is what I'm currently using but the restore_point just reverts to the regular Backup.

Code: Select all

$source_job_name = "Backup copy of server A"
$source_vm = "serverA"
$job_restore_point = Get-VBRRestorePoint -Backup $source_job_name -Name $source_vm | Sort-Object creationtime -Descending | Select-Object -First 1
$restore_point = Get-VBRApplicationRestorePoint -SQL -Name $job_restore_point.Name | Sort-Object creationtime -Descending | Select-Object -First 1
PetrM
Veeam Software
Posts: 3264
Liked: 528 times
Joined: Aug 28, 2013 8:23 am
Full Name: Petr Makarov
Location: Prague, Czech Republic
Contact:

Re: SQL restore from specific backup job

Post by PetrM »

Hi Haus,

I think you can find all the necessary cmdlets in this section of Veeam Explorers Powershell Reference.

Thanks!
iihaus
Influencer
Posts: 12
Liked: 1 time
Joined: Oct 19, 2023 2:05 pm
Full Name: Haus Fupepe
Contact:

Re: SQL restore from specific backup job

Post by iihaus »

@PetrM - Thank you for your response. However, I have gone over that section and more. As far as I can tell there doesn't appear to be a way for me to restore from a specific Backup job/Backup Copy job. I can see the other restore points from different jobs with Get-VBRRestorePoint but it doesn't appear I can then pass or use different restore points. Seems like Get-VBRApplicationRestorePoint should have a parameter that allows for this but it does not.
oleg.feoktistov
Veeam Software
Posts: 1918
Liked: 636 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: SQL restore from specific backup job

Post by oleg.feoktistov »

Hi Haus,

I think it's the issue with string conversion that happens on that line in your script:

Code: Select all

$job_restore_point = Get-VBRRestorePoint -Backup $source_job_name -Name $source_vm | Sort-Object creationtime -Descending | Select-Object -First 1
I would advise against getting restore points using string conversion (passing String instead of CBackup type to -Backup parameter). Instead, make sure to get backups of a backup copy job explicitly and then pass them as objects to Get-VBRRestorePoint cmdlet.
Example:

Code: Select all

$job = Get-VBRBackupCopyJob -Name "Backup Copy Job 1"
$backups = Get-VBRBackup | where {$_.JobId -eq $job.Id}
$rps = Get-VBRRestorePoint -Backup $backup
Then you should be able to get correct application points.

Best regards,
Oleg
iihaus
Influencer
Posts: 12
Liked: 1 time
Joined: Oct 19, 2023 2:05 pm
Full Name: Haus Fupepe
Contact:

Re: SQL restore from specific backup job

Post by iihaus »

@OIeg

Thank you for the response. However, I am still running into the same issue. I believe this is because I am trying to restore a specific SQL database and when I try to run the GET-VBRApplicationRestorePoint -SQL -Name $rps.Name cmdlet it is reverting back to the regular Backup job instead of using the Backup Copy job. I know I am losing it because of the $rps.Name parameter but this appears to be the only way to pass to the Application Restore Point, if I could use $rps.Id there that would be ideal.

Here is what I tried:

Code: Select all

$job = Get-VBRBackupCopyJob -Name "Backup Copy Job 1"
$backups = Get-VBRBackup | where {$_.JobId -eq $job.Id}
$rps = Get-VBRRestorePoint -Backup $backup
$restore_point = GET-VBRApplicationRestorePoint -SQL -Name $rps.Name | Sort-Object creationtime -Descending | Select-Object -First 1
iihaus
Influencer
Posts: 12
Liked: 1 time
Joined: Oct 19, 2023 2:05 pm
Full Name: Haus Fupepe
Contact:

Re: SQL restore from specific backup job

Post by iihaus »

@Oleg

Guess I was thinking too hard on this. I changed my script to the following and it now appears to be working (I was able to pass in the Id instead!)

Code: Select all

$source_vm = "serverA"
$job = Get-VBRBackupCopyJob -Name "Backup Copy Job 1"
$backups = Get-VBRBackup | where {$_.JobId -eq $job.Id}
$rps = Get-VBRRestorePoint -Backup $backup -Name $source_vm | Sort-Object creationtime -Descending | Select-Object -First 1
$restore_point = GET-VBRApplicationRestorePoint -Id $rps.Id 
david.domask
Veeam Software
Posts: 1226
Liked: 322 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: SQL restore from specific backup job

Post by david.domask »

Glad you got it working @iihaus; I want to comment a bit on "sane" practices for anything powershell, specifically text validation/regex since I know we've had a lot of topics on this forum from many users about string validation issues and Powershell "nuances".

Powershell is not great at it unlike bash or other sh descendants, so it's best to avoid it whenever possible. As best I know, every object our cmdlets return will have an ID and also potentially other properties with IDs linking to related resources (e.g., a CBackup object will have RepositoryID, JobID, etc) and many cmdlets will accept -Id as a parameter making for fast lookups from the DB typically.

Powershell has some weird behaviors with strings, especially if you use non-alphanumerics like brackets (e.g { } or [ ] or ( ) ) or diacritic marks, stuff like that. There are workarounds, but they're ugly and don't always work. Just avoid strings :)

Whenever possible, use those UUIDs, it will save you many headaches. Similarly, you can pre-populate some arrays with things like Get-VBRBackup, Get-VBRRestorePoint, etc at the beginning of monitoring scripts and just have nice data arrays you can pull from via the UUIDs you need.

Try to avoid string validation whenever possible imo, it's just a headache especially when you can pipe to Where-Object {$_.[relevantProperty] -eq "your uuid"} or use -Id parameter.

Study the objects returned by Get-VBRJob, Get-VBRBackup, Get-VBRRestorePoint by piping them to Get-Member (or gm for short) and just see how they relate to each other. It will make your scripting way more simple, and if any relationship isn't clear, feel free to ask here.

If people would like a "cheat sheet" on how the main cmdlets relate to one another, this can be created if there's enough interest.
David Domask | Product Management: Principal Analyst
iihaus
Influencer
Posts: 12
Liked: 1 time
Joined: Oct 19, 2023 2:05 pm
Full Name: Haus Fupepe
Contact:

Re: SQL restore from specific backup job

Post by iihaus »

@david.domask
Thank you for the further insight. I am new to working with Powershell and Veeam so getting this refresh script up and running has been a bit daunting. Lots of trial and error and I try to avoid forums in general as I enjoy learning things on my own. I have been running simple cmdlet scripts to try and better understand what each does and the objects/arrays that I can pull from. Being so new to both PS and Veeam, I appreciate this community and the support I've received so far. I am successfully refreshing 4 databases on a nightly basis and doing some other general SQL commands, makes me feel smart :D

I would love a cheat sheet or any further documentation that would help me navigate the nuances of working with PS and Veeam.
oleg.feoktistov
Veeam Software
Posts: 1918
Liked: 636 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: SQL restore from specific backup job

Post by oleg.feoktistov »

Hi Haus,

Can you please elaborate on the information about VBR powershell cmdlets you are looking for that is not available in the user guide?

Thank you,
Oleg
Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests