Example #1
0
        public VssWriterDescriptor(IUIHost host, IVssExamineWriterMetadata writerMetadata)
        {
            m_host         = host ?? throw new ArgumentNullException(nameof(host), $"{nameof(host)} is null.");
            WriterMetadata = writerMetadata ?? throw new ArgumentNullException(nameof(writerMetadata), $"{nameof(writerMetadata)} is null.");
            m_components   = new List <VssComponentDescriptor>(writerMetadata.Components.Select(c => new VssComponentDescriptor(host, WriterMetadata.WriterName, c)));

            // Discover top-level components
            for (int i = 0; i < m_components.Count; i++)
            {
                m_components[i].IsTopLevel = true;
                for (int j = 0; j < m_components.Count; j++)
                {
                    if (m_components[j].IsAncestorOf(m_components[i]))
                    {
                        m_components[i].IsTopLevel = false;
                    }
                }
            }
        }
      public VssWriterDescriptor(IUIHost host, IVssExamineWriterMetadata writerMetadata)
      {
         if (writerMetadata == null)
            throw new ArgumentNullException("writerMetadata", "writerMetadata is null.");

         m_host = host;
         WriterMetadata = writerMetadata;
         m_components = new List<VssComponentDescriptor>(writerMetadata.Components.Select(c => new VssComponentDescriptor(host, WriterMetadata.WriterName, c)));         

         // Discover top-level components
         for (int i = 0; i < m_components.Count; i++)
         {
            m_components[i].IsTopLevel = true;
            for (int j = 0; j < m_components.Count; j++)
            {
               if (m_components[j].IsAncestorOf(m_components[i]))
                  m_components[i].IsTopLevel = false;
            }
         }
      }
Example #3
0
            public VssWriterDescriptor(IVssExamineWriterMetadata writerMetadata)
            {
                if (writerMetadata == null)
                {
                    throw new ArgumentNullException("writerMetadata", "writerMetadata is null.");
                }

                WriterMetadata = writerMetadata;
                _components    = new List <VssComponentDescriptor>(writerMetadata.Components.Select(c => new VssComponentDescriptor(WriterMetadata.WriterName, c)));

                for (int i = 0; i < _components.Count; i++)
                {
                    _components[i].IsTopLevel = true;
                    for (int j = 0; j < _components.Count; j++)
                    {
                        if (_components[j].IsAncestorOf(_components[i]))
                        {
                            _components[i].IsTopLevel = false;
                        }
                    }
                }
            }
        private List <string> PrepareHyperVBackup(Dictionary <string, string> options, IVssExamineWriterMetadata writerMetaData)
        {
            var resultPaths       = new List <string>();
            var requestedHyperVMs = new List <string>();

            if (options.ContainsKey("hyperv-backup-vm"))
            {
                requestedHyperVMs = options["hyperv-backup-vm"].Split(';').Where(x => !string.IsNullOrWhiteSpace(x) && x.Trim().Length > 0).ToList();
            }

            if (requestedHyperVMs.Count == 0)
            {
                return(resultPaths);
            }

            Logging.Log.WriteMessage("Starting to gather Hyper-V information.", Logging.LogMessageType.Information);

            var hyperVGuests = new HyperVUtility().GetHyperVGuests();

            Logging.Log.WriteMessage(string.Format("Found {0} virtual machines on Hyper-V.", hyperVGuests.Count), Logging.LogMessageType.Information);

            bool bNotFound  = false;
            var  notFoundVM = new List <string>();

            foreach (var requestedHyperVM in requestedHyperVMs)
            {
                var foundVMs = hyperVGuests.FindAll(x => (string.Equals(requestedHyperVM, x.ID, StringComparison.CurrentCultureIgnoreCase)));

                if (foundVMs.Count != 1)
                {
                    bNotFound = true;
                    notFoundVM.Add(requestedHyperVM);
                    Logging.Log.WriteMessage(string.Format("Cannot find virtual machine with ID {0} on Hyper-V.", requestedHyperVM), Logging.LogMessageType.Error);
                }
                else
                {
                    Logging.Log.WriteMessage(string.Format("Found virtual machine {0} with ID {1} on Hyper-V.", foundVMs[0].Name, requestedHyperVM), Logging.LogMessageType.Information);
                }
            }

            if (bNotFound)
            {
                throw new Exception(string.Format("Cannot find virtual machine with ID {0} on Hyper-V.", string.Join(", ", notFoundVM.ToArray())));
            }

            var productType = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem")
                              .Get().OfType <ManagementObject>()
                              .Select(o => (uint)o.GetPropertyValue("ProductType")).First();

            if (productType != 1) // Hyper-V writer is present only on Server version of Windows
            {
                if (writerMetaData == null)
                {
                    throw new Exception("Microsoft Hyper-V VSS Writer not found - cannot backup Hyper-V machines.");
                }

                foreach (var component in writerMetaData.Components)
                {
                    if (requestedHyperVMs.Contains(component.ComponentName, StringComparer.CurrentCultureIgnoreCase))
                    {
                        foreach (var file in component.Files)
                        {
                            if (file.FileSpecification.Contains("*"))
                            {
                                if (Directory.Exists(Utility.Utility.AppendDirSeparator(file.Path)))
                                {
                                    resultPaths.Add(Utility.Utility.AppendDirSeparator(file.Path));
                                    Logging.Log.WriteMessage(string.Format("For VM {0} - adding {1}.", component.ComponentName, Utility.Utility.AppendDirSeparator(file.Path)), Logging.LogMessageType.Profiling);
                                }
                            }
                            else
                            {
                                if (File.Exists(Path.Combine(file.Path, file.FileSpecification)))
                                {
                                    resultPaths.Add(Path.Combine(file.Path, file.FileSpecification));
                                    Logging.Log.WriteMessage(string.Format("For VM {0} - adding {1}.", component.ComponentName, Path.Combine(file.Path, file.FileSpecification)), Logging.LogMessageType.Profiling);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                Logging.Log.WriteMessage("This is client version of Windows. Hyper-V VSS writer is present only on Server version. Backup will continue, but will be crash consistent only in opposite to application consistent in Server version.", Logging.LogMessageType.Warning);

                foreach (var hyperVGuest in hyperVGuests)
                {
                    if (requestedHyperVMs.Contains(hyperVGuest.ID, StringComparer.CurrentCultureIgnoreCase))
                    {
                        foreach (var path in hyperVGuest.DataPaths)
                        {
                            resultPaths.Add(path);
                            Logging.Log.WriteMessage(string.Format("For VM {0} - adding {1}.", hyperVGuest.ID, path), Logging.LogMessageType.Profiling);
                        }
                    }
                }
            }

            return(resultPaths.Distinct(Utility.Utility.ClientFilenameStringComparer).OrderBy(a => a).ToList());
        }