Esempio n. 1
0
        public ResumeBackupOperation(Models.Backup backup, BackupOperationOptions options)
            : base(options)
        {
            Assert.IsNotNull(backup);
            Assert.AreEqual(TransferStatus.RUNNING, backup.Status);

            Backup = backup;
        }
Esempio n. 2
0
        //private string _RootDir;
        //public string RootDir
        //{
        //	get
        //	{
        //		if (_RootDir == null)
        //			_RootDir = string.Format("backup-{0}", Plan.Id);
        //		return _RootDir;
        //	}
        //}

        #endregion

        #region Constructors

        public BackupOperation(BackupOperationOptions options)
        {
            Options = options;
        }
Esempio n. 3
0
        protected async void DoBackup(CustomBackupAgent agent, Models.Backup backup, BackupOperationOptions options)
        {
            try
            {
                CurrentState = BackupOperationState.STARTING;
                OnStart(agent, backup);

                // Mount all network mappings and abort if there is any network mapping failure.
                CurrentState = BackupOperationState.MAPPING_NETWORK_DRIVES;
                Helper.MountAllNetworkDrives();

                // Execute pre-actions
                CurrentState = BackupOperationState.EXECUTING_PRE_ACTIONS;
                Helper.ExecutePreActions();

                //
                // Scanning
                //

                CurrentState = BackupOperationState.SCANNING_FILES;
                LinkedList <string> filesToProcess = null;
                {
                    Task <PathScanResults <string> > filesToProcessTask = GetFilesToProcess(backup);

                    {
                        var message = string.Format("Scanning files started.");
                        Info(message);
                        //StatusInfo.Update(BackupStatusLevel.INFO, message);
                        OnUpdate(new BackupOperationEvent {
                            Status = BackupOperationStatus.ScanningFilesStarted, Message = message
                        });
                    }

                    try
                    {
                        await filesToProcessTask;
                    }
                    catch (Exception ex)
                    {
                        if (ex.IsCancellation())
                        {
                            string message = string.Format("Scanning files was canceled.");

                            Report.AddErrorMessage(message);
                            logger.Warn(message);
                        }
                        else
                        {
                            string message = string.Format("Caught exception during scanning files: {0}", ex.Message);

                            Report.AddErrorMessage(message);
                            logger.Log(LogLevel.Error, ex, message);
                        }

                        if (filesToProcessTask.IsFaulted || filesToProcessTask.IsCanceled)
                        {
                            if (filesToProcessTask.IsCanceled)
                            {
                                OnCancelation(agent, backup, ex);                                 // filesToProcessTask.Exception
                            }
                            else
                            {
                                OnFailure(agent, backup, ex);                                 // filesToProcessTask.Exception
                            }
                            return;
                        }
                    }

                    filesToProcess = filesToProcessTask.Result.Files;

                    {
                        foreach (var entry in filesToProcessTask.Result.FailedFiles)
                        {
                            Report.AddErrorMessage(entry.Value);
                        }

                        if (filesToProcessTask.Result.FailedFiles.Count > 0)
                        {
                            StringBuilder sb = new StringBuilder();
                            sb.AppendLine("Scanning failed for the following drives/files/directories:");
                            foreach (var entry in filesToProcessTask.Result.FailedFiles)
                            {
                                sb.AppendLine(string.Format("  Path: {0} - Reason: {1}", entry.Key, entry.Value));
                            }
                            Warn(sb.ToString());
                        }

                        var message = string.Format("Scanning files finished.");
                        Info(message);
                        //StatusInfo.Update(BackupStatusLevel.INFO, message);
                        OnUpdate(new BackupOperationEvent {
                            Status = BackupOperationStatus.ScanningFilesFinished, Message = message
                        });
                    }
                }

                //
                // Update synced files
                //

                CurrentState = BackupOperationState.UPDATING_SYNCED_FILES;
                {
                    Task updateSyncedFilesTask = ExecuteOnBackround(() =>
                    {
                        DoUpdateSyncedFiles(backup, filesToProcess);
                    }, CancellationTokenSource.Token);

                    {
                        var message = string.Format("Update of synced files started.");
                        Info(message);
                    }

                    try
                    {
                        await updateSyncedFilesTask;
                    }
                    catch (Exception ex)
                    {
                        if (ex.IsCancellation())
                        {
                            string message = string.Format("Update of synced files was canceled.");

                            Report.AddErrorMessage(message);
                            logger.Warn(message);
                        }
                        else
                        {
                            string message = string.Format("Caught exception during update of synced files: {0}", ex.Message);

                            Report.AddErrorMessage(message);
                            logger.Log(LogLevel.Error, ex, message);
                        }

                        if (updateSyncedFilesTask.IsFaulted || updateSyncedFilesTask.IsCanceled)
                        {
                            Versioner.Undo();
                            if (updateSyncedFilesTask.IsCanceled)
                            {
                                OnCancelation(agent, backup, ex);                                 // updateSyncedFilesTask.Exception
                            }
                            else
                            {
                                OnFailure(agent, backup, ex);                                 // updateSyncedFilesTask.Exception
                            }
                            return;
                        }
                    }

                    {
                        var message = string.Format("Update of synced files finished.");
                        Info(message);
                    }
                }

                //
                // Versioning
                //

                CurrentState = BackupOperationState.VERSIONING_FILES;
                {
                    Task <FileVersionerResults> versionerTask = DoVersionFiles(backup, filesToProcess);
                    Report.VersionerResults = versionerTask.Result;

                    {
                        var message = string.Format("Processing files started.");
                        Info(message);
                        //StatusInfo.Update(BackupStatusLevel.INFO, message);
                        OnUpdate(new BackupOperationEvent {
                            Status = BackupOperationStatus.ProcessingFilesStarted, Message = message
                        });
                    }

                    try
                    {
                        await versionerTask;
                    }
                    catch (Exception ex)
                    {
                        if (ex.IsCancellation())
                        {
                            string message = string.Format("Processing files was canceled.");

                            Report.AddErrorMessage(message);
                            logger.Warn(message);
                        }
                        else
                        {
                            string message = string.Format("Caught exception during processing files: {0}", ex.Message);

                            Report.AddErrorMessage(message);
                            logger.Log(LogLevel.Error, ex, message);
                        }

                        if (versionerTask.IsFaulted || versionerTask.IsCanceled)
                        {
                            Versioner.Undo();
                            if (versionerTask.IsCanceled)
                            {
                                OnCancelation(agent, backup, ex);                                 // versionerTask.Exception
                            }
                            else
                            {
                                OnFailure(agent, backup, ex);                                 // versionerTask.Exception
                            }
                            return;
                        }
                    }

                    agent.Files = Versioner.FilesToTransfer;

                    {
                        var message = string.Format("Processing files finished.");
                        Info(message);
                        //StatusInfo.Update(BackupStatusLevel.INFO, message);
                        OnUpdate(new BackupOperationEvent {
                            Status = BackupOperationStatus.ProcessingFilesFinished, Message = message
                        });
                    }

                    {
                        agent.Results.Stats.BytesTotal = agent.EstimatedTransferSize;

                        var message = string.Format("Estimated backup size: {0} files, {1}",
                                                    agent.Files.Count(), FileSizeUtils.FileSizeToString(agent.EstimatedTransferSize));
                        Info(message);
                    }
                }

                //
                // Transfer files
                //

                CurrentState = BackupOperationState.TRANSFERRING_FILES;
                {
                    Task <TransferResults> transferTask = agent.Start();
                    Report.TransferResults = transferTask.Result;

                    {
                        var message = string.Format("Transfer files started.");
                        Info(message);
                    }

                    try
                    {
                        await transferTask;
                    }
                    catch (Exception ex)
                    {
                        if (ex.IsCancellation())
                        {
                            string message = string.Format("Transfer files was canceled.");

                            Report.TransferResults.ErrorMessages.Add(message);
                            logger.Warn(message);
                        }
                        else
                        {
                            string message = string.Format("Caught exception during transfer files: {0}", ex.Message);

                            Report.TransferResults.ErrorMessages.Add(message);
                            logger.Log(LogLevel.Error, ex, message);
                        }

                        if (transferTask.IsFaulted || transferTask.IsCanceled)
                        {
                            if (transferTask.IsCanceled)
                            {
                                OnCancelation(agent, backup, ex);                                 // transferTask.Exception
                            }
                            else
                            {
                                OnFailure(agent, backup, ex);                                 // transferTask.Exception
                            }
                            return;
                        }
                    }

                    {
                        var message = string.Format("Transfer files finished.");
                        Info(message);
                    }
                }

                CurrentState = BackupOperationState.EXECUTING_POST_ACTIONS;
                Helper.ExecutePostActions(Report.TransferResults);

                CurrentState = BackupOperationState.FINISHING;
                OnFinish(agent, backup);
            }
            catch (Exception ex)
            {
                OnFinish(agent, backup, ex);
            }
        }
Esempio n. 4
0
 public NewBackupOperation(Models.BackupPlan plan, BackupOperationOptions options)
     : base(options)
 {
     Backup = new Models.Backup(plan);
 }