參數(shù)1:虛擬機名稱
參數(shù)2:快照名稱
shell腳本詳情如下:
#!/bin/bash
basepath=$(cd `dirname $0`; pwd)
cd $basepath
if [ $# != 2 ] ; then
echo "USAGE: $0 vm_name snapshot_name"
exit 1;
fi
vm_name=$1
snapshot_name=$2
echo ${vm_name}
echo ${snapshot_name}
# 獲取虛擬機ID
vm_id=`vim-cmd vmsvc/getallvms | grep ${vm_name} | awk '{print $1}'`
if [ ! $vm_id]; then
echo "vm_id獲取失敗"
exit 1
fi
echo ${vm_id}
# 獲取快照ID
snapshot_id=`vim-cmd vmsvc/snapshot.get ${vm_id}? | grep -A 1 ${snapshot_name} | grep Id | awk '{print $4}'`
if [ ! $snapshot_id]; then
echo "snapshot_id獲取失敗"
exit 1
fi
echo ${snapshot_id}
# 執(zhí)行快照
vim-cmd vmsvc/snapshot.revert ${vm_id} ${snapshot_id} suppressPowerOn
# 打開電源
vim-cmd vmsvc/power.on ${vm_id}