Article number
Affected Versions
Source Hypervisor
Target Hypervisor

Pre/Post Recovery Scripts with Zerto

Viewed 233 times


Administrators need to perform specific actions on the VPGs, before and/or after a recovery operation (test failover/live failover/move).

Note: In a DRaaS scenario, the end customer is not able to configure pre/post recovery scripts, as the recovery cloud ZVM is masked. Instead, the cloud provider (who is hosting the DR site), can configure the pre/post recovery scripts.


Important: Zerto recommends having the pre/post recovery scripts available at both Protected and DR ZVM machines, using the same path, for situations when you failover with reverse replication, and the ZVMs switch roles. (site A-> site B, failover with reverse will become: site B -> site A). The following example is a PowerShell post-recovery script, that logs the name of the VPG, the time of execution and the DR operation performed on the VPG, after (post) a DR operation. First, set up the post-recovery script at the DR ZVM machine, for example at “C:\ZertoScripts”.    The script in this example is ‘PostScript.ps1’. It produces a text file called “VPGOperations.txt” that logs the time of the execution, name of VPG, and what operation was performed on the VPG, after its DR operation.   Command To Run: powershell.exe Params: “C:\ZertoScripts\PostScript.ps1” %ZertoOperation% %ZertoVPGName% Note that the quotes in the script path are required when there are spaces in the path to the script. You can use the following script, and the screenshots attached, as a guideline when creating your own pre/post recovery scripts: $Operation = $env:ZertoOperation $VPG = $env:ZertoVPGName $time = Get-Date if ($Operation -eq "Test") {     "$time VPG: $VPG has been tested successfully!" >> "C:\ZertoScripts\VPGOperations.txt" } if ($Operation -eq "FailoverBeforeCommit") {     "$time Failover Live before commit has been performed on VPG: $VPG" >> "C:\ZertoScripts\VPGOperations.txt" } if ($Operation -eq "MoveBeforeCommit"){     "$time Move before commit has been performed on VPG: $VPG" >> "C:\ZertoScripts\VPGOperations.txt" } This script references two of the built-in environment variables that Zerto provides. By passing these parameters at the time of the execution, in this example, the script will output %ZertoOperation% as the current operation that runs on the VPG, and %ZertoVPGName% as the current VPG name that the script is running on. Note: Testing this via a command line will not work because the environment variables are set at execution time. The following print is a sample output (VPGOperations.txt) from the example script:  01/03/2016 16:08:02 VPG: TestVPG has been tested successfully! (A failover test was successfully performed on a VPG named TestVPG) Bat vs. Ps1 You can run both Windows batch (.bat) and Windows PowerShell (.ps1) scripts as pre/post recovery scripts with Zerto. However, there are differences to consider between the two. Besides the obvious syntax differences between the two, Windows Batch (.bat) is an executable file that does not require anything in order to run. Windows PowerShell scripts, on the other hand, requires Windows PowerShell (.exe) to execute. This difference affects how you set up the scripts in Zerto Virtual Replication. To illustrate the difference, refer to the following screenshots. Windows Batch script example: Command To Run: C:\ZertoScripts\PostScript.bat Params: %ZertoOperation% %ZertoVPGName% Note: Since batch files are executable files, as opposed to .ps1 scripts, they are included as the command to run, and not in the parameters section. Windows PowerShell script example: Command To Run: powershell.exe Params: “C:\ZertoScripts\PostScript.ps1” %ZertoOperation% %ZertoVPGName% NOTE: PowerShell scripts require Windows PowerShell to run, therefore you must include powershell.exe as a command to run, and the path to the script, along with the environment variables, as parameters. Troubleshooting: Consider the following if a pre/post recovery script fails to execute: Syntax - This is the most obvious and somewhat overlooked option in troubleshooting pre/post recovery scripts. To eliminate this as a fault point, Zerto recommends running pre/post recovery scripts that do not include Zerto variables from the command line, with your parameters, to make sure it runs successfully. Execution Policy - When writing a PowerShell script, similar to the one in the example, you will need to set the execution policy to remote signed. To achieve this, open PowerShell as an administrator and type the following command: Set-ExecutionPolicy RemoteSigned. PowerShell will then prompt you to authorize this step. Path - When running pre/post recovery scripts, the path to the script that is provided in the “Params” field, must match the configuration chosen. That is, pre-recovery script: Script must reside somewhere at that VPG’s protected ZVM while post-recovery script: Script must reside somewhere at that VPG’s DR ZVM. Zerto best practice: ALL recovery scripts are available both at protected and DR ZVM machines. If all troubleshooting steps are exhausted, please contact Zerto support for further assistance.