PowerShell script exchange
Vitaliy S.
VP, Product Management
Posts: 27055
Liked: 2710 times
Joined: Mar 30, 2009 9:13 am
Full Name: Vitaliy Safarov
Contact:

Re: backup single VM (not all VMs) within job?

Post by Vitaliy S. »

Yes, this makes total sense and is already acknowledged as an important feature > Backup VM within a Job - especially SQL?
stephen.loera
Service Provider
Posts: 39
Liked: 7 times
Joined: May 11, 2016 4:59 am
Full Name: Stephen Loera
Contact:

Re: backup single VM (not all VMs) within job?

Post by stephen.loera »

Any chance that this will be a feature in Update 3?

I know that "Quick Backup" is an option but that only works if there is an existing full backup. If I add a new VM to backup, I would like to be able to kick off a "full" backup on that newly added VM without the need to run the eintire job on all the VMs.
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: backup single VM (not all VMs) within job?

Post by veremin »

Any chance that this will be a feature in Update 3?
Update 3 does not have this feature implemented. So, for now the scripts and Quick Backup (for cases where full backup is there) are the only options. Thanks.
Dani
Novice
Posts: 5
Liked: never
Joined: Mar 12, 2015 11:44 am
Full Name: Daniel
Contact:

[MERGED] Backup only one VM within a job with powershell

Post by Dani »

Hello

We have a backup job configured to backup all vms on a specific volume.

Virtual machines to backup: "cluster shared volume 01"
Runtime: every day at 10PM.

Additionally, we want to run a backup of one VM whithin this volume every 2 hours from 8AM to 6PM.

What would be the best way to do that?
Would it be possible with a powershell script?
If yes, how?

Thanks,
Daniel
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: backup single VM (not all VMs) within job?

Post by veremin »

Your post has been merged into existing discussion talking about similar matter. Please, check the answers provided above. Thanks.
t481
Enthusiast
Posts: 82
Liked: 1 time
Joined: Apr 28, 2015 7:52 am
Contact:

Re: backup single VM (not all VMs) within job?

Post by t481 »

hi Veeam engineers,
Surely this is an indication that there is a requirement for this feature to be implemented. I'm having the same issue whereby I need to manually backup individual VMs that have just been added to a job and I need to test how this would work within the same job. I could create a separate backup job to test the backup but I don't see why such a simple feature hasn't been added especially since it's been present in legacy backup software for as long as I can remember!
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: backup single VM (not all VMs) within job?

Post by veremin »

Isn't Quick Backup something you're looking for? Thanks.
t481
Enthusiast
Posts: 82
Liked: 1 time
Joined: Apr 28, 2015 7:52 am
Contact:

Re: backup single VM (not all VMs) within job?

Post by t481 »

Almost the same thing here.

I added an SQL VM to a job and wanted to back it up but could not use QuickBackup due to lack of existing full backup file. Sometimes you just want to run a job against a VM immediately for political as well as practical reasons e.g. SQL log backup and truncation.
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: backup single VM (not all VMs) within job?

Post by veremin »

Then, you can exclude all but one VM, back it up and include other VMs back. Some scripts examples are provided above. Thanks.
t481
Enthusiast
Posts: 82
Liked: 1 time
Joined: Apr 28, 2015 7:52 am
Contact:

Re: backup single VM (not all VMs) within job?

Post by t481 »

I contacted your support team seeking help with the script as I had an issue with it (case no. 02593627) and was told that this is for an older version of Veeam. I was also advised to write a "feature request" here in the forum.
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: backup single VM (not all VMs) within job?

Post by veremin »

Consider your feature request counted.

As to the script, can you tell me how VMs are added to backup job? As individual VMs or as a part of container (folder, resource pool, etc.)?

Thanks.
adb98
Enthusiast
Posts: 63
Liked: 13 times
Joined: Jul 21, 2016 5:03 pm
Full Name: Aaron B
Contact:

Re: backup single VM (not all VMs) within job?

Post by adb98 »

