参数1:虚拟机名称
参数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}
# 执行快照
vim-cmd vmsvc/snapshot.revert ${vm_id} ${snapshot_id} suppressPowerOn
# 打开电源
vim-cmd vmsvc/power.on ${vm_id}