PowerShell script exchange
Post Reply
jevao
Enthusiast
Posts: 29
Liked: 3 times
Joined: Oct 28, 2011 11:38 pm
Full Name: James Collison
Contact:

Search for Repository with highest Free Space for Linux

Post by jevao »

I use the following code, from an Andreas script "Add VM to Job/Create Job and Add VM",
which works great for Windows Backup repositories.
Unfortunately, we have mostly Linux Backup Repositories.
I have looked, but have not found code that will also work on Linux Repositories.
Is there a way to get "Freespace" from a Linux Repository?
Thanks for any help.

Code: Select all

#Search for the Repository with the highest free space.
$ActualDate = get-date -format u
Write-host $ActualDate "Information: Search for the Repository with the highest free space"
$Repository1 = Get-VBRBackupRepository
$b = "0"
foreach ($RepositoryNames in $Repository1) {
$r = Get-VBRBackupRepository -name $RepositoryNames.name
$rServer = $r.GetHost()
$FileCommander = [Veeam.Backup.Core.CRemoteWinFileCommander]::Create($rServer.Info)
$storage = $FileCommander.GetDrives([ref]$null) | ?{$_.Name -eq $r.Path.Substring(0,3)}
$outputObj = [Math]::Round([Decimal]$storage.FreeSpace/1GB,0)
$ia = $RepositoryNames.name
If ($ia -eq "Default Backup Repository") {
$outputObj = 0
$ActualDate = get-date -format u
Write-host $ActualDate "Information: Repository:" $ia "manually set to 0GB free space (disabled)"
} ELSE {}
$ib = $outputObj
$ActualDate = get-date -format u
write-host $ActualDate "Information: Repository: " $ia " with "$ib "GB free space"

if ($ib -ge $b) {
$b = $ib
$a = $ia
} ELSE {
}
}
$ActualDate = get-date -format u
write-host $ActualDate "Information: Selected Repository:" $a " with " $b "GB free space"
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Search for Repository with highest Free Space for Linux

Post by veremin »

I'm wondering whether you have Veeam ONE deployed. It has predefined reports that provide up-to-date information about both Linux and Windows repositories; might be worth taking a look. Thanks.
jevao
Enthusiast
Posts: 29
Liked: 3 times
Joined: Oct 28, 2011 11:38 pm
Full Name: James Collison
Contact:

Re: Search for Repository with highest Free Space for Linux

Post by jevao » 1 person likes this post

Sorry. After posting before, I found the topic "Repository Size" with code from "ThomasMc" at http://forums.veeam.com/powershell-f26/ ... 10674.html

Using his code, I was able to produce this solution. Thanks

Code: Select all

# Define Function

function Get-vPCRepoInfo {
    [CmdletBinding()]
       param (
          [Parameter(Position=0, ValueFromPipeline=$true)]
          [PSObject[]]$Repository
          )
       Begin {
          $outputAry = @()
          [Reflection.Assembly]::LoadFile("C:\Program Files\Veeam\Backup and Replication\Veeam.Backup.Common.dll") | Out-Null
          function Build-Object {param($name, $path, $free, $total)
             $repoObj = New-Object -TypeName PSObject -Property @{
                   Target = $name
                   storepath = $path
                   StorageFree = [Math]::Round([Decimal]$free/1GB,2)
                   StorageTotal = [Math]::Round([Decimal]$total/1GB,2)
                   FreePercentage = [Math]::Round(($free/$total)*100)
                }
             return $repoObj
          }
       }
       Process {
          foreach ($r in $Repository) {
             if ($r.GetType().Name -eq [String]) {
                $r = Get-VBRBackupRepository -Name $r
             }
             if ($r.Type -eq "WinLocal") {
                $Server = $r.GetHost()
                $FileCommander = [Veeam.Backup.Core.CRemoteWinFileCommander]::Create($Server.Info)
                $storage = $FileCommander.GetDrives([ref]$null) | ?{$_.Name -eq $r.Path.Substring(0,3)}
                $outputObj = Build-Object $r.Name $r.Path $storage.FreeSpace $storage.TotalSpace
             }
             elseif ($r.Type -eq "LinuxLocal") {
                $Server = $r.GetHost()
                $FileCommander = new-object Veeam.Backup.Core.CSshFileCommander $server.info
                $storage = $FileCommander.FindDirInfo($r.Path)
                $outputObj = Build-Object $r.Name $r.Path $storage.FreeSpace $storage.TotalSize
             }
             elseif ($r.Type -eq "CifsShare") {
                $fso = New-Object -Com Scripting.FileSystemObject
                $storage = $fso.GetDrive($r.Path)
                $outputObj = Build-Object $r.Name $r.Path $storage.AvailableSpace $storage.TotalSize
             }
             $outputAry = $outputAry + $outputObj
          }
       }
       End {
          $outputAry
       }
}
#End of function Get-vPCRepoInfo



#Search for the Repository with the highest free space.
$ActualDate = get-date -format u
Write-host $ActualDate "Information: Search for the Repository with the highest free space"
$Repository1 = Get-VBRBackupRepository
$b = "0"
foreach ($RepositoryNames in $Repository1) {
$outputObjs = Get-VBRBackupRepository -name $RepositoryNames.name | Get-vPCRepoinfo
$outputObj = $outputObjs.StorageFree
$ia = $RepositoryNames.name
If ($ia -eq "Default Backup Repository") {
    $outputObj = 0
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: Repository:" $ia "manually set to 0GB free space (disabled)"
} ELSE {}
$ib = $outputObj
$ActualDate = get-date -format u
write-host $ActualDate "Information: Repository: " $ia " with "$ib "GB free space"

if ($ib -ge $b) {
$b = $ib
$a = $ia
} ELSE {
}
}
$ActualDate = get-date -format u
write-host $ActualDate "Information: Selected Repository:" $a " with " $b "GB free space"
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Search for Repository with highest Free Space for Linux

Post by veremin »

I'm glad to hear that you've found the solution suiting your needs. Anyway, if you haven't done it yet, I would still recommend deploying Veeam ONE with all of its robust monitoring and reporting capabilities, such as Capacity Planning for Backup Repositories and others. Thanks.
jevao
Enthusiast
Posts: 29
Liked: 3 times
Joined: Oct 28, 2011 11:38 pm
Full Name: James Collison
Contact:

Re: Search for Repository with highest Free Space for Linux

Post by jevao »

I do have Veeam One, and it has been very helpful. The "Protected VMs" includes an "Unprotected VMs" report, that I still use to see which VMs are not being backed up. I also receive reports on "Datastore" usage and "Active Snapshots", etc. For anyone not doing powershell scripting, I recommend it.
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Search for Repository with highest Free Space for Linux

Post by veremin »

Just out of curiosity - why Veeam ONE doesn't work you in terms of monitoring/reporting on backup repositories. Or what information seems to be missing in the said pre-defined report or other views provided? May be, your feedback will help us to bridge the current gap. Thanks.
tsightler
VP, Product Management
Posts: 6009
Liked: 2842 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Search for Repository with highest Free Space for Linux

Post by tsightler »

I'm interested as well but I'd guess that he's attempting to automate adding a VM automatically to a job and trying to pick the best available repo. Veeam One is great and all but it can't easily be used for instrumentation of automation workflows since it has no native API of it's own.
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Search for Repository with highest Free Space for Linux

Post by veremin »

Veeam ONE used to have PowerShell module. The module sank into oblivion years ago, as not many people were interested in it. What a pity.
jevao
Enthusiast
Posts: 29
Liked: 3 times
Joined: Oct 28, 2011 11:38 pm
Full Name: James Collison
Contact:

Re: Search for Repository with highest Free Space for Linux

Post by jevao »

Both of you are correct. "Instrumentation of automation workflows" is the focus. I am hoping the benefits of automation far outweigh the efforts. Veeam One was very helpful when getting started. Powershell allows for automating workflows.
Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests