PowerShell script exchange
Post Reply
steegy
Novice
Posts: 4
Liked: never
Joined: Nov 13, 2014 7:49 am
Full Name: kevin
Contact:

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

Post by steegy »

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: Nov 13, 2014 7:49 am
Full Name: kevin
Contact:

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

Post by steegy »

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
veremin
Product Manager
Posts: 20271
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

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

Post by veremin »

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.
Marcus
Enthusiast
Posts: 35
Liked: 1 time
Joined: Jul 13, 2016 4:33 pm
Full Name: Marcus MaC
Contact:

[MERGED] How to schedule a VM restore to target server?

Post by Marcus »

Hi Guys,

I wonder could anyone help me out with the following.

I have a host server (Server1) and target server (server2)

I need to set up a scheduled restore of my 4 vm's (vm1 , vm2 , vm3 ,vm4) every Friday at 8pm.

Would nay of you have dine this before or have any handy scripts.
Thanks
Mark
veremin
Product Manager
Posts: 20271
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

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

Post by veremin »

Hi, Marcus,

Take a look at the example provided above and ask for additional clarification, should need be.

Thanks.
Marcus
Enthusiast
Posts: 35
Liked: 1 time
Joined: Jul 13, 2016 4:33 pm
Full Name: Marcus MaC
Contact:

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

Post by Marcus »

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

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

exit

above is the sample code the VM i want to restore is GTLVM01 and want to restore to GTLS01. Where do i reference the target server in code?
veremin
Product Manager
Posts: 20271
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

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

Post by veremin »

You should reference VM you want to restore in -RestorePoint parameter and target host in -Serve one. Check this page of our Help Center. Thanks.
Marcus
Enthusiast
Posts: 35
Liked: 1 time
Joined: Jul 13, 2016 4:33 pm
Full Name: Marcus MaC
Contact:

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

Post by Marcus »

Code: Select all

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

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

exit


*********************************************************************************************************************

I have backed up GTLVM01 TO A NASS box using veeam backups.

I need this script to restore to my target server which is GTLS02.

Does above script look ok ?
If i wanted to restore 2 vm's do i just add to above code?

Code: Select all

$respool = Get-VBRServer -Name "GTLVM03" | Find-VBRViResourcePool -Name "test-restore"
$datastore = Get-VBRServer -Name "GTLVM03" | Find-VBRViDatastore -Name "veeam"
veremin
Product Manager
Posts: 20271
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

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

Post by veremin »

It's rather hard to check scripts via forum correspondence. I recommend running it in test mode and seeing whether everything works as expected.

As to your second question, assigning resource pool and datastore variables won't help with multiple VM restore.

Thanks.
Marcus
Enthusiast
Posts: 35
Liked: 1 time
Joined: Jul 13, 2016 4:33 pm
Full Name: Marcus MaC
Contact:

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

Post by Marcus »

Hi,

Need help with this please.
I have a host server(Server1) that has vm1 and vm2.

I would like to use a script that will restore these two vm's to my target server(Server2).

Does below look ok or do i need to reference both VM's


Add-PSSnapin VeeamPSSnapin

$8hour = (get-date).addHours(-8)

$respool = Get-VBRServer -Name "Server1" | Find-VBRViResourcePool -Name "test-restore"

$datastore = Get-VBRServer -Name "Server1" | 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 "Server2" -ResourcePool $respool -Datastore $datastore
}

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

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

Post by veremin »

Looks OK to me. However, you have to be sure that there are only restore points have been created within last 8 hours, otherwise, more than two restore sessions will be executed. Thanks!
Marcus
Enthusiast
Posts: 35
Liked: 1 time
Joined: Jul 13, 2016 4:33 pm
Full Name: Marcus MaC
Contact:

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

Post by Marcus »

Do i need to edit above code?? anywhere !!
Thanks for your reply
veremin
Product Manager
Posts: 20271
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

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

Post by veremin »

Overall it looks good, I'm just slightly concerned with this part:

Code: Select all

$rpo = Get-VBRBackup -Name "customer1*" | Get-VBRRestorePoint -Name * | where {$_.CreationTime -gt $8hour} | sort-object @{Expression={$_.CreationTime}; Ascending=$false}
It returns all restore points created within last 8 hours. If you want to limit the scope to the latest restore points of specific VMs, both -Last switch and VM name need to be specified in sorting query.

Thanks!
Marcus
Enthusiast
Posts: 35
Liked: 1 time
Joined: Jul 13, 2016 4:33 pm
Full Name: Marcus MaC
Contact:

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

Post by Marcus »

The vM's are only been backed up once at 7pm on a daily basis.

Maybe it should be where {$_.CreationTime -gt $24hour}
Virtual VM name VM01


$rpo = Get-VBRBackup -Name "VM01*" | Get-VBRRestorePoint -Name * | where {$_.CreationTime -gt $24hour} | sort-object @{Expression={$_.CreationTime}; Ascending=$false}
veremin
Product Manager
Posts: 20271
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

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

Post by veremin »

VM Name should be used with Get-VBRRestorePoint cmdlet, not with Get-VBRBackup one. Also, I'd select only one latest restore point for each VM (just in case):

Code: Select all

Get-VBRBackup | Get-VBRRestorepoint -name "Name of your VM" | sort CreationTime -Descending | select -First 1
Thanks.
Post Reply

Who is online

Users browsing this forum: No registered users and 26 guests