LOGO

Restore Citrix-Xen VMs for Free with Xen-Phoenix (Bash)

May 15, 2013
Restore Citrix-Xen VMs for Free with Xen-Phoenix (Bash)

Streamlining VM Restoration with Xen-Phoenix

Do you require a method for rapidly restoring multiple Citrix-Xen virtual machines, perhaps for disaster recovery planning or simply to validate backup integrity? This article details the utilization of Xen-Phoenix, a freely available bash script, to facilitate VM restoration processes.

Understanding the Need for Automated Restoration

As previously discussed in "How To Backup Citrix Xen VMs for Free with Xen-pocalypse," a significant advantage of Citrix Xen is the availability of numerous features without licensing costs.

However, automated VM protection and recovery capabilities necessitate the purchase of the "Advance" license.

Xen-Phoenix: A Cost-Effective Alternative

While we acknowledge the importance of a robust, fully-featured backup solution, Xen-Phoenix presents a viable option for organizations operating under budgetary constraints.

If VM images have already been extracted from the hypervisor, and a need exists to automate "test restore" or "DR refresh" procedures, Xen-Phoenix can serve as a practical solution prior to committing to a more expensive licensing model.

Xen-Phoenix provides a means to efficiently manage and execute VM restoration tasks without incurring additional expenses.

This script offers a reasonable approach to maintaining data recovery preparedness in resource-limited environments.

Automated VM Restoration with XE and Cron

This document details a method for automating the restoration of virtual machines (VMs). While restoring via right-click actions within XenCenter is functional, a scripted approach offers the benefits of automation and scheduled execution.

The solution leverages the XE command-line interface (CLI), which provides programmatic access to the functionalities available through XenCenter’s graphical user interface.

Core Restoration Process

The script, designed for use with Cron for scheduling, follows a straightforward restore flow:

  • All previously existing VMs on the server are deleted.
  • VMs are imported from backup files located in a designated directory.
  • Operational status is verified by activating each VM individually and monitoring for guest tools heartbeats.
  • VMs are powered off upon heartbeat detection or after a predefined timeout period.

The complete removal of existing VMs is configurable and can be disabled, with options to exclude specific VMs from deletion.

Let's proceed with the implementation.

Script Functionality Breakdown

The script's primary function is to streamline the VM restoration process. It achieves this by automating several key steps that would otherwise require manual intervention through the XenCenter interface.

Using the XE CLI, the script replicates the actions of right-clicking within XenCenter, but in a repeatable and scheduled manner.

Deletion of Existing VMs

Initially, the script addresses existing VMs on the server. This step ensures a clean slate before importing backups.

However, this behavior is not mandatory and can be bypassed. Furthermore, exceptions can be defined to prevent the deletion of specific VMs, allowing for a more controlled restoration process.

Importing VMs from Backup

Following the optional deletion phase, the script proceeds to import VMs from backup files. These files are assumed to reside in a pre-defined backup location.

The XE CLI is utilized to initiate the import process, effectively recreating the VMs from their stored images.

Verification and Power Management

After importing, the script verifies the operability of each restored VM. This is accomplished by activating the VM and monitoring for a heartbeat signal from the guest tools.

A heartbeat indicates that the VM is functioning correctly. Once detected, the script powers off the VM. If a heartbeat is not received within a specified timeout, the VM is also powered off, indicating a potential issue.

Scheduling with Cron

To enable automated, scheduled restorations, the script is integrated with Cron, a time-based job scheduler commonly found in Unix-like operating systems.

Cron allows you to define specific times and intervals at which the script will be executed, ensuring that VM restorations occur automatically without manual intervention.

Acquiring the Xen-phoenix Script

The Xen-phoenix script is available for download from GitHub, utilizing standard git procedures. However, for users unfamiliar with git, a zip file can be accessed through this link. It’s important to extract the script directly onto one of your Xen servers to maintain the necessary execution permissions.

Installation via Command Line

The following commands demonstrate the download and extraction process:

  • wget https://github.com/aviadra/Xen-phoenix/archive/master.zip
  • unzip master

Although the above method is functional, employing the git method is recommended to ensure you receive future updates and improvements to the script.

Utilizing git allows for streamlined updates, keeping your Xen-phoenix installation current with the latest features and security enhancements. This is particularly beneficial for maintaining a stable and secure Xen server environment.

Locating VM Export Destinations

