Veeam Replication – PowerShell
There is a large adoption of people using PowerShell to make their lives easier and reduce time spent on performing lengthy repeatable tasks. Veeam Replication could be one of those where in the last post I showed the simple steps to setup your replication jobs. If you have many different groups or even sites, then to do that a number of times is going to be a pain.
What I want to show you in this post is how easy it is to use the Veeam PowerShell Snap-In and the full set of capabilities to run and manage those replication tasks.
Before I begin there is a more in-depth resource on all different PowerShell commands that can be found here – https://helpcenter.veeam.com/docs/backup/powershell/replication.html?ver=95
For the purpose of this post I am going to be using VMware as my platform the command structure for Hyper-V is slightly different and the above link can also help there to determine the required steps.
Add-VBRViReplicaJob
Resource – https://helpcenter.veeam.com/docs/backup/powershell/add-vbrvireplicajob.html?ver=95
There are many parameters available for this cmdlet these can be found in more detail at the above link. I am going to be using the same parameters that we used during the walkthrough in the last post.
First, we need to add the snap in and connect to our Veeam Backup & Replication Server this can be done with the following. All code will be available in the end script.
#Add the Veeam PSSnapIn to get access to the Veeam Backup & Replication cmdlets library. Run the following command: Add-PSSnapin VeeamPSSnapin #Connects to Veeam backup server. Connect-VBRServer -server "10.0.40.10"
This command will set the server you want the replica to be sent to, it will also save it to a variable of $Server.
#Set Destination ESXi Server $server = Get-VBRServer -Name "tpm03-131.aperaturelabs.biz"
Next, we need to determine which VMs or objects we would like to replicate we can do this with the following command. And this will set the VM to a variable.
#Define Source Virtual Machine(s) $vm = Find-VBRViEntity -Server vc03.aperaturelabs.biz -Name "TPM04-DC-01"
Now we need to determine the resource pool that we would want the replica to be located in and set that as a variable.
#Define destination resource pool $pool = Find-VBRViResourcePool -Server $server -Name "TPM04-MC"
The final task of finding and setting variables comes in the form of which datastore do we want our replica to be stored in.
#Define destination datastore $datastore = Find-VBRViDatastore -Server "tpm03-131.aperaturelabs.biz" -Name "SolidFire005_iSCSI"
I will share the commands that I used to find out the below information at the end of the post. Now that we have set our variables and we know the:
- What we are replication
- Where are we replicating
Now that we have set the variables for our script it might be worth just checking that they all return the right objects this can be done with the following code.
#Now that you have defined your variables I would suggest running the following to ensure all have been populated. $server $vm $pool $datastore
Creating the Replication Job
We can now create the replication job using the above information.
#This command will create the backup job with no schedule defined. Add-VBRViReplicaJob -Name "PS Replication Job" -Server $server -Entity $vm -ResourcePool $pool -Datastore $datastore -Suffix "_replicated"
Now that we have the job created you can also see the job created in the Veeam Backup & Replication console.
At this point or later we also have the option to add additional VMs to the replication job. The first one will add the additional VMs to the variable. The second is how you would update the job.
#If you then wanted to add additional VMs to the job then you could do by adding to this line of code
$vm = Find-VBRViEntity -Server vc03.aperaturelabs.biz -Name "TPM04-DC-01", "TPM04-SQL-02"
#This command will then update the job with the newly added Virtual Machines. Set-VBRViReplicaJob -Job $job -Server $server -Entity $vm
See below the console view now with the newly added extra VM in the job,
If we had a large number of Virtual Machines to add to the job maybe with a similar naming convention such as the one I have used here, then we also have the ability to add against a wildcard to the job.
#This command will also allow you to add wildcards to your replication job Note that this will add all objects with this name.
Find-VBRViEntity -Name TPM04-* | Add-VBRViJobObject -Job $job
We can then use the following command to give us the configuration for the job. As well as setting a variable for the job.
#This command will show the job configuration
Get-VBRJob -name "PS Replication Job"
Setting the schedule
For those that have noticed we have the job created but no schedule so next up is creating a schedule for the requirements. The below command will set the schedule on the job.
#This command schedules the job represented by the $job variable to run every 1 hour Set-VBRJobSchedule -Job $job -Periodicaly -FullPeriod 1 -PeriodicallyKind Hours
As well as setting the schedule we need to enable the schedule on the job this can be done like this.
#This will enable the job schedule
Enable-VBRJobSchedule -Job $job
Navigating back into the job in the console then you will now see the schedule is now configured.
Starting the Job
We have come this far so let’s continue and start our job from the PowerShell script as well.
#The following command will allow us to start the job Start-VBRJob -Job $job
within the console
As the job progresses we will see on the destination ESXi host our replicated virtual machine.
When the job completes we get the following summary back in our PowerShell window.
Obviously, we can get that same information and some more from the console.
The complete script
#Add the Veeam PSSnapIn to get access to the Veeam Backup & Replication cmdlets library. Run the following command: Add-PSSnapin VeeamPSSnapin #Connects to Veeam backup server. Connect-VBRServer -server "10.0.40.10" #Returns hosts connected to Veeam Backup & Replication. Get-VBRServer #This will show all objects Find-VBRViEntity #This will show all resource pools available on the defined server either use variable or add in your ESXi host name/ip Find-VBRViResourcePool -Server $server #This will show available datastores on host Find-VBRViDatastore -Server "tpm03-131.aperaturelabs.biz" #This will show availabel VMs Find-VBRViEntity -Server vc03.aperaturelabs.biz -name "TPM04-*" #Set Destination ESXi Server $server = Get-VBRServer -Name "tpm03-131.aperaturelabs.biz" #Define Source Virtual Machine(s) $vm = Find-VBRViEntity -Server vc03.aperaturelabs.biz -Name "TPM04-DC-01" #Define destination resource pool $pool = Find-VBRViResourcePool -Server $server -Name "TPM04-MC" #Define destination datastore $datastore = Find-VBRViDatastore -Server "tpm03-131.aperaturelabs.biz" -Name "SolidFire005_iSCSI" #Now that you have defined your variables I would suggest running the following to ensure all have been populated. $server $vm $pool $datastore #This command will create the backup job with no schedule defined. Add-VBRViReplicaJob -Name "PS Replication Job" -Server $server -Entity $vm -ResourcePool $pool -Datastore $datastore -Suffix "_replicated" #This command will show the job configuration Get-VBRJob -name "PS Replication Job" #If you then wanted to add additional VMs to the job then you could do by adding to this line of code $vm = Find-VBRViEntity -Server vc03.aperaturelabs.biz -Name "TPM04-DC-01", "TPM04-SQL-02" #Setting a variable to the newly created replication job $job = Get-VBRJob -name "PS Replication Job" #Confirming that the variable has populated $job #This command will then update the job with the newly added Virtual Machines. Set-VBRViReplicaJob -Job $job -Server $server -Entity $vm #This command will also allow you to add wildcards to your replication job Note that this will add all objects with this name. Find-VBRViEntity -Name TPM04-* | Add-VBRViJobObject -Job $job #This command schedules the job represented by the $job variable to run every 1 hour Set-VBRJobSchedule -Job $job -Periodicaly -FullPeriod 1 -PeriodicallyKind Hours #This will enable the job schedule Enable-VBRJobSchedule -Job $job #The following command will allow us to start the job Start-VBRJob -Job $job
Reference – https://helpcenter.veeam.com/docs/backup/powershell/create_replica_vmware.html?ver=95