XenServer 5.0 & 5.5 Uber Backup Script
Description
I wanted to upload my script to share and get some ideas from the community. I chose the good ole fashion BASH for the language. The script does a lot of stuff for you and includes 4 major functions: install, buildlist, backup and restore. The script takes one argument of the listed previously. I have also implemented a retention policy which is useful when you have only so much space on one of those USB drives or dont want to fill your file server with more GB than it can handle.
Install does what you think it does. Installs the program to the location of /root/vm-backup by default, it does prompt you for an alternate location which at the moment might not work so just hit enter and use the default. Install also creates the log file in /var/log and sets the logrotate settings for you.
Buildlist builds a backup list of vm uuids and strips out the dom0 uuid for you ![]()
Backup does the magic and in a loop, snaps the VM, exports it to the mount location you specify in the variable section and then deletes the snapshot which then moves onto the next. If the xenserver is not 5.5 it will halt the machine and export it. So this wont be XenServer 6.0 safe till it comes out
.
Restore is a little function that finds all exported .xva files in the mounted backup location and lists them out for you to pick which to restore. Input the location to the xva file you want to restore and it will be imported into xenserver. This will NOT power it on for you just in case another version of that vm is running or you want to change networks etc.
Cut and past this into a file on xenserver called vmbackup, then chmod +x vmbackup. Run sh vmbackup install. You will have to create your own crontab entry for scheduling. You may want to change the variables at the top of the script for your backup device and backup mount point. Sorry no email alerting at the moment.
Download
http://virtually-engineered.blogspot.com/2009/10/damn-good-xenserver-backup-script.html
Code Snippet
Full code is in the download section. This is just the basics here. The future version will be in PowerShell for you windows users and not require any command line editing which will run on your dedicated management server. Here is a snippet of the restore function.
restore function:
echo "RESTORE: Please select an image to restore:"
find $BACKUP_DIR -name *.xva
echo ""
echo -n "Image:"
read IMAGE
echo -n $IMAGE "was selected for restore, continue? [Y/N]:"
read YN
case "$YN" in
Y)
echo $IMAGE "Image is being restored, restoring..."
xe vm-import filename=$IMAGE
;;
y)
echo $IMAGE "Image is being restored, restoring..."
xe vm-import filename=$IMAGE
;;
N)
echo $IMAGE "is not being restored, exiting."
exit 1
;;
n)
echo $IMAGE "is not bein restored, exiting."
exit 1
;;
*)
echo "Uh ah, you didnt say the magic word."
exit 1
Backup function:
echo "INFO: SnapShotting Virtual machine " $VMNAME >> $LOGFILE
SNAPSHOT_UUID="`xe vm-snapshot vm=$VMNAME new-name-label="$VMNAME"_snap`"
xe template-param-set is-a-template=false ha-always-run=false
uuid=$SNAPSHOT_UUID
echo "INFO: Exporting Virtual Machine:" $VMNAME >> $LOGFILE
xe vm-export vm="$VMNAME"_snap filename=$BACKUP_DIR/$VMNAME/$BKFILENAME
echo "INFO: Export Complete, removing backup snapshot"
xe vm-uninstall uuid=$SNAPSHOT_UUID force=true
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.
Comments (1)
Jun 01, 2010
Brad Boening says:
Do you have the powershell verion working?Do you have the powershell verion working?