I have been wanting to write a script for a while that will do a quick backup via powershell. I have done so but had been contemplating wither to publish it out since I know I will get my fair share of snickers as I am no expert but I decided to go for it. Be easy on me as I not a Powershell god. Once you source the script and have the snappin registered, it will load the snappin, connect to your VBR server you tell it to, lookup the name you are trying to backup, check to see if that name is valid, if not it gives a list that are a close match. If it does match the script moves on and ensures that the backup job the VM is in is not currently running, if that passes it starts the quick backup.

Legal requires me to say run at your own risk. Adjust to suite your needs and enjoy.

Code: Select all

Function QuickBackup { 
# ----------------------------------------------------------------------------------------------
# Author - ABelisle - Sr. Sys Admin - THS 03/01/18
#**Legal stuff I have to state** This script comes with no warranty and I am not responsible for any issues this script may cause.
#Prequisites you will need to have the VeeamSnappin registered. It will add the snapping automatically but it has to be there.
#Function of this script is to load VeeamPSSnappin module, log into the main VBR server, check server name not given, and if valid start a quick backup
#Before using source the script . ./veeam quick Backup - after it is sourced. Type QuickBackup to run the script. You must use -vbrserver and type in a Veeam Server
# ----------------------------------------------------------------------------------------------
<#
Examples of this script are as follows:

QuickBackup server1,server2,server3

Example 1 - Quickbackup -vbrserver test -computername server1
Example 2 - Quickbackup -vbrserver test -computername server1,server2


#>
#Parameters   
Param(
[parameter(ValueFromPipeLineByPropertyName=$true,Mandatory=$true)][string[]]$computername, 
[parameter(Mandatory=$true)][string[]]$VBRServer)


#Loading Veeam Snappin Piece and Veeam Connection to the server - If it is not registered it tells you and stops the script. Once the Snappin is loaded it trys to connect to the VBR Server

    if( -Not( Get-PSSnapin -Name "VeeamPSSnapIn" -ErrorAction:SilentlyContinue ) ) { 
        if( Get-PSSnapin -Registered | Where-Object { $_.Name -eq "VeeamPSSnapIn" } ) { 
            # Snap-in is available, but not loaded; Add the snap-in
            Add-PSSnapin -Name "VeeamPSSnapIn" 
        } else { 
            # Module not available on the computer
            Write-Host -ForegroundColor Red "ERROR: The specified snap-in VeeamPSSnapIn is not found on this computer."
            break
        } # End if
    } else { 
        # Module is already loaded

    } # End if


try {((Get-VBRServer -name "$vbrserver" -ErrorAction SilentlyContinue) -eq $null)
    Write-Host -ForegroundColor Green "You are already connected to Veeam VBR server $vbrserver...Skipping sign in"}
catch
    {Write-Host "Please enter your credentials to connect to $vbrserver server"
    $creds = get-credential
    (Connect-VBRServer -server "$VBRServer" -Credential $creds)
}  


#This piece is where we check the server names given and ensure all is ok and run the script. It does this for each VM.

foreach ($c in $computername){
if((Find-VBRViEntity -Name $c) -eq $null){
    Write-Host -ForegroundColor Red "Server $c not valid. These server names listed below are a close match to what you might be looking for. Once name is corrected run it again. "
    (Find-VBRviEntity -Name "*$c*" |select Name)
     break
}
else{
    Write-Host -ForegroundColor Green "Server $c Found. Checking if Job containing VM is running."} 
    

if((Find-VBRViEntity -Name $c |Start-VBRQuickBackup -ErrorAction SilentlyContinue) -eq $null){
    write-error "Backup Job with $c in it already running! Check VBR Console"
    break
}
else{
    Write-Host -ForegroundColor Green "Quick Backup Request submitted for $c. Check VBR Console."}
    }
    
} # End Function Load-Module 

veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: backup single VM (not all VMs) within job?

Post by veremin »

Thank you, Aaron, for sharing the script; much appreciated.

However, it won't necessarily help in this particular case: Quick Backup requires full backup which does not exist for newly-added VMs.

