Using Code to Control Windows Back Up & Restore

(For windows vista, windows 7 and windowsRE) (Help Request)

Page 1 of 1

3 Replies - 9141 Views - Last Post: 29 July 2009 - 09:09 AM Rate Topic: -----

#1 Tyranin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 22-July 09

Using Code to Control Windows Back Up & Restore

Post icon  Posted 22 July 2009 - 09:06 AM

Hi everyone, sorry if I've broke any rules in asking this question. I've just signed up.

I am writing a third party back up system for windows 7. They way I intend it to work is to take control of the actual windows controls as opposed to doing the back up in my own code. The reason for this is compatibility, if I were to simply copy files and folders with xcopy or robocopy, it won't work in WindowsRE (Recovery Environment) by using the "System Image Recovery" option. The idea is to have a system tray application take the back-up procedure and simplify it. Whatever backups get made, they must be retreiveable through WindowRE's System Image Recovery (Or Complete PC Recovery in Vista)... If that's actually possible (I can imagine windows making it so that you can't use third party back up tools with the recovery environment, even though thats really unhelpful and totally backwards)...

So far, my solutions include: WBadmin (Console commands) VSSadmin (Console commands), Using WMI with C# or using C/C++ with the COM/DCOM API stuff. The latter I'd prefer to avoid because my C/C++ coding is very weak in comparison to my managed code programming. But if someone can show me how to connect to the api in C/C++, I'd really appreiate it.

As for C# I found the documentation for Volume Shadow Copy. Now I managed to call "Win32_ShadowCopy" and I invoked it's Create Method. And it did something, but I'm really not sure what it actually did. I also got "Win32_ShadowStorage" to do something, but then I'm not actually sure if Volume Shadow Copies are performing the actual back-up. (Could someone Clarify VSS & WBengine for me?)

Anyway, here's the C# code I created. This is a class which you can call from your main class using CreateShadow.RunCreation(volumeDriveLetter); you could just replace volumeDriveLetter with ("c"). I won't take any responsibility for any side-effect this has on your computer. It will create shadow copies on your computer, even though I don't actually know what they're for. You can use "VSSadmin" console commands to delete them etc.

Oh and don't forget to reference System.Management in your project!

using System;
using System.Collections;
using System.Text;
using System.Management;

namespace BackUpUtility
{
	class CreateShadow
	{
		public CreateShadow()
		{
		}
		public static void RunCreation(string volume)
		{
			try
			{
				// Create a management object for the ShadowCopy wmi class

Console.WriteLine("Calling the WMI Management Class"); // For debugging

				ManagementClass shadowCopy = new ManagementClass("Win32_ShadowCopy");

Console.WriteLine("Retrieving Parameter settings"); // For debugging

				ManagementBaseObject inParams = shadowCopy.GetMethodParameters("Create");

				// Get an array of the properties
Console.WriteLine("Counting Properties"); // For debugging

				int numItems = inParams.Properties.Count;

Console.WriteLine("Number of items: " + numItems); // For debugging

				// Get an enumerator? And add some properties
				IEnumerator eProps = inParams.Properties.GetEnumerator();

Console.WriteLine("Preparing Properties"); // For debugging

				inParams.SetPropertyValue("Volume", (volume + ":\\"));
				inParams.SetPropertyValue("Context", "ClientAccessible");

Console.WriteLine("Modifying Properties"); // For debugging
Console.WriteLine("");

				for (int i = 0; i < numItems; i++)
				{
					eProps.MoveNext();
					PropertyData pData = (PropertyData)eProps.Current;
					Console.WriteLine("Name: " + pData.Name);
					Console.WriteLine("Value: " + pData.Value);
				}

				Console.WriteLine("Invoking Creation");
				ManagementBaseObject outParams = shadowCopy.InvokeMethod("Create", inParams, null);
				Console.WriteLine("");
				
				// List outParams
				Console.WriteLine("Out parameters:");
				Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
				Console.WriteLine("ShadowID: " + outParams["ShadowID"]);

				string message = DiagnoseReturn((uint)outParams["ReturnValue"]);
				Console.WriteLine("Result: " + message);
			}
			catch (ManagementException err)
			{
				Console.WriteLine("An error occurred while trying to execute the WMI method: " + err.Message);
				Console.WriteLine(err.StackTrace);
			}
		}
	}
}



Here are the api's and stuff I found too.
Volume Shadow Copy API
Win32_ShadowCopy

