示例#1
0
        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();
        }
示例#2
0
        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);
            }

        }