2012年6月25日月曜日

Linux CentOS 6.2 (x86_64) にて、KVM (Kernel-based Virtual Machine) の xml ファイルと仮想imgファイルをバックアップするスクリプト

1.「kvm-bkonevm.sh」スクリプト概要

Linux CentOS 6.2 (x86_64) にて、KVM (Kernel-based Virtual Machine) の xml ファイルフルパスを指定すると、その KVM の xml ファイル自体と仮想imgファイルをバックアップするスクリプトを作成しました。

  • 「指定された仮想マシンの電源を自動的にシャットダウン、仮想マシンの xml ファイルと仮想 HDD ファイルのバックアップ、バックアップ終了後に仮想マシンの電源をオン」という機能をひと通り備えています。

もちろん事前の動作確認や設定は必要ですが、「毎晩、とある重要な仮想マシン1台を、自動的にシャットダウン、バックアップ、電源をオンにする」という利用方法も可能です。

2.前準備

事前に、cpulimit というソフトをインストールしておく必要があります。

(参考) Linux (CentOS 6.2 x86_64) に、「cpulimit」をインストールして、実行する (= 絶対的な優先度を下げる) 手順

http://hachitaro.blogspot.jp/2012/06/linux-centos-62-x8664-cpulimit.html

3.「kvm-bkonevm.sh」スクリプト利用方法

「kvm-bkonevm.sh」スクリプトの利用方法は以下の通りです。

書式

(this scriptfile) (kvm xml file fullpath) (copy destination directory for copying kvm xml file and hd image file)

利用例

./kvm-bkonevm.sh /etc/libvirt/qemu/pc014.xml /mnt/pc079/d/bak/0101_phasedbak

上のとおりに実行すると、以下の処理を実行します。

  1. スクリプトは、「/etc/libvirt/qemu/pc014.xml」で指定されている仮想マシンの電源がオン(ON) の場合は、シャットダウン処理を開始します。

    kvm-bkonevm.sh を実行した後で「仮想マシンの電源がオンになっている」場合は、この kvm-bkonevm.sh スクリプトが仮想マシンに対してシャットダウン命令 (virsh shutdown) を発行します。

    仮想マシンが何らかの理由で自動的にシャットダウンしない場合は、手作業で仮想マシンをシャットダウン操作をする必要があります。

  2. スクリプトは、シャットダウン処理が完全に終了して、仮想マシンの電源がオフ(OFF) になるまで待機します。
  3. スクリプトは、xml ファイルと、仮想 HDD イメージファイルをバックアップ (コピー) します。

    xml ファイル、仮想 HDD イメージファイルいずれとも、できるかぎり元のディレクトリ構成を保ったままバックアップします。

    • xmlファイルのバックアップ先の例

      「/etc/libvirt/qemu/pc014.xml」

    • 仮想 HDD イメージファイルのバックアップ先の例

      「/var/lib/libvirt/images/pc014.img」

  4. スクリプトは、バックアップ終了後に仮想マシンの電源をオンにします。

4.「kvm-bkonevm.sh」スクリプト本体

スクリプトご利用上の注意」をよくお読みの上、ご了承いただいた方はご利用下さい。

「kvm-bkonevm.sh」スクリプトは以下の通りです。

#!/bin/sh

# usage
# (this scriptfile) (kvm xml file fullpath) (copy destination directory for copying kvm xml file and hd image file)
# example
# ./kvm-bkonevm.sh /etc/libvirt/qemu/pc014.xml /mnt/pc079/d/bak/0101_phasedbak


function funcCPULimit() {
curdttm=`date +%Y%m%d%H%M%S`
msg="cpulimit -e qemu-kvm -l 5 &"
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}
cpulimit -e qemu-kvm -l 10 &

curdttm=`date +%Y%m%d%H%M%S`
msg="cpulimit -e rsync -l 10 &"
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}
cpulimit -e rsync -l 20 &

curdttm=`date +%Y%m%d%H%M%S`
msg="cpulimit -e mysqldump -l 5 &"
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}
cpulimit -e qemu-kvm -l 10 &

return 11
}


# パスの文字列に「//」のように不要な「/ (スラッシュ)」が入っている場合は
# 「//」を「/」へ置き換える。
function FuncReplaceDoubleSlashtoSingle() {
wstr=$1
retstr=`echo "${wstr}" | sed -e 's/\/\//\//g'`
echo ${retstr}
}