Thanks.
t481
Enthusiast
Posts: 82
Liked: 1 time
Joined: Apr 28, 2015 7:52 am
Contact:

Re: backup single VM (not all VMs) within job?

Post by t481 »

The VMs are normally added individually.
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: backup single VM (not all VMs) within job?

Post by veremin »

I've just checked and the modified script provided on the second page works as expected. Feel free to leverage it. Thanks.
wwx500
Novice
Posts: 4
Liked: 1 time
Joined: Jul 11, 2017 8:57 pm
Full Name: William Wallhausser
Contact:

Re: backup single VM (not all VMs) within job?

Post by wwx500 »

Quick backup is handy, but it needs more options.
Eg. I have a vm with a failed backup but the failed vm is in multiple jobs and I have no way to tell quick backup which job should be rerun with the single vm.
I would also like the option to add several vms to rerun them in quick job rather than having to run, wait and monitor it several times.
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: backup single VM (not all VMs) within job?

Post by veremin »

I would also like the option to add several vms to rerun them in quick job rather than having to run, wait and monitor it several times.
Should be already there, as -VM parameter accepts the array of VMs for which you want to perform the QuickBackup.
reaperhammer
Service Provider
Posts: 27
Liked: 9 times
Joined: Aug 18, 2016 7:59 pm
Full Name: Will S
Contact:

Re: backup single VM (not all VMs) within job?

Post by reaperhammer » 1 person likes this post

I modified the script on Page2 to be compatible with Hyper-V VM's as well as VMware as the previous was only working with VMware.
WARNING I noticed this script will reset any custom disk exclusions, guest processing and credentials on the VM's that are removed then re-added to the job.
Another reason why Veeam should implement this feature in the GUI.

Code: Select all

# Load Veeam snapin
Add-PsSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
 
Clear-Host
Write-Host "Veeam Single VM Backup Script`n"
Write-Host "Notes:"
Write-Host "VM must be explicitly defined in the job"
Write-Host "VM can not be inherited by a folder, datastore or other object"
Write-Host "else it will get excluded as well and job will be empty."
Write-Host "Always confirm job completion via Veeam BR Console!"
 
# Check Veeam Version (Must be v8)
If ((Get-PSSnapin VeeamPSSnapin).Version.Major -ne 8) {
   Write-Host "You must be running VBR v8 to run this script...Exiting"
   Exit
}
$ErrorActionPreference = 'Inquire'
 
# User Input
$jobName = Read-Host "Enter Job Name"
$vmName = Read-Host "Enter VM Name"
 
 
# Find the job that has our VM
$job = Get-VBRJob | ?{$_.Name -eq $jobName}
 
# Get all objects in job apart from our target VM
$execObjs = $job.GetObjectsInJob() | ?{$_.Name -ne $vmName}
 
# Exclude the objects from the job
Remove-VBRJobObject -Objects $execObjs
 
# Start the job only backing up the target VM
$runJob = Start-VBRJob -Job $job
 
# Find the exclude job objects
$incObjs = $job.GetObjectsInJob() | ?{$_.Type -eq "Exclude"}
 
# Delete the exclude objects and re-add to job
foreach ($obj in $incObjs) {
try {
    if ($obj.info.platform.platform -eq 'EHyperV') {
    $Excitem = Find-VBRHvEntity -Name $obj.Name
    Add-VBRHvJobObject -job $Job -Entities $Excitem | Out-Null
    $obj.Delete()
    } elseif ($obj.info.platform.platform -eq 'EVmware') {
    $Excitem = Find-VBRViEntity -Name $obj.Name
    Add-VBRViJobObject -job $Job -Entities $Excitem | Out-Null
    $obj.Delete()
    } else {
    Write-Error "This script does not support the entity type $($obj.info.platform.platform)"
    }
    } catch {
    $error[0]
    } #catch
}
$ErrorActionPreference = 'SilentlyContinue'
Write-Host "`nBackup Complete"
mrholm
Expert
Posts: 170
Liked: 15 times
Joined: Apr 20, 2018 8:12 am
Full Name: Mats Holm
Contact:

