November 27, 2009 in citrix, virtualization, xen by admin

Page edited by Peter Barganski

Description

This script allows you to take an automated offline backups of all your XenServer 5.0 Virtual Machines, restarting them after backup.

Use this script if you haven't upgraded yet (and before you upgrade to) XenServer 5.5 can't utilize hardware supported snapshots for backup because you don't have NetApp Filer or Equalogic SAN's.

This script is based on jeremy tirrell's "0 downtime snapshot based XENServer 5.5 backup script"

Download

http://community.citrix.com/download/attachments/102236496/vmbackup.pl

Change Log/Enhancements

* Replaced snapshot creation with vm-shutdown and vm-export for offline backups
* Added power-state checks
* Added automatic vm restart upon backup completion
* Added Status Bar Progress using CPAN Smart::Comments and some debug information
* Added POD documentation

Code Snippet

print("Shuting down: ".$VMName ."\n");					#Shuting down the VM
  $vm_shutdown_cmd = "xe vm-shutdown power-state=running uuid=$guest"; ### [<now>] Shutting down running VM...
  $status = `$vm_shutdown_cmd`;

	if ($?) {													#checking if shutdown was successfull
		print("Error shuting down " .$VMName." using command [$cmd]\n");
	} else {

		print($VMName.": was shutdown\n");
		$Message .= $VMName." was shutdown\n";
	}

  $fdate = `date +%d%m%y`;									#get the current date in a format we can write
  print("Exporting: ".$VMName."\n");						#export the VM
  $Message .= "Exporting: ".$VMName."\n";
  $exportstring = $backupdir.$VMName.".xva-".$fdate;
  $status= `xe vm-export vm=$guest filename=$exportstring`; ### [<now>] Performing VM export...
  print($status."\n");
  $Message .= $status."\n";

  $restartStatus= `xe vm-start power-state=halted vm=$guest`; ### [<now>] Restarting VM...
  print($restartStatus."\n");
  $restartMessage .= $restartStatus."\n";

POD Documentation (run perldoc vmbackup.pl)

__END__
# Perldoc documentation
=pod
=head1 NAME
Xen Virtual Machine Backup Script

=head1 SYNOPSIS

At the DOM0 console type I<perl -w backup-offline.pl>.

=head1 DESCRIPTION

This script will create a list of all available virtual machines running on the XenHost it is run except DOM0. It will then attempt to firstly, shutdown each vm in turn, export the vm to a .xva disk file.

After attempting the export the script will restart each vm it had previously shutdown.

Disclaimer

These software applications are provided to you as is with no representations, warranties or conditions of any kind. You may use and distribute it at your own risk. CITRIX DISCLAIMS ALL WARRANTIES WHATSOEVER, EXPRESS, IMPLIED, WRITTEN, ORAL OR STATUTORY, INCLUDING WITHOUT LIMITATION WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NONINFRINGEMENT. Without limiting the generality of the foregoing, you acknowledge and agree that (a) the software application may exhibit errors, design flaws or other problems, possibly resulting in loss of data or damage to property; (b) it may not be possible to make the software application fully functional; and (c) Citrix may, without notice or liability to you, cease to make available the current version and/or any future versions of the software application. In no event should the code be used to support of ultra-hazardous activities, including but not limited to life support or blasting activities. NEITHER CITRIX NOR ITS AFFILIATES OR AGENTS WILL BE LIABLE, UNDER BREACH OF CONTRACT OR ANY OTHER THEORY OF LIABILITY, FOR ANY DAMAGES WHATSOEVER ARISING FROM USE OF THE SOFTWARE APPLICATION, INCLUDING WITHOUT LIMITATION DIRECT, SPECIAL, INCIDENTAL, PUNITIVE, CONSEQUENTIAL OR OTHER DAMAGES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. You agree to indemnify and defend Citrix against any and all claims arising from your use, modification or distribution of the code.

Changes between revision 9 and revision 10:

 h2. Description
  
 This script allows you to take an automated offline backups of *{_}{+}all{+}{_}* your XenServer 5.0 Virtual Machines, restarting them after backup.
  
 Use this script if you haven't upgraded yet (and *{+}before{+}* you upgrade to) XenServer 5.5 can't utilize hardware supported snapshots for backup because you don't have NetApp Filer or Equalogic SAN's.
  
 This script is based on [jeremy tirrell's|http://community.citrix.com/display/~jtirrell] "[0 downtime snapshot based XENServer 5.5 backup script|http://community.citrix.com/display/xs/0+downtime+snapshot+based+XENServer+5.5+backup+script.?showComments=true&showCommentArea=true#addcomment]"
 \\
  
 h2. Download
  
 [http://community.citrix.com/download/attachments/102236496/vmbackup.pl]
  
 h2. Change Log/Enhancements
  
 \* Replaced snapshot creation with vm-shutdown and vm-export for offline backups
 \* Added power-state checks
 \* Added automatic vm restart upon backup completion
 \* Added Status Bar Progress using CPAN Smart::Comments and some debug information
 \* Added POD documentation
  
 h2. Code Snippet
  
 {code}
 print("Shuting down: ".$VMName ."\n"); #Shuting down the VM
  $vm_shutdown_cmd = "xe vm-shutdown power-state=running uuid=$guest"; ### [<now>] Shutting down running VM...
  $status = `$vm_shutdown_cmd`;
  
  if ($?) { #checking if shutdown was successfull
  print("Error shuting down " .$VMName." using command [$cmd]\n");
  } else {
  
  print($VMName.": was shutdown\n");
  $Message .= $VMName." was shutdown\n";
  }
  
  $fdate = `date +%d%m%y`; #get the current date in a format we can write
  print("Exporting: ".$VMName."\n"); #export the VM
  $Message .= "Exporting: ".$VMName."\n";
  $exportstring = $backupdir.$VMName.".xva-".$fdate;
  $status= `xe vm-export vm=$guest filename=$exportstring`; ### [<now>] Performing VM export...
  print($status."\n");
  $Message .= $status."\n";
  
  $restartStatus= `xe vm-start power-state=halted vm=$guest`; ### [<now>] Restarting VM...
  print($restartStatus."\n");
  $restartMessage .= $restartStatus."\n";
 {code}
 POD Documentation (run perldoc vmbackup.pl)
 {code}
 __END__
 # Perldoc documentation
 =pod
 =head1 NAME
 Xen Virtual Machine Backup Script
  
 =head1 SYNOPSIS
  
 At the DOM0 console type I<perl -w backup-offline.pl>.
  
 =head1 DESCRIPTION
  
 This script will create a list of all available virtual machines running on the XenHost it is run except DOM0. It will then attempt to firstly, shutdown each vm in turn, export the vm to a .xva disk file.
  
 After attempting the export the script will restart each vm it had previously shutdown.
 {code}
  
 h2. Disclaimer
  
 These software applications are provided to you as is with no representations, warranties or conditions of any kind. You may use and distribute it at your own risk. CITRIX DISCLAIMS ALL WARRANTIES WHATSOEVER, EXPRESS, IMPLIED, WRITTEN, ORAL OR STATUTORY, INCLUDING WITHOUT LIMITATION WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NONINFRINGEMENT. Without limiting the generality of the foregoing, you acknowledge and agree that (a) the software application may exhibit errors, design flaws or other problems, possibly resulting in loss of data or damage to property; (b) it may not be possible to make the software application fully functional; and (c) Citrix may, without notice or liability to you, cease to make available the current version and/or any future versions of the software application. In no event should the code be used to support of ultra-hazardous activities, including but not limited to life support or blasting activities. NEITHER CITRIX NOR ITS AFFILIATES OR AGENTS WILL BE LIABLE, UNDER BREACH OF CONTRACT OR ANY OTHER THEORY OF LIABILITY, FOR ANY DAMAGES WHATSOEVER ARISING FROM USE OF THE SOFTWARE APPLICATION, INCLUDING WITHOUT LIMITATION DIRECT, SPECIAL, INCIDENTAL, PUNITIVE, CONSEQUENTIAL OR OTHER DAMAGES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. You agree to indemnify and defend Citrix against any and all claims arising from your use, modification or distribution of the code.