去年我們單位只有一臺IBM 3650 M2 要將一些行政用AP server導入
由於沒有預算作HA於是就開始找尋一些備份的機制,不管是商用軟體或是BY script的方式
在測試一陣子發現如果你的回復時間沒有要到幾秒內回復,幾分或幾十分鐘可以接受的話
那麼接下要介紹的作法會非常簡單實用而且不花一毛錢
=========================================================
以下要介紹的是利用Powershell裡針對Hyper-v的備份指令搭配Winodws排程來備份
script是從網路上找來的,出處小弟已經忘了,稍後上網找或是有網兄可以幫忙補充先謝謝大家
先介紹備份原理:
由於虛擬化的guest os是一個檔案只要將該guest os關閉後匯出將其硬體設定檔與os檔案
然後之後就是利用排程備份os檔案覆蓋備份檔,當發生問題要回復的時候直接將該os檔貼回去即可
匯出設訂檔的主要原因是再回復guest os時有兩種方式,匯入與新增
這兩種方式都可以回復系統,問題再於windows作業系統網卡的資訊會寫入到登錄檔
如果你是新增的方式回復系統那麼會再插入一張網卡,然後你的IP位置一定也會設的一樣
那麼就會有衝突,雖然可以利用登錄檔刪除舊的網卡資訊,但是這樣就多一道工,也多了不必要的處理時間
==================================================================
接下來就是程式碼的介紹
總共有5個檔案
StartStopVMs:主要的powershell Hyper-v函數與指令位置
StartStopVMsPlusLogFile:同StartStopVM,但是多了LOG的功能
VmBackup:執行用的批次檔,可單獨執行或是利用排程
VmNamesStart:利用powershell Hyper-v指令啟動此檔案內的Guest清單,該list清單就是Hyper-v上Guest的名稱,也就是在Hyper-v管理員上顯示的主機名稱
VmNamesStop:利用powershell Hyper-v指令關閉此檔案內的Guest清單,該list清單就是Hyper-v上Guest的名稱,也就是在Hyper-v管理員上顯示的主機名稱
==========================================================
程式的流程如下
1.執行VmBackup
2.會先去VmNamestop檔案裡所列出的主機清單檢查目前系統是否在開啟狀態,如果是就將其依序關閉
3.將其實體os檔案copy到要備份的位置
4.備份完成後,檢查VmNamesStart檔案裡所列出的主機清單檢查目前系統是否在關閉狀態,如果是就將其依序開啟
==========================================================
貼上程式碼給大家參考:
StartStopVMs
<pre class="c" name="code">
# ---------- SCRIPT STARTS HERE--------------
$waitstart = 200
$waitshutdown = 120
if ($args[1] -match "0") {
$inputfile=get-content $args[0]
foreach ($guest in $inputfile) {
write-host "Starting $guest"
$vm = gwmi -namespace root\virtualization -query "select * from msvm_computersystem where elementname='$guest'"
$result = $vm.requeststatechange(2)
if ($result.returnvalue -match "0") {
start-sleep -s $waitstart
write-host ""
write-host "$guest is started" -foregroundcolor green
write-host ""
}
else {
write-host ""
write-host "unable to start $guest" -foregroundcolor red
write-host ""
}}}
if ($args[1] -match "1") {
$inputfile=get-content $args[0]
foreach ($guest in $inputfile) {
write-host "shutting down $guest"
$vm = gwmi -namespace root\virtualization -query "select * from msvm_computersystem where elementname='$guest'"
$vmname = $vm.name
$vmshut = gwmi -namespace root\virtualization -query "SELECT * FROM Msvm_ShutdownComponent WHERE SystemName='$vmname'"
$result = $vmshut.InitiateShutdown("$true","no comment")
if ($result.returnvalue -match "0") {
start-sleep -s $waitshutdown
write-host ""
write-host "no error while shutting down $guest"
write-host "shutdown of $guest completed" -foregroundcolor green
write-host ""}
else {
write-host ""
write-host "unable to shutdown $guest" -foregroundcolor red
write-host ""
}}}
else {
write-host "USAGE: to shutdown VMs," -nonewline; write-host ".\managehyperV.ps1 c:\hosts.txt 1" -foregroundcolor yellow
write-host "USAGE: to start VMs," -nonewline; write-host ".\managehyperV.ps1 c:\hosts.txt 0" -foregroundcolor yellow
}
# ---------- SCRIPT ENDS HERE--------------
StartStopVMsPlusLogFile
<pre class="c" name="code">
# ---------- SCRIPT STARTS HERE--------------
$waitstart = 200
$waitshutdown = 120
$log = "C:\Scripts\VmBackup\backupLog.log"
Get-Date >> $log
if ($args[1] -match "0") {
$inputfile=get-content $args[0]
foreach ($guest in $inputfile) {
write-host "Starting $guest"
$vm = gwmi -namespace root\virtualization -query "select * from msvm_computersystem where elementname='$guest'"
$result = $vm.requeststatechange(2)
if ($result.returnvalue -match "0") {
start-sleep -s $waitstart
write-host ""
write-host "$guest is started" -foregroundcolor green >> $log "$guest is started" >> $log write-host ""
}
else {
write-host ""
write-host "unable to start $guest" -foregroundcolor red >> $log "unable to start $guest" >> $log write-host ""
}}}
if ($args[1] -match "1") {
$inputfile=get-content $args[0]
foreach ($guest in $inputfile) {
write-host "shutting down $guest"
$vm = gwmi -namespace root\virtualization -query "select * from msvm_computersystem where elementname='$guest'"
$vmname = $vm.name
$vmshut = gwmi -namespace root\virtualization -query "SELECT * FROM Msvm_ShutdownComponent WHERE SystemName='$vmname'"
$result = $vmshut.InitiateShutdown("$true","no comment") if ($result.returnvalue -match "0") { start-sleep -s $waitshutdown write-host ""
write-host "no error while shutting down $guest"
write-host "shutdown of $guest completed" -foregroundcolor green >> $log "shutdown of $guest completed" >> $log write-host ""}
else {
write-host ""
write-host "unable to shutdown $guest" -foregroundcolor red >> $log "unable to shutdown $guest" >> $log write-host ""
}}}
else {
write-host "USAGE: to shutdown VMs," -nonewline; write-host ".\managehyperV.ps1 c:\hosts.txt 1" -foregroundcolor yellow write-host "USAGE: to start VMs," -nonewline; write-host ".\managehyperV.ps1 c:\hosts.txt 0" -foregroundcolor yellow }
# ---------- SCRIPT ENDS HERE--------------
VmBackup
<pre class="c" name="code">
REM Hyper-V Virtual Machine Backup
REM Shutdown virtual machines
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe c:\scripts\vmbackup\StartStopVMsPlusLogFile.ps1 c:\scripts\vmbackup\VmNamesStop.txt 1
REM Copy virtual machines, /Y overwrites the file if it already exists
copy /Y "d:\hyper-v\virtual hard disks\xp.vhd" "e:\backup\xp\xp.vhd"
copy /Y "d:\hyper-v\virtual hard disks\vista.vhd" "e:\backup\vista\vista.vhd"
copy /Y "d:\hyper-v\virtual hard disks\server2003.vhd" "e:\backup\server2003\server2003.vhd"
copy /Y "d:\hyper-v\virtual hard disks\server2008.vhd" "e:\backup\server2005\server2005.vhd"
copy /Y "d:\hyper-v\virtual hard disks\dc.vhd" "e:\backup\dc\dc.vhd"
REM Power on virtual machines
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe c:\scripts\vmbackup\StartStopVMsPlusLogFile.ps1 c:\scripts\vmbackup\VmNamesStart.txt 0
REM Finished!
VmNamesStart與VmNamesStop
<pre class="c" name="code">
dc
server2008
server2003
vista
xp
==================================================
針對這樣的作法有疑問的網兄,可以一起來討論
這種方式我已經作了一年多,也常利用這樣的方式演練災害還原
目前沒發生過問題,對於沒有經費做HA的公司應該是有幫助的唷
完整程式碼我上傳到免空,有興趣的人可以去下載
不好意思,雖不是我發的文,但目前也有這方面的需求
想請問一下
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe c:\scripts\vmbackup\StartStopVMsPlusLogFile.ps1 c:\scripts\vmbackup\VmNamesStart.txt 0
上面這段文是什麼意思??
你第三種備份方式,應該是將guest os匯出,針對設定檔與guest os做備份對嗎?
所以用第三種方法,要自已先將guest os關機匯出,才能下批次執行??
那段程式碼就是將VmNamesStart.txt裡的主機一個個帶入StartStopVMsPlusLogFile.ps1 做主機狀態的檢查,如果是關閉狀態就開啟
這個備份的程式碼只是單純將guest os的檔案也就是vhd檔備份出去而已,如果真的發生問題也只是單純將vhd覆蓋回去
至於先做匯出動作的部份只是當該臺Hyper-v主機掛點的時候,加速Guest os移去其他主機的時間與減少問題而已
Likeyly you do not have any support plan for the setup and not really know the features of Hyper-V in your deployment environment.
It is good to get the following books :
Hyper-V R2叢集虛擬化技術--容錯移轉、線上備份、集中管理(附DVD)
Windows Server 2008 R2虛擬化技術 Hyper-V R2(平裝)
Before somethings turn ugly, you should know how to manage it at least.
您可以使用支援Hyper-V虛擬環境的備份軟體,例如 GFI Backup
請參考
http://www.gfi.com/backup-software-for-business
可以免費下載測試