[MERGED] Script to only run one VM in a job

Post by mrholm »

Hi
I have an old script from this forum that worked before (not sure what version of Veeam) but now with 9.5 it doesn't work anymore.

The script works fine until it should delete excluded objects and then re-add those objects to the job in the section "#* Delete the exclude objects and re-add to job"
The script looks like this:

#* Load Veeam snapin
Add-PsSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue

#* User Input
$jobName = Read-Host "Enter Job Name"
$vmName = Read-Host "Enter VM Name"


#* Find the job that has our VM
$job = Get-VBRJob | ?{$_.Name -eq $jobName}

#* Get all objects in job apart from our target VM
$execObjs = $job.GetObjectsInJob() | ?{$_.Name -ne $vmName}

#* Exclude the objects from the job(*Note: this isn't removing the objects
#* from the job)
Remove-VBRJobObject -Objects $execObjs

#* Start the job only backing up the target VM
Start-VBRJob -Job $job

#* Find the exclude job objects
$incObjs = $job.GetObjectsInJob() | ?{$_.Type -eq "Exclude"}

#* Delete the exclude objects and re-add to job
foreach ($obj in $incObjs) {
Excitem = $obj.GetObject().GetItem()
Add-VBRViJobObject -job $Job -Entities $Excitem
$obj.Delete() | Out-Null
}


The error I get is:

Method invocation failed because [Veeam.Backup.Core.CHierarchyObj] does not contain a method named 'GetItem'.
At C:\Veeam_Scripts\test1.ps1:27 char:4
+ $Excitem = $obj.GetObject().GetItem()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound


Anyone with knowledge that can share som light on how to do this?
Regnor
Veeam Software
Posts: 934
Liked: 287 times
Joined: Jan 31, 2011 11:17 am
Full Name: Max
Contact:

Re: Script to only run one VM in a job

Post by Regnor »

Wouldn't a quick backup be much easier?

https://helpcenter.veeam.com/docs/backu ... tml?ver=95
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: backup single VM (not all VMs) within job?

Post by veremin »

If quick backup does not work for some reason, check suggestions above. Thanks.
Herbert Sz
Veeam Software
Posts: 1027
Liked: 201 times
Joined: Oct 02, 2017 7:28 pm
Full Name: Herbert Szumovski
Contact:

Re: backup single VM (not all VMs) within job?

Post by Herbert Sz » 1 person likes this post

I quick and dirty adapted the first of the above scripts for a VMWare customer, to support VBR V10.

Code: Select all

# Load Veeam snapin
Add-PsSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
 
Clear-Host
Write-Host "Veeam Single VM Backup Script`n"
Write-Host "Notes:"
Write-Host "VM must be explicitly defined in the job"
Write-Host "VM can not be inherited by a folder, datastore or other object"
Write-Host "else it will get excluded as well and job will be empty."
Write-Host "Always confirm job completion via Veeam BR Console!"
 
# Check Veeam Version (Must be v10)
If ((Get-PSSnapin VeeamPSSnapin).Version.Major -ne 10) {
	Write-Host "You must be running VBR v10 to run this script...Exiting"
	Exit
}
 
# User Input
$jobName = Read-Host "Enter Job Name"
$vmName = Read-Host "Enter VM Name"
  
# Find all objects in job except our target VM
$exclObjs = Get-VBRJobObject -job $jobName | ? {$_.name -ne $vmName }
 
# Exclude the objects from the job
Remove-VBRJobObject -Objects $exclObjs
 
# Start the job only backing up the target VM
$runJob = Start-VBRJob -Job $jobName
  
# Delete the exclude objects and re-add to job
foreach ($obj in $exclObjs) {
    Find-VBRViEntity -Name $obj.Name | Add-VBRViJobObject -job $jobName -Force | Out-Null
    $obj.Delete()
}
 