The initial step involves configuring the source location for Virtual Machine (VM) exports.

Exploration within Citrix Xen revealed that Storage Repositories (SRs) are accessible via the path "/var/run/sr-mount/%UUID%". Here, UUID represents the unique identifier for each SR, obtainable through the graphical user interface.

Utilizing the GUI for Mounting

This discovery allows us to leverage the standard "Next -> Next -> Finish" wizard to establish a mount point to the desired backup destination. Consequently, the script can utilize this defined path, avoiding complex command-line mounting procedures which fall outside the scope of this guide.

To initiate the creation of a new mount, right-click the server name within the XenCenter interface and select the "New SR" option.

how-to-restore-citrix-xen-vms-for-free-with-xen-phoenix-bash-1.jpg

Configuring a Windows File Share

For this example, we will configure access to a Windows share. Select "Windows File Sharing (CIFS)" as the storage repository type.

how-to-restore-citrix-xen-vms-for-free-with-xen-phoenix-bash-2.jpg

Proceed through the wizard by selecting "Next -> Next -> Finish" to complete the SR creation.

Retrieving the SR UUID

The SR's UUID can be found by selecting the SR name in XenCenter and navigating to the "General" tab.

how-to-restore-citrix-xen-vms-for-free-with-xen-phoenix-bash-3.jpg

To copy the UUID to your clipboard, simply right-click on the displayed identifier and choose the "copy" option.

how-to-restore-citrix-xen-vms-for-free-with-xen-phoenix-bash-4.jpg

Having obtained this crucial information, you are now prepared to modify the configuration file.

Configuring the Settings File

The Xen-phoenix project includes a pre-configured "settings" file template. This template requires modification to align with your specific environment and is subsequently provided as the initial argument to the script.

The settings file defines several crucial parameters, including:

  • Source Export Location: If you’ve adhered to the preceding instructions, simply substitute the placeholder "%UUID%" with the SR identifier obtained earlier.
  • SendEmail Path: Should you choose to activate email notifications, specify the directory where the Perl executable has been extracted.
  • Verification Process: The verifier component manages post-restore validation. It is activated by default, recognizing that a successful file-level restoration doesn't guarantee a fully operational Virtual Machine.
  • Server Preparation: This setting governs the pre-restore removal of all VMs residing on the Disaster Recovery (DR) server. Enabled by default, it assumes the DR server is dedicated to this function. Configure exclusions for any VMs you wish to preserve. Disable this feature entirely if it doesn’t suit your requirements.
  • Email Configuration: If email functionality is enabled, provide essential details like recipient address (To), sender address (From), and server information (Servername/IP, etc.).
  • Debugging Mode: Debugging is initially disabled, indicated by a value of "0". Activation is generally unnecessary, but further details are available in the troubleshooting section should it become required.

Proper configuration of these settings is essential for the successful operation of the Xen-phoenix project.

Operational Procedure

The script initiates its process by accepting the settings file as its primary input, followed by any subsequent arguments designated as "Chevrons" for search purposes, which are delimited by spaces.

These Chevrons are defined as strings capable of utilizing regular expressions, representing at least a portion of the target file name associated with the VM export.

Basic Invocation

In its most straightforward application, a call to Xen-phoenix would resemble the following:

./Xen-phoenix.sh settings.cfg DevTools

This example assumes execution from the directory containing both the script and the settings file, with "DevTools" serving as the Chevron the script will utilize during its search.

Consequently, all files within the exports directory possessing "DevTools" within their filename will be imported.

Advanced Usage

A more intricate example of script execution is demonstrated below:

./Xen-phoenix.sh settings.cfg [Aa][Dd] [Bb][iI] [Dd]ev[Tt]ools

Here, the script is configured to import files containing the term "devtools," irrespective of capitalization for the letters "D" and "T."

Furthermore, it will also import files containing "AD" and "BI," again, regardless of whether these characters are uppercase or lowercase.

Automated Scheduling

As previously mentioned, the Cron utility will be employed for task scheduling. Prior to configuration, it is strongly advised to configure the pre-installed SSMTP package on your Xen server.

Although this step is not mandatory, implementing it will provide a "backwash" collection mechanism. This "backwash collector" can potentially notify you of issues the script itself may not detect.

Access Cron's editing interface by executing the following command:

crontab -e