function FuncRemoveRightSlash() {
wstr=$1

# wstr の最後の一文字を抜き出す。
wstrlenall=`echo $((${#wstr}))`
lastchar=${wstr:${wstrlenall}:${wstrlenall}}

if [[ ${lastchar} = "/" ]]
then
# 一番右端の一文字が「/」なら
# 「/」を削除する。
retstr=${wstr##*/}
else
# 一番右端の一文字が「/」以外なら
# 削除処理は不要
retstr=${wstr}
fi

echo ${retstr}

}

function FuncAddRightSlash() {
wstr=$1

# wstr の最後の一文字を抜き出す。
wstrlenall=`echo $((${#wstr}))`
lastchar=${wstr:${wstrlenall}:${wstrlenall}}

if [[ ${lastchar} = "/" ]]
then
# 一番右端の一文字が「/」なら
# 「/」を追加する必要はない。
retstr=${wstr}
else
# 一番右端の一文字が「/」以外なら
# 「/」を追加する。
retstr=${wstr}'/'
fi

echo ${retstr}

}

function FuncGetStrQuotedWithDifferentStr() {

# getting kvm sv251.xml information
# wallstr=`virsh domxml-to-native qemu-argv /etc/libvirt/qemu/sv251.xml`

# 第1引数は、
# 「virsh domxml-to-native qemu-argv (kvm の xml ファイルフルパス)」
# の値。
# wallstr=$1
# wallstr=`virsh domxml-to-native qemu-argv /etc/libvirt/qemu/sv251.xml`

# echo '${1}..........'$1
wallstr=$1
# echo '${wallstr}..........'${wallstr}

wallstrlen=`echo ${#wallstr}`

# echo 'wallstrlen is '${wallstrlen}

# searchtarget_leftstr='-drive file='
searchtarget_leftstr=$2
searchtarget_leftstrlen=`echo ${#searchtarget_leftstr}`
# searchtarget_rightstr=','
searchtarget_rightstr=$3


tempstr[0]=`echo "${wallstr##*${searchtarget_leftstr}}"`
# echo '${tempstr[0]} is '${tempstr[0]}
tempstrlen[0]=`echo ${#tempstr[0]}`
# echo '${tempstrlen[0]} is '${tempstrlen[0]}

targetstr_startpos=`expr ${wallstrlen} - ${tempstrlen[0]}`
# echo '${targetstr_startpos} is '${targetstr_startpos}
targetstr_startpos_fixed=`expr ${targetstr_startpos} + 1`

tempstr[1]=`echo "${tempstr[0]#*${searchtarget_rightstr}}"`
# echo '${tempstr[1]} is '${tempstr[1]}
tempstrlen[1]=`echo ${#tempstr[1]}`
# echo '${tempstrlen[1} is '${tempstrlen[1]}

targetstr_endpos=`expr ${wallstrlen} - ${tempstrlen[1]}`
# echo '${targetstr_endpos} is '${targetstr_endpos}
targetstr_endpos_fixed=`expr ${targetstr_endpos} - 1`
# echo '${targetstr_endpos_fixed} is '${targetstr_endpos_fixed}


# retstr=`echo ${wallstr} | cut -c ${targetstr_startpos}-${targetstr_endpos_fixed}`
retstr=`echo ${wallstr} | cut -c ${targetstr_startpos_fixed}-${targetstr_endpos_fixed}`

echo ${retstr}


}


function funcGetKVMInfo() {
# getting KVM xml file copy source, destination,
# and getting KVM hd file copy source, destination

# getting KVM HDD information
# wstr=`virsh domxml-to-native qemu-argv /etc/libvirt/qemu/sv251.xml`


wstr=`virsh domxml-to-native qemu-argv ${kvmxml_fullpath_src}`

# KVM の 仮想マシン名を取得する。
# 左側が「'-name '」で、(一番右端に半角スペースを含める)
leftstr='-name '
# 右側が「' -uuid'」で、(一番左端に半角スペースを含める)
# rightstr=' -uuid'
rightstr=' '
# 囲まれた文字列を、${wstr} から探し出して、
# kvm_hd_fullpath_src に代入する。
# 引数 ${wstr} の内容が半角スペースを含むため、"" でくくる。
targetvmname=`FuncGetStrQuotedWithDifferentStr "${wstr}" "${leftstr}" "${rightstr}"`
# echo '${targetvmname} is "'${targetvmname}'"'

curdttm=`date +%Y%m%d%H%M%S`
msg='${targetvmname} is "'${targetvmname}'"'
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}


# KVM の 仮想 HDD のフルパスを取得する。
# 左側が「'-drive file='」で、
leftstr='-drive file='
# 右側が「','」で、
rightstr=','
# 囲まれた文字列を、${wstr} から探し出して、
# kvm_hd_fullpath_src に代入する。
# 引数 ${wstr} の内容が半角スペースを含むため、"" でくくる。
kvm_hd_fullpath_src=`FuncGetStrQuotedWithDifferentStr "${wstr}" "${leftstr}" "${rightstr}"`
# echo '${kvm_hd_fullpath_src} is '${kvm_hd_fullpath_src}
curdttm=`date +%Y%m%d%H%M%S`
msg='${kvm_hd_fullpath_src} is '${kvm_hd_fullpath_src}
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}

# kvm_hd_dir_src=`echo ${kvm_hd_fullpath_src%/*}`
kvm_hd_dir_src=${kvm_hd_fullpath_src%/*}
# echo '${kvm_hd_dir_src} is '${kvm_hd_dir_src}
curdttm=`date +%Y%m%d%H%M%S`
msg='${kvm_hd_dir_src} is '${kvm_hd_dir_src}
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}

# echo '${kvmxml_fullpath_src} is '${kvmxml_fullpath_src}
curdttm=`date +%Y%m%d%H%M%S`
msg='${kvmxml_fullpath_src} is '${kvmxml_fullpath_src}
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}

kvm_xml_dir_src=${kvmxml_fullpath_src%/*}
# echo '${kvm_xml_dir_src} is '${kvm_xml_dir_src}
curdttm=`date +%Y%m%d%H%M%S`
msg='${kvm_xml_dir_src} is '${kvm_xml_dir_src}
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}

# echo '${copybasedir_dest} is '${copybasedir_dest}
curdttm=`date +%Y%m%d%H%M%S`
msg='${copybasedir_dest} is '${copybasedir_dest}
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}

curdttm=`date +%Y%m%d%H%M%S`
curdt=`date +%Y%m%d`

thispchostname=`hostname -s`

kvmxml_copydir_dest=`FuncAddRightSlash ${copybasedir_dest}`
# kvmxml_copydir_dest=${kvmxml_copydir_dest}`FuncAddRightSlash ${curdt}`
kvmxml_copydir_dest=${kvmxml_copydir_dest}`FuncAddRightSlash ${curdttm}`
kvmxml_copydir_dest=${kvmxml_copydir_dest}`FuncAddRightSlash ${thispchostname}`
kvmxml_copydir_dest=${kvmxml_copydir_dest}`FuncRemoveRightSlash ${kvm_xml_dir_src}`
kvmxml_copydir_dest=`FuncReplaceDoubleSlashtoSingle ${kvmxml_copydir_dest}`
curdttm=`date +%Y%m%d%H%M%S`
msg='${kvmxml_copydir_dest} is '${kvmxml_copydir_dest}
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}
kvmhd_copydir_dest=`FuncAddRightSlash ${copybasedir_dest}`
# kvmhd_copydir_dest=${kvmhd_copydir_dest}`FuncAddRightSlash ${curdt}`
kvmhd_copydir_dest=${kvmhd_copydir_dest}`FuncAddRightSlash ${curdttm}`
kvmhd_copydir_dest=${kvmhd_copydir_dest}`FuncAddRightSlash ${thispchostname}`
kvmhd_copydir_dest=${kvmhd_copydir_dest}`FuncRemoveRightSlash ${kvm_hd_dir_src}`
kvmhd_copydir_dest=`FuncReplaceDoubleSlashtoSingle ${kvmhd_copydir_dest}`
# echo '${kvmhd_copydir_dest} is '${kvmhd_copydir_dest}
curdttm=`date +%Y%m%d%H%M%S`
msg='${kvmhd_copydir_dest} is '${kvmhd_copydir_dest}
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}

}

function funcWaitWithCountDown() {
wWaitSecCount=$1
wBaseMsg=$2
curdttm=`date +%Y%m%d%H%M%S`
msg="${wWaitSecCount} ${wBaseMsg}"
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}
until [ ${wWaitSecCount} -eq 0 ]
do
curdttm=`date +%Y%m%d%H%M%S`
msg="${wWaitSecCount}"
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}
wWaitSecCount=`expr ${wWaitSecCount} - 1`
sleep 1
done

}

function funcKVM_VM_PowerOnProcStart() {
# echo "now start vm "${targetvmname}
curdttm=`date +%Y%m%d%H%M%S`
msg="Now PowerOn the vm "${targetvmname}
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}

curdttm=`date +%Y%m%d%H%M%S`
msg=`virsh start ${targetvmname}`
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}

curdttm=`date +%Y%m%d%H%M%S`
msg="The PowerOn Process has finished. Please wait for OS to start."${targetvmname}
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}

}

function funcKVM_VM_ShutdownStart() {
curdttm=`date +%Y%m%d%H%M%S`
msg="starting to shutdown ${targetvmname}..."
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}
# echo ${targetvmname}"shutdown starting..."
# ssh -t -t root@${targetvmname} shutdown -h now
curdttm=`date +%Y%m%d%H%M%S`
# msg=`ssh -t -t root@${targetvmname} shutdown -h now`
msg=`virsh shutdown ${targetvmname}`
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}

curdttm=`date +%Y%m%d%H%M%S`
msg="shutdown process of ${targetvmname} has started..."
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}
}

function funcKVM_VM_ShutdownFinishWait() {
while [ 1 -eq 1 ]
do
curvmdomstat=`virsh domstate ${targetvmname}`
# echo "curent ${targetvmname} status is ${curvmdomstat}"
curdttm=`date +%Y%m%d%H%M%S`
msg="curent ${targetvmname} status is ${curvmdomstat}. Now waiting for shut off..."
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}
sleep 1
if [ `echo ${curvmdomstat} | grep シャットオフ` ]; then
break
fi
if [ `echo ${curvmdomstat} | grep "shut off"` ]; then
break
fi
done

curdttm=`date +%Y%m%d%H%M%S`
msg="curent ${targetvmname} status is ${curvmdomstat}"
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}
curdttm=`date +%Y%m%d%H%M%S`
msg="Now ${targetvmname} is confirmed to be ${curvmdomstat}"
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}

}


function funcKVM_VM_XML_backup() {
curdttm=`date +%Y%m%d%H%M%S`
msg=`mkdir -p -m +w ${kvmxml_copydir_dest} 2>&1`
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}

curdttm=`date +%Y%m%d%H%M%S`
msg=`rsync -avvv --delete --timeout=3600 --bwlimit=8192 ${kvmxml_fullpath_src} ${kvmxml_copydir_dest} 2>&1`
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}
return 11
}

# rsync option
# --bwlimit=8192 --timeout=3600
# is added to avoid the following error
# rsync: connection unexpectedly closed
#
# reference
# http://kawama.jp/archives/2007/12/rsyncssh.html
function funcKVM_VM_HDImgFile_backup() {
curdttm=`date +%Y%m%d%H%M%S`
msg=`mkdir -p -m +w ${kvmhd_copydir_dest} 2>&1`
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}

curdttm=`date +%Y%m%d%H%M%S`
msg=`rsync -avvv --delete --timeout=3600 --bwlimit=8192 ${kvm_hd_fullpath_src} ${kvmhd_copydir_dest} 2>&1`
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}


return 11
}

function funcDispFinishedMessage() {
curdttm=`date +%Y%m%d%H%M%S`
msg="${targetvmname} backup process has finished. Thank you for your cooperation."
echo "${quotstr}${curdttm}${quotstr} ${quotstr}${msg}${quotstr}" | tee -a ${logdirfilename}
return 11
}

function funcMainProc() {
# funcWaitWithCountDown 30 " now waiting for starting a process of KVM xml and hd file backup... If you want to stop, Ctrl+C"
funcCPULimit
funcGetKVMInfo
funcWaitWithCountDown 30 " now waiting for shutdown ${targetvmname}... If you want to stop, Ctrl+C"
funcKVM_VM_ShutdownStart
funcKVM_VM_ShutdownFinishWait
funcWaitWithCountDown 30 " now waiting for staring file copy of ${targetvmname} files... If you want to stop, Ctrl+C"
funcKVM_VM_XML_backup
funcKVM_VM_HDImgFile_backup
funcKVM_VM_PowerOnProcStart
funcDispFinishedMessage
}

function funcMain() {
funcMainProc
}


curdttm=`date +%Y%m%d%H%M%S`
curdt=`date +%Y%m%d`
# ログファイル名を決定する。
curdttmforlogfilename=${curdttm}
logdir='/var/log/1050/'
mkdir -p -m +w ${logdir} 2>&1
logfilename_suffix='_log_'
shfilename=`basename $0`
logfilename_ext='.txt'
quotstr='"'
logdirfilename=${logdir}${curdttmforlogfilename}${logfilename_suffix}${shfilename}${logfilename_ext}


kvmxml_fullpath_src=$1
copybasedir_dest=$2
# funcMain ${kvmxml_fullpath_src} ${copydir_dest}
funcMain ${kvmxml_fullpath_src} ${copydir_dest}

0 件のコメント:

コメントを投稿