Ejemplo n.º 1
0
        public void SetupWriters(Guid[] includedWriters, Guid[] excludedWriters)
        {
            if (includedWriters != null && includedWriters.Length > 0)
            {
                _vssBackupComponents.EnableWriterClasses(includedWriters);
            }

            if (excludedWriters != null && excludedWriters.Length > 0)
            {
                _vssBackupComponents.DisableWriterClasses(excludedWriters);
            }

            try
            {
                _vssBackupComponents.GatherWriterMetadata();
            }
            finally
            {
                _vssBackupComponents.FreeWriterMetadata();
            }

            if (includedWriters == null)
            {
                return;
            }

            // check if writers got enabled
            foreach (var writerGUID in includedWriters)
            {
                if (!_vssBackupComponents.WriterMetadata.Any(o => o.WriterId.Equals(writerGUID)))
                {
                    throw new Exception(string.Format("Writer with GUID {0} was not added to VSS writer set.", writerGUID.ToString()));
                }
            }
        }
Ejemplo n.º 2
0
 public void DoSnapshot()
 {
     try
     {
         logger.Debug("Doing snapshot...");
         IVssImplementation vssImplementation = VssUtils.LoadImplementation();
         backup = vssImplementation.CreateVssBackupComponents();
         backup.InitializeForBackup(null);
         backup.SetBackupState(false, false, VssBackupType.Copy, false);
         backup.EnableWriterClasses();
         backup.GatherWriterMetadata();
         backup.GatherWriterStatus();
         backup.SetContext(VssSnapshotContext.AppRollback);
         snapshotSetId = backup.StartSnapshotSet();
         String rootPath = Volume.GetUniqueVolumeNameForPath(path);
         logger.Debug("RootPath: {0}.", rootPath);
         snapshotId = backup.AddToSnapshotSet(rootPath);
         backup.PrepareForBackup();
         backup.DoSnapshotSet();
         //File.WriteAllText(@"d:" + Path.DirectorySeparatorChar + "Document.xml", backup.SaveAsXml());
         backup.BackupComplete();
     }
     catch (Exception e)
     {
         logger.Error("Error occurred when doing snapshot, info: {0}.", e);
     }
 }
Ejemplo n.º 3
0
        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);
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// For all Hyper-V guests it enumerate all associated paths using VSS data
        /// </summary>
        /// <returns>A collection of VMs and paths</returns>
        private Dictionary <string, List <string> > GetAllVMsPathsVSS()
        {
            IVssBackupComponents m_backup = null;
            var ret = new Dictionary <string, List <string> >();

            try
            {
                //Substitute for calling VssUtils.LoadImplementation(), as we have the dlls outside the GAC
                string             alphadir = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "alphavss");
                string             alphadll = System.IO.Path.Combine(alphadir, VssUtils.GetPlatformSpecificAssemblyShortName() + ".dll");
                IVssImplementation vss      = (IVssImplementation)System.Reflection.Assembly.LoadFile(alphadll).CreateInstance("Alphaleonis.Win32.Vss.VssImplementation");

                m_backup = vss.CreateVssBackupComponents();
                m_backup.InitializeForBackup(null);
                m_backup.SetContext(VssSnapshotContext.Backup);
                m_backup.SetBackupState(false, true, VssBackupType.Full, false);
                m_backup.EnableWriterClasses(new Guid[] { HyperVWriterGuid });

                try
                {
                    m_backup.GatherWriterMetadata();
                    var writerMetaData = m_backup.WriterMetadata.FirstOrDefault(o => o.WriterId.Equals(HyperVWriterGuid));

                    if (writerMetaData == null)
                    {
                        throw new Exception("Microsoft Hyper-V VSS Writer not found - cannot backup Hyper-V machines.");
                    }

                    foreach (var component in writerMetaData.Components)
                    {
                        var paths = new List <string>();

                        foreach (var file in component.Files)
                        {
                            if (file.FileSpecification.Contains("*"))
                            {
                                if (Directory.Exists(Utility.Utility.AppendDirSeparator(file.Path)))
                                {
                                    paths.Add(Utility.Utility.AppendDirSeparator(file.Path));
                                }
                            }
                            else
                            {
                                if (File.Exists(Path.Combine(file.Path, file.FileSpecification)))
                                {
                                    paths.Add(Path.Combine(file.Path, file.FileSpecification));
                                }
                            }
                        }

                        ret.Add(component.ComponentName, paths.Distinct(Utility.Utility.ClientFilenameStringComparer).OrderBy(a => a).ToList());
                    }
                }
                finally
                {
                    m_backup.FreeWriterMetadata();
                }
            }
            finally
            {
                try
                {
                    if (m_backup != null)
                    {
                        m_backup.Dispose();
                    }
                }
                catch { }
            }

            return(ret);
        }