将 Xenserver VM 快照 UUID 存储为 shell 脚本中的变量,以从快照创建 .xva 文件

将 Xenserver VM 快照 UUID 存储为 shell 脚本中的变量,以从快照创建 .xva 文件

我正在看这个问题,它询问了一些类似的帮助,也是我能找到的最接近帮助的东西,但它并不能解决我的问题: 通过控制台将 XenServer 快照导出为文件

我特别需要的是一种方法,可以在脚本中通过 curl 或 sed 之类的工具从快照列表信息中提取 UUID 本身并将其作为变量存储在脚本中(我们在任何时候每台机器都只有一个,但任何可扩展到包含多个 UUID 的答案也是可以接受的)。

简单地说,我只需要将 UUID 本身作为变量存储在此输出或任何具有 UUID 的类似输出的 .sh 脚本中:

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

这主要需要在具有 Xenserver 7.0 或可能更新一点的服务器上进行,我非常感谢这方面的帮助,因为我似乎无法找到正确执行此操作的方法,因为快照的 UUID 会发生变化,以便我可以将变量放在此命令或脚本中的如下命令中:

xe vm-export vm=SNAPSHOT_UUID filename=/mnt/anything

答案1

我在网上进行了更多的搜索,并在 citrix 论坛上的一个讨论帖子中找到了一个脚本,该脚本可以完成我需要做的事情:https://discussions.citrix.com/topic/345960-xenserver-automated-snapshots-script/

以下是脚本本身,我可以验证它是否可以在 XenServer 7.0 上运行,您所要做的就是在配置中指定 VM UUID 和导出路径:

#!/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

相关内容