/// <summary> /// Create a restore data object that includes the plan to do the restore operation /// </summary> /// <param name="requestParam"></param> /// <returns></returns> private void UpdateRestorePlan(RestoreDatabaseTaskDataObject restoreDataObject) { if (!string.IsNullOrEmpty(restoreDataObject.RestoreParams.BackupFilePaths)) { restoreDataObject.AddFiles(restoreDataObject.RestoreParams.BackupFilePaths); } restoreDataObject.RestorePlanner.ReadHeaderFromMedia = !string.IsNullOrEmpty(restoreDataObject.RestoreParams.BackupFilePaths); if (string.IsNullOrWhiteSpace(restoreDataObject.RestoreParams.SourceDatabaseName)) { restoreDataObject.RestorePlanner.DatabaseName = restoreDataObject.DefaultDbName; } else { restoreDataObject.RestorePlanner.DatabaseName = restoreDataObject.RestoreParams.SourceDatabaseName; } restoreDataObject.TargetDatabase = restoreDataObject.RestoreParams.TargetDatabaseName; restoreDataObject.RestoreOptions.KeepReplication = restoreDataObject.RestoreParams.GetOptionValue <bool>(RestoreOptionsHelper.KeepReplication); restoreDataObject.RestoreOptions.ReplaceDatabase = restoreDataObject.RestoreParams.GetOptionValue <bool>(RestoreOptionsHelper.ReplaceDatabase); restoreDataObject.RestoreOptions.SetRestrictedUser = restoreDataObject.RestoreParams.GetOptionValue <bool>(RestoreOptionsHelper.SetRestrictedUser); string recoveryState = restoreDataObject.RestoreParams.GetOptionValue <string>(RestoreOptionsHelper.RecoveryState); object databaseRecoveryState; if (Enum.TryParse(typeof(DatabaseRecoveryState), recoveryState, out databaseRecoveryState)) { restoreDataObject.RestoreOptions.RecoveryState = (DatabaseRecoveryState)databaseRecoveryState; } bool isTailLogBackupPossible = restoreDataObject.IsTailLogBackupPossible(restoreDataObject.RestorePlanner.DatabaseName); if (isTailLogBackupPossible) { restoreDataObject.RestorePlanner.BackupTailLog = restoreDataObject.RestoreParams.GetOptionValue <bool>(RestoreOptionsHelper.BackupTailLog); restoreDataObject.TailLogBackupFile = restoreDataObject.RestoreParams.GetOptionValue <string>(RestoreOptionsHelper.TailLogBackupFile); restoreDataObject.TailLogWithNoRecovery = restoreDataObject.RestoreParams.GetOptionValue <bool>(RestoreOptionsHelper.TailLogWithNoRecovery); } else { restoreDataObject.RestorePlanner.BackupTailLog = false; } restoreDataObject.CloseExistingConnections = restoreDataObject.RestoreParams.GetOptionValue <bool>(RestoreOptionsHelper.CloseExistingConnections); restoreDataObject.UpdateRestorePlan(restoreDataObject.RestoreParams.RelocateDbFiles); }
/// <summary> /// Creates a restore plan, The result includes the information about the backup set, /// the files and the database to restore to /// </summary> /// <param name="requestParam">Restore request</param>s /// <returns>Restore plan</returns> public RestorePlanResponse CreateRestorePlanResponse(RestoreDatabaseTaskDataObject restoreDataObject) { RestorePlanResponse response = new RestorePlanResponse() { DatabaseName = restoreDataObject.RestoreParams.TargetDatabaseName, PlanDetails = new System.Collections.Generic.Dictionary <string, object>() }; try { if (restoreDataObject != null && restoreDataObject.IsValid) { UpdateRestorePlan(restoreDataObject); if (restoreDataObject != null && restoreDataObject.IsValid) { response.SessionId = restoreDataObject.SessionId; response.DatabaseName = restoreDataObject.TargetDatabase; response.DbFiles = restoreDataObject.DbFiles.Select(x => new RestoreDatabaseFileInfo { FileType = x.DbFileType, LogicalFileName = x.LogicalName, OriginalFileName = x.PhysicalName, RestoreAsFileName = x.PhysicalNameRelocate }); response.CanRestore = CanRestore(restoreDataObject); if (!response.CanRestore) { response.ErrorMessage = SR.RestoreNotSupported; } response.PlanDetails.Add(LastBackupTaken, restoreDataObject.GetLastBackupTaken()); response.BackupSetsToRestore = restoreDataObject.GetSelectedBakupSets(); var dbNames = restoreDataObject.GetSourceDbNames(); response.DatabaseNamesFromBackupSets = dbNames == null ? new string[] { } : dbNames.ToArray(); // Adding the default values for some of the options in the plan details bool isTailLogBackupPossible = restoreDataObject.IsTailLogBackupPossible(restoreDataObject.RestorePlanner.DatabaseName); // Default backup tail-log. It's true when tail-log backup is possible for the source database response.PlanDetails.Add(RestoreOptionsHelper.DefaultBackupTailLog, isTailLogBackupPossible); // Default backup file for tail-log bacup when Tail-Log bachup is set to true response.PlanDetails.Add(RestoreOptionsHelper.DefaultTailLogBackupFile, restoreDataObject.Util.GetDefaultTailLogbackupFile(restoreDataObject.RestorePlan.DatabaseName)); // Default stand by file path for when RESTORE WITH STANDBY is selected response.PlanDetails.Add(RestoreOptionsHelper.DefaultStandbyFile, restoreDataObject.Util.GetDefaultStandbyFile(restoreDataObject.RestorePlan.DatabaseName)); // Default Data folder path in the target server response.PlanDetails.Add(RestoreOptionsHelper.DefaultDataFileFolder, restoreDataObject.DefaultDataFileFolder); // Default log folder path in the target server response.PlanDetails.Add(RestoreOptionsHelper.DefaultLogFileFolder, restoreDataObject.DefaultLogFileFolder); } else { if (restoreDataObject.ActiveException != null) { response.ErrorMessage = restoreDataObject.ActiveException.Message; } else { response.ErrorMessage = SR.RestorePlanFailed; } response.CanRestore = false; } } else { response.ErrorMessage = SR.RestorePlanFailed; } } catch (Exception ex) { response.ErrorMessage = ex.Message; if (ex.InnerException != null) { response.ErrorMessage += Environment.NewLine; response.ErrorMessage += ex.InnerException.Message; } } return(response); }