|
|
Comments and Discussions
|
|
 |
|
|

|
Hello,
Whenever I use this MakeShadowCopy class, it seems like the shadow "expires" or something after the first couple of files it copies.. The copy process returns error codes of 55, 2, and 3, which all relate to a file/path not existing.
Does anyone know if shadow copies expire after a certain amount of time, and if there is anything I can do to keep it alive during this process?
Thanks in advance!
Derek Pecka
|
|
|
|
|

|
Hi,
I am also working with VSS but i am unable to do VSS Incremental and Differential Backup process in C++. Could you help me about this metter?
Thanks
hi!!
|
|
|
|

|
void vssTest()
{
VssWMI vss = new VssWMI();
try
{
vss.SetVolume(@"C:\");
}
finally
{
vss.DelVolume();
}
}
public class VssWMI
{
public string DeviceName = string.Empty;
public string Volume = string.Empty;
public string SetVolume(string volume)
{
DelVolume();
shadowId = createShadow(volume);
DeviceName = getShadowDevice(shadowId);
Volume = volume;
return DeviceName;
}
public void DelVolume()
{
if (shadowId != string.Empty)
deleteShadow(shadowId);
shadowId = string.Empty;
DeviceName = string.Empty;
Volume = string.Empty;
}
static string shadowId = string.Empty;
static string shadowNamespace = @"\\localhost\root\cimv2";
static string createShadow(string volume)
{
string res = string.Empty;
ManagementClass mgmtClass = new ManagementClass(shadowNamespace, "Win32_ShadowCopy", null);
ManagementBaseObject inParams = mgmtClass.GetMethodParameters("Create");
inParams.SetPropertyValue("Volume", volume);
inParams.SetPropertyValue("Context", "ClientAccessible");
ManagementBaseObject outParams =
mgmtClass.InvokeMethod("Create", inParams, null);
res = (string)outParams["ShadowID"];
if (res == string.Empty)
throw new Exception("Error creating shadow copy!");
return res;
}
static void deleteShadow(string shadowId)
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(shadowNamespace,
"SELECT * FROM Win32_ShadowCopy where ID = '" + shadowId + "'");
foreach (ManagementObject queryObj in searcher.Get()) {
queryObj.Delete();
break;
}
}
static string getShadowDevice(string shadowId)
{
string res = string.Empty;
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(shadowNamespace,
"SELECT * FROM Win32_ShadowCopy where ID = '" + shadowId + "'");
foreach (ManagementObject queryObj in searcher.Get()) {
res = (string)queryObj["DeviceObject"];
break;
}
return res;
}
}
|
|
|
|

|
while (true) {
int hr = 0;
int x = 0;
vssAsync.QueryStatus(out hr, ref x);
if (hr == VSS_S_ASYNC_FINISHED) {
isSnapOpened = true;
break;
}
if (hr == VSS_S_ASYNC_CANCELLED) {
throw new Exception("VSS asynchronous operation was canceled by timeout!");
}
if (asyncSnapCounter >= 60) {
vssAsync.Cancel();
}
asyncSnapCounter++;
Thread.Sleep(500);
}
modified on Wednesday, June 18, 2008 2:21 PM
|
|
|
|

|
public struct VSSErrHelper
{
public long ErrorId;
public string ErrorMsg;
public VSSErrHelper(long errorId, string errorMsg)
{
ErrorId = errorId;
ErrorMsg = errorMsg;
}
}
public static VSSErrHelper[] VSSErrors =
{
new VSSErrHelper(VSS_E_BAD_STATE, "A function call was invalid because of the state of either the\nbackup extensions or the coordinator. For example calling AddToSnapshot\nset prior to calling StartSnapshotSet."),
new VSSErrHelper(VSS_E_PROVIDER_ALREADY_REGISTERED, "Calling RegisterProvider"),
new VSSErrHelper(VSS_E_PROVIDER_NOT_REGISTERED, "Calling UnregisterProvider"),
new VSSErrHelper(VSS_E_PROVIDER_VETO, "Calling DoSnapshotSet"),
new VSSErrHelper(VSS_E_PROVIDER_IN_USE, "Calling UnregisterProvider, StartSnapshotSet"),
new VSSErrHelper(VSS_E_OBJECT_NOT_FOUND, "Calling DeleteSnapshots, Query"),
new VSSErrHelper(VSS_S_ASYNC_PENDING, "Calling IVssAsync::QueryStatus"),
new VSSErrHelper(VSS_S_ASYNC_FINISHED, "Calling IVssAsync::QueryStatus"),
new VSSErrHelper(VSS_S_ASYNC_CANCELLED, "Calling IVssAsync::QueryStatus"),
new VSSErrHelper(VSS_E_VOLUME_NOT_SUPPORTED, "Calling AddToSnapshotSet"),
new VSSErrHelper(VSS_E_VOLUME_NOT_SUPPORTED_BY_PROVIDER, "Calling AddToSnapshotSet"),
new VSSErrHelper(VSS_E_OBJECT_ALREADY_EXISTS, "Calling ExposeCurrentState"),
new VSSErrHelper(VSS_E_UNEXPECTED_PROVIDER_ERROR, "Calling several methods supported by the providers."),
new VSSErrHelper(VSS_E_CORRUPT_XML_DOCUMENT, "XML document unexpectedly does not match schema."),
new VSSErrHelper(VSS_E_CORRUPT_XML_DOCUMENT, "An XML document passes as an argument is not valid, i.e., is either\nnot correctly formed XML or does not match the schema"),
new VSSErrHelper(VSS_E_MAXIMUM_NUMBER_OF_VOLUMES_REACHED, "We cannot add any more volumes since we passed the maximum limit."),
new VSSErrHelper(VSS_E_FLUSH_WRITES_TIMEOUT, "VSS couldn't flush I/O writes anymore."),
new VSSErrHelper(VSS_E_HOLD_WRITES_TIMEOUT, "VSS couldn't hold I/O writes anymore."),
new VSSErrHelper(VSS_E_UNEXPECTED_WRITER_ERROR, "VSS encountered problems while sending events to writers"),
new VSSErrHelper(VSS_E_SNAPSHOT_SET_IN_PROGRESS, "StartSnapshotSet was called when another snapshot set in the\nprocess of being created."),
new VSSErrHelper(VSS_E_MAXIMUM_NUMBER_OF_SNAPSHOTS_REACHED, "AddToSnapshotSet was called on a volume that has already reached\nits maxinum number"),
new VSSErrHelper(VSS_E_WRITER_INFRASTRUCTURE, "The Writer infrastructure is not operating properly. Check that the\nEvent Service and the Volume Snapshot Service are started and check for\nerrors associdated with these services in the error log."),
new VSSErrHelper(VSS_E_WRITER_NOT_RESPONDING, "A writer did not respond to a GetWriterStatus call. This means that\nthe process containing the writer died or is hung."),
new VSSErrHelper(VSS_E_WRITER_ALREADY_SUBSCRIBED, "A writer has already sucessfully called the Subscribe function. It cannot call\nsubscribe multiple times."),
new VSSErrHelper(VSS_E_UNSUPPORTED_CONTEXT, "Attempt to use an unsupported context."),
new VSSErrHelper(VSS_E_VOLUME_IN_USE, "Calling ChangeDiffAreaMaximumSize"),
new VSSErrHelper(VSS_E_MAXIMUM_DIFFAREA_ASSOCIATIONS_REACHED, "Calling AddDiffArea"),
new VSSErrHelper(VSS_E_INSUFFICIENT_STORAGE, "Calling EndPrepareSnapshots, ChangeDiffAreaMaximumSize"),
new VSSErrHelper(VSS_E_NO_SNAPSHOTS_IMPORTED, "Calling ImportSnapshots, no volumes were successfully imported"),
new VSSErrHelper(VSS_S_SOME_SNAPSHOTS_NOT_IMPORTED, "Calling ImportSnapshots, some volumes were not successfully imported")
};
private static Exception getVSSErrMsg(Exception e)
{
int indexOfErrorId = -1;
long errorId = 0;
string srcErrMsg = "";
indexOfErrorId = e.Message.IndexOf("HRESULT:");
if (indexOfErrorId >= 0) {
srcErrMsg = e.Message.Substring(indexOfErrorId);
try {
errorId = Convert.ToInt64(e.Message.Substring(indexOfErrorId + 8).Trim(), 16);
} catch { errorId = 0; }
}
if (errorId > 0) {
foreach (VSSErrHelper dscr in VSSErrors) {
if (dscr.ErrorId == errorId)
return new Exception("VSS ERROR: " + dscr.ErrorMsg + " (" +
srcErrMsg + ")", e);
}
}
return e;
}
|
|
|
|

|
Hello,
Nice article, my question is: who created the interop.vss.dll and how?
Please let me know..
Thanks
|
|
|
|

|
Using Volume Shadow Services in Vb.NET thru an Interop (Interop.VSS.Dll for Windows Server 2003) there appears to be random errors occuring.
After creating the snapshot with the code below
Private Function MakeNewSnapshot( _
ByRef vss As VSS.VSSCoordinatorClass, _
ByVal sVolume As String, _
ByRef output_SnapShotSetID As Guid, _
ByRef output_SnapShotID As Guid) As Boolean
Dim bRet As Boolean
Try
vss.StartSnapshotSet(output_SnapShotSetID)
vss.AddToSnapshotSet(sVolume, Guid.Empty, output_SnapShotID)
Dim ovssAsync As VSS.IVssAsync = Nothing
Dim oCallBack As Object = Nothing
vss.DoSnapshotSet(oCallBack, ovssAsync)
Do While (True)
Dim hr As Integer = 0
Dim x As Integer = 0
ovssAsync.QueryStatus(hr, x)
If hr = VSS_S_ASYNC_FINISHED Then
Exit Do
End If
Threading.Thread.Sleep(1000)
Loop
bRet = True
Catch ex As Exception
' clean up any progressing snapshots
vss.AbortAllSnapshotsInProgress()
End Try
Return bRet
End Function
I then attempt to copy a file using hte CopyFile API. On random occasions for some files and all the time for others I get the CopyFile failing with "The system cannot find the file specified.". It doesn't always happen right either, it can get X % thru the copy then fail.
I then try to clean up the VSS Snapshot with
vss.DeleteSnapshots( _
moSnapshotSetID, _
Global.VSS._VSS_OBJECT_TYPE.VSS_OBJECT_SNAPSHOT_SET, _
CInt(True), _
iCountOfDeletedSnapshots, _
oNonDeletedSnapshotID)
bRet = iCountOfDeletedSnapshots = 1
This gives an error message like "0x80042308" (VSS_E_OBJECT_NOT_FOUND) indicating the Snapshot is missing...
What can be going wrong? -- it only seems to happen on Windows 2003... maybe once or twice on XP
|
|
|
|

|
One small question: Where can I find the referenced vss.dll on Windows Vista?
modified on Sunday, March 23, 2008 4:38 PM
|
|
|
|

|
I compiled the code on my XP Machine and it works great. However when I run it on Vista I get COM interface error.
It does not seem to be a permission issue because I start the program as Administrator.
Anyone have any ideas or thoughts?
|
|
|
|

|
Hello - Great sample. I modified the Snapshot class to create a snapshot via a method call. But, I noticed the snapshot is removed when my WinForms app shuts down (even if I do not delete the snapshot/snapshot set). Do you know how I can prevent this from happening?
T.C.
|
|
|
|
|

|
Hi Joe,
your article is a very nice one.
Small error:
ElseIf args(i).StartsWith("-T:") Then
sTargetPath = args(i).Substring(3, args(i).Length - 3)
If Not sTargetPath.EndsWith("\") Then sTargetPath = sTargetPath & "\"
If Not Directory.Exists(sSourcePath) Then
bSuccess = False
ShowError(4)
ShowHelp()
Exit Sub
End If
Else
sSourcePath should be sTargetPath
Cheers, Mohamed
|
|
|
|

|
Joe,
I'm a VB.NET guy too. Like this backup idea, I may change from what I'm currently doing.
Nice job providing links to resources. I think this is a VERY good practice. I do this in my production code too.
I have a few coding suggestions/comments if that is OK.
Use the callback (currently you are passing Nothing) as opposed to polling. This makes your code much cleaner, exception handling is all in one place, testing for the result is easy and without polling. Many resources on the Internet and here at Code Project on the ASYNC pattern.
The below block has the potential of never exiting since you are only testing for FINISHED. Again, using the callback avoids this problem.
Do While (True)
Dim hr As Integer = 0
Dim x As Integer = 0
ovssAsync.QueryStatus(hr, x)
Console.Write(".")
If hr = VSS_S_ASYNC_FINISHED Then
Exit Do
End If
Thread.Sleep(1000)
Loop
Maybe consider moving all the code in the constructor to a public method. This way your consumer could call the routine several times if they so desired. Putting all this activity in the constructor can also be problematic for debugging. Additionally, most folks would "expect" a method to call to perform a backup, rather than the backup lighting up during object construction.
What is the purpose of the delete method? I know your article states, "When done witht he shadown volume, relase it..." Do I have to call it? What happens if I don't? If I delete it, will I loose what I just backed up?
If you provide a small code snippet that consumes your Shapshot class that would also be helpful to the reader.
Not being critial. Please keep the articles coming.
Have a great day,
|
|
|
|

|
Great article. This is exactly what I've been looking for. Thank you.
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
|
Use the Windows Volume Shadow Copy service from .NET.
| Type | Article |
| Licence | Ms-PL |
| First Posted | 7 Jan 2008 |
| Views | 71,708 |
| Downloads | 1,429 |
| Bookmarked | 64 times |
|
|