Creating the backup scripts
The following code snippets enable you to backup virtual machine vhd files on a hyper-v core installation to a backup directory on the same machine. From there you can pick up the disk files for further archiving.
Before creating and scheduling these files, make sure you enable the running of unsigned scripts, and change the zone of the files to non-internet as outlined in this document
The controlling batch file is a “normal” batch file and can be scheduled with the AT command or run manualy, and can stop, backup, and start your vhd files one by one.
SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe c:\scripts\vmbackup\StopVM.ps1 “somevm”
xcopy /Y “c:\Users\Public\Documents\Hyper-V\Virtual hard disks\somedisk.vhd” “c:\dbbackup”
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe c:\scripts\vmbackup\StartVM.ps1 “somevm”
Two additional powershell scripts do the actual suspending and restarting of the virtual machines:
StopVM.ps1
# ———- SCRIPT STARTS HERE————–
#
# Script origin and credit: http://www.infotechguyz.com/server2008/manageHyperVvms.html
# Embedding as a backup script: http://www.bunkerhollow.com/blogs/matt/archive/2008/09/24/hyper-v-vm-backup-script-batch-file.aspx
#
# 2010-02-10 Bob Noordam (http://www.nhcoding.nl)
# – Modified to call suspend instead of shutdown to enable backing up of Linux VM’s that dont have integration
# – Modified to stop/start a single vm specified on the command line, to enable backing up vm’s one by one
# and have only one down vm at a time (instead of batch-stop and batch-start)
#
$waitshutdown = 60
$guest = $args[0]
write-host “shutting down $guest”
$vm = gwmi -namespace root\virtualization -query “select * from msvm_computersystem where elementname=’$guest’”
$vmname = $vm.name
$result = $vm.requeststatechange(32769)
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 result $result” -foregroundcolor red
write-host “”
}
# ———- SCRIPT ENDS HERE————–
And StartVM.ps1
# ———- SCRIPT STARTS HERE————–
#
# Script origin and credit: http://www.infotechguyz.com/server2008/manageHyperVvms.html
# Embedding as a backup script: http://www.bunkerhollow.com/blogs/matt/archive/2008/09/24/hyper-v-vm-backup-script-batch-file.aspx
#
# 2010-02-10 Bob Noordam (http://www.nhcoding.nl)
# – Modified to call suspend instead of shutdown to enable backing up of Linux VM’s that dont have integration
# – Modified to stop/start a single vm specified on the command line, to enable backing up vm’s one by one
# and have only one down vm at a time (instead of batch-stop and batch-start)
#
$waitstart = 60
$guest = $args[0]
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 “”
}
# ———- SCRIPT ENDS HERE————–
Scheduling your backup task
One of the remote managable components of the 2008R2 server core is the task scheduler. You can therefore easily schedule the backup task from your workstation.
•Install the remote system administration tools (RSTA)
•Start MMC
•Add the module for the task scheduler, and choose “remote machine”
Creating a share on the HYPER-V server
Finaly create a directory and a share to retrive the backups you created to be able to store them externaly
md c:\dbbackup
net share dbbackup=c:\dbbackup