private void Report7ZipProgress(IVssWMComponent component, IDictionary <string, string> volumeMap, BackupProgressEventArgs ebp) { if (ebp == null) { return; } ebp.Components = new Dictionary <string, string> { { component.ComponentName, component.Caption } }; ebp.VolumeMap = new Dictionary <string, string>(); foreach (var volume in volumeMap) { ebp.VolumeMap.Add(volume.Key, volume.Value); } BackupProgress(this, ebp); if (ebp.Cancel) { _cancel = true; } }
private void RaiseEvent(EventAction action, IList <IVssWMComponent> components, IDictionary <string, string> volumeMap) { if (BackupProgress != null) { var ebp = new BackupProgressEventArgs() { Action = action }; if (components != null) { ebp.Components = new Dictionary <string, string>(); foreach (var component in components) { ebp.Components.Add(component.ComponentName, component.Caption); } } if (volumeMap != null) { ebp.VolumeMap = new Dictionary <string, string>(); foreach (var volume in volumeMap) { ebp.VolumeMap.Add(volume); } } BackupProgress(this, ebp); if (ebp.Cancel) { throw new BackupCancelledException(); } } }
private void ReportZipProgress(IVssWMComponent component, IDictionary <string, string> volumeMap, ZipProgressEventArgs e) { BackupProgressEventArgs ebp = null; if (e.EventType == ZipProgressEventType.Saving_Started) { ebp = new BackupProgressEventArgs() { AcrhiveFileName = e.ArchiveName, Action = EventAction.StartingArchive }; } else if (e.EventType == ZipProgressEventType.Saving_BeforeWriteEntry || e.EventType == ZipProgressEventType.Saving_EntryBytesRead) { var action = e.EventType == ZipProgressEventType.Saving_BeforeWriteEntry ? EventAction.StartingEntry : EventAction.SavingEntry; ebp = new BackupProgressEventArgs() { AcrhiveFileName = e.ArchiveName, Action = action, CurrentEntry = e.CurrentEntry.FileName, EntriesTotal = e.EntriesTotal, TotalBytesToTransfer = e.TotalBytesToTransfer, BytesTransferred = e.BytesTransferred }; } else if (e.EventType == ZipProgressEventType.Saving_Completed) { ebp = new BackupProgressEventArgs() { AcrhiveFileName = e.ArchiveName, Action = EventAction.ArchiveDone }; } if (ebp != null) { ebp.Components = new Dictionary <string, string> { { component.ComponentName, component.Caption } }; ebp.VolumeMap = new Dictionary <string, string>(); foreach (var volume in volumeMap) { ebp.VolumeMap.Add(volume.Key, volume.Value); } BackupProgress(this, ebp); // Close the zip file operation neatly and throw the exception afterwards e.Cancel = ebp.Cancel; } }
private void Report7ZipProgress(IVssWMComponent component, IDictionary<string, string> volumeMap, BackupProgressEventArgs ebp) { if (ebp == null) return; ebp.Components = new Dictionary<string, string>(); ebp.Components.Add(component.ComponentName, component.Caption); ebp.VolumeMap = new Dictionary<string, string>(); foreach (var volume in volumeMap) ebp.VolumeMap.Add(volume.Key, volume.Value); BackupProgress(this, ebp); if (ebp.Cancel) { cancel = true; } }
private void RaiseEvent(EventAction action, IList<IVssWMComponent> components, IDictionary<string, string> volumeMap) { if (BackupProgress != null) { var ebp = new BackupProgressEventArgs() { Action = action }; if (components != null) { ebp.Components = new Dictionary<string, string>(); foreach (var component in components) ebp.Components.Add(component.ComponentName, component.Caption); } if (volumeMap != null) { ebp.VolumeMap = new Dictionary<string, string>(); foreach (var volume in volumeMap) ebp.VolumeMap.Add(volume); } BackupProgress(this, ebp); if (ebp.Cancel) throw new BackupCancelledException(); } }
private void BackupFiles(IList<IVssWMComponent> components, IDictionary<string, string> volumeMap, IDictionary<string, string> snapshotVolumeMap, IDictionary<string, string> vmNamesMap, Options options) { IList<System.IO.Stream> streams = new List<System.IO.Stream>(); try { foreach (var component in components) { string vmBackupPath = Path.Combine(options.Output, string.Format(options.OutputFormat, vmNamesMap[component.ComponentName], component.ComponentName, DateTime.Now, "7z")); File.Delete(vmBackupPath); var files = new Dictionary<string, System.IO.Stream>(); foreach (var file in component.Files) { string path; if (file.IsRecursive) path = file.Path; else path = Path.Combine(file.Path, file.FileSpecification); // Get the longest matching path var volumePath = volumeMap.Keys.OrderBy((o) => o.Length).Reverse().Where((o) => path.StartsWith(o, StringComparison.OrdinalIgnoreCase)).First(); var volumeName = volumeMap[volumePath]; var fileName = Path.GetFileName(path.Substring(volumePath.Length)).ToUpperInvariant(); var include = Path.GetExtension(fileName).ToLowerInvariant() != ".avhdx" && Path.GetExtension(fileName).ToLowerInvariant() != ".vmrs"; if (include && options.VhdInclude != null) { if (options.VhdInclude.Count(x => string.CompareOrdinal(x.ToUpperInvariant(), fileName) == 0) == 0) include = false; } if (include && options.VhdIgnore != null) { if (options.VhdIgnore.Count(x => string.CompareOrdinal(x.ToUpperInvariant(), fileName) == 0) != 0) include = false; } if (include) AddPathToSevenZip(files, streams, snapshotVolumeMap[volumeName], volumePath.Length, path); } SevenZipExtractor.SetLibraryPath(Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "7z.dll")); var sevenZip = new SevenZipCompressor(); sevenZip.ArchiveFormat = OutArchiveFormat.SevenZip; sevenZip.CompressionMode = CompressionMode.Create; sevenZip.DirectoryStructure = true; sevenZip.PreserveDirectoryRoot = false; sevenZip.CustomParameters.Add("mt", "on"); switch (options.CompressionLevel) { case 0: sevenZip.CompressionLevel = CompressionLevel.None; break; case 1: sevenZip.CompressionLevel = CompressionLevel.Fast; break; case 2: sevenZip.CompressionLevel = CompressionLevel.Fast; break; case 3: sevenZip.CompressionLevel = CompressionLevel.Low; break; case 4: sevenZip.CompressionLevel = CompressionLevel.Low; break; case 5: sevenZip.CompressionLevel = CompressionLevel.Low; break; case 6: sevenZip.CompressionLevel = CompressionLevel.Normal; break; case 7: sevenZip.CompressionLevel = CompressionLevel.High; break; case 8: sevenZip.CompressionLevel = CompressionLevel.High; break; case 9: sevenZip.CompressionLevel = CompressionLevel.Ultra; break; } if (BackupProgress != null) { sevenZip.FileCompressionStarted += (sender, e) => { var ebp = new BackupProgressEventArgs() { AcrhiveFileName = e.FileName, Action = EventAction.StartingArchive }; sevenZipCurrentFile = e.FileName; Report7ZipProgress(component, volumeMap, ebp); if (cancel) { e.Cancel = true; } }; sevenZip.FileCompressionFinished += (sender, e) => { var ebp = new BackupProgressEventArgs() { AcrhiveFileName = sevenZipCurrentFile, Action = EventAction.ArchiveDone }; sevenZipCurrentFile = String.Empty; Report7ZipProgress(component, volumeMap, ebp); }; sevenZip.Compressing += (sender, e) => { var ebp = new BackupProgressEventArgs() { AcrhiveFileName = sevenZipCurrentFile, Action = EventAction.PercentProgress, CurrentEntry = sevenZipCurrentFile, PercentDone = e.PercentDone }; Report7ZipProgress(component, volumeMap, ebp); if (cancel) { e.Cancel = true; } }; } if (string.IsNullOrEmpty(options.Password)) sevenZip.CompressStreamDictionary(files, vmBackupPath); else sevenZip.CompressStreamDictionary(files, vmBackupPath, options.Password); if (cancel) { if (File.Exists(vmBackupPath)) { File.Delete(vmBackupPath); } throw new BackupCancelledException(); } } } finally { // Make sure that all streams are closed foreach (var s in streams) s.Close(); } }
private void BackupFiles(IList <IVssWMComponent> components, IDictionary <string, string> volumeMap, IDictionary <string, string> snapshotVolumeMap, IDictionary <string, string> vmNamesMap, Options options, ILogger logger) { var streams = new List <Stream>(); try { foreach (var component in components) { string vmBackupPath; if (options.DirectCopy) { vmBackupPath = Path.Combine(options.Output, string.Format(options.OutputFormat, vmNamesMap[component.ComponentName], component.ComponentName, DateTime.Now, "")); } else { vmBackupPath = Path.Combine(options.Output, string.Format(options.OutputFormat, vmNamesMap[component.ComponentName], component.ComponentName, DateTime.Now, options.ZipFormat ? ".zip" : ".7z")); File.Delete(vmBackupPath); } var files = new Dictionary <string, Stream>(); foreach (var file in component.Files) { string path; if (file.IsRecursive) { path = file.Path; } else { path = Path.Combine(file.Path, file.FileSpecification); } // Get the longest matching path var volumePath = volumeMap.Keys.OrderBy(o => o.Length).Reverse() .First(o => path.StartsWith(o, StringComparison.OrdinalIgnoreCase)); var volumeName = volumeMap[volumePath]; // Exclude snapshots var fileName = Path.GetFileName(path.Substring(volumePath.Length)).ToUpperInvariant(); var include = !path.EndsWith("\\*"); var pathItems = path.Split(Path.DirectorySeparatorChar); if (pathItems.Length >= 2) { if (pathItems[pathItems.Length - 2].ToLowerInvariant() == "snapshots") { include = false; } } if (include && options.VhdInclude != null) { if (options.VhdInclude.Count( x => string.CompareOrdinal(x.ToUpperInvariant(), fileName) == 0) == 0) { include = false; } } if (include && options.VhdIgnore != null) { if (options.VhdIgnore.Count( x => string.CompareOrdinal(x.ToUpperInvariant(), fileName) == 0) != 0) { include = false; } } if (include) { if (options.DirectCopy) { DoDirectCopy(vmBackupPath, snapshotVolumeMap[volumeName], volumePath.Length, path); } else { AddPathToCompressionList(files, streams, snapshotVolumeMap[volumeName], volumePath.Length, path); } } else { var errorText = $"Ignoring file {path}"; logger.Info(errorText); Console.WriteLine(errorText); } } if (!options.DirectCopy) { logger.Debug($"Start compression. File: {vmBackupPath}"); if (options.ZipFormat) { if (options.CompressionLevel == -1) { options.CompressionLevel = 6; } using (var zf = new ZipFile(vmBackupPath)) { zf.ParallelDeflateThreshold = -1; zf.UseZip64WhenSaving = Zip64Option.Always; zf.Encryption = EncryptionAlgorithm.WinZipAes256; switch (options.CompressionLevel) { case 0: zf.CompressionLevel = Ionic.Zlib.CompressionLevel.Level0; break; case 1: zf.CompressionLevel = Ionic.Zlib.CompressionLevel.Level1; break; case 2: zf.CompressionLevel = Ionic.Zlib.CompressionLevel.Level2; break; case 3: zf.CompressionLevel = Ionic.Zlib.CompressionLevel.Level3; break; case 4: zf.CompressionLevel = Ionic.Zlib.CompressionLevel.Level4; break; case 5: zf.CompressionLevel = Ionic.Zlib.CompressionLevel.Level5; break; case 6: zf.CompressionLevel = Ionic.Zlib.CompressionLevel.Level6; break; case 7: zf.CompressionLevel = Ionic.Zlib.CompressionLevel.Level7; break; case 8: zf.CompressionLevel = Ionic.Zlib.CompressionLevel.Level8; break; case 9: zf.CompressionLevel = Ionic.Zlib.CompressionLevel.Level9; break; } if (BackupProgress != null) { zf.SaveProgress += (sender, e) => ReportZipProgress(component, volumeMap, e); } if (!string.IsNullOrEmpty(options.Password)) { zf.Password = options.Password; } foreach (var file in files) { logger.Debug($"Adding file: {file.Key}"); zf.AddEntry(file.Key, file.Value); } zf.Save(); } } else { if (options.CompressionLevel == -1) { options.CompressionLevel = 3; } SevenZipBase.SetLibraryPath( Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "7z.dll")); var sevenZip = new SevenZipCompressor { ArchiveFormat = OutArchiveFormat.SevenZip, CompressionMode = CompressionMode.Create, DirectoryStructure = true, PreserveDirectoryRoot = false }; if (options.MultiThreaded) { sevenZip.CustomParameters.Add("mt", "on"); } sevenZip.CustomParameters.Add("d", "24"); switch (options.CompressionLevel) { case 0: sevenZip.CompressionLevel = CompressionLevel.None; break; case 1: case 2: sevenZip.CompressionLevel = CompressionLevel.Fast; break; case 3: case 4: case 5: sevenZip.CompressionLevel = CompressionLevel.Low; break; case 6: sevenZip.CompressionLevel = CompressionLevel.Normal; break; case 7: case 8: sevenZip.CompressionLevel = CompressionLevel.High; break; case 9: sevenZip.CompressionLevel = CompressionLevel.Ultra; break; } if (BackupProgress != null) { sevenZip.FileCompressionStarted += (sender, e) => { var ebp = new BackupProgressEventArgs { AcrhiveFileName = e.FileName, Action = EventAction.StartingArchive }; _currentFile = e.FileName; Report7ZipProgress(component, volumeMap, ebp); if (_cancel) { e.Cancel = true; } }; sevenZip.FileCompressionFinished += (sender, e) => { var ebp = new BackupProgressEventArgs { AcrhiveFileName = _currentFile, Action = EventAction.ArchiveDone }; _currentFile = string.Empty; Report7ZipProgress(component, volumeMap, ebp); }; sevenZip.Compressing += (sender, e) => { var ebp = new BackupProgressEventArgs { AcrhiveFileName = _currentFile, Action = EventAction.PercentProgress, CurrentEntry = _currentFile, PercentDone = e.PercentDone }; Report7ZipProgress(component, volumeMap, ebp); }; } if (string.IsNullOrEmpty(options.Password)) { sevenZip.CompressStreamDictionary(files, vmBackupPath); } else { sevenZip.CompressStreamDictionary(files, vmBackupPath, options.Password); } } logger.Debug("Compression finished"); if (_cancel) { if (File.Exists(vmBackupPath)) { File.Delete(vmBackupPath); } throw new BackupCancelledException(); } } } } finally { // Make sure that all streams are closed foreach (var s in streams) { s.Close(); } } }
private void BackupFiles(IList <IVssWMComponent> components, IDictionary <string, string> volumeMap, IDictionary <string, string> snapshotVolumeMap, IDictionary <string, string> vmNamesMap, Options options) { IList <System.IO.Stream> streams = new List <System.IO.Stream>(); try { foreach (var component in components) { string vmBackupPath = Path.Combine(options.Output, string.Format(options.OutputFormat, vmNamesMap[component.ComponentName], component.ComponentName, DateTime.Now, "7z")); File.Delete(vmBackupPath); var files = new Dictionary <string, System.IO.Stream>(); foreach (var file in component.Files) { string path; if (file.IsRecursive) { path = file.Path; } else { path = Path.Combine(file.Path, file.FileSpecification); } // Get the longest matching path var volumePath = volumeMap.Keys.OrderBy((o) => o.Length).Reverse().Where((o) => path.StartsWith(o, StringComparison.OrdinalIgnoreCase)).First(); var volumeName = volumeMap[volumePath]; // Exclude snapshots var fileName = Path.GetFileName(path.Substring(volumePath.Length)).ToUpperInvariant(); var include = !path.EndsWith("\\*"); var pathItems = path.Split(Path.DirectorySeparatorChar); if (pathItems.Length >= 2) { if (pathItems[pathItems.Length - 2].ToLowerInvariant() == "snapshots") { include = false; } } if (include && options.VhdInclude != null) { if (options.VhdInclude.Count(x => string.CompareOrdinal(x.ToUpperInvariant(), fileName) == 0) == 0) { include = false; } } if (include && options.VhdIgnore != null) { if (options.VhdIgnore.Count(x => string.CompareOrdinal(x.ToUpperInvariant(), fileName) == 0) != 0) { include = false; } } if (include) { AddPathToSevenZip(files, streams, snapshotVolumeMap[volumeName], volumePath.Length, path); } else { Console.WriteLine("Ignoring file {0}", path); } } SevenZipExtractor.SetLibraryPath(Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "7z.dll")); var sevenZip = new SevenZipCompressor(); sevenZip.ArchiveFormat = OutArchiveFormat.SevenZip; sevenZip.CompressionMode = CompressionMode.Create; sevenZip.DirectoryStructure = true; sevenZip.PreserveDirectoryRoot = false; sevenZip.CustomParameters.Add("mt", "on"); switch (options.CompressionLevel) { case 0: sevenZip.CompressionLevel = CompressionLevel.None; break; case 1: sevenZip.CompressionLevel = CompressionLevel.Fast; break; case 2: sevenZip.CompressionLevel = CompressionLevel.Fast; break; case 3: sevenZip.CompressionLevel = CompressionLevel.Low; break; case 4: sevenZip.CompressionLevel = CompressionLevel.Low; break; case 5: sevenZip.CompressionLevel = CompressionLevel.Low; break; case 6: sevenZip.CompressionLevel = CompressionLevel.Normal; break; case 7: sevenZip.CompressionLevel = CompressionLevel.High; break; case 8: sevenZip.CompressionLevel = CompressionLevel.High; break; case 9: sevenZip.CompressionLevel = CompressionLevel.Ultra; break; } if (BackupProgress != null) { sevenZip.FileCompressionStarted += (sender, e) => { var ebp = new BackupProgressEventArgs() { AcrhiveFileName = e.FileName, Action = EventAction.StartingArchive }; sevenZipCurrentFile = e.FileName; Report7ZipProgress(component, volumeMap, ebp); if (cancel) { e.Cancel = true; } }; sevenZip.FileCompressionFinished += (sender, e) => { var ebp = new BackupProgressEventArgs() { AcrhiveFileName = sevenZipCurrentFile, Action = EventAction.ArchiveDone }; sevenZipCurrentFile = String.Empty; Report7ZipProgress(component, volumeMap, ebp); }; sevenZip.Compressing += (sender, e) => { var ebp = new BackupProgressEventArgs() { AcrhiveFileName = sevenZipCurrentFile, Action = EventAction.PercentProgress, CurrentEntry = sevenZipCurrentFile, PercentDone = e.PercentDone }; Report7ZipProgress(component, volumeMap, ebp); if (cancel) { e.Cancel = true; } }; } if (string.IsNullOrEmpty(options.Password)) { sevenZip.CompressStreamDictionary(files, vmBackupPath); } else { sevenZip.CompressStreamDictionary(files, vmBackupPath, options.Password); } if (cancel) { if (File.Exists(vmBackupPath)) { File.Delete(vmBackupPath); } throw new BackupCancelledException(); } } } finally { // Make sure that all streams are closed foreach (var s in streams) { s.Close(); } } }
static void MgrBackupProgress(object sender, BackupProgressEventArgs e) { switch (e.Action) { case EventAction.InitializingVSS: System.Console.WriteLine("Initializing VSS"); break; case EventAction.StartingSnaphotSet: System.Console.WriteLine(); System.Console.WriteLine("Starting snapshot set for:"); foreach (var componentName in e.Components.Values) System.Console.WriteLine(componentName); System.Console.WriteLine(); System.Console.WriteLine("Volumes:"); foreach (var volumePath in e.VolumeMap.Keys) System.Console.WriteLine(volumePath); break; case EventAction.DeletingSnapshotSet: System.Console.WriteLine("Deleting snapshot set"); break; case EventAction.StartingArchive: System.Console.WriteLine(); foreach (var componentName in e.Components.Values) System.Console.WriteLine(string.Format("Component: \"{0}\"", componentName)); System.Console.WriteLine(string.Format("Archive: \"{0}\"", e.AcrhiveFileName)); break; case EventAction.StartingEntry: System.Console.WriteLine(string.Format("Entry: \"{0}\"", e.CurrentEntry)); currentWidth = 0; break; case EventAction.SavingEntry: if (e.TotalBytesToTransfer > 0) { int width = (int)Math.Round(e.BytesTransferred * consoleWidth / (decimal)e.TotalBytesToTransfer); for (int i = 0; i < width - currentWidth; i++) System.Console.Write("."); currentWidth = width; if (e.BytesTransferred == e.TotalBytesToTransfer) System.Console.WriteLine(); //Console.WriteLine(string.Format("{0:0.#}%", e.BytesTransferred * 100 / (decimal)e.TotalBytesToTransfer)); } break; case EventAction.PercentProgress: int progressWidth = e.PercentDone * consoleWidth / 100; for (int i = 0; i < progressWidth - currentWidth; i++) System.Console.Write("."); currentWidth = progressWidth; if (e.PercentDone == 100) System.Console.WriteLine(); break; } e.Cancel = cancel; }