Esempio n. 1
0
        public override void OnStart(CustomRestoreAgent agent, Models.Restore restore)
        {
            base.OnStart(agent, restore);

            _daoRestore.Insert(restore);

            var message = string.Format("Restore started at {0}", StartedAt);

            Info(message);
            //StatusInfo.Update(BackupStatusLevel.OK, message);

            OnUpdate(new RestoreOperationEvent {
                Status = RestoreOperationStatus.Started, Message = message
            });
        }
Esempio n. 2
0
        public void OnFailure(CustomRestoreAgent agent, Models.Restore restore, Exception exception)
        {
            IsRunning = false;

            var message = string.Format("Restore failed: {0}", exception != null ? exception.Message : "Canceled?");

            Report.AddErrorMessage(message);
            Error(message);
            //StatusInfo.Update(RestoreStatusLevel.ERROR, message);

            restore.DidFail();
            _daoRestore.Update(restore);

            OnUpdate(new RestoreOperationEvent {
                Status = RestoreOperationStatus.Failed, Message = message
            });
        }
Esempio n. 3
0
        /// <summary>
        /// Implements the Dispose pattern
        /// </summary>
        /// <param name="disposing">Whether this object is being disposed via a call to Dispose
        /// or garbage collected.</param>
        protected override void Dispose(bool disposing)
        {
            if (!this._isDisposed)
            {
                if (disposing && _shouldDispose)
                {
                    if (RestoreAgent != null)
                    {
                        RestoreAgent.Dispose();
                        RestoreAgent = null;
                    }

                    if (Versioner != null)
                    {
                        Versioner.Dispose();
                        Versioner = null;
                    }
                }
                this._isDisposed = true;
            }
            base.Dispose(disposing);
        }
Esempio n. 4
0
        public void OnFinish(CustomRestoreAgent agent, Models.Restore restore, Exception ex = null)
        {
            IsRunning = false;

            switch (CurrentState)
            {
            default:
            {
                var message = string.Format("Restore failed: {0}", ex.Message);
                Warn(message);
                //StatusInfo.Update(RestoreStatusLevel.WARN, message);
                Report.AddErrorMessage(ex.Message);

                Report.OperationStatus = OperationStatus.FAILED;
                restore.DidFail();
                _daoRestore.Update(restore);
                OnUpdate(new RestoreOperationEvent {
                        Status = RestoreOperationStatus.Failed, Message = message
                    });

                break;
            }

            case RestoreOperationState.FINISHING:
            {
                TransferResults.Statistics stats = agent.Results.Stats;

                var message = string.Format(
                    "Restore finished! Stats: {0} completed, {1} failed, {2} canceled, {3} pending, {4} running",
                    stats.Completed, stats.Failed, stats.Canceled, stats.Pending, stats.Running);
                Info(message);
                //StatusInfo.Update(RestoreStatusLevel.OK, message);

                switch (agent.Results.OverallStatus)
                //switch (backup.Status)
                {
                default: throw new InvalidOperationException("Unexpected TransferStatus");

                case TransferStatus.CANCELED:
                    Report.OperationStatus = OperationStatus.CANCELED;
                    restore.WasCanceled();
                    _daoRestore.Update(restore);
                    OnUpdate(new RestoreOperationEvent {
                            Status = RestoreOperationStatus.Canceled, Message = message
                        });
                    break;

                case TransferStatus.FAILED:
                    Report.OperationStatus = OperationStatus.FAILED;
                    restore.DidFail();
                    _daoRestore.Update(restore);
                    OnUpdate(new RestoreOperationEvent {
                            Status = RestoreOperationStatus.Failed, Message = message
                        });
                    break;

                case TransferStatus.COMPLETED:
                    Report.OperationStatus = OperationStatus.COMPLETED;
                    restore.DidComplete();
                    _daoRestore.Update(restore);
                    OnUpdate(new RestoreOperationEvent {
                            Status = RestoreOperationStatus.Finished, Message = message
                        });
                    break;
                }

                break;
            }
            }

            Report.StartedAt  = Restore.StartedAt;
            Report.FinishedAt = Restore.FinishedAt.Value;
        }
Esempio n. 5
0
        public virtual void OnStart(CustomRestoreAgent agent, Models.Restore restore)
        {
            IsRunning = true;

            restore.DidStart();
        }
Esempio n. 6
0
 protected void DoCancel(CustomRestoreAgent agent)
 {
     agent.Cancel();
     CancellationTokenSource.Cancel();
 }
