/// <summary> /// Constructs a new backup snapshot, using all the required disks /// </summary> /// <param name="sourcepaths">The folders that are about to be backed up</param> /// <param name="options">A set of commandline options</param> public WindowsSnapshot(string[] sourcepaths, Dictionary <string, string> options) { 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"); List <Guid> excludedWriters = new List <Guid>(); if (options.ContainsKey("vss-exclude-writers")) { foreach (string s in options["vss-exclude-writers"].Split(';')) { if (!string.IsNullOrEmpty(s) && s.Trim().Length > 0) { excludedWriters.Add(new Guid(s)); } } } //Check if we should map any drives bool useSubst = Utility.Utility.ParseBoolOption(options, "vss-use-mapping"); //Prepare the backup m_backup = vss.CreateVssBackupComponents(); m_backup.InitializeForBackup(null); if (excludedWriters.Count > 0) { m_backup.DisableWriterClasses(excludedWriters.ToArray()); } m_backup.StartSnapshotSet(); m_sourcepaths = new string[sourcepaths.Length]; for (int i = 0; i < m_sourcepaths.Length; i++) { m_sourcepaths[i] = System.IO.Directory.Exists(sourcepaths[i]) ? Utility.Utility.AppendDirSeparator(sourcepaths[i]) : sourcepaths[i]; } try { //Gather information on all Vss writers m_backup.GatherWriterMetadata(); m_backup.FreeWriterMetadata(); } catch { try { m_backup.FreeWriterMetadata(); } catch { } throw; } //Figure out which volumes are in the set m_volumes = new Dictionary <string, Guid>(StringComparer.InvariantCultureIgnoreCase); foreach (string s in m_sourcepaths) { string drive = Alphaleonis.Win32.Filesystem.Path.GetPathRoot(s); if (!m_volumes.ContainsKey(drive)) { if (!m_backup.IsVolumeSupported(drive)) { throw new VssVolumeNotSupportedException(drive); } m_volumes.Add(drive, m_backup.AddToSnapshotSet(drive)); } } //Signal that we want to do a backup m_backup.SetBackupState(false, true, VssBackupType.Full, false); //Make all writers aware that we are going to do the backup m_backup.PrepareForBackup(); //Create the shadow volumes m_backup.DoSnapshotSet(); //Make a little lookup table for faster translation m_volumeMap = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase); foreach (KeyValuePair <string, Guid> kvp in m_volumes) { m_volumeMap.Add(kvp.Key, m_backup.GetSnapshotProperties(kvp.Value).SnapshotDeviceObject); } //If we should map the drives, we do that now and update the volumeMap if (useSubst) { m_mappedDrives = new List <DefineDosDevice>(); foreach (string k in new List <string>(m_volumeMap.Keys)) { try { DefineDosDevice d; m_mappedDrives.Add(d = new DefineDosDevice(m_volumeMap[k])); m_volumeMap[k] = Utility.Utility.AppendDirSeparator(d.Drive); } catch { } } } } catch { //In case we fail in the constructor, we do not want a snapshot to be active try { Dispose(); } catch { } throw; } }
/// <summary> /// Constructs a new backup snapshot, using all the required disks /// </summary> /// <param name="sources">Sources to determine which volumes to include in snapshot</param> /// <param name="options">A set of commandline options</param> public WindowsSnapshot(IEnumerable <string> sources, IDictionary <string, string> options) { try { // Substitute for calling VssUtils.LoadImplementation(), as we have the dlls outside the GAC var assemblyLocation = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); if (assemblyLocation == null) { throw new InvalidOperationException(); } var alphadir = Path.Combine(assemblyLocation, "alphavss"); var alphadll = Path.Combine(alphadir, VssUtils.GetPlatformSpecificAssemblyShortName() + ".dll"); var vss = (IVssImplementation)System.Reflection.Assembly.LoadFile(alphadll).CreateInstance("Alphaleonis.Win32.Vss.VssImplementation"); if (vss == null) { throw new InvalidOperationException(); } var excludedWriters = new Guid[0]; if (options.ContainsKey("vss-exclude-writers")) { excludedWriters = options["vss-exclude-writers"].Split(';').Where(x => !string.IsNullOrWhiteSpace(x) && x.Trim().Length > 0).Select(x => new Guid(x)).ToArray(); } //Check if we should map any drives var useSubst = Utility.Utility.ParseBoolOption(options, "vss-use-mapping"); //Prepare the backup m_backup = vss.CreateVssBackupComponents(); m_backup.InitializeForBackup(null); m_backup.SetContext(VssSnapshotContext.Backup); m_backup.SetBackupState(false, true, VssBackupType.Full, false); if (excludedWriters.Length > 0) { m_backup.DisableWriterClasses(excludedWriters.ToArray()); } try { m_backup.GatherWriterMetadata(); } finally { m_backup.FreeWriterMetadata(); } m_backup.StartSnapshotSet(); //Figure out which volumes are in the set m_volumes = new Dictionary <string, Guid>(StringComparer.OrdinalIgnoreCase); foreach (var s in sources) { var drive = AlphaFS.Path.GetPathRoot(s); if (!m_volumes.ContainsKey(drive)) { //TODO: that seems a bit harsh... we could fall-back to not using VSS for that volume only if (!m_backup.IsVolumeSupported(drive)) { throw new VssVolumeNotSupportedException(drive); } m_volumes.Add(drive, m_backup.AddToSnapshotSet(drive)); } } //Make all writers aware that we are going to do the backup m_backup.PrepareForBackup(); //Create the shadow volumes m_backup.DoSnapshotSet(); //Make a little lookup table for faster translation m_volumeMap = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); foreach (var kvp in m_volumes) { m_volumeMap.Add(kvp.Key, m_backup.GetSnapshotProperties(kvp.Value).SnapshotDeviceObject); } m_volumeReverseMap = m_volumeMap.ToDictionary(x => x.Value, x => x.Key); //If we should map the drives, we do that now and update the volumeMap if (useSubst) { m_mappedDrives = new List <DefineDosDevice>(); foreach (var k in new List <string>(m_volumeMap.Keys)) { try { DefineDosDevice d; m_mappedDrives.Add(d = new DefineDosDevice(m_volumeMap[k])); m_volumeMap[k] = Utility.Utility.AppendDirSeparator(d.Drive); } catch (Exception ex) { Logging.Log.WriteVerboseMessage(LOGTAG, "SubstMappingfailed", ex, "Failed to map VSS path {0} to drive", k); } } } } catch { //In case we fail in the constructor, we do not want a snapshot to be active try { Dispose(); } catch (Exception ex) { Logging.Log.WriteVerboseMessage(LOGTAG, "VSSCleanupOnError", ex, "Failed during VSS error cleanup"); } throw; } }
/// <summary> /// Constructs a new backup snapshot, using all the required disks /// </summary> /// <param name="sourcepaths">The folders that are about to be backed up</param> /// <param name="options">A set of commandline options</param> public WindowsSnapshot(string[] sourcepaths, Dictionary <string, string> options) { 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"); var excludedWriters = new Guid[0]; if (options.ContainsKey("vss-exclude-writers")) { excludedWriters = options["vss-exclude-writers"].Split(';').Where(x => !string.IsNullOrWhiteSpace(x) && x.Trim().Length > 0).Select(x => new Guid(x)).ToArray(); } //Check if we should map any drives bool useSubst = Utility.Utility.ParseBoolOption(options, "vss-use-mapping"); //Prepare the backup m_backup = vss.CreateVssBackupComponents(); m_backup.InitializeForBackup(null); m_backup.SetContext(VssSnapshotContext.Backup); m_backup.SetBackupState(false, true, VssBackupType.Full, false); if (excludedWriters.Length > 0) { m_backup.DisableWriterClasses(excludedWriters.ToArray()); } m_sourcepaths = sourcepaths.Select(x => Directory.Exists(x) ? Utility.Utility.AppendDirSeparator(x) : x).ToList(); List <string> hypervPaths; try { m_backup.GatherWriterMetadata(); hypervPaths = PrepareHyperVBackup(options, m_backup.WriterMetadata.FirstOrDefault(o => o.WriterId.Equals(HyperVWriterGuid))); } finally { m_backup.FreeWriterMetadata(); } if (hypervPaths != null) { m_sourcepaths.AddRange(hypervPaths); } //Sanity check for duplicate files/folders var pathDuplicates = m_sourcepaths.GroupBy(x => x, Utility.Utility.ClientFilenameStringComparer) .Where(g => g.Count() > 1).Select(y => y.Key).ToList(); foreach (var pathDuplicate in pathDuplicates) { Logging.Log.WriteMessage(string.Format("Removing duplicate source: {0}", pathDuplicate), Logging.LogMessageType.Information); } if (pathDuplicates.Count > 0) { m_sourcepaths = m_sourcepaths.Distinct(Utility.Utility.ClientFilenameStringComparer).OrderBy(a => a).ToList(); } //Sanity check for multiple inclusions of the same files/folders var pathIncludedPaths = m_sourcepaths.Where(x => m_sourcepaths.Where(y => y != x).Any(z => x.StartsWith(z, Utility.Utility.ClientFilenameStringComparision))).ToList(); foreach (var pathIncluded in pathIncludedPaths) { Logging.Log.WriteMessage(string.Format("Removing already included source: {0}", pathIncluded), Logging.LogMessageType.Information); } if (pathIncludedPaths.Count > 0) { m_sourcepaths = m_sourcepaths.Except(pathIncludedPaths, Utility.Utility.ClientFilenameStringComparer).ToList(); } m_backup.StartSnapshotSet(); //Figure out which volumes are in the set m_volumes = new Dictionary <string, Guid>(StringComparer.InvariantCultureIgnoreCase); foreach (string s in m_sourcepaths) { string drive = Alphaleonis.Win32.Filesystem.Path.GetPathRoot(s); if (!m_volumes.ContainsKey(drive)) { if (!m_backup.IsVolumeSupported(drive)) { throw new VssVolumeNotSupportedException(drive); } m_volumes.Add(drive, m_backup.AddToSnapshotSet(drive)); } } //Make all writers aware that we are going to do the backup m_backup.PrepareForBackup(); //Create the shadow volumes m_backup.DoSnapshotSet(); //Make a little lookup table for faster translation m_volumeMap = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase); foreach (KeyValuePair <string, Guid> kvp in m_volumes) { m_volumeMap.Add(kvp.Key, m_backup.GetSnapshotProperties(kvp.Value).SnapshotDeviceObject); } //If we should map the drives, we do that now and update the volumeMap if (useSubst) { m_mappedDrives = new List <DefineDosDevice>(); foreach (string k in new List <string>(m_volumeMap.Keys)) { try { DefineDosDevice d; m_mappedDrives.Add(d = new DefineDosDevice(m_volumeMap[k])); m_volumeMap[k] = Utility.Utility.AppendDirSeparator(d.Drive); } catch { } } } } catch { //In case we fail in the constructor, we do not want a snapshot to be active try { Dispose(); } catch { } throw; } }
/// <summary> /// Constructs a new backup snapshot, using all the required disks /// </summary> /// <param name="sourcepaths">The folders that are about to be backed up</param> /// <param name="options">A set of commandline options</param> public WindowsSnapshot(string[] sourcepaths, Dictionary<string, string> options) { 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.GetPlatformSpecificAssemblyName().Name + ".dll"); IVssImplementation vss = (IVssImplementation)System.Reflection.Assembly.LoadFile(alphadll).CreateInstance("Alphaleonis.Win32.Vss.VssImplementation"); List<Guid> excludedWriters = new List<Guid>(); if (options.ContainsKey("vss-exclude-writers")) { foreach (string s in options["vss-exclude-writers"].Split(';')) if (!string.IsNullOrEmpty(s) && s.Trim().Length > 0) excludedWriters.Add(new Guid(s)); } //Check if we should map any drives bool useSubst = Utility.Utility.ParseBoolOption(options, "vss-use-mapping"); //Prepare the backup m_backup = vss.CreateVssBackupComponents(); m_backup.InitializeForBackup(null); if (excludedWriters.Count > 0) m_backup.DisableWriterClasses(excludedWriters.ToArray()); m_snapshotId = m_backup.StartSnapshotSet(); m_sourcepaths = new string[sourcepaths.Length]; for(int i = 0; i < m_sourcepaths.Length; i++) m_sourcepaths[i] = Utility.Utility.AppendDirSeparator(sourcepaths[i]); try { //Gather information on all Vss writers using (IVssAsync async = m_backup.GatherWriterMetadata()) async.Wait(); m_backup.FreeWriterMetadata(); } catch { try { m_backup.FreeWriterMetadata(); } catch { } throw; } //Figure out which volumes are in the set m_volumes = new Dictionary<string, Guid>(StringComparer.InvariantCultureIgnoreCase); foreach (string s in m_sourcepaths) { string drive = Alphaleonis.Win32.Filesystem.Path.GetPathRoot(s); if (!m_volumes.ContainsKey(drive)) { if (!m_backup.IsVolumeSupported(drive)) throw new VssVolumeNotSupportedException(drive); m_volumes.Add(drive, m_backup.AddToSnapshotSet(drive)); } } //Signal that we want to do a backup m_backup.SetBackupState(false, true, VssBackupType.Full, false); //Make all writers aware that we are going to do the backup using (IVssAsync async = m_backup.PrepareForBackup()) async.Wait(); //Create the shadow volumes using (IVssAsync async = m_backup.DoSnapshotSet()) async.Wait(); //Make a little lookup table for faster translation m_volumeMap = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase); foreach(KeyValuePair<string, Guid> kvp in m_volumes) m_volumeMap.Add(kvp.Key, m_backup.GetSnapshotProperties(kvp.Value).SnapshotDeviceObject); //If we should map the drives, we do that now and update the volumeMap if (useSubst) { m_mappedDrives = new List<DefineDosDevice>(); foreach (string k in new List<string>(m_volumeMap.Keys)) { try { DefineDosDevice d; m_mappedDrives.Add(d = new DefineDosDevice(m_volumeMap[k])); m_volumeMap[k] = Utility.Utility.AppendDirSeparator(d.Drive); } catch { } } } } catch { //In case we fail in the constructor, we do not want a snapshot to be active try { Dispose(); } catch { } throw; } }