KVM删除命令,管理快照命令

作者: ropon 分类: Kvm 发布时间: 2018-12-07 15:16

自动化清理KVM

#!/bin/bash

Echo()
{
	case $1 in
	    success)  flag="\033[1;32m"
	    ;;
	    failure)  flag="\033[1;31m"
	    ;;
	    warning)  flag="\033[1;33m"
	    ;;
	    msg)  flag="\033[1;34m"
	    ;;
	    *)  flag="\033[1;34m"
	    ;;
	esac
	if [[ $LANG =~ [Uu][Tt][Ff] ]]
	then
		echo -e "${flag}${2}\033[0m"
	else
		echo -e "${flag}${2}\033[0m" | iconv -f utf-8 -t gbk
	fi
}

Main()
{
	read -p "please input ebs_name:" ebs_name
	virsh destroy $ebs_name
	virsh undefine $ebs_name
	rm -rf  /vmdata/${ebs_name}/
	Echo "success" "$ebs_name删除成功"
}
Main

快照创建|恢复|删除

#!/bin/bash

Echo()
{
	case $1 in
	    success)  flag="\033[1;32m"
	    ;;
	    failure)  flag="\033[1;31m"
	    ;;
	    warning)  flag="\033[1;33m"
	    ;;
	    msg)  flag="\033[1;34m"
	    ;;
	    *)  flag="\033[1;34m"
	    ;;
	esac
	if [[ $LANG =~ [Uu][Tt][Ff] ]]
	then
		echo -e "${flag}${2}\033[0m"
	else
		echo -e "${flag}${2}\033[0m" | iconv -f utf-8 -t gbk
	fi
}

if [ ! $1 ];then
	Echo "msg" "缺少必要参数"
	Echo "msg" "创建快照请使用Create,如sh $(basename $0) Create"
	Echo "msg" "恢复快照请使用Restore,如sh $(basename $0) Restore"
	Echo "msg" "删除快照请使用Delete,如sh $(basename $0) Delete"
	exit
else
	flag=$1
fi

#创建快照
Create_snapshot()
{	
	virsh list --all
	read -p "please input ebs_name:" ebs_name
	read -p "please input snapsho_name:" snapsho_name
	virsh snapshot-create-as $ebs_name $snapsho_name
	virsh snapshot-current $ebs_name|grep name
	virsh snapshot-list $ebs_name
	qemu-img info /vmdata/${ebs_name}/${ebs_name}_os.qcow2
	qemu-img info /vmdata/${ebs_name}/${ebs_name}_data.qcow2
}

#恢复快照
Restore_snapshot()
{	
	virsh list --all
	read -p "please input ebs_name:" ebs_name
	virsh snapshot-list $ebs_name
	read -p "please input snapsho_name:" snapsho_name
	virsh snapshot-revert $ebs_name $snapsho_name
	virsh snapshot-current $ebs_name|grep name
}

#删除快照
Delete_snapshot()
{
	virsh list --all
	read -p "please input ebs_name:" ebs_name
	virsh snapshot-list $ebs_name
	read -p "please input snapsho_name:" snapsho_name
	virsh snapshot-delete $ebs_name $snapsho_name
	virsh snapshot-list $ebs_name

}
Main()
{
	[ "$flag" == "Create" ] && Create_snapshot
	[ "$flag" == "Restore" ] && Restore_snapshot
	[ "$flag" == "Delete" ] && Delete_snapshot
}
Main

 

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!