Esempio n. 7
0
        protected async void DoRestore(CustomRestoreAgent agent, Models.Restore restore, RestoreOperationOptions options)
        {
            try
            {
                CurrentState = RestoreOperationState.STARTING;
                OnStart(agent, restore);

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

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

                //
                // Scanning
                //

                CurrentState = RestoreOperationState.SCANNING_FILES;
                LinkedList <CustomVersionedFile> filesToProcess = null;
                {
                    Task <PathScanResults <CustomVersionedFile> > filesToProcessTask = GetFilesToProcess(restore);

                    {
                        var message = string.Format("Scanning files started.");
                        Info(message);
                        //StatusInfo.Update(BackupStatusLevel.INFO, message);
                        OnUpdate(new RestoreOperationEvent {
                            Status = RestoreOperationStatus.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, restore, ex);                                 // filesToProcessTask.Exception
                            }
                            else
                            {
                                OnFailure(agent, restore, 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 RestoreOperationEvent {
                            Status = RestoreOperationStatus.ScanningFilesFinished, Message = message
                        });
                    }
                }

                //
                // Versioning
                //

                CurrentState = RestoreOperationState.VERSIONING_FILES;
                {
                    Task versionerTask = DoVersionFiles(restore, filesToProcess);

                    {
                        var message = string.Format("Processing files started.");
                        Info(message);
                        //StatusInfo.Update(RestoreStatusLevel.INFO, message);
                        OnUpdate(new RestoreOperationEvent {
                            Status = RestoreOperationStatus.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, restore, ex);                                 // versionerTask.Exception
                            }
                            else
                            {
                                OnFailure(agent, restore, ex);                                 // versionerTask.Exception
                            }
                            return;
                        }
                    }

                    agent.Files = Versioner.FilesToTransfer;

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

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

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

                //
                // Transfer files
                //

                CurrentState = RestoreOperationState.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.AddErrorMessage(message);
                            logger.Warn(message);
                        }
                        else
                        {
                            string message = string.Format("Caught exception during transfer files: {0}", ex.Message);

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

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

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

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

                CurrentState = RestoreOperationState.FINISHING;
                OnFinish(agent, restore);
            }
            catch (Exception ex)
            {
                OnFinish(agent, restore, ex);
            }
        }
Esempio n. 8
0
        public override void Start()
        {
            Assert.IsFalse(IsRunning);
            Assert.IsNotNull(Restore);
            Assert.IsNotNull(Restore.RestorePlan.StorageAccount);
            Assert.AreEqual(Models.EStorageAccountType.AmazonS3, Restore.RestorePlan.StorageAccountType);

            AmazonS3AccountRepository dao = new AmazonS3AccountRepository();

            Models.AmazonS3Account s3account = dao.GetForReadOnly(Restore.RestorePlan.StorageAccount.Id);

            //
            // Dispose and recycle previous objects, if needed.
            //
            if (TransferAgent != null)
            {
                TransferAgent.Dispose();
            }
            if (TransferListControl != null)
            {
                TransferListControl.ClearTransfers();
            }
            if (RestoreAgent != null)
            {
                RestoreAgent.Dispose();
            }
            if (Versioner != null)
            {
                Versioner.Dispose();
            }

            //
            // Setup agents.
            //
            AWSCredentials       awsCredentials = new BasicAWSCredentials(s3account.AccessKey, s3account.SecretKey);
            TransferAgentOptions options        = new TransferAgentOptions
            {
                UploadChunkSizeInBytes = Teltec.Everest.Settings.Properties.Current.UploadChunkSize * 1024 * 1024,
            };

            TransferAgent = new S3TransferAgent(options, awsCredentials, s3account.BucketName, CancellationTokenSource.Token);
            TransferAgent.RemoteRootDir = TransferAgent.PathBuilder.CombineRemotePath("TELTEC_BKP",
                                                                                      Restore.RestorePlan.StorageAccount.Hostname);

            RestoreAgent = new CustomRestoreAgent(TransferAgent);
            RestoreAgent.Results.Monitor = TransferListControl;

            Versioner = new RestoreFileVersioner(CancellationTokenSource.Token);

            RegisterResultsEventHandlers(Restore, RestoreAgent.Results);

            Report.PlanType        = "restore";
            Report.PlanName        = Restore.RestorePlan.Name;
            Report.BucketName      = s3account.BucketName;
            Report.HostName        = Restore.RestorePlan.StorageAccount.Hostname;
            Report.TransferResults = RestoreAgent.Results;

            Helper = new BaseOperationHelper(Restore.RestorePlan);

            //
            // Start the restore.
            //
            DoRestore(RestoreAgent, Restore, Options);
        }