My experience with Microsoft technology

My experience with Microsoft technology

Scheduled Virtual Machine backups with Powershell and 7zip

leave a comment »

Recently I had a request to do regular backups from the virtual machines on one of our development Hyper-V servers. The requirements were as follows:

  1. The solution should be fully automated
  2. No online backup necesarry
  3. Only one backup has to be stored

For sure there are several ways to achieve those goals mine is as follows:

  1. Use Powershell to interact with Hyper-V
  2. Suspend the running VMs
  3. Create a folder for the current date
  4. Export the VMs to this path
  5. Compress them with 7zip
  6. Delete the uncompressed files and the folder from the last week
  7. Schedule those steps with the Windows Task Scheduler

While searching the internet for a cmdlet reference for Hyper-V i found this great project on codeplex: pshyperv.codeplex.com It is a PowerShell Management Library for Hyper-V. The current release is written for Powershell v2 which you can get from support.microsoft.com/kb/968929 for earlier versions than Windows Server 2008 R2. You can get 7zip from www.7zip.org

At first install the library on the Hyper-V host where you want to backup the VMs. With the Powershell Library for Hyper-V it’s quite easy to write the script to do what we want. I’m not allowed to post my script here because of intellectual property rights but i can show you the steps I used.

  1. Logging as described here.
  2. Import module and set execution policy as described in the .pdf from the Hyper-V Library
  3. Suspend all running VMs and save them in a variable
  4. Set backup path variable
  5. Loop through the supsended VMs export them to the backup path. Compress them using the command line version of 7zip to a folder with the current date within the backup folder. The syntax can be found in the help file. After that do the same for stopped VMs.
  6. Remove everything from the backup folder exluding the folder with the current date.
  7. Start all suspended VMs

Next create a task in the Task Scheduler with the following action:

Action: Start a program

Program: %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe

Arguments: <fullpath of the script>

Start in <path of the folder the script resides in>

1

Configure the schedule as you wish and that’s it. If you want the script to run under a low priviledge account remember that it needs the log on as batch job permission.

Regards Alex

Advertisements

Written by alexanderschmitt

3. March 2010 at 20:56

Posted in Hyper-V

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: