Solved

Hyper-V Server 2012 R2 Backup VMs

Posted on 2014-09-11
7
551 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 58

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
2017 Webroot Threat Report

MSPs: Get the facts you need to protect your clients.
The 2017 Webroot Threat Report provides a uniquely insightful global view into the analysis and discoveries made by the Webroot® Threat Intelligence Platform to provide insights on key trends and risks as seen by our users.

 
LVL 58

Accepted Solution

by:
Cliff Galiher earned 500 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 58

Expert Comment

by:Cliff Galiher
ID: 40318119
TechNet.
0
 
LVL 10

Expert Comment

by:convergint
ID: 40318150
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Veeam Backup & Replication has added a new integration – Veeam Backup for Microsoft Office 365.  In this blog, we will discuss how you can benefit from Office 365 email backup with the Veeam’s new product and try to shed some light on the needs and …
Is your company's data protection keeping pace with virtualization? Here are 7 dynamic ways to adapt to rapid breakthroughs in technology.
In this Micro Tutorial viewers will learn how to restore their server from Bare Metal Backup image created with Windows Server Backup feature. As an example Windows 2012R2 is used.
In this Micro Tutorial viewers will learn how to restore single file or folder from Bare Metal backup image of their system. Tutorial shows how to restore files and folders from system backup. Often it is not needed to restore entire system when onl…

739 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