PowerShell script exchange
Post Reply
Sindhuja
Influencer
Posts: 21
Liked: never
Joined: Jan 22, 2018 11:01 pm
Full Name: Sindhuja Balasubramanian
Contact:

Associating a task session with a backup file

Post by Sindhuja »

Hello,

I have been working on generating few reports for Veeam.
We would like to capture the association of a session with the actual backup that was generated as a result.

Example,
  • Let's say, I have a job that is scheduled to run twice or thrice a day.
  • For every run, a session and corresponding task sessions are created.
  • If the task sessions are successful, they generate the backup files (like *.vbk or *.vib).
The objective is to associate the backup files generated with their corresponding session or task sessions.

I am aware of the commands that gives us the session details (Get-VBRBackupSession, Get-VBRTaskSession) and backup details (Get-VBRBackup, $backup.GetAllStorages())
I couldn't get the session information from backup object ($backup.GetAllStorages()).

Is there any powershell command/ function I can use to find out the session details from the backup?

Thanks,
Sindhuja
tsightler
VP, Product Management
Posts: 6011
Liked: 2843 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Associating a task session with a backup file

Post by tsightler » 1 person likes this post

Veeam has a fairly loose association between jobs and backups. This makes sense because it's completely possible to delete a job and still have backups on disk, or delete the backups for a job and still have the job and all session data in the database. However, assuming the job session data and backup data is still current, it is possible to determine the association between a backup session ID and a restore point ID. Once you've associated a specific backup session to a specific restore point ID, you can then match the individual task sessions and restore points by object ID. Here is a simple example:

Code: Select all

$JobName = "<Name_of_Job>"

# Get a backup session for the job (in this example I'm just grabbing the most recent)
$JobSessions = Get-VBRBackupSession | ?{$_.JobName -eq $JobName} | Sort -Property CreationTime | Select -Last 1

# Get the task sessions for the specific backup session
$TaskSessions = $JobSessions | Get-VBRTaskSession

# Get the restore points for the specific backup session
$RestorePoints = Get-VBRBackup -Name $JobName | Get-VBRRestorePoint | ?{$_.PointId -eq $JobSessions.CurrentPointId}

# Now that we have the task sessions and restore points for the given job session we can match them up via ObjectID
# This example just loops through each task session and displays the associated restore point
ForEach ($TaskSession in $TaskSessions) {
    $RestorePoints | ?{$_.ObjectId -eq $TaskSession.Info.ObjectId}
}
Note that the above code has no logic for dealing with error'd sessions, you could potentially filter out sessions that did not end in success or warning state, and also filter out incomplete restore points (perhaps using the IsCorrupted flag for example). Also, in the example above I'm just grabbing the most recent backup session, however, it's totally possible to grab any session, including sessions where the restore points are no longer on disk due to retention since session history is commonly kept much longer than restore points. In that case the query for restore points will return no points for the specific session.

There may be a better way to do this, but this is the simplest method that I've found. Also, if you are reporting on many jobs/sessions it's easy to have the code collect all Backup Sessions, Task Sessions, and Restores Points for all jobs into variables at the start of the script and then just filter out the data as needed.
johan.h
Veeam Software
Posts: 712
Liked: 182 times
Joined: Jun 05, 2013 9:45 am
Full Name: Johan Huttenga
Contact:

Re: Associating a task session with a backup file

Post by johan.h »

This method works great for backup jobs. But not so well for replicas as CurrentPointId is empty. So for that you would have to find the closest matching time instead.

Keep in mind that the Get-VBRReplica command is very slow and that the example below assumes that you have a single VM in a job. But conceptually you would do something like this.

Code: Select all

$RestorePoints = Get-VBRReplica -Name "<Name_of_Job"> | Get-VBRRestorePoint

$ClosestTime = $null
ForEach($RestorePoint in $RestorePoints) {
    $RestorePointTimeDifference = New-TimeSpan -Start $RestorePoint.CreationTime -End $JobSessions.CreationTime
    if (($ClosestTime -eq $null) -or ($RestorePointTimeDifference.Seconds -lt $ClosestTime.Seconds)) { 
        $ClosestTime = $RestorePointTimeDifference 
        $RelatedRestorePoint = $RestorePoint }
    }
   $RelatedRestorePoint
}
tsightler
VP, Product Management
Posts: 6011
Liked: 2843 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Associating a task session with a backup file

Post by tsightler » 1 person likes this post

Good point Johan. I actually use a similar approach for replicas. Basically if the object ID matches, and the replica creation time is between the start and stop time of the task session, I assume that point is associated with the task session, so something like this:

Code: Select all

$JobName = "<Replica_Job_Name>"

# Get the sessions for the job (in this example I'm just grabbing the most recent)
$JobSessions = Get-VBRBackupSession | ?{$_.JobName -eq $JobName} | Sort -Property CreationTime | Select -Last 1

# Get the task sessions for the specific job session
$TaskSessions = $JobSessions | Get-VBRTaskSession

# Get all of the replica points for the given job
$RestorePoints = Get-VBRReplica -Name $JobName | Get-VBRRestorePoint

# Now that we have the task sessions and restore points for the given job session we can match them up via ObjectID
# and creation time. This example just loops through each task session and displays the associated replica point
ForEach ($TaskSession in $TaskSessions) {
    $RestorePoints | ?{$_.ObjectId -eq $TaskSession.Info.ObjectId -and $_.CreationTimeUtc -gt $TaskSession.Progress.StartTimeUtc -and $_.CreationTimeUtc -lt $TaskSession.Progress.StopTimeUtc}
}
Sindhuja
Influencer
Posts: 21
Liked: never
Joined: Jan 22, 2018 11:01 pm
Full Name: Sindhuja Balasubramanian
Contact:

Re: Associating a task session with a backup file

Post by Sindhuja »

That's a lot of useful information!

As mentioned in the earlier posts, the use-case may be like adding a 'resourcepool' or a 'datastore' to a single policy.
As a result of the policy configurations, we can have multiple VMs associated to a single backup file (*.vbk or *.vib).

To associate the session with the created backup file, I was trying to check if the creation time of the backup file (Get-VBRBackup - GetAllStorages()) is between the start and end time of the session. If there is a match, then the associate is created.

Similar to what is mentioned in the above post, by getting the replica and restore points.

Thanks for your inputs!
Sindhuja
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests