Most enterprise backup software has come with powershell support to make backup administrators life easier. In this post, we will play some veeam powershell
commands to backup/restore VMs or for listing current backup jobs. The veeam powershell has more cmdlets for advanced Vss aware backup (such as SQL, Exchange) which I do not cover here. Here, I will show only VM level backup/restore with powershell. Assume, I have already connected my testing vCenter vc-01.contoso.com in Veeam. If not, you can connect to vCenter by Add-VBRvCenter cmdlet.
Now, we are going to do the following tasks in later sections. You can pick any topic in any order if the previous conditions are met.
1) Open veeam powershell or load VBRToolkit in windows powershell.
2) List connected vCenter servers
3) List VM Folders under vCenter
4) List available repositories available on Veeam server
5) Creating/Delete backup job (optional step)
5-1) Creating backup job according to my backup job creation rule
6) Applying schedule for backup jobs and activate the backup jobs.
7) Listing all backup jobs, their assigned backup repository and VMs.
8) Listing the restore points of VMs being back up.
9) Add new VM to existing backup job.
10) Re-list all VMs in backup job.
11) Remove VM from backup job.
12) Run the backup job on demand.
13) Delete the original VM and Restore the VM to original location
1) Open Veeam Powershell Console
1) Open veeam powershell or load VBRToolkit in windows powershell
Open Powershell from Veeam Backup & Replication Console (See Fig-1). Alternatively, you can start from windows powershell and run the Initialization script which is located in “C:Program FilesVeeamBackup and ReplicationConsole” (See Fig-2).
You can check the available veeam commands with Get-Command *vbr*
2) List connected vCenter servers
Since, vCenter is already added to inventory. You can check the available vCenters. See Fig-4.
Get-VBRServer -type VC
3) List Folders under vCenter
VMs are grouped into Folder in vCenter and to list these folders (See Fig-5) use Find-VBRViFolder. These folder names will be used as backup job names later.
Find-VBRViFolder -Server vc-01.contoso.com | select name,path
4) List available repositories available on Veeam server
This command will list all available repositories. The local volumes will be automatically populated as repository. You can also connect to other repositories where Veeam Transport Service installed. Network File Shares are supported. See Fig-6.
Get-VBRBackupRepository
5) Creating/Delete backup job (optional step)
You can now create backup jobs. The following command will create the backup job “TestBackupJob” with VMs of which names starting with TestVM into the “Big Backup Repo” repository.
Add-VBRViBackupJob -Name TestBackupJob -BackupRepository “Big Backup Repo” -Entity (Find-VBRViEntity -Name TestVM*)
Now list which VMs are in the selected backup job.
Get-VBRJob -Name TestBackupJob | Get-VBRJobObject
You can delete the “TestBackupJob” by the following command.
Remove-VBRJob -Job TestBackupJob -Confirm:$false
All these commands can be found in Fig-7.
5-1) Creating backup job according to my backup job creation rule
You can simply create backup jobs as shown step-6. Now, I want to create more backup jobs. The name of the backup job must be the VM folder name of each VM group. So, I use my pre-scripted command. You can change Description and Backup Repository value to suite your environment. See Fig-8.
Find-VBRViEntity | group vmfoldername | ? {$_.Name} | foreach { Add-VBRviBackupJob -name $_.Name -Entity $_.group -BackupRepository “Small
Backup Repo” -Description “Created by BackupAdmin at $((get-date).Tostring(“MM/dd/yy hh:mm tt”)) including all VMs in VMfolder” }
6) Applying schedule for backup jobs and activate the backup jobs
After creating the backup job, I have to schedule (Daily, Weekly etc) these jobs. I pre-scripted to make the job run every 30 min starting from
11:30PM. Change $StartTime and $IntervalHr to suite your environment. See Fig-9.
$StartTime=”23:30″; $IntervalHr= 0.5 ; $i=0; Get-VBrJob | foreach { If($i -eq 0) { $Time = [datetime]”$StartTime” } else { $Time = ([datetime]”$StartTime”).AddHours($IntervalHr) } ; $i++; $_ | Set-VBRJobSchedule -Daily -At $Time | Enable-VBRJobSchedule; }
7) Listing all backup jobs, their assigned backup repository and VMs
Now we can list the backup job, the VM sizes, job name, next runtime and target repository, by this command. See Fig-10.
$var = foreach($job in Get-VBRJob) { $job.GetObjectsInJob() | Add-Member “NoteProperty” -Name “NextRun” -Value $Job.ScheduleOptions.NextRun –
PassThru | select Name,ApproxSizeString,@{Name=”JobName”;Exp={$Job.Name}}, NextRun,@{Name=”TargetDir”;Exp={$Job.Target Dir }} }; $var | ft
If you want to export the result into csv file, you can use the following command.
$var = foreach($job in Get-VBRJob) { $job.GetObjectsInJob() | Add-Member “NoteProperty” -Name “NextRun” -Value $Job.ScheduleOptions.NextRun -PassThru | select Name,ApproxSizeString,@{Name=”JobName”;Exp={$Job.Name}}, NextRun,@{Name=”TargetDir”;Exp={$Job.TargetDir }} }; $var | Export-csv -Notype VmBackupJobList.csv
8) Listing the restore points of VMs being backup
You can also get all VMs and their restore points from backup catalogue.
Get-VBRRestorePoint
9) Add new VM to existing backup job
You can add new VM into existing job. Here “TestVM-3” is added to our “Test-VMs” backup job. See Fig-12.
Find-VBRViEntity -name “TestVM-3” | Add-VBRViJobObject -Job “Test-VMs”
You can find all VMs in the specific backup job. See Fig-13.
Get-VBRJobObject -Job “Test-VMs”
11) Remove VM from backup job
You can remove the specfic VM from backup job. See Fig-14.
Get-VBRJobObject -Job “Test-VMs” -Name “TestVM-3” | Remove-VBRJobObject -Completely
12) Run the backup job on demand
Now, run the backup job on demand. I run “Windows-7” backup job. See Fig-15. You will see the completion status as in Fig-16.
Start-VBRJob -Job “Windows-7”
13) Delete the original VM and Restore the VM to original location
We will now test the VM restore. First, delete the “Win7-1” VM as shown in Fig-17.
We will restore the VM into original location. You will need more parameters to restore VM with advanced options (such as different storage location, disk type, resource pool). As I deleted the “Win7-2” VM, I restored the latest backup to original location. The restore point must be retrieved first, as shown in the first command. See Fig-18.
$RestorePoint = Get-VBRRestorePoint | ? { $_.VMName -eq “Win7-2” } | select -Last 1
Start-VBRRestoreVM -RestorePoint $RestorePoint -Reason “Test Restore by Admin” -ToOriginalLocation -StoragePolicyAction Default
https://helpcenter.veeam.com/docs/backup/powershell/cmdlets.html