0

I was looking at this question which was asking something sort of similar for help and is the closest thing I could find to helping, but it doesn't solve my issue: export XenServer snapshot as file via console

What I specifically need is a way to make it so that in a script either through something like curl or sed to pull just the UUID itself from the snapshot-list info and store it as a variable in the script (we'll only have one per machine at any time, but any answer that scales to encompass multiple UUIDs will also be more than acceptable).

To put it in simpler terms, I need only the UUID itself to store as a variable in a .sh script from this output or any output like it that has a UUID:

uuid ( RO)                : 30820e58-886e-972e-8435-c5cf83140446
      name-label ( RW): xgdc00-7-20
name-description ( RW):
is-vmss-snapshot ( RO): false

This will mostly be needed on servers with Xenserver 7.0 or possibly a bit newer, I would definitely appreciate help on this as I can't seem to figure out a way to properly do this as the UUID changes for the snapshots so that I can put the variable in this command or a command like this in the script:

xe vm-export vm=SNAPSHOT_UUID filename=/mnt/anything
tommy61157
  • 101
  • 4

1 Answers1

0

I did some more hunting online and I found a script in a discussions post on the citrix forums that has a script that does exactly what I need to do: https://discussions.citrix.com/topic/345960-xenserver-automated-snapshots-script/

Here's the script itself which I can verify works for me on XenServer 7.0, all you have to do is specify the VM UUIDs and export path in the config:

#!/bin/bash


# [usage & Config]
# put this script on Xenserver master and execute with root privilege
# Change VM UUID(s) and the location to export
# Configs:
# VMs: a list of VM uuid, separated by comma
# ExportPath: the mount path to store exported xva file
# 
# [How it work]
# step1: iterate specified VM UUID(s) and create a snapshot for each
# step2: backup the snapshots to specified location
# step3: delete temporary snapshot created in step 1
# 
# [Note]
# please make sure you have enough disk space for ExportPath, or backup will fail
# tested on Xenserver 5.6 sp2 and 6.2
# Xenserver 6.2's performance is at least 4 times better than Xenserver 5.6
# on error, script will print the reason, and proceed to handle next VM 
# backed up file format: vm_label + "backup" + date of snapshot, i.e, 
win71_backup_2013-12-31_17-11-47.xva
#

##### Config #######

VMs="4bce3dc4-a1f1-66c7-48b5-31536be6f123,278fc9f6-f377-fa30-bd54- 
a3b239027456,167e0e3e-a991-bae0-2b04-abad270d0789"
ExportPath="/mnt/test"

####################


vm_array=(${VMs//,/ })
ret_code=0
snapshot_uuid_array=
snapshot_name_array=
backup_ext=".xva"

echo "Starting to backup..."
echo "VM list: ${vm_array[@]}"
echo "ExportPath: ${ExportPath}"

if [[ "$ExportPath" != */ ]]; then
    ExportPath="$ExportPath/"
fi

for i in "${!vm_array[@]}"; do
    # get vm label
    uuid=${vm_array[$i]}
    vm_label_raw=`xe vm-param-get param-name=name-label uuid=$uuid`
    if [ $? -ne 0 ]; then
        echo "failed to get VM label uuid = $uuid"
        ret_code=1
        continue
    fi
    vm_label=`echo "$vm_label_raw" | tr ' ' _ | tr -d '(' | tr -d ')'`

    # prepare snapshot name
    date=$(date +%Y-%m-%d_%H-%M-%S)
snapshot_name=$vm_label"_backup_"$date

# snapshot vm
snapshot_uuid=`xe vm-snapshot uuid=$uuid new-name-label=$snapshot_name`
if [ $? -ne 0 ]; then
    echo "failed to snapshot VM uuid = $uuid"
    ret_code=1
    continue
fi
snapshot_uuid_array[$i]=$snapshot_uuid
snapshot_name_array[$i]=$snapshot_name

# remove is-a-template attribute from snapshot
echo=`xe template-param-set is-a-template=false uuid=$snapshot_uuid`
if [ $? -ne 0 ]; then
    echo "failed to remove template attribute from VM uuid = $uuid"
    ret_code=1
fi
done


# backup each VM to specified path and delete
for i in "${!snapshot_uuid_array[@]}"; do
snapshot_uuid=${snapshot_uuid_array[$i]}
snapshot_name=${snapshot_name_array[$i]}

echo "Start backup $snapshot_name ..."
echo=`xe vm-export uuid=$snapshot_uuid filename="$ExportPath$snapshot_name$backup_ext"`
if [ $? -ne 0 ]; then
    echo "failed to export snapshot name = $snapshot_name$backup_ext"
    ret_code=1
else    
    echo "Successfully backup $snapshot_name to $ExportPath"
fi

echo=`xe vm-uninstall force=true uuid=$snapshot_uuid`
if [ $? -ne 0 ]; then
    echo "failed to remove temporary snapshot name = $snapshot_name"
    ret_code=1 
fi
done

exit $ret_code
tommy61157
  • 101
  • 4