Hyper-V Server 2012 R2 Backup VMs

I am looking for a script that will automatically shutdown VMs so I can copy the VHDs to an alternate location and then start them back up.

Any suggestions?
BSModlinAsked:
Who is Participating?
 
Cliff GaliherConnect With a Mentor Commented:
You don't need two AD sites. The two hyper-v machines can exist on the LAN.

Replica requires the destination to be a hyoer-V machine.

But if you only want to copy to a file share, why nit just us a backup product. At the very least, windows server backup is free, supports Hyper-V and will pause VMs if necessary, and can back up to a file share. Still no need for a script.
0
 
convergintCommented:
I found this script on this site that should help you.

I personally use a program called Hyperoo which is excellent at doing backups real time at block level which is also great value for the price.

1.      Information
Virtual Machine Automated Backup System
=======================================

There are three files:

BackupVm.bat
VMStartStop.ps1
hosts.txt

Currently I am running this on version 2.0 of Powershell, according to some of the information I have read it should run on version 1.0 but I have not tested it.

Keep in mind that Windows installs Powershell in the %SystemRoot%\system32\WindowsPowerShell\v1.0 folder even if it is version 2.0 ... to determine the version you are running, start the Powershell cmd prompt and then issue the following command:
$Host.Version

If you are running version 2.0 it will show Major as 2 (see below)

PS C:\> $Host.Version

Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1

Now to the meat and potatoes ...

Backupvm.bat
============

This is a cmd file that can be set to run with the Windows scheduler.

In my example, I created a C:\Powershell folder which contains all of my scripts and cmd files.

The cmd file calls Powershell with a script file called VMStartStop.ps1, this script file is passed the name of a text file that contains a list of the virtual machine names you want shut down or started up (in this case it is called "hosts.txt"). The 1 at the end of the line is passed to the script file to instruct it to Shut down the virtuals.

Next the cmd file will simply copy the vhd files from the host server to a drive on another server/computer.

Once the copy is completed, the cmd file once again calls Powershell with a script file called VMStartStop.ps1, which is passed the name of the text file that contains a list of the virtual machine names you want started up (in this case it is called "hosts.txt"). The 0 (zero) at the end of the line is passed to the script file to instruct it to Start the virtuals.

hosts.txt
=========

This is a simple text file that contains a list of the virtual server names you want used in the VMStartStop.ps1 script. Simply list each virtual server name on a separate line in the text file.

VMStartStop.ps1
===============

This is a script file I found on the internet, I can take no credit or responsibilty for this script file. However, it does work well and I am happy with the results.

2.      VMStartStop.ps1
I cannot take credit or responsibility for the following script, however, I have had very good success with this script.

Copy and paste the following information into notepad and save the file with a .ps1 extension.

# ---------- 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--------------

3.      hosts.txt
You will need to create a simple text file containing a list of the virtual machine names you want started and/or stopped. In my case I called the file "hosts.txt"

for example:

Virtual Srv1
Virtual Srv2
VirtXP 1

4.      BackupVM.bat
This is a simple cmd/batch file that calls Powershell with the VMStartStop.ps1 script file and passes the hosts.txt (list of virtual machines) and a single parameter that instructs the script to shutdown the virtual machine or start it up.

REM Hyper-V Virtual Machine Backup

REM Shutdown virtual machines
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe c:\Powershell\VMStartStop.ps1 c:\Powershell\Hosts.txt 1

REM Copy virtual machines, /Y overwrites the file if it already exists

copy /Y "D:\Virtual Hard Disks\V1\Virtual Srv1.vhd" "\\computername\k$\Virtual Servers\V1\Virtual Srv1.vhd"
copy /Y "D:\Virtual Hard Disks\V2\Virtual Srv2.vhd" "\\computername\k$\Virtual Servers\V2\Virtual Srv2.vhd"
copy /Y "D:\Virtual Hard Disks\V3\VirtXP 1.vhd" "\\computername\k$\Virtual Servers\V3\VirtXP 1.vhd"

REM Start Up virtual machines

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe c:\Powershell\VMStartStop.ps1 c:\Powershell\Hosts.txt 0

REM Finished!
0
 
Cliff GaliherCommented:
Perhaps I am missing something. But why would you script this? Scripts are fragile and you get no reporting on failures. And for hat you want, hyper-V Replica does this without the extra manual effort.
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
BSModlinAuthor Commented:
I just did some quick reading and it seems like I would require 2 "sites" for the replication.... I want to copy the VHDs to a regular Windows File Share... Not another Hyper-V Host.  Is this possible using hyper-V Replica?
0
 
BSModlinAuthor Commented:
I was unaware that Windows Server Backup would Pause VMs automatically and perform backups.... Do you have a useful link for this?
0
 
Cliff GaliherCommented:
TechNet.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.