Automatically backup Hyper-V VMs using PowerShell

Trying something new! Today I have a guest blogger. Joel Webster works at Dell with me and works quite a bit on the PowerShell support for vWorkspace. He has been an intern for quite a while and really knows his stuff. 

Adam

A few weeks ago, I came in to work and after I unlocked my machine, I noticed something strange: the clock on my development VM was different than the clock on my host machine. As I gave the dev VM mouse focus, it went black and I got the “lost connection to virtual machine” message, and all of the VMs in my Hyper-V Manager went from Running to Critical.

This was not good.

I started checking into it, and when I opened up windows explorer, I noticed something curious: my workhorse drive was gone. The one with all of my VMs stored on it. It turns out the drive died in the middle of the night.

Thankfully, our in-house IT guy was able to recover my most important VMs, so I didn’t have to rebuild my environment completely from scratch. But I did learn an important lesson: ALWAYS BACKUP YOUR STUFF

I looked in to the built-in Hyper-V Export functions, but these took a long time to execute. I figured there had to be a better way to backup. So I made one, with help from some other folks who had a similar problem.

First, I looked into the PowerShell Management Library for Hyper-V and its cmdlets. I found that, through this module, I could discover VMs on a host, save their state, export them, and resume them. I also discovered that Stan Czerno had already done this part for me, which saved me a good deal of time. All I had to do was modify his code a little.

Once I had all of these exported VMs lying around, I noticed that they took up a great deal of space on my backup drive. I started looking for a good way to compress these archives. In Windows Explorer I can just right-click a folder and compress it. Through PowerShell this is much more tricky. Thankfully, Jeremy Jameson already did much of the heavy lifting here, and again, all I needed to do was modify his scripts to suit very large files.

With these two core components in place, all I needed to do was write a script that tied them together and did some happy bookending and logging.

The end product of my efforts is the module linked below. Point it at a drive to backup onto, tell it which host to look at, tell it which VMs to back up, and then let it fly. You can even tell it to shut down the host when it’s finished. You can make a script out of it and back up an entire farm of VMs on multiple hosts. I use it to protect myself for if/when my drive dies again.

You could even set it to run on a schedule so you don’t even have to think about it.

In the weeks since I first made this module, I’ve already used it when I accidentally deleted a terminal server VM out of my farm. I just grabbed the previous night’s backup and imported the VM out of it – complete recovery in less than 15 minutes. I dare you to rebuild a terminal server from scratch in 15 minutes.

Because of its size (566 lines) I’ve decided to simply link to it instead of posting the whole module. The module is available on PoshCode.org: Backup Hyper-V VMs

Use it, so you don’t have to suffer the loss of your drives and VMs!

You can leave a response, or trackback from your own site.

5 Responses to “Automatically backup Hyper-V VMs using PowerShell”

  1. [...] Automatically backup Hyper-V VMs using PowerShell by Adam Driscoll [...]

Leave a Reply


− seven = 1