/// <summary> /// Fetch the next restore request to be processed. Status = ACCEPTED /// </summary> /// <returns>RestoreReqResponse</returns> public async Task <RestoreReqResponse> GetRestoreRequest() { RestoreReqResponse reqRespData = null; CloudTable restoreReqTable = GetCloudTable(); EventDateDetails dateDetails = new EventDateDetails(DateTime.Now); var whereCondition = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, $"{dateDetails.year.ToString()}_{dateDetails.WeekNumber.ToString()}"); var statusCondition = TableQuery.GenerateFilterCondition("CurrentStatus", QueryComparisons.Equal, Constants.Constants.RESTORE_STATUS_ACCEPTED); string query = TableQuery.CombineFilters(whereCondition, TableOperators.And, statusCondition); TableQuery <RestoreReqEntity> partitionQuery = new TableQuery <RestoreReqEntity>().Where(query); partitionQuery.TakeCount = 1; // Return only one record at a time; TableQuerySegment <RestoreReqEntity> resultSegment = await restoreReqTable.ExecuteQuerySegmentedAsync(partitionQuery, null); if ((resultSegment.Results != null) && (resultSegment.Results.Count == 1)) { foreach (RestoreReqEntity entity in resultSegment.Results) { reqRespData = JsonConvert.DeserializeObject <RestoreReqResponse>(entity.RestoreReqRespDataJSON); } } ; return(reqRespData); }
/// <summary> /// Returns all the dates between date range and with the corresponding week number. /// </summary> /// <param name="startDate"></param> /// <param name="EndDate"></param> /// <returns></returns> public List <Tuple <int, int, DateTime> > GetDatesForDateRange(DateTime startDate, DateTime EndDate) { List <Tuple <int, int, DateTime> > dates = new List <Tuple <int, int, DateTime> >(); Tuple <int, int, DateTime> dateData; EventDateDetails dateDetails; while (startDate <= EndDate) { dateDetails = new EventDateDetails(startDate); dateData = new Tuple <int, int, DateTime>(dateDetails.year, dateDetails.WeekNumber, startDate); dates.Add(dateData); startDate = startDate.AddDays(1); } return(dates); }
/// <summary> /// CopyBlobFromSourceToBackup: /// - Copies blob from source SA container to destination/backup SA container /// </summary> /// <returns></returns> public async Task <DestinationBlobInfo> CopyBlobFromSourceToBackup(IBlobEvent eventData) { DestinationBlobInfo destinationBlobInfo = null; string destinationStorageAccountConnectionString = Environment.GetEnvironmentVariable("RES_STORAGE_ACCOUNT_CONN"); string sourceStorageAccountConnectionString = Environment.GetEnvironmentVariable("SRC_STORAGE_ACCOUNT_CONN"); if (eventData is BlobEvent <CreatedEventData> ) { // Retrieve the storage account from the connection string. CloudStorageAccount sourceStorageAccount = CloudStorageAccount.Parse(sourceStorageAccountConnectionString); CloudBlobClient sourceBlobClient = sourceStorageAccount.CreateCloudBlobClient(); // Retrieve the storage account from the connection string. CloudStorageAccount destinationStorageAccount = CloudStorageAccount.Parse(destinationStorageAccountConnectionString); CloudBlobClient destinationBlobClient = destinationStorageAccount.CreateCloudBlobClient(); BlobEvent <CreatedEventData> createdEventData = (BlobEvent <CreatedEventData>)eventData; string url = createdEventData.data.url; CloudBlockBlob sourceBlockBlob = new CloudBlockBlob(new Uri(url), sourceBlobClient); bool sourceBlobExists = await sourceBlockBlob.ExistsAsync(); if (sourceBlobExists) { long blobSize = sourceBlockBlob.Properties.Length; EventDateDetails dateDetails = new EventDateDetails(createdEventData.eventTime); string destinationContaninerName = dateDetails.year.ToString(); string destinationBlobName = $"wk{dateDetails.WeekNumber}/dy{(int)dateDetails.DayOfWeek}/{sourceBlockBlob.Container.Name}/{sourceBlockBlob.Name}"; destinationBlobName += "."; destinationBlobName += DateTimeUtil.GetString; CloudBlobContainer destinationContainer = destinationBlobClient.GetContainerReference(destinationContaninerName); bool result = await destinationContainer.CreateIfNotExistsAsync(); CloudBlockBlob destinationBlob = destinationContainer.GetBlockBlobReference(destinationBlobName); _logger.LogInformation($"About to sync copy from Container: {sourceBlockBlob.Container.Name}, Blob: {sourceBlockBlob.Name}. Blob size: {sourceBlockBlob.Properties.Length} bytes"); // copyResult = "SYNCCOPY"; string copyResult = await destinationBlob.StartCopyAsync(sourceBlockBlob); destinationBlobInfo = new DestinationBlobInfo(); destinationBlobInfo.ContainerName = destinationContainer.Name; destinationBlobInfo.BlobName = destinationBlobName; destinationBlobInfo.CopyReferenceId = copyResult; destinationBlobInfo.OrgContainerName = sourceBlockBlob.Container.Name; destinationBlobInfo.OrgBlobName = sourceBlockBlob.Name; _logger.LogInformation($"Copy Scheduled. Source Blob Name: {sourceBlockBlob.Name}, Destination Blob Name: {destinationBlobInfo.BlobName}, Copy Id: {copyResult}."); return(destinationBlobInfo); } else { _logger.LogInformation($"Not able to locate the block blob in source storage account---Block blob Name: {sourceBlockBlob.Name}"); } } else { _logger.LogInformation($"Input event data is not of Created Event Type."); } return(destinationBlobInfo); }
/// <summary> /// CopyBlobFromSourceToBackup: /// - Copies blob from source SA container to destination/backup SA container /// </summary> /// <returns></returns> public async Task <DestinationBlobInfo> CopyBlobFromSourceToBackup(IBlobEvent eventData) { DestinationBlobInfo destinationBlobInfo = null; string destinationStorageAccountConnectionString = Environment.GetEnvironmentVariable("RES_STORAGE_ACCOUNT_CONN"); string sourceStorageAccountConnectionString = Environment.GetEnvironmentVariable("SRC_STORAGE_ACCOUNT_CONN"); int blobTier = string.IsNullOrEmpty(Environment.GetEnvironmentVariable("TargetBlobTier")) ? 2 : int.Parse(Environment.GetEnvironmentVariable("TargetBlobTier")); StandardBlobTier targetBlobTier; // Default tier is set to Cool switch (blobTier) { case 1: targetBlobTier = StandardBlobTier.Hot; break; case 2: targetBlobTier = StandardBlobTier.Cool; break; case 3: targetBlobTier = StandardBlobTier.Archive; break; default: targetBlobTier = StandardBlobTier.Cool; break; } ; if (eventData is BlobEvent <CreatedEventData> ) { // Retrieve the storage account from the connection string. CloudStorageAccount sourceStorageAccount = CloudStorageAccount.Parse(sourceStorageAccountConnectionString); CloudBlobClient sourceBlobClient = sourceStorageAccount.CreateCloudBlobClient(); // Retrieve the storage account from the connection string. CloudStorageAccount destinationStorageAccount = CloudStorageAccount.Parse(destinationStorageAccountConnectionString); CloudBlobClient destinationBlobClient = destinationStorageAccount.CreateCloudBlobClient(); BlobEvent <CreatedEventData> createdEventData = (BlobEvent <CreatedEventData>)eventData; string url = createdEventData.data.url; CloudBlockBlob sourceBlockBlob = new CloudBlockBlob(new Uri(url), sourceBlobClient); bool sourceBlobExists = await sourceBlockBlob.ExistsAsync(); if (sourceBlobExists) { long blobSize = sourceBlockBlob.Properties.Length; EventDateDetails dateDetails = new EventDateDetails(createdEventData.eventTime); string destinationContaninerName = dateDetails.year.ToString(); string destinationBlobName = $"wk{dateDetails.WeekNumber}/dy{(int)dateDetails.DayOfWeek}/{sourceBlockBlob.Container.Name}/{sourceBlockBlob.Name}"; destinationBlobName += "."; destinationBlobName += DateTimeUtil.GetString; CloudBlobContainer destinationContainer = destinationBlobClient.GetContainerReference(destinationContaninerName); bool result = await destinationContainer.CreateIfNotExistsAsync(); CloudBlockBlob destinationBlob = destinationContainer.GetBlockBlobReference(destinationBlobName); _logger.LogInformation($"About to sync copy from Container: {sourceBlockBlob.Container.Name}, Blob: {sourceBlockBlob.Name}. Blob size: {sourceBlockBlob.Properties.Length} bytes"); // copyResult = "SYNCCOPY"; string copyResult = await destinationBlob.StartCopyAsync( sourceBlockBlob, targetBlobTier, null, // Rehydrate priority null, // Source access condition null, // Destination access condition null, // Blob request options null, // Operation context (new CancellationTokenSource()).Token); destinationBlobInfo = new DestinationBlobInfo(); destinationBlobInfo.ContainerName = destinationContainer.Name; destinationBlobInfo.BlobName = destinationBlobName; destinationBlobInfo.CopyReferenceId = copyResult; destinationBlobInfo.OrgContainerName = sourceBlockBlob.Container.Name; destinationBlobInfo.OrgBlobName = sourceBlockBlob.Name; _logger.LogInformation($"Copy Scheduled. Source Blob Name: {sourceBlockBlob.Name}, Destination Blob Name: {destinationBlobInfo.BlobName}, Copy Id: {copyResult}."); return(destinationBlobInfo); } else { _logger.LogInformation($"Not able to locate the block blob in source storage account---Block blob Name: {sourceBlockBlob.Name}"); } } else { _logger.LogInformation($"Input event data is not of Created Event Type."); } return(destinationBlobInfo); }
/// <summary> /// CopyBlobFromSourceToBackup /// </summary> /// <returns></returns> public async Task <DestinationBlobInfo> CopyBlobFromSourceToBackup(IBlobEvent eventData) { DestinationBlobInfo destinationBlobInfo = null; string destinationStorageAccountConnectionString = _config.GetConnectionString("BackupBlobStorage"); string sourceStorageAccountConnectionString = _config.GetConnectionString("SourceBlobStorage"); bool isServerCopy = bool.Parse(_config.GetSection("AppSettings")["IsServerCopy"]); if (eventData is BlobEvent <CreatedEventData> ) { // Retrieve the storage account from the connection string. CloudStorageAccount sourceStorageAccount = CloudStorageAccount.Parse(sourceStorageAccountConnectionString); CloudBlobClient sourceBlobClient = sourceStorageAccount.CreateCloudBlobClient(); // Retrieve the storage account from the connection string. CloudStorageAccount destinationStorageAccount = CloudStorageAccount.Parse(destinationStorageAccountConnectionString); CloudBlobClient destinationBlobClient = destinationStorageAccount.CreateCloudBlobClient(); BlobEvent <CreatedEventData> createdEventData = (BlobEvent <CreatedEventData>)eventData; string url = createdEventData.data.url; CloudBlockBlob sourceBlockBlob = new CloudBlockBlob(new Uri(url), sourceBlobClient); bool sourceBlobExists = await sourceBlockBlob.ExistsAsync(); if (sourceBlobExists) { long blobSize = sourceBlockBlob.Properties.Length; EventDateDetails dateDetails = new EventDateDetails(createdEventData.eventTime); string destinationContaninerName = dateDetails.year.ToString(); string destinationBlobName = $"wk{dateDetails.WeekNumber}/dy{(int)dateDetails.DayOfWeek}/{sourceBlockBlob.Container.Name}/{sourceBlockBlob.Name}"; CloudBlobContainer destinationContainer = destinationBlobClient.GetContainerReference(destinationContaninerName); bool result = await destinationContainer.CreateIfNotExistsAsync(); CloudBlockBlob destinationBlob = destinationContainer.GetBlockBlobReference(destinationBlobName); string copyResult = string.Empty; if (isServerCopy) { string blobToken = GenerateSASBlobToken(sourceBlockBlob); _logger.LogInformation($"About to server copy {sourceBlockBlob.Name}. Blob size {sourceBlockBlob.Properties.Length} bytes"); copyResult = await destinationBlob.StartCopyAsync(new Uri(sourceBlockBlob.Uri.AbsoluteUri + blobToken)); } else { _logger.LogInformation($"About to sync copy {sourceBlockBlob.Name}. Blob size {sourceBlockBlob.Properties.Length} bytes"); copyResult = "SYNCCOPY"; await TransferManager.CopyAsync(sourceBlockBlob, destinationBlob, false); } destinationBlobInfo = new DestinationBlobInfo(); destinationBlobInfo.ContainerName = destinationContainer.Name; destinationBlobInfo.BlobName = destinationBlobName; destinationBlobInfo.CopyReferenceId = copyResult; destinationBlobInfo.OrgContainerName = sourceBlockBlob.Container.Name; destinationBlobInfo.OrgBlobName = sourceBlockBlob.Name; return(destinationBlobInfo); } else { _logger.LogInformation($"Not able to locate the block blob in source storage account---Block blob Name {sourceBlockBlob.Name}"); } } else { _logger.LogInformation($"Input event data is not of Created Event Type."); } return(destinationBlobInfo); }