PowerShell script exchange
Post Reply
pt2k11
Lurker
Posts: 1
Liked: never
Joined: May 25, 2011 1:50 pm
Full Name: Platform Team
Contact:

Powershell script to report daily backup sizes

Post by pt2k11 »

We are building a script to report on the daily backup jobs using powershell.

We have the following so far but are unable to get the sizes of the actual backup files using the cmdlets. Can anyone advise how we can get this informaiton into the html report?

Code: Select all

#load Veeam Powershell Snapin
Add-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
#Style
$style = @"
<style>
TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:orange}
TD{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:lightblue}
tr.special {background: #000080;} <tr class="special"></tr>
</style>
"@

$Report = @()
$Jobs = Get-VBRJob | ?{$_.Name -match "server01"}

foreach ($job in $Jobs) {
$jobName = $job.Name
$table = New-Object system.Data.DataTable "$table01"

#region Setup table columns
$col1 = New-Object system.Data.DataColumn Index,([int])
$col2 = New-Object system.Data.DataColumn JobName,([string])
$col3 = New-Object system.Data.DataColumn VMList,([String])
$col4 = New-Object system.Data.DataColumn StartTime,([DateTime])
$col5 = New-Object system.Data.DataColumn StopTime,([DateTime])
$col6 = New-Object system.Data.DataColumn FileName,([string])
$col7 = New-Object system.Data.DataColumn CreationTime,([DateTime])
$col8 = New-Object system.Data.DataColumn AvgSpeedMB,([int])
$col9 = New-Object system.Data.DataColumn Duration,([TimeSpan])
$col10 = New-Object system.Data.DataColumn Result,([String])


$table.columns.add($col1)
$table.columns.add($col2)
$table.columns.add($col3)
$table.columns.add($col4)
$table.columns.add($col5)
$table.columns.add($col6)
$table.columns.add($col7)
$table.columns.add($col8)
$table.columns.add($col9)
$table.columns.add($col10)
#endregion

#Grab all Backup Sessions on the server where their .JobId property is the same as the Get-VBRJob objects .Id property
$session = Get-VBRBackupSession | ?{$_.JobId -eq $job.Id} | %{
$row = $table.NewRow()
$row.JobName = $_.JobName
$row.StartTime = $_.CreationTime
$row.StopTime = $_.EndTime
#Work out average speed in MB and round this to 0 decimal places, just like the Veeam GUI does.
$row.AvgSpeedMB = [Math]::Round($_.Progress.AvgSpeed/1024/1024,0)
#Duration is a Timespan value, so I am formatting in here using 3 properties - HH,MM,SS
$row.Duration = '{0:00}:{1:00}:{2:00}' -f $_.Progress.Duration.Hours, $_.Progress.Duration.Minutes, $_.Progress.Duration.Seconds

if ($_.Result -eq "Failed") {
#This is highlight is going to later be searched and replaced with HTML code to highlight failed jobs in RED  
$row.Result = "#HIGHLIGHTRED"+$_.Result+"HIGHLIGHTRED#"
} else {
#Don't highlight if the backup session didn't fail.
$row.Result = $_.Result
}
#Add this calculated row to the $table.Rows
$table.Rows.Add($row)
}

$interestingsess = $table | Sort StartTime -descending | select -first 7
$pkc = 1
$interestingsess | foreach {
#for every object in $interestingsess (which has now been sorted by StartTime) assign the current value of $pkc to the .Index property. 1,2,3,4,5,6 etc...
$_.Index = $pkc
#Increment $pkc, so the next foreach loop assigns a higher value to the next .Index property on the next row.
$pkc+=1
}


#Now we are grabbing all the backup objects (same as viewing Backups in the Veeam B&R GUI Console
$backup = Get-VBRBackup | ?{$_.JobId -eq $job.Id}

$points = $backup.GetStorages() | sort CreationTime -descending | Select -First 7 #Find and assign the Veeam Backup files for each job we are going through and sort them in descending order. Select the specified amount.
#Increment variable is set to 1 to start off
$ic = 1
ForEach ($point in $points) {
#Match the $ic (Increment variable) up with the Index number we kept earlier, and assign $table to $rows where they are the same. This happens for each object in $points
$rows = $table | ?{$_.Index -eq $ic}




#inner ForEach loop to assign the value of the backup point's filename and VMs to the row's .FileName property as well as the creation time.
ForEach ($row in $rows) {
$Backups = Get-VBRBackup | ?{$_.JobId -eq $job.Id}
$vms =
foreach ($Backup in $Backups) {
$Backup.GetObjects() | Select Name 
}
$vms1 = $vms | out-string
$vms1 = $vms1.replace(" " , "")
$vms1 = $vms1.replace("Name" , "")
$vms1 = $vms1.replace("----" , "")
$vms1 = $vms1.replace("`r" , "")
$vms1 = $vms1.replace("`n" , ";")
$vms1 = $vms1.replace(";;;" , "")
$vms1 = $vms1.replace("; ; ;" , "")
$vms1 = $vms1.replace(" ;" , "; ")
($row.FileName = $point.FileName) -and ($row.CreationTime = $point.CreationTime) -and ($row.VMList = $vms1)


#Increment the $ic variable ( +1 )
$ic+=1
}


}
#Tally up the current results into our $Report Array (add them)
$Report += $interestingsess
}


#Now we select those values of interest to us and convert the lot into HTML, assigning the styling we defined at the beginning of this script too.
$Report = $Report | Select Index, JobName, VMList, StartTime, StopTime, FileName, CreationTime, AvgSpeedMB, Duration, Result| ConvertTo-HTML -head $style

#Interesting bit - replace the highlighted parts with HTML code to flag up Failed jobs.
$Report = $Report -replace "#HIGHLIGHTRED","<font color='red'><B>"
$Report = $Report -replace "HIGHLIGHTRED#","</font></B>"
#Finally, save the report to a file on your drive.
$Report | Set-Content C:\VBRpowershell\Veeam-Backup-Report.htm

ThomasMc
Veteran
Posts: 293
Liked: 19 times
Joined: Apr 13, 2011 12:45 pm
Full Name: Thomas McConnell
Contact:

Re: Powershell script to report daily backup sizes

Post by ThomasMc »

Quick Q, v5 or 6?
Platform1
Influencer
Posts: 13
Liked: never
Joined: Jul 19, 2011 12:14 pm
Full Name: Scott Cooney
Contact:

Re: Powershell script to report daily backup sizes

Post by Platform1 »

We're using version 6.
pizzim13
Enthusiast
Posts: 94
Liked: 6 times
Joined: Apr 21, 2011 7:37 pm
Contact:

Re: Powershell script to report daily backup sizes

Post by pizzim13 »

Code: Select all

($backup.GetStorages() | Select-Object -ExpandProperty stats | ForEach-Object {$_.backupsize} | Measure-Object -Sum).sum
will return total backup size in bytes.
1-0-1
Enthusiast
Posts: 58
Liked: 2 times
Joined: Nov 30, 2010 1:38 pm
Full Name: Bernd
Contact:

Re: Powershell script to report daily backup sizes

Post by 1-0-1 »

And with v5?
pizzim13
Enthusiast
Posts: 94
Liked: 6 times
Joined: Apr 21, 2011 7:37 pm
Contact:

Re: Powershell script to report daily backup sizes

Post by pizzim13 »

Same code works
JohnPA
Novice
Posts: 5
Liked: never
Joined: Oct 13, 2011 8:42 am
Contact:

Re: Powershell script to report daily backup sizes

Post by JohnPA »

Did p2k11 get this script to work and would he/she care to share it?
mickeybyte
Influencer
Posts: 10
Liked: 5 times
Joined: Apr 06, 2011 12:28 pm
Full Name: Michiel Peene
Contact:

Re: Powershell script to report daily backup sizes

Post by mickeybyte » 2 people like this post

Hi all, I know this is a rather old post, but I stumbled upon it while searching a script to report my backup sizes.
The script in the OP was not working completely. It gave all the sessions, but not the sizes of the backup files, so I've done some improvements to get it working on 9.5u4 (could work on other versions also, but not tested. The "getstorages cmd has been replaced with getallstorages in some Veeam version).
The script now shows the history of all backup jobs over the last $days specified on top of the script (if you have jobs that have less retention than the number of $days, you'll get $days of sessions, but only backup sizes for the effective retention days, which makes sense).
Also added extra columns with dedup & compression ratios for each file

REMARK: I've added a query on the backup jobs to exclude "Archive*" jobs, because I didn't want my copy jobs to be in the report.

Here's the script:

Code: Select all

$days = 21 #Report x number of days

#load Veeam Powershell Snapin
Add-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
#Style
$style = @"
<style>
TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:orange}
TD{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:lightblue}
tr.special {background: #000080;} <tr class="special"></tr>
</style>
"@

$Report = @()
Connect-VBRServer -Server localhost
$Jobs = Get-VBRJob | where Name -notlike "Archive*"

foreach ($job in $Jobs) {
    $jobName = $job.Name
    write-host "Processing $jobName"
    $table = New-Object system.Data.DataTable "$table01"

    #region Setup table columns
    $col1 = New-Object system.Data.DataColumn Index,([int])
    $col2 = New-Object system.Data.DataColumn JobName,([string])
    $col3 = New-Object system.Data.DataColumn VMList,([String])
    $col4 = New-Object system.Data.DataColumn StartTime,([DateTime])
    $col5 = New-Object system.Data.DataColumn StopTime,([DateTime])
    $col6 = New-Object system.Data.DataColumn FileName,([string])
    $col6a = New-Object system.Data.DataColumn FileSize,([String])
    $col6b = New-Object system.Data.DataColumn BackupSize,([String])
    $col6c = New-Object system.Data.DataColumn DataSize,([String])
    $col6d = New-Object system.Data.DataColumn DedupRatio,([String])
    $col6e = New-Object system.Data.DataColumn CompressRatio,([String])
    $col7 = New-Object system.Data.DataColumn CreationTime,([DateTime])
    $col8 = New-Object system.Data.DataColumn AvgSpeedMB,([int])
    $col9 = New-Object system.Data.DataColumn Duration,([TimeSpan])
    $col10 = New-Object system.Data.DataColumn Result,([String])


    $table.columns.add($col1)
    $table.columns.add($col2)
    $table.columns.add($col3)
    $table.columns.add($col4)
    $table.columns.add($col5)
    $table.columns.add($col6)
    $table.columns.add($col6a)
    $table.columns.add($col6b)
    $table.columns.add($col6c)
    $table.columns.add($col6d)
    $table.columns.add($col6e)
    $table.columns.add($col7)
    $table.columns.add($col8)
    $table.columns.add($col9)
    $table.columns.add($col10)
    #endregion

    #Grab all Backup Sessions on the server where their .JobId property is the same as the Get-VBRJob objects .Id property
    $session = Get-VBRBackupSession | ?{$_.JobId -eq $job.Id} | %{
        $row = $table.NewRow()
        $row.JobName = $_.JobName
        $row.StartTime = $_.CreationTime
        $row.StopTime = $_.EndTime
        #Work out average speed in MB and round this to 0 decimal places, just like the Veeam GUI does.
        $row.AvgSpeedMB = [Math]::Round($_.Progress.AvgSpeed/1024/1024,0)
        #Duration is a Timespan value, so I am formatting in here using 3 properties - HH,MM,SS
        $row.Duration = '{0:00}:{1:00}:{2:00}' -f $_.Progress.Duration.Hours, $_.Progress.Duration.Minutes, $_.Progress.Duration.Seconds

        if ($_.Result -eq "Failed") {
        #This is highlight is going to later be searched and replaced with HTML code to highlight failed jobs in RED  
        $row.Result = "#HIGHLIGHTRED"+$_.Result+"HIGHLIGHTRED#"
        } else {
        #Don't highlight if the backup session didn't fail.
        $row.Result = $_.Result
        }
        #Add this calculated row to the $table.Rows
        $table.Rows.Add($row)
                
    }

    
    #Now we are grabbing all the backup objects (same as viewing Backups in the Veeam B&R GUI Console
    $backup = Get-VBRBackup | ?{$_.JobId -eq $job.Id}

    $points = $backup.GetAllStorages() | sort CreationTime -descending | Select -First $days #Find and assign the Veeam Backup files for each job we are going through and sort them in descending order. Select the specified amount.
    #if ($days -gt $points.Count) { $days = $points.Count} #if days is more than backup files found, limit to number of backups, otherwise, empty data lines ==> commented out, because it's not working as it should for now...
        
    
    $interestingsess = $table | Sort StartTime -descending | select -first $days
    
    $pkc = 1
    $interestingsess | foreach {
        
        #for every object in $interestingsess (which has now been sorted by StartTime) assign the current value of $pkc to the .Index property. 1,2,3,4,5,6 etc...
        $_.Index = $pkc
        #Increment $pkc, so the next foreach loop assigns a higher value to the next .Index property on the next row.
        $pkc+=1
    }
    

    #Increment variable is set to 1 to start off
    $ic = 1
    ForEach ($point in $points) {
        #Match the $ic (Increment variable) up with the Index number we kept earlier, and assign $table to $rows where they are the same. This happens for each object in $points
        $rows = $table | ?{$_.Index -eq $ic}

        #inner ForEach loop to assign the value of the backup point's filename and VMs to the row's .FileName property as well as the creation time.
        ForEach ($row in $rows) {
            $Backups = Get-VBRBackup | ?{$_.JobId -eq $job.Id}
            $vms =
            foreach ($Backup in $Backups) {
            $Backup.GetObjects() | Select Name 
            }
            $vms1 = $vms | out-string
            $vms1 = $vms1.replace(" " , "")
            $vms1 = $vms1.replace("Name" , "")
            $vms1 = $vms1.replace("----" , "")
            $vms1 = $vms1.replace("`r" , "")
            $vms1 = $vms1.replace("`n" , ";")
            $vms1 = $vms1.replace(";;;" , "")
            $vms1 = $vms1.replace("; ; ;" , "")
            $vms1 = $vms1.replace(" ;" , "; ")
            if ($point.PartialPath -ne "") 
            {
                ($row.FileName = $point.PartialPath) -and ($row.CreationTime = $point.CreationTime) -and ($row.VMList = $vms1) | out-null
                ($row.BackupSize = ($point.Stats.BackupSize/1GB).ToString(".00")) -and ($row.CreationTime = $point.CreationTime) -and ($row.VMList = $vms1) | out-null
                ($row.DataSize = ($point.Stats.DataSize/1GB).ToString(".00")) -and ($row.CreationTime = $point.CreationTime) -and ($row.VMList = $vms1) | out-null
                ($row.DedupRatio = (100/($point.Stats.DedupRatio)).ToString(".0")) -and ($row.CreationTime = $point.CreationTime) -and ($row.VMList = $vms1) | out-null
                ($row.CompressRatio = (100/($point.Stats.CompressRatio)).ToString(".0")) -and ($row.CreationTime = $point.CreationTime) -and ($row.VMList = $vms1) | out-null
            
                #Increment the $ic variable ( +1 )
                $ic+=1
                
            }
        }
    }
    #Tally up the current results into our $Report Array (add them)
    $Report += $interestingsess
}

Disconnect-VBRServer 
#Now we select those values of interest to us and convert the lot into HTML, assigning the styling we defined at the beginning of this script too.
$Report = $Report | Select Index, JobName, VMList, StartTime, StopTime, FileName, BackupSize, DataSize, DedupRatio, CompressRatio, CreationTime, AvgSpeedMB, Duration, Result| ConvertTo-HTML -head $style

#Interesting bit - replace the highlighted parts with HTML code to flag up Failed jobs.
$Report = $Report -replace "#HIGHLIGHTRED","<font color='red'><B>"
$Report = $Report -replace "HIGHLIGHTRED#","</font></B>"
#Finally, save the report to a file on your drive.
$Report | Set-Content C:\temp\Veeam-Backup-Report.htm -Force

Maybe it can be of use for anyone :)

Regards,
Michiel
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Powershell script to report daily backup sizes

Post by oleg.feoktistov » 2 people like this post

Hi Michiel,

Looks and works great! Thanks for sharing.
You can also upload it to VeeamHub.

Best regards,
Oleg
rakli
Lurker
Posts: 1
Liked: never
Joined: Feb 22, 2017 12:02 pm
Full Name: Rainer Klingenberg
Contact:

[MERGED] The sum of all full and incremental backup

Post by rakli »

Hi,

How can I determine the size of all Full Backup or all Incremental Backup.

Best Regards

Rainer
Egor Yakovlev
Veeam Software
Posts: 2536
Liked: 680 times
Joined: Jun 14, 2013 9:30 am
Full Name: Egor Yakovlev
Location: Prague, Czech Republic
Contact:

Re: Powershell script to report daily backup sizes

Post by Egor Yakovlev »

Hi Rainer,
I have merged your post into existing thread. Please check script above.
/Thanks!
Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests