XenServer create snapshots for all machines
Friday, August 7th, 2009XenServer is a wonderful tool. One of the better parts of it is its powerful scripting language, powered by the ‘xe’ command.
In order to capture a mass of snapshots, you can either do it manually from the GUI, or scripted. The script supplied below will include shell functions to capture Quiesce snapshots, and it that fails, normal snapshots of every running VM on the system.
Reason: NetApp SnapMirror, or other backup (maybe for later export) scheduled actions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | #!/bin/bash # This script will supply functions for snapshotting and snapshot destroy including disks # Written by Ez-Aton # Visit my web blog for more stuff, at https://run.tournament.org.il # Global variables: UUID_LIST_FILE=/tmp/SNAP_UUIDS.txt # Function function assign_all_uuids () { # Construct artificial non-indexed list with name (removing annoying characters) and UUID LIST="" for UUID in `xe vm-list power-state=running is-control-domain=false | grep uuid | awk '{print $NF}'` do NAME=`xe vm-param-get param-name=name-label uuid=$UUID | tr ' ' _ | tr -d '(' | tr -d ')'` LIST="$LIST $NAME:$UUID" done echo $LIST } function take_snap_quiesce () { # We attempt to take a snapshot with quench # Arguments: $1 name ; $2 uuid # We attempt to snapshot the machine and set the value of snap_uuid to the snapshot uuid, if successful. # Return 1 if failed if SNAP_UUID=`xe vm-snapshot-with-quiesce vm=$2 new-name-label=${1}_snapshot` then # echo "Snapshot-with-quiesce for $1 successful" return 0 else echo "Snapshot-with-quiesce for $1 failed" return 1 fi } function take_snap () { # We attempt to take a snapshot # Arguments: $1 name ; $2 uuid # We attempt to snapshot the machine and set the value of snap_uuid to the snapshot uuid, if successful. # Return 1 if failed if SNAP_UUID=`xe vm-snapshot vm=$2 new-name-label=${1}_snapshot` then #echo "Snapshot for $1 successful" echo $SNAP_UUID return 0 else echo "Snapshot-with-quiesce for $1 failed" return 1 fi } function stop_ha_template () { # Templates inherit their settings from the origin # We need to turn off HA # $1 : Template UUID if [ -z "$1" ] then echo "Missing template UUID" return 1 fi xe template-param-set ha-always-run=false uuid=$1 } function get_vdi () { # This function will get a space delimited list of VDI UUIDs of a given snapshot/template UUID # Arguments: $1 template UUID # It will also verify that each VBD is an actual snapshot if [ -z "$1" ] then echo "No arguments? We need the template UUID" return 1 fi VDIS="" for VBD in `xe vbd-list vm-uuid=$1 | grep ^uuid | awk '{print $NF}'` do echo "VBD: $VBD" if [ ! `xe vbd-param-get param-name=type uuid=$VBD` = "CD" ] then CUR_VDI=`xe vdi-list vbd-uuids=$VBD | grep ^uuid | awk '{print $NF}'` if `xe vdi-param-get uuid=$CUR_VDI param-name=is-a-snapshot` then VDIS="$VDIS $CUR_VDI" else echo "VDI is not a snapshot!" return 1 fi CUR_VDI="" fi done echo $VDIS } function remove_vdi () { # This function will get a list of VDIs and remove them # Carefull! for VDI in $@ do if xe vdi-destroy uuid=$VDI then echo "Success in removing VDI $VDI" else echo "Failure in removing VDI $VDI" return 1 fi done } function remove_template () { # This funciton will remove a template # $1 template UUID if [ -z "$1" ] then echo "Required UUID" return 1 fi xe template-param-set is-a-template=false uuid=$1 if ! xe vm-uninstall force=true uuid=$1 then echo "Failure to remove VM/Template" return 1 fi } function remove_all_template () { # This function will completely remove a template # The steps are as follow: # $1 is the UUID of the template # Calculate its VDIs # Remove the template # Remove the VDIs if [ -z "$1" ] then echo "No Template UUID was supplied" return 1 fi # We now collect the value of $VDIS get_vdi $1 if [ "$?" -ne "0" ] then echo "Failed to get VDIs for Template $1" return 1 fi if ! remove_template $1 then echo "Failure to remove template $1" return 1 fi if ! remove_vdi $VDIS then return 1 fi } function create_all_snapshots () { # In this function we will run all over $LIST and create snapshots of each machine, keeping the UUID of it inside a file # [email protected] - list of machines in the $LIST format if [ -f $UUID_LIST_FILE ] then mv $UUID_LIST_FILE $UUID_LIST_FILE.$ fi for i in $@ do SNAP_UUID=`take_snap_quiesce ${i%%:*} ${i##*:}` if [ "$?" -ne "0" ] then echo "Problem taking snapshot with quiesce for ${i%%:*}" echo "Attempting normal snapshot" SNAP_UUID=`take_snap ${i%%:*} ${i##*:}` if [ "$?" -ne "0" ] then echo "Problem taking snapshot for ${i%%:*}" SNAP_UUID="" fi fi stop_ha_template $SNAP_UUID echo $SNAP_UUID >> $UUID_LIST_FILE done } |
Possible use will be like this:
. /usr/local/bin/xen_functions.sh
create_all_snapshots `assign_all_uuids` &> /tmp/snap_create.log