public async Task <ActionResult <RestoreReqResponse> > Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "restore/blobs")]
            HttpRequest req, Microsoft.Extensions.Logging.ILogger log)
        {
            log.LogInformation($"PerformRestore: Invoked at: {DateTime.Now}");
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            RestoreReqResponse reqRespData = await ValidateInput(req);

            if (string.IsNullOrEmpty(reqRespData.ExceptionMessage))
            {
                try
                {
                    // First check if async restore is requested
                    if (reqRespData.ReqType.Equals(Constants.RESTORE_REQUEST_TYPE_ASYNC))
                    {
                        reqRespData.Status            = Constants.RESTORE_STATUS_ACCEPTED;
                        reqRespData.StatusLocationUri = $"{req.Scheme}://{req.Host}{req.PathBase}/api/restore/";

                        await _restoreTable.InsertRestoreRequest(reqRespData);
                    }
                    else
                    {
                        log.LogInformation($"PerformRestore: Start date : {reqRespData.StDate.ToString("MM/dd/yyyy")}, End date {reqRespData.EnDate.ToString("MM/dd/yyyy")}. Proceeding with restore process ...");

                        if (!String.IsNullOrEmpty(reqRespData.ContainerName))
                        {
                            log.LogInformation($"PerformRestore: Container Name : {reqRespData.ContainerName}");
                        }

                        await _restoreBackup.Run(reqRespData);
                    };
                    log.LogInformation($"PerformRestore: Completed execution at: {DateTime.Now}");
                }
                catch (Exception ex)
                {
                    log.LogError($"PerformRestore: Exception occurred. Exception: {@ex.ToString()}");
                    reqRespData.ExceptionMessage = $"Encountered exception : {@ex.ToString()}";
                    reqRespData.Status           = Constants.RESTORE_STATUS_FAILED;
                }
            }
            ;

            stopWatch.Stop();
            reqRespData.ExecutionTime = DateTimeUtil.getTimeString(stopWatch.Elapsed);

            return(reqRespData);
        }
Esempio n. 2
0
        public async Task Run(
            [TimerTrigger("%RestoreTriggerSchedule%")] TimerInfo callTimer,
            Microsoft.Extensions.Logging.ILogger log)
        {
            if (callTimer.IsPastDue)
            {
                log.LogInformation("PerformBackup: Timer is running late!");
            }

            log.LogInformation($"PerformRestoreAsync: Invoked at: {DateTime.Now}");
            Stopwatch          stopWatch   = new Stopwatch();
            RestoreReqResponse reqRespData = null;

            try
            {
                // Fetch an asynchronous restore request
                reqRespData = await _restoreTable.GetRestoreRequest();

                if (reqRespData != null)
                {
                    stopWatch.Start();

                    // Update the status of the restore request to in-progress
                    reqRespData.Status    = Constants.RESTORE_STATUS_INPROCESS;
                    reqRespData.StartTime = DateTime.Now.ToString();
                    await _restoreTable.UpdateRestoreRequest(reqRespData);

                    // Execute the restore process
                    DateTime startDate = DateTime.MinValue;
                    DateTime endDate   = DateTime.MinValue;
                    DateTime.TryParse(reqRespData.StartDate, out startDate);
                    DateTime.TryParse(reqRespData.EndDate, out endDate);
                    reqRespData.StDate = startDate;
                    reqRespData.EnDate = endDate;
                    log.LogInformation($"PerformRestore: Start date : {reqRespData.StDate.ToString("MM/dd/yyyy")}, End date {reqRespData.EnDate.ToString("MM/dd/yyyy")}. Proceeding with restore process ...");
                    if (!String.IsNullOrEmpty(reqRespData.ContainerName))
                    {
                        log.LogInformation($"PerformRestore: Container Name : {reqRespData.ContainerName}");
                    }
                    await _restoreBackup.Run(reqRespData);

                    // Update the status of the restore request to completed
                    reqRespData.Status  = Constants.RESTORE_STATUS_COMPLETED;
                    reqRespData.EndTime = DateTime.Now.ToString();
                    stopWatch.Stop();
                    reqRespData.ExecutionTime = DateTimeUtil.getTimeString(stopWatch.Elapsed);
                    await _restoreTable.UpdateRestoreRequest(reqRespData);
                }
            }
            catch (Exception ex)
            {
                log.LogError($"PerformRestoreAsync: Exception occurred while processing message. Exception: {@ex.ToString()}");
                // Update the status of the restore request to exception
                if (reqRespData != null)
                {
                    reqRespData.Status           = Constants.RESTORE_STATUS_FAILED;
                    reqRespData.ExceptionMessage = $"PerformRestoreAsync: Encountered Exception: {@ex.ToString()}";
                    reqRespData.EndTime          = DateTime.Now.ToString();
                    stopWatch.Stop();
                    reqRespData.ExecutionTime = DateTimeUtil.getTimeString(stopWatch.Elapsed);

                    await _restoreTable.UpdateRestoreRequest(reqRespData);
                }
                ;
            }
            stopWatch = null;
            log.LogInformation($"PerformRestoreAsync: Completed execution at: {DateTime.Now}");
        }
Esempio n. 3
0
        /// <summary>
        /// Main for restore.
        /// </summary>
        /// <param name="args"></param>
        static async Task Main(string[] args)
        {
            try
            {
                if (args == null || args.Length < 2)
                {
                    Console.WriteLine("In order to restore the backup please provide start and end date. Date format is mm/dd/yyyy!");

                    Console.ReadKey();

                    return;
                }

                DateTime startDate = DateTime.MinValue;

                DateTime endDate = DateTime.MinValue;

                bool startDateParsed = false;

                bool endDateParsed = false;

                // if (args.Length == 2) ID05192020.o
                // {
                startDateParsed = DateTime.TryParse(args[0], out startDate);

                endDateParsed = DateTime.TryParse(args[1], out endDate);
                // }

                if (!startDateParsed || !endDateParsed)
                {
                    Console.WriteLine($"Unable to parse start and end date. Provide dates in mm/dd/yyyy format. Start date value {args[0]} End date value {args[1]}. ");

                    Console.ReadKey();

                    return;
                }

                if (startDate > endDate)
                {
                    Console.WriteLine($"Start date cannot be greater than end date.");

                    Console.ReadKey();

                    return;
                }

                string containerName = (args.Length > 2) ? args[2] : null; // ID05192020.n

                // Console.WriteLine($"Here are the captured values. Start date : {startDate.ToString("MM/dd/yyyy")} End date {endDate.ToString("MM/dd/yyyy")}."); ID05192020.o
                Console.WriteLine($"Here are the captured values. Start date : {startDate.ToString("MM/dd/yyyy")} End date : {endDate.ToString("MM/dd/yyyy")}. Container Name : {containerName}"); // ID05192020.n

                Console.WriteLine($"Please enter \"Y\" to continue performing the restore'");

                string response = Console.ReadKey().Key.ToString();

                if (!response.ToLower().Equals("y"))
                {
                    Console.WriteLine($"Press any key to exit!");

                    Console.ReadKey();

                    return;
                }

                //to do start the restore process.

                // Create service collection
                var serviceCollection = new ServiceCollection();

                ConfigureServices(serviceCollection);

                // Create service provider
                _serviceProvider = serviceCollection.BuildServiceProvider();

                var config = _serviceProvider.GetService <IConfigurationRoot>();

                var logger = _serviceProvider.GetService <ILogger <RestoreBackupWorker> >();

                IRestoreBackup restoreBackup = _serviceProvider.GetService <IRestoreBackup>();

                // Run the restore process
                // await restoreBackup.Run(startDate, endDate); ID05192020.o
                // await restoreBackup.Run(startDate, endDate, containerName); // ID05202020.o
                // ID05202020.sn
                RestoreReqResponse reqResData = new RestoreReqResponse();
                reqResData.StDate        = startDate;
                reqResData.EnDate        = endDate;
                reqResData.ContainerName = containerName;
                await restoreBackup.Run(reqResData);

                // ID05202020.en

                Console.WriteLine($"Press any key to exit!");

                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An exception occurred. {ex.ToString()}");
            }
        }
Esempio n. 4
0
        public async Task <ActionResult <RestoreReqResponse> > Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "restore/blobs")]
            HttpRequest req, Microsoft.Extensions.Logging.ILogger log)
        {
            log.LogInformation($"PerformRestore: Invoked at: {DateTime.Now}");
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            string             requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            RestoreReqResponse reqRespData = JsonConvert.DeserializeObject <RestoreReqResponse>(requestBody);

            if (String.IsNullOrEmpty(reqRespData.StartDate) || String.IsNullOrEmpty(reqRespData.EndDate))
            {
                reqRespData.ExceptionMessage = "Request is missing JSON payload containing start and end dates!";
                stopWatch.Stop();
                reqRespData.ExecutionTime = getTimeString(stopWatch.Elapsed);

                return(reqRespData);
            }
            ;

            DateTime startDate = DateTime.MinValue;
            DateTime endDate   = DateTime.MinValue;

            bool startDateParsed = false;
            bool endDateParsed   = false;

            startDateParsed = DateTime.TryParse(reqRespData.StartDate, out startDate);
            endDateParsed   = DateTime.TryParse(reqRespData.EndDate, out endDate);

            if (!startDateParsed || !endDateParsed)
            {
                reqRespData.ExceptionMessage = $"Unable to parse start and end dates. Provide dates in mm/dd/yyyy format. Start date value {reqRespData.StartDate} End date value {reqRespData.EndDate}. ";
                stopWatch.Stop();
                reqRespData.ExecutionTime = getTimeString(stopWatch.Elapsed);

                return(reqRespData);
            }
            ;


            if (startDate > endDate)
            {
                reqRespData.ExceptionMessage = "Start date cannot be greater than end date.";
                stopWatch.Stop();
                reqRespData.ExecutionTime = getTimeString(stopWatch.Elapsed);

                return(reqRespData);
            }
            ;

            log.LogInformation($"PerformRestore: Start date : {startDate.ToString("MM/dd/yyyy")}, End date {endDate.ToString("MM/dd/yyyy")}. Proceeding with restore process ...");

            reqRespData.StDate = startDate;
            reqRespData.EnDate = endDate;

            // ID05192020.sn
            if (!String.IsNullOrEmpty(reqRespData.ContainerName))
            {
                log.LogInformation($"PerformRestore: Container Name : {reqRespData.ContainerName}");
            }

            if (!String.IsNullOrEmpty(reqRespData.BlobName))
            {
                if (String.IsNullOrEmpty(reqRespData.ContainerName))
                {
                    reqRespData.ExceptionMessage = $"To restore File : {reqRespData.BlobName}, container name is required!";
                    stopWatch.Stop();
                    reqRespData.ExecutionTime = getTimeString(stopWatch.Elapsed);

                    return(reqRespData);
                }
                ;
            }
            ;
            // ID05192020.en

            try
            {
                // Run the restore process
                await _restoreBackup.Run(reqRespData);
            }
            catch (Exception ex)
            {
                log.LogError($"PerformRestore: Exception occurred. Exception: {@ex.ToString()}");
                reqRespData.ExceptionMessage = $"Encountered exception : {@ex.ToString()}";
                stopWatch.Stop();
                reqRespData.ExecutionTime = getTimeString(stopWatch.Elapsed);

                return(reqRespData);
            }
            log.LogInformation($"PerformRestore: Completed execution at: {DateTime.Now}");

            stopWatch.Stop();
            reqRespData.ExecutionTime = getTimeString(stopWatch.Elapsed);
            // return (ActionResult) new OkObjectResult("Restore process finished OK"); ID05202020.o
            return(reqRespData); // ID05202020.n
        }