Write-Host "`nBackup Complete"
Herbert Sz
Veeam Software
Posts: 1027
Liked: 201 times
Joined: Oct 02, 2017 7:28 pm
Full Name: Herbert Szumovski
Contact:

Re: backup single VM (not all VMs) within job?

Post by Herbert Sz »

Dear Powershellers,
are you sure that the above scripts really worked as expected (including my own above, because I just replaced some commands without checking it a lot) ?
From my point of view these scripts have a dangerous approach:
They remove VMs from a backup job AND put them into the exclude-list, and after running the backup of the remaining VM just re-added the excluded VMs.
That means, these VMs are never backed up again, though they were added back to the job, because they are still on the exclude list. The job will run through green, and you may possibly run like that for weeks, if you don't look into it.
To fix this problem, you must manually edit the job and remove all VMs from the exclude list. But next time you run the script you will have the same problem again!

I found this, because I took a closer look into it to add HyperV support.
So below is the new script for V10 fixing the above issue, and supporting VMWare and HyperV.

Code: Select all

<# ---
Script purpose:
	temporarily remove all VM's except one from a job, then run the job, 
	and re-add all VM's again.  
	E.g. used after adding a new VM, and run a first Full for 
	it immediately, without generating new backups for the others.
--- #>

# Load Veeam snapin
Add-PsSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue

Write-Host "Veeam Single VM Backup Script`n"
Write-Host "Notes:"
Write-Host "VM must be explicitly defined in the job."
Write-Host "VM can not be inherited by a folder, datastore or other object,"
Write-Host "else it will get excluded as well and job will be empty."
Write-Host "Application settings (like prefreeze scripts," 
Write-Host "db logs, etc) will not be kept for removed and re-added VMs."
Write-Host "Always confirm job completion via Veeam BR Console!"
 
# Check Veeam Version (Must be at least v10)
If ((Get-PSSnapin VeeamPSSnapin).Version.Major -ne 10) {
	Write-Host "You must be running VBR v10 to run this script...Exiting"
	Exit
}
 
# User Input
$jobName = Read-Host "`nEnter Job Name"
$vmName = Read-Host "Enter VM Name"

# Error stop
trap {"`nError, terminating: "; break}

# Find all objects in job except our target VM
$exclObjs = ($allObjs = Get-VBRJobObject -job $jobName) | ? {$_.name -ne $vmName }
if ($exclObjs.count -eq $allObjs.count) { throw "`nError: $jobName does not contain $vmName, check your input." }

# Remove the objects from the job
Remove-VBRJobObject -Completely -Objects $exclObjs 
Write-Host "$($exclObjs.count) objects temporarily removed from job $jobName, starting backup $vmName ..."
 
Start-Sleep -s 1
# Start the job only backing up the target VM
$runJob = Start-VBRJob -Job $jobName
  
# Re-add the removed objects to the job (all VM specific application backup settings will be lost)
Write-Host "Adding $($exclObjs.count) objects to job $jobName again, please wait ..."
foreach ($obj in $exclObjs) {
	$cmdletType = @{$true="VBRVi"; $false="VBRHv"}[$obj.info.platform.platform -eq 'EVmware']
	"Find-{0}Entity -Name {1} | Add-{0}JobObject -job {2} -Force | Out-Null" -f $cmdletType,$obj.name,$jobName | iex 
}
Write-Host "`nSingleVMBackup finished."
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: backup single VM (not all VMs) within job?

Post by veremin »

Why not to remove excluded VMs in the script? Just add the line below Start-VBRJob to remove VMs from exclusion list. Thanks!
Herbert Sz
Veeam Software
Posts: 1027
Liked: 201 times
Joined: Oct 02, 2017 7:28 pm
Full Name: Herbert Szumovski
Contact:

Re: backup single VM (not all VMs) within job?

Post by Herbert Sz »

To avoid misunderstandings: these scripts are for VMs added by name.
Why not to remove excluded VMs in the script? Just add the line below Start-VBRJob to remove VMs from exclusion list.
That's exactly what I do with my last script proposal above: removing the VMs from the exclusion list, which was not done before.
Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests