Exemple #1
0
        private async void RemoveVirtualMachineProtectionAsync(Guid subscriptionId, Guid vmId, string id)
        {
            bool removeProtectionError = false;

            try
            {
                VmBackupTarget target = await this.configStorageClient.SelectProtetionGroupByVirtualmachineAsync <VmBackupTarget>(subscriptionId, vmId, VirtualMachineMapping.CreateVmBackupTarget);

                await this.dpmServerClient.RemoveVirtualMachineFromProtectionGroup(target.BackupServerName, target.ProtectionGroupName, id, target.UserName, target.Password);

                await this.configStorageClient.DeleteVirtualMachineRecordAsync(subscriptionId, vmId);

                cache.Remove(subscriptionId.ToString());
            }
            catch (Exception e)
            {
                VmBackupLog.Current.WriteErrorMessage("RemoveVirtualMachineProtectionAsync", VmBackupEventId.UnexpectedTenantException, e, subscriptionId);
                removeProtectionError = true;
            }

            if (removeProtectionError)
            {
                await this.configStorageClient.UpdateVirtualMachineActionStateAsync(subscriptionId, vmId, (int)VirtualMachineActionState.RemoveProtectionFailed);
            }
        }
 public async Task RestoreVirtualMachine(VmBackupTarget target, string vmId, string recoverySourceId)
 {
     await this.RunScriptAsync(
         PowerShellCommandFactory.GetRestoreVirtualMachineScript(target.BackupServerName, target.ProtectionGroupName, vmId, recoverySourceId),
         target.BackupServerName,
         target.UserName,
         target.Password);
 }
        private static VmBackupTarget GenerateVmBackupTarget(SqlDataReader sqlDataReader)
        {
            VmBackupTarget target = new VmBackupTarget();

            target.BackupServerName    = (string)sqlDataReader["BackupServerName"];
            target.UserName            = (string)sqlDataReader["BackupServerUserName"];
            target.Password            = (string)sqlDataReader["BackupServerPassword"];
            target.ProtectionGroupName = (string)sqlDataReader["ProtectionGroupName"];

            return(target);
        }
        public static VmBackupTarget CreateVmBackupTarget(SqlDataReader sqlDataReader)
        {
            VmBackupTarget target = new VmBackupTarget();

            if (sqlDataReader.Read())
            {
                target = GenerateVmBackupTarget(sqlDataReader);
            }

            return(target);
        }
Exemple #5
0
        public async Task <VirtualMachine> AddVmToProtectionGroup(VirtualMachine vm)
        {
            try
            {
                Guid subId = Validator.ValidateSubscriptionId(vm.SubscriptionId);

                //Create the database record and get the backup target
                VmBackupTarget target = await this.configStorageClient.InsertVirtaulMachineRecordAsync <VmBackupTarget>(subId, vm.Id, vm.VmId, vm.Name, VirtualMachineMapping.CreateVmBackupTarget);

                //Fire and forget as this could be a long running process - For some reason just calling an async function did not work so using an actual thread.
                var t = new System.Threading.Thread(() => this.AddVirtualMachineToProtectionGroupAsync(subId, vm, target));
                t.Start();

                vm.Status       = "NotProtected";
                vm.ActionStatus = VirtualMachineActionState.Protecting.ToString();
                return(vm);
            }
            catch (SqlException e)
            {
                string    exceptionMessage;
                const int NoProtectionGroupFound          = 56001;
                const int VirtualMachineAlreadyBeingAdded = 56002;

                int  messageNumber;
                bool parseSucceeded = int.TryParse(e.Message, out messageNumber);

                if (parseSucceeded && VirtualMachineAlreadyBeingAdded == messageNumber)
                {
                    exceptionMessage = string.Format(CultureInfo.CurrentCulture, ErrorMessages.AnotherVirtualMachineAlreadyBeingAdded, vm.Name);
                    VmBackupLog.Current.WriteErrorMessage("AddVmToProtectionGroup", VmBackupEventId.TooManyProtectionGroupAddRequests, null, vm.SubscriptionId, vm.Name);
                    throw Utility.CreateHttpResponseException(
                              exceptionMessage,
                              HttpStatusCode.Conflict);
                }
                else if (parseSucceeded && NoProtectionGroupFound == messageNumber)
                {
                    exceptionMessage = string.Format(CultureInfo.CurrentCulture, ErrorMessages.NoProtectionGroupFound, vm.Name);
                    VmBackupLog.Current.WriteErrorMessage("AddVmToProtectionGroup", VmBackupEventId.ProtectionGroupNotFound, null, vm.SubscriptionId);
                    throw Utility.CreateHttpResponseException(
                              exceptionMessage,
                              HttpStatusCode.BadRequest);
                }
                else
                {
                    VmBackupLog.Current.WriteErrorMessage("AddVmToProtectionGroup", VmBackupEventId.UnexpectedTenantException, e, vm.SubscriptionId);
                    throw;
                }
            }
        }
Exemple #6
0
        public async Task <RecoveryPointList> ListVirtualMachineRestorePoints(string subscriptionId, string id)
        {
            try
            {
                Guid subId = Validator.ValidateSubscriptionId(subscriptionId);
                Guid vmId  = Guid.Parse(id);

                VmBackupTarget target = await this.configStorageClient.SelectProtetionGroupByVirtualmachineAsync <VmBackupTarget>(subId, vmId, VirtualMachineMapping.CreateVmBackupTarget);

                RecoveryPointList recoveryPoints = await this.dpmServerClient.ListRecoveryPoints(target.BackupServerName, target.ProtectionGroupName, id, target.UserName, target.Password);

                return(recoveryPoints);
            }
            catch (Exception e)
            {
                VmBackupLog.Current.WriteErrorMessage("ListVirtualMachineRestorePoints", VmBackupEventId.UnexpectedTenantException, e, subscriptionId);
                string message = ErrorMessages.ListVirtualMachineRestorePointsFailed;
                throw Utility.CreateHttpResponseException(message, HttpStatusCode.BadRequest);
            }
        }
Exemple #7
0
        private async void RestoreVirtualMachineAsync(Guid subscriptionId, Guid vmId, string id, string recoverySourceId)
        {
            bool restoreError = false;

            try
            {
                VmBackupTarget target = await this.configStorageClient.SelectProtetionGroupByVirtualmachineAsync <VmBackupTarget>(subscriptionId, vmId, VirtualMachineMapping.CreateVmBackupTarget);

                await this.dpmServerClient.RestoreVirtualMachine(target, id, recoverySourceId);

                await this.configStorageClient.UpdateVirtualMachineActionStateAsync(subscriptionId, vmId, (int)VirtualMachineActionState.RestoreComplete);
            }
            catch (Exception e)
            {
                VmBackupLog.Current.WriteErrorMessage("RestoreVirtualMachineAsync", VmBackupEventId.UnexpectedTenantException, e, subscriptionId);
                restoreError = true;
            }

            if (restoreError)
            {
                await this.configStorageClient.UpdateVirtualMachineActionStateAsync(subscriptionId, vmId, (int)VirtualMachineActionState.RestoreFailed);
            }
        }
Exemple #8
0
        private async void AddVirtualMachineToProtectionGroupAsync(Guid subscriptionId, VirtualMachine vm, VmBackupTarget target)
        {
            bool protectionError = false;

            try
            {
                //Protect the VM and get back the data soruce ID
                Guid id = await this.dpmServerClient.AddVirtualMachineToProtectionGroup(target.BackupServerName, target.ProtectionGroupName, vm.VmId.ToString(), target.UserName, target.Password);

                //Complete the record
                await this.configStorageClient.CompleteVirtualMachineRecordAsync(subscriptionId, vm.VmId, id);

                cache.Remove(subscriptionId.ToString());
            }
            catch (System.Management.Automation.RuntimeException e)
            {
                switch (e.ErrorRecord.FullyQualifiedErrorId)
                {
                case "10005":
                    VmBackupLog.Current.WriteErrorMessage("AddVirtualMachineToProtectionGroupAsync", VmBackupEventId.ProtectionGroupJobDeadlockDetecked, null, vm.SubscriptionId, target.ProtectionGroupName);
                    break;

                default:
                    VmBackupLog.Current.WriteErrorMessage("AddVirtualMachineToProtectionGroupAsync", VmBackupEventId.UnexpectedTenantException, e, vm.SubscriptionId);
                    break;
                }
            }
            catch (Exception e)
            {
                VmBackupLog.Current.WriteErrorMessage("AddVirtualMachineToProtectionGroupAsync", VmBackupEventId.UnexpectedTenantException, e, vm.SubscriptionId);
                protectionError = true;
            }

            if (protectionError)
            {
                await this.configStorageClient.UpdateVirtualMachineActionStateAsync(subscriptionId, vm.VmId, (int)VirtualMachineActionState.ProtectionFailed);
            }
        }