Assuming you’ve adhered to the preceding instructions and wish to schedule a backup to run every Sunday at 01:31 (1:31 AM), the following entry should be added:

31 01 * * sun /root/Xen-phoenix-master/Xen-phoenix.sh /root/Xen-phoenix-master/settings.cfg Ad DB Bi Dttools

This configuration is accurate provided both the script and the settings file reside within the "/root/Xen-phoenix-master/" directory.

The command specifies the minute (31), hour (01), day of the month (* - any), month (* - any), and day of the week (sun - Sunday) for execution.

Understanding the Cron Entry

The Cron entry is structured as follows: minute hour day_of_month month day_of_week command.

Each field is separated by a space, and wildcards (*) can be used to represent any value. The command itself is the full path to the script, followed by any necessary arguments.

  • Minute: 0-59
  • Hour: 0-23
  • Day of Month: 1-31
  • Month: 1-12
  • Day of Week: 0-7 (0 and 7 both represent Sunday)

Carefully review the syntax to ensure the backup is scheduled at the desired time and frequency.

Acquiring SendEmail (Optional)

Our previous discussions have covered the SendEmail perl program, so a repetition isn't necessary. It functions identically on both Linux and Windows operating systems.

Although enabling email functionality is not mandatory, it is strongly advised. This allows the script to perform crucial tasks, including:

  • Providing notifications regarding the script's start and completion times.
  • Immediately alerting you to any errors identified and resolved during operation.

The program can be downloaded to the Xen server and then unpacked.

wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gztar xvzhf sendEmail-v1.56.tar.gz

Remember the directory where the files were extracted. This path will be required when configuring the settings file.

Utilizing the "Delete Exception" Tag (Optional)

Xen-Phoenix, building upon functionality found in its predecessor, Xen-Pocalypse, introduces a feature allowing for the selective exclusion of virtual machines from system-wide deletion processes. This is achieved through the implementation of a control TAG.

To implement this functionality, a new "Custom Field" must be defined. This process is initiated through the properties of either the server itself or individual virtual machines.

how-to-restore-citrix-xen-vms-for-free-with-xen-phoenix-bash-5.jpg

If a "custom field" has not previously been defined, as illustrated in the example, the "Phoenix_keeper" field will not be present for data input. Therefore, its creation is necessary.

To begin creation, select "Edit Custom Fields" within the displayed dialog box, and then click the "Add..." button.

how-to-restore-citrix-xen-vms-for-free-with-xen-phoenix-bash-6.jpg

A new field of type "Text" should be created, and it must be named precisely "Phoenix_keeper".

Important Note: The script utilizes a "hard coded" reference to this custom field name. Deviation from the specified spelling is not permitted unless corresponding modifications are made to the relevant code.

Upon successful creation of the field, the interface should reflect its availability:

how-to-restore-citrix-xen-vms-for-free-with-xen-phoenix-bash-7.jpg

Close the configuration window. The "Phoenix_keeper" field will now be available for completion, as shown below.

how-to-restore-citrix-xen-vms-for-free-with-xen-phoenix-bash-8.jpg

To activate the exception, simply populate this field with any arbitrary remark. This action will instruct Xen-Phoenix to bypass the virtual machine during deletion operations.

Addressing Potential Issues

Despite considerable effort to ensure ease of use and reliability, unforeseen circumstances can occur. The guidance provided below should assist in identifying the origin of any difficulties you may encounter.

Record Keeping

All system logging is managed by the Xen host utilizing the syslog facility. This data can be accessed and reviewed using the following command:

less +F /var/log/messages

When examining the logs, focus your attention on entries containing the keyword "Xen-Phoenix".

It's important to be aware that Citrix maintains a two-day data retention policy for server syslog information. Consider this when conducting post-incident analysis.

Diagnostic Procedures

As highlighted within the configuration file, a setting exists to activate debugging mode. When debugging is enabled, the script generates detailed logging output to the console. Furthermore, it prevents the sending of email notifications and the execution of actual imports, unless specific flags are also activated.

The available flags, detailed in the settings file template, allow for precise control over the debugging process.

Ideally, you won't require debugging and will successfully benefit from the script's functionality.

Consider the prevalence of hardship, animosity, and deceit,
And envision the obliteration of everything you abhor.
Gradually, from the remnants, the phoenix will be reborn...

#Citrix#Xen#VM#virtual machine#restore#recovery