Also here are some openSource stuff.
Hobocopy is a good one I found, but like I said I struggle with C/C++ and this guys code is real fancy.
HoboCopy - C/C++ back up utility
Also, Some guy called Joe managed this with VSS, but I think Volume Shadow Copy is a red herring for this project!
Code Project - Volume Shadow Copies from .NET

So now that you may or may not be up to speed with me... any ideas?/information?/help?
By the way, I'm a Student doing an industrial placement for a Diagnostics Software Company and I think I'm way in over my head!! :o

This post has been edited by Tyranin: 22 July 2009 - 09:13 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Using Code to Control Windows Back Up & Restore

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9389
  • View blog
  • Posts: 35,264
  • Joined: 12-June 08

Re: Using Code to Control Windows Back Up & Restore

Posted 22 July 2009 - 10:43 AM

So let me get this right.. you are doing this as a placement exam for a company you are looking to work for?
Was This Post Helpful? 0
  • +
  • -

#3 Tyranin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 22-July 09

Re: Using Code to Control Windows Back Up & Restore

Posted 22 July 2009 - 11:55 AM

No no, not exam, it's real world project! This will be sold as a product for capital. I even drew up the requirements document last week. Basically I've been given this project so I can see a real development cycle from start to finish.

Are limited company software development questions against the rules? If so then please remove this thread.

A better word for the placement would probably be internship. Where I'm actually employed by the company and paid a salary. This is for a "sandwich" course, which is a gap year where I get work experience in the industry.

The only answer I'm looking for is what would be the best technology, and how do I interface with it? I'm trying the .net route, but I haven't had much success. I also tried WBadmin, but the system restore feature won't automatically read from the backup. My mentor said we should open the virtual hard drive (vhd), that is on the backup disk, using our own software, so we're going to be trying that tomorrow.

This post has been edited by Tyranin: 22 July 2009 - 11:58 AM

Was This Post Helpful? 0
  • +
  • -

#4 Tyranin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 22-July 09

Re: Using Code to Control Windows Back Up & Restore

Posted 29 July 2009 - 09:09 AM

Not that anyone in this community would care about what a new progammer like me would have to say but I thought I'd bring some closure to this thread, in case anyone in the future came along with the same problem.

After scanning the interwebs, it seems that invoking the wbadmin process is a good way to go. It pratically is the back up service. However, WBadmin, or one of it's dependencies like wbengine is bugged in windows 7 RC, and a copy it produces will not be read by windows RE. Which is a shame really. A copy made using WBadmin in vista works fine. Hopefully this will be sorted out when the Release to manufactuer (RTM) comes out so hopefully we'll get an answer in the next week or so. But then, if it's still bugged, then we'll have to wait for the first service pack.

My best bet is probably to produce the VHD's and folder tree manually, but getting the compatibillity right is gonna be a pain.

// Invoking a windows process
using System.Diagnostics;

private Process winProcess = new Process();		// Use the Process class to define a process

winProcess.StartInfo.FileName = processName;			// Process's name as a string (eg. "WBADMIN.EXE")
winProcess.StartInfo.Arguments = processArguments;  // Arguments as a string (eg. "-backupTarget:D: -allCritical")

winProcess.Start();	// And then it's as simple as this command to start the lot off!

/* You can also read and write, to and from the process using StreamWriter */

private StreamWriter streamWriter;		  // Stream writers adds input into a process.
private StreamReader streamReader;		  // Stream readers will read the output and
private StreamReader streamErrors;		  // errors generated by the process.

/* But you also need to re-direct them. I'm not sure if you have to do this before or after you start the process, but I've placed my after the start function */

streamWriter = winProcess.StandardInput;
streamReader = winProcess.StandardOutput;
streamErrors = winProcess.StandardError;

// So now the writers I declared can be used to write input

streamWriter.AutoFlush = true;  // Can't remember what this does excatly, but it makes sure the input is done at the correct time.

/* Then to input keystrokes */

string inputString = "C,Y,C,Y,Y";	// Create a string for the inputs

string[] delimitedString = inputString.Split(new char[] {','}); // Delimit the string into an array

foreach (char newInput in delimitedString)	  // Loop through the array 
{
	streamWriter.WriteLine(newInput);   // input each character into the process using the streamWriter
}


pretty cool 'eh. I've attached a class I created that handles starting, stopping, configuring and managing input and output for process. Hopes you likes. (p.s. I couldn't upload the .cs file so it's in a .txt)

Attached File(s)


This post has been edited by Tyranin: 30 July 2009 - 01:04 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1