Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Hyper-V Server 2012 R2 Backup VMs

Posted on 2014-09-11
7
Medium Priority
?
575 Views
Last Modified: 2014-11-12
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?
0
Comment
Question by:BSModlin
  • 3
  • 2
  • 2
7 Comments
 
LVL 10

Expert Comment

by:convergint
ID: 40317838
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
 
LVL 59

Expert Comment

by:Cliff Galiher
ID: 40317850
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
 

Author Comment

by:BSModlin
ID: 40317875
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
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.

 
LVL 59

Accepted Solution

by:
Cliff Galiher earned 2000 total points
ID: 40317884
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
 

Author Comment

by:BSModlin
ID: 40317909
I was unaware that Windows Server Backup would Pause VMs automatically and perform backups.... Do you have a useful link for this?
0
 
LVL 59

Expert Comment

by:Cliff Galiher
ID: 40318119
TechNet.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to deal with a specific error when using the Enable-RemoteMailbox cmdlet to create a mailbox in the cloud-based service, for an existing user in an on-premises Active Directory.
This article will explain How to fix Broken backup chain in Veeam Backup & Replication.
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question