Powershell: Restore the most recent backup of all VM's

PowerShell script exchange

Powershell: Restore the most recent backup of all VM's

Veeam Logoby steegy » Thu Nov 13, 2014 8:01 am

Hello everyone,

Using powershell, I would like to restore all VM's, made by a specific backup job, but only the latest version of the VM's.
So far I ran my tests with this script;


Add-PSSnapin VeeamPSSnapin
$respool = Get-VBRServer -Name "esx04" | Find-VBRViResourcePool -Name "test-restore"
$datastore = Get-VBRServer -Name "esx04" | Find-VBRViDatastore -Name "veeam"
$rpo = Get-VBRBackup -Name "customer1*" | Get-VBRRestorePoint -Name * | Sort-Object $_.creationtime -Descending | Select -Last 1
Start-VBRRestoreVM -restorepoint $rpo –Server "esx04" -ResourcePool $respool -Datastore $datastore
exit


Of course, due to the parameter "Select -Last 1", the script only selects the last version of the last backupped VM, and restores it.
All other VM's in the backup job are not restored.
Does anyone know if this is possible please?

Thanks in advance
steegy
Novice
 
Posts: 4
Liked: never
Joined: Thu Nov 13, 2014 7:49 am
Full Name: kevin

Re: Powershell: Restore the most recent backup of all VM's

Veeam Logoby steegy » Thu Nov 13, 2014 9:57 am

Hello,

I found a solution myself.
This script makes a list of all VM's in a specific backup job, and gets only the backups that were created in the last 8 hours.
Then it does a restore of the entire list:


Add-PSSnapin VeeamPSSnapin
$8hour = (get-date).addHours(-8)
$respool = Get-VBRServer -Name "esx04" | Find-VBRViResourcePool -Name "test-restore"
$datastore = Get-VBRServer -Name "esx04" | Find-VBRViDatastore -Name "veeam"
$rpo = Get-VBRBackup -Name "customer1*" | Get-VBRRestorePoint -Name * | where {$_.CreationTime -gt $8hour} | sort-object @{Expression={$_.CreationTime}; Ascending=$false}

foreach ($rpopervm in $rpo) {
Start-VBRRestoreVM -restorepoint $rpopervm –Server "esx04" -ResourcePool $respool -Datastore $datastore
}

exit
steegy
Novice
 
Posts: 4
Liked: never
Joined: Thu Nov 13, 2014 7:49 am
Full Name: kevin

Re: Powershell: Restore the most recent backup of all VM's

Veeam Logoby v.Eremin » Thu Nov 13, 2014 12:14 pm

Hi, Kevin,

Glad to hear that you've nailed it yourself. However, be aware that the fist script provided uses the incorrect sorting algorithm. With such expression ("Sort-Object $_.creationtime -Descending | Select -Last 1") not the latest, but rather the oldest restore point will be selected.

Thanks.
v.Eremin
Veeam Software
 
Posts: 13291
Liked: 973 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin


Return to PowerShell



Who is online

Users browsing this forum: No registered users and 4 guests