Virtual-Machines

將 Xenserver VM 快照 UUID 作為變數儲存在 shell 腳本中以從快照創建 .xva 文件

  • March 7, 2019

我正在查看這個問題,該問題正在尋求類似的幫助,這是我能找到的最接近幫助的問題,但它並沒有解決我的問題: export XenServer snapshot as file via console

我特別需要的是一種方法,以便在腳本中通過 curl 或 sed 之類的東西從快照列表資訊中僅提取 UUID 本身並將其作為變數儲存在腳本中(我們只有一個任何時候每台機器,但任何可以擴展到包含多個 UUID 的答案也將是完全可以接受的)。

簡而言之,我只需要將 UUID 本身作為變數儲存在來自此輸出的 .sh 腳本中,或者任何類似具有 UUID 的輸出中:

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

我在網上做了更多的搜尋,並在 citrix 論壇上的討論文章中找到了一個腳本,該腳本的腳本完全符合我的需要:https ://discussions.citrix.com/topic/345960-xenserver-automated-快照腳本/

這是腳本本身,我可以驗證它在 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

引用自:https://serverfault.com/questions/957186