public void ScanDirectory(DirectoryScanProgressMonitor monitor = null) { var scannedData = this.ScanDirectory(this.ScannedDirectory, monitor); monitor?.FinishScan(); this.ScannedData = scannedData; }
private DirectoryEntry ScanDirectory(string scannedDirectory, DirectoryScanProgressMonitor monitor = null) { var dirInfo = new DirectoryInfo(scannedDirectory); if (dirInfo.Parent != null && dirInfo.Attributes.HasFlag(FileAttributes.System)) { return(null); } var dirEntry = new DirectoryEntry(); dirEntry.Name = dirInfo.Name; string[] files = Directory.GetFiles(scannedDirectory); string[] directories = Directory.GetDirectories(scannedDirectory); foreach (var file in files) { try { var fileEntry = this.ScanFile(file, monitor); if (fileEntry != null) { fileEntry.ParentDirectory = dirEntry; dirEntry.Files.Add(fileEntry.Name, fileEntry); } } catch (Exception e) { monitor?.AddError(file, e, dirEntry); } } monitor?.Descent(directories.Length); foreach (var dir in directories) { try { var childDirectoryEntry = this.ScanDirectory(dir, monitor); if (childDirectoryEntry != null) { childDirectoryEntry.ParentDirectory = dirEntry; dirEntry.Directories.Add(childDirectoryEntry.Name, childDirectoryEntry); } } catch (Exception e) { monitor?.AddError(dir, e, dirEntry); } } monitor?.Ascend(directories.Length); return(dirEntry); }
private FileEntry ScanFile(string scannedFile, DirectoryScanProgressMonitor monitor = null) { var fileInfo = new FileInfo(scannedFile); if (fileInfo.Attributes.HasFlag(FileAttributes.System)) { return(null); } var fileEntry = new FileEntry(); fileEntry.Name = fileInfo.Name; fileEntry.Path = fileInfo.FullName; fileEntry.LastWriteTime = fileInfo.LastWriteTimeUtc; fileEntry.Length = fileInfo.Length; monitor?.ScanFile(fileInfo.FullName); return(fileEntry); }
private void CreateBackup() { this.LocationGroup.Enabled = false; this.ConfigurationGroup.Enabled = false; this.CreateBackupButton.Enabled = false; this.ProgressGroup.Enabled = true; var sourceDirectory = this.BackupSourceInput.Text; var destinationDirectory = this.BackupDestinationInput.Text; var backupProcess = new BackupProcess(sourceDirectory, destinationDirectory); var precedingBackupName = (string)this.PrecedingBackupSelect.SelectedItem; var backupName = DateTime.Now.ToString("yyyy-MM-ddTHH_mm_ssZ"); var scanMonitor = new DirectoryScanProgressMonitor(); var backupMonitor = new BackupProgressMonitor(); scanMonitor.ErrorReceived += (path, error, directory) => this.Log($"Could not scan path \"{path}\": {error.GetType()}"); var timer = new Timer(); timer.Interval = 1; timer.Tick += (a, b) => { this.ScanProgressLabel.Text = scanMonitor.LastScanned; this.ScanProgressBar.Value = (int)(scanMonitor.Progress * this.ScanProgressBar.Maximum); this.BackupProgressLabel.Text = backupMonitor.LastCopied; this.BackupProgressBar.Value = (int)backupMonitor.FilesCopied; }; timer.Start(); new Task(() => { try { this.Log("Scanning source directory..."); this.StatusInformationLabel.Text = "Scanning source directory..."; backupProcess.LoadSourceData(scanMonitor); this.ScanProgressBar.Value = this.ScanProgressBar.Maximum; this.ScanProgressLabel.Text = ""; this.Log($"Found {scanMonitor.FilesScanned} files, {scanMonitor.FoldersScanned} directories"); if (this.FullBackupToggle.Checked) { backupProcess.UseEmptyReferenceData(); } else { this.Log("Loading preceding backup..."); this.StatusInformationLabel.Text = "Loading preceding backup..."; var referenceBackupDataFile = Path.Combine(destinationDirectory, precedingBackupName, BackupProcess.BackupDataFilename); backupProcess.LoadReferenceData(referenceBackupDataFile); } this.Log("Preparing backup data..."); this.StatusInformationLabel.Text = "Preparing backup data..."; backupProcess.ProcessDataDifference(); this.Log($"Detected {backupProcess.DetectedFileChanges} changed files"); this.Log("Writing backup data..."); this.StatusInformationLabel.Text = "Writing backup data..."; this.BackupProgressBar.Maximum = (int)backupProcess.DetectedFileChanges + 1; backupProcess.CreateBackup(backupName, backupMonitor); this.BackupProgressLabel.Text = ""; this.Log($"{backupMonitor.FilesCopied} files created"); this.Log($"Created backup \"{backupName}\""); this.StatusInformationLabel.Text = "Done."; } catch (Exception e) { this.LogBox.Enabled = false; } timer.Stop(); timer.Dispose(); }).Start(); }
private static void CreateBackup(string backupSource, string backupsDestination, string backupId, string precedingBackupId) { var backupProcess = new BackupProcess(backupSource, backupsDestination); var scanMonitor = new DirectoryScanProgressMonitor(); var backupMonitor = new BackupProgressMonitor(); var errors = new Queue<string>(); scanMonitor.ErrorReceived += (path, error, directory) => errors.Enqueue($"Could not scan path \"{path}\": {error.GetType()}"); var backupState = StatusDisplayState.LoadingReferenceData; var loggerTask = new Task(() => { Console.WriteLine(string.IsNullOrEmpty(precedingBackupId) ? "Creating full backup..." : "Loading preceding backup..."); while (backupState == StatusDisplayState.LoadingReferenceData) ; Console.WriteLine("Scanning source directory..."); while (backupState == StatusDisplayState.ScanningSourceData) { CommandLineExecution.RewindConsole(); while (errors.Count > 0) Console.Error.WriteLine(errors.Dequeue()); Console.Write($"{scanMonitor.FilesScanned} files, {scanMonitor.FoldersScanned} directories found..."); Thread.Sleep(20); } CommandLineExecution.RewindConsole(); Console.WriteLine($"{scanMonitor.FilesScanned} files, {scanMonitor.FoldersScanned} directories found"); Console.WriteLine("Preparing backup data..."); while (backupState == StatusDisplayState.PreparingBackupData) ; Console.WriteLine($"Detected {backupProcess.DetectedFileChanges} changed files"); Console.WriteLine("Writing backup data..."); while (backupState == StatusDisplayState.WritingBackupData) { CommandLineExecution.RewindConsole(); Console.Write($"({backupMonitor.FilesCopied} / {backupProcess.DetectedFileChanges + 1}) {backupMonitor.LastCopied}"); Thread.Sleep(20); } CommandLineExecution.RewindConsole(); Console.WriteLine($"{backupMonitor.FilesCopied} files created"); Console.WriteLine($"Created backup {backupId}"); }); loggerTask.Start(); try { if (string.IsNullOrEmpty(precedingBackupId)) backupProcess.UseEmptyReferenceData(); else { var referenceBackupDataFile = Path.Join(backupsDestination, precedingBackupId, BackupProcess.BackupDataFilename); backupProcess.LoadReferenceData(referenceBackupDataFile); } backupState = StatusDisplayState.ScanningSourceData; backupProcess.LoadSourceData(scanMonitor); backupState = StatusDisplayState.PreparingBackupData; backupProcess.ProcessDataDifference(); backupState = StatusDisplayState.WritingBackupData; backupProcess.CreateBackup(backupId, backupMonitor); backupState = StatusDisplayState.Done; loggerTask.Wait(); } catch (Exception e) { Console.Error.WriteLine(e.ToString()); Environment.Exit(1); } }