-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
184 lines (165 loc) · 7.73 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using AzureBackupTool.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using Microsoft.Azure.Management;
using Microsoft.Azure.Management.Storage.Models;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using System.Threading.Tasks;
namespace AzureBackupTool
{
class Program
{
public static IConfigurationRoot Configuration;
static void Main(string[] args)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.SetBasePath(Path.Combine(AppContext.BaseDirectory))
.AddJsonFile("appSettings.json", optional: true);
Configuration = builder.Build();
try
{
GetDataForBackup();
}
catch(Exception ex)
{
throw ex;
}
}
public static void GetDataForBackup()
{
using (var context = new AppDBContext(Configuration))
{
IList<DMSServiceInfo> dmsServiceList = new List<DMSServiceInfo>();
dmsServiceList = context.DMSServiceInfo.AsNoTracking()
.Where(x => x.AzStorageConnectionString != null && !x.IsDeleted && x.WantBackup)
.ToList();
string destinationConnectionString = Configuration.GetSection("GenericStorageSettings:ConnectionString").Value;
BlobServiceClient destBlobClient = new BlobServiceClient(destinationConnectionString);
var destContainers = destBlobClient.GetBlobContainers();
//Create Containers if destination does not have
foreach (var service in dmsServiceList)
{
service.AzStorageContainer = service.AzStorageContainer.ToLower();
if (destContainers.Where(b => b.Name == service.AzStorageContainer).Count() == 0)
{
destBlobClient.CreateBlobContainer(service.AzStorageContainer);
}
BlobContainerClient sourceContainerClient = new BlobContainerClient(service.AzStorageConnectionString.Replace(" ", ""), service.AzStorageContainer.Replace(" ", ""));
BlobContainerClient destContainerClient = destBlobClient.GetBlobContainerClient(service.AzStorageContainer);
try
{
DeleteOldFiles(destContainerClient);
}
catch(Exception ex)
{
ExceptionLog log = new ExceptionLog();
log.DMSServiceInfoId = service.Id;
if (ex.InnerException == null)
log.InnerException = string.Empty;
else
log.InnerException = ex.InnerException.Message;
if (ex.StackTrace == null)
log.StackTrace = string.Empty;
else
log.StackTrace = ex.StackTrace.ToString();
log.ExceptionMessage = "Error Executing while Backup- Deleting old files:: "+ ex.Message.ToString();
context.ExceptionLogs.Add(log);
context.SaveChanges();
throw ex;
}
try
{
ExecuteBackup(context,service, sourceContainerClient, destContainerClient).ConfigureAwait(false);
}
catch (Exception ex)
{
ExceptionLog log = new ExceptionLog();
log.DMSServiceInfoId = service.Id;
if (ex.InnerException == null)
log.InnerException = string.Empty;
else
log.InnerException = ex.InnerException.Message;
if (ex.StackTrace == null)
log.StackTrace = string.Empty;
else
log.StackTrace = ex.StackTrace.ToString();
log.ExceptionMessage = "Error Executing while Backup- Executing Backup:: " + ex.Message.ToString();
context.ExceptionLogs.Add(log);
context.SaveChanges();
throw ex;
}
}
}
}
public static void DeleteOldFiles(BlobContainerClient destContainerClient)
{
var blobs = destContainerClient.GetBlobs().ToList();
bool dailyBackup = Configuration.GetSection("ScheduleSettings:Daily").Value.ToLower() == "true" ? true : false;
bool weeklyBackup = Configuration.GetSection("ScheduleSettings:Weekly").Value.ToLower() == "true" ? true : false;
foreach (var blob in blobs)
{
if (dailyBackup && blob.Name.Contains("Daily"))
{
destContainerClient.DeleteBlob(blob.Name);
}
if (weeklyBackup && blob.Name.Contains("Weekly"))
{
destContainerClient.DeleteBlob(blob.Name);
}
}
}
public static async Task ExecuteBackup(AppDBContext context,DMSServiceInfo service, BlobContainerClient sourceContainerClient, BlobContainerClient destContainerClient)
{
var blobList = sourceContainerClient.GetBlobs().ToList();
bool dailyBackup = Configuration.GetSection("ScheduleSettings:Daily").Value.ToLower() == "true" ? true : false;
bool weeklyBackup = Configuration.GetSection("ScheduleSettings:Weekly").Value.ToLower() == "true" ? true : false;
int dailyCounter = 0;
int weeklyCounter = 0;
foreach (var blob in blobList)
{
BlobClient sourceBlob = sourceContainerClient.GetBlobClient(blob.Name);
if (dailyBackup)
{
BlobClient destBlob = destContainerClient.GetBlobClient("Daily/" + blob.Name);
// Start the copy operation.
destBlob.StartCopyFromUri(sourceBlob.Uri);
dailyCounter = dailyCounter + 1;
}
if(weeklyBackup)
{
BlobClient destBlob = destContainerClient.GetBlobClient("Weekly/" + blob.Name);
// Start the copy operation.
destBlob.StartCopyFromUri(sourceBlob.Uri);
weeklyCounter = weeklyCounter + 1;
}
}
if (dailyBackup)
{
AzureBackupLogs log = new AzureBackupLogs();
log.Category = "Daily";
log.ContainerName = service.AzStorageContainer;
log.NoOfBackupFiles = dailyCounter;
log.DMSServiceInfoId = service.Id;
context.AzureBackupLogs.Add(log);
}
if(weeklyBackup)
{
AzureBackupLogs log = new AzureBackupLogs();
log.Category = "Weekly";
log.ContainerName = service.AzStorageContainer;
log.NoOfBackupFiles = weeklyCounter;
log.DMSServiceInfoId = service.Id;
context.AzureBackupLogs.Add(log);
}
context.SaveChanges();
}
}
}