private bool disposedValue = false; // To detect redundant calls protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { BackupComplete(); if (snapShotSet != Guid.Empty) { backup.DeleteSnapshotSet(snapShotSet, true); snapShotSet = Guid.Empty; } if (backup != null) { backup.Dispose(); backup = null; } } // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below. // TODO: set large fields to null. disposedValue = true; } }
private void Cleanup() { if (_session._mountPoint != null) { _backup.BackupComplete(); VerifyWriterStatus(); _session._mountPoint = null; } if (_snapshotSetID != Guid.Empty) { // apparently fails on Windows 2008 - the non-persistent shadow copy // should be deleted as soon as _backup is disposed of anyway, // so just try to explicitly delete the snapshot set and ignore any issues try { _backup.DeleteSnapshotSet(_snapshotSetID, forceDelete: true); } catch { } } if (_backup != null) { _backup.Dispose(); } lock (_disposeEventSyncObject) { _disposeEvent.Dispose(); _disposeEvent = null; } }
static void BackupFile(string srcPath, string destPath) { srcPath = Path.GetFullPath(srcPath); IVssImplementation vssImpl = VssUtils.LoadImplementation(); using (IVssBackupComponents vss = vssImpl.CreateVssBackupComponents()) { vss.InitializeForBackup(null); vss.SetBackupState(true, true, VssBackupType.Full, false); vss.SetContext(VssSnapshotContext.FileShareBackup); using (IVssAsync async = vss.GatherWriterMetadata()) async.Wait(); Guid vssSet = vss.StartSnapshotSet(); var rootPath = Path.GetPathRoot(srcPath); var snapshotId = vss.AddToSnapshotSet(rootPath, Guid.Empty); using (IVssAsync async = vss.DoSnapshotSet()) async.Wait(); try { var snapshotPath = vss.GetSnapshotProperties(snapshotId).SnapshotDeviceObject; var pathNoRoot = srcPath.Substring(rootPath.Length); var path = Path.Combine(snapshotPath, pathNoRoot); if (File.Exists(destPath)) { File.Delete(destPath); } Alphaleonis.Win32.Filesystem.File.Copy(path, destPath); } finally { vss.DeleteSnapshotSet(vssSet, true); } } }
private void BackupSubset(IDictionary <string, string> vmNamesMapSubset, Options options) { IVssImplementation vssImpl = VssUtils.LoadImplementation(); using (IVssBackupComponents vss = vssImpl.CreateVssBackupComponents()) { RaiseEvent(EventAction.InitializingVSS, null, null); vss.InitializeForBackup(null); vss.SetBackupState(true, true, VssBackupType.Full, false); vss.SetContext(VssSnapshotContext.Backup); // Add Hyper-V writer Guid hyperVwriterGuid = new Guid("66841cd4-6ded-4f4b-8f17-fd23f8ddc3de"); vss.EnableWriterClasses(new Guid[] { hyperVwriterGuid }); vss.GatherWriterMetadata(); IList <IVssWMComponent> components = new List <IVssWMComponent>(); // key: volumePath, value: volumeName. These values are equivalent on a standard volume, but differ in the CSV case IDictionary <string, string> volumeMap = new Dictionary <string, string>(); var wm = vss.WriterMetadata.Where((o) => o.WriterId.Equals(hyperVwriterGuid)).FirstOrDefault(); foreach (var component in wm.Components) { if (vmNamesMapSubset.ContainsKey(component.ComponentName)) { components.Add(component); vss.AddComponent(wm.InstanceId, wm.WriterId, component.Type, component.LogicalPath, component.ComponentName); foreach (var file in component.Files) { string volumeName = null; string volumePath = null; volumePath = Path.GetPathRoot(file.Path).ToUpper(); volumeName = volumePath; if (!volumeMap.ContainsKey(volumePath)) { volumeMap.Add(volumePath, volumeName); } } } } if (components.Count > 0) { Guid vssSet = vss.StartSnapshotSet(); // Key: volumeName, value: snapshotGuid IDictionary <string, Guid> snapshots = new Dictionary <string, Guid>(); foreach (var volumeName in volumeMap.Values) { snapshots.Add(volumeName, vss.AddToSnapshotSet(volumeName, Guid.Empty)); } vss.PrepareForBackup(); RaiseEvent(EventAction.StartingSnaphotSet, components, volumeMap); vss.DoSnapshotSet(); RaiseEvent(EventAction.SnapshotSetDone, components, volumeMap); // key: volumeName, value: snapshotVolumePath IDictionary <string, string> snapshotVolumeMap = new Dictionary <string, string>(); foreach (var kv in snapshots) { snapshotVolumeMap.Add(kv.Key, vss.GetSnapshotProperties(kv.Value).SnapshotDeviceObject); } BackupFiles(components, volumeMap, snapshotVolumeMap, vmNamesMapSubset, options); foreach (var component in components) { vss.SetBackupSucceeded(wm.InstanceId, wm.WriterId, component.Type, component.LogicalPath, component.ComponentName, true); } vss.BackupComplete(); RaiseEvent(EventAction.DeletingSnapshotSet, components, volumeMap); vss.DeleteSnapshotSet(vssSet, true); } } }
/// <summary> /// Remove all snapshots. /// </summary> public void Delete() { _backup.DeleteSnapshotSet(_set_id, false); }
/// <summary> /// Makes a recursive snapshot of the directory as it is. /// </summary> /// <param name="baseDirectory">The directory to start in.</param> /// <returns>A file system snapshot</returns> public static SnapshotFilesystem MakeFsSnapshot(System.IO.DirectoryInfo baseDirectory) { DirectoryInfo baseDir = new DirectoryInfo(baseDirectory.FullName); if (!baseDir.Exists) { throw new ArgumentException("Base directory doesn't exist"); } SnapshotFilesystem res = new SnapshotFilesystem(); if (!baseDir.FullName.EndsWith(Path.DirectorySeparatorChar) && !baseDir.FullName.EndsWith(Path.AltDirectorySeparatorChar)) { // Add ending slash to get a uniform output baseDir = new DirectoryInfo(baseDir.FullName + Path.DirectorySeparatorChar); } res.BasePath = baseDir.FullName; res.Items = new LinkedList <SnapshotFilesystemItem>(); // Make VSS // Sequence of calls: http://us.generation-nt.com/answer/volume-shadow-copy-backupcomplete-vss-e-bad-state-help-29094302.html IVssImplementation vssImplementation = VssUtils.LoadImplementation(); IVssBackupComponents backupComponents = vssImplementation.CreateVssBackupComponents(); backupComponents.InitializeForBackup(null); backupComponents.SetContext(VssSnapshotContext.Backup); backupComponents.SetBackupState(false, false, VssBackupType.Copy, false); backupComponents.GatherWriterMetadata(); try { Guid snapshotSetGuid = backupComponents.StartSnapshotSet(); Guid backupVolumeGuid = backupComponents.AddToSnapshotSet(baseDir.Root.FullName); backupComponents.PrepareForBackup(); backupComponents.DoSnapshotSet(); VssSnapshotProperties properties = backupComponents.GetSnapshotProperties(backupVolumeGuid); DirectoryInfo shadowCopyBase = new DirectoryInfo(Path.Combine(properties.SnapshotDeviceObject, Path.GetDirectoryNameWithoutRoot(baseDir.FullName))); if (!shadowCopyBase.FullName.EndsWith(Path.DirectorySeparatorChar) && !shadowCopyBase.FullName.EndsWith(Path.AltDirectorySeparatorChar)) { // Add ending slash to get a uniform output shadowCopyBase = new DirectoryInfo(shadowCopyBase.FullName + Path.DirectorySeparatorChar); } // Do stuff DoFsSnapshot(shadowCopyBase, shadowCopyBase, res.Items); // Delete snapshot backupComponents.BackupComplete(); backupComponents.DeleteSnapshotSet(snapshotSetGuid, false); } catch (Exception) { backupComponents.AbortBackup(); } //DoFsSnapshot(baseDirectory, baseDirectory, res.Items); return(res); }
// Remove all snapshots. public void DeleteSnapshots() { _backup.DeleteSnapshotSet(_snapshotSetId, false); }