forked from TheByteStuff/AzureTableBackupRestore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandLineTool.cs
397 lines (368 loc) · 21.4 KB
/
CommandLineTool.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
using System;
using System.IO;
using System.Configuration;
using System.Collections.Generic;
using System.Security;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Binder;
using Microsoft.Extensions.Configuration.FileExtensions;
using Microsoft.Extensions.Configuration.Json;
using TheByteStuff.AzureTableUtilities;
namespace TheByteStuff.AzureTableBackupRestore
{
public class CommandLineTool : CommandLineBase
{
private readonly static string ConfigFileNameParmName = "ConfigFileName";
private readonly static string ConfigFilePathParmName = "ConfigFilePath";
private readonly static string AzureBlobStorageConfigConnectionParmName = "AzureBlobStorageConfigConnection";
private readonly static string AzureStorageConfigConnectionParmName = "AzureStorageConfigConnection";
private readonly static string AzureStorageConfigConnectionDestinationParmName = "AzureStorageDestinationConfigConnection";
private readonly static string CommandNameParmName = "Command";
private readonly static string TargetParmName = "Target";
private readonly static string CompressParmName = "Compress";
private readonly static string ValidateParmName = "Validate";
private readonly static string RetentionDaysParmName = "RetentionDays";
private readonly static string TimeoutSecondsParmName = "TimeoutSeconds";
private readonly static string TableNameParmName = "TableName";
private readonly static string DestinationTableNameParmName = "DestinationTableName";
private readonly static string OriginalTableNameParmName = "OriginalTableName";
private readonly static string TableNameToDeleteParmName = "TableNameToDelete";
private readonly static string BlobRootParmName = "BlobRoot";
private readonly static string BlobFileNameParmName = "BlobFileName";
private readonly static string WorkingDirectoryParmName = "WorkingDirectory";
private readonly static string OutFileDirectoryParmName = "OutFileDirectory";
private readonly static string RestoreFileNamePathParmName = "RestoreFileNamePath";
private readonly static string HelpParmName = "help";
private readonly static string HelpParmName2 = "-h";
private SecureString AzureBlobStorageConfigConnection = new SecureString();
private SecureString AzureStorageConfigConnection = new SecureString();
private SecureString AzureStorageDestinationConfigConnection = new SecureString();
private void WriteOutput(string s)
{
Console.WriteLine(s);
}
private void DisplayHelp()
{
string[] ArgOptions = new string[] { ConfigFileNameParmName //0
, ConfigFilePathParmName // 1
, AzureBlobStorageConfigConnectionParmName //2
, AzureStorageConfigConnectionParmName //3
, CommandNameParmName //4
, TargetParmName //5
, CompressParmName //6
, ValidateParmName //7
, RetentionDaysParmName //8
, TimeoutSecondsParmName //9
, TableNameParmName //10
, DestinationTableNameParmName //11
, OriginalTableNameParmName //12
, BlobRootParmName //13
, BlobFileNameParmName //14
, WorkingDirectoryParmName //15
, OutFileDirectoryParmName //16
, RestoreFileNamePathParmName //17
, AzureStorageConfigConnectionDestinationParmName //18
, TableNameToDeleteParmName //19
};
WriteOutput("Backup, Copy, Delete and Restore Azure Tables. Backup maybe to local system file or Azure blob storage. Restore may be from local system file or from Azure blob storage. Command line parameter values will override a value in the settings file.");
WriteOutput("");
WriteOutput(String.Format("AzureTableBackupRestore [{0}] [{1}] [{2}] [{3}] [{18}] [{4}] [{5}] [{6}] [{7}] [{8}] [{9}] [{10}] [{11}] [{12}] [{13}] [{14}] [{15}] [{16}] [{17}]", ArgOptions));
WriteOutput("");
WriteOutput(String.Format("{0}=<Configuration File Name> (default appsettings.json)", ArgOptions));
WriteOutput(String.Format("{1}=<Configuration File Path> (default current directory)", ArgOptions));
WriteOutput(String.Format("{2}=<ConnectionSpec> (or use Configuration File)", ArgOptions));
WriteOutput(String.Format("{3}=<ConnectionSpec> (or use Configuration File)", ArgOptions));
WriteOutput(String.Format("{18}=<ConnectionSpec> (or use Configuration File)", ArgOptions));
WriteOutput(String.Format("{4}=<Backup|Copy|Delete|Restore>", ArgOptions));
WriteOutput(String.Format("{5}=<Blob|File|Row|Table|All> (indicates source/destination for backup/restore or Row vs Table for delete)", ArgOptions));
WriteOutput(String.Format("{5}=<ToBlob|ToFile|FromBlob|FromFile|Blob|BlobDirect|File> (indicates source/destination for backup/restore)", ArgOptions));
WriteOutput(String.Format("{6}=<true|false>, valid on backup (or use Configuration File)", ArgOptions));
WriteOutput(String.Format("{7}=<true|false> (or use Configuration File)", ArgOptions));
WriteOutput(String.Format("{8}=<int> Number of days to retain file, valid on backup (or use Configuration File)", ArgOptions));
WriteOutput(String.Format("{9}=<int> Azure call timeout in seconds (or use Configuration File)", ArgOptions));
WriteOutput(String.Format("{10}=<name of table>, valid on backup (or use Configuration File)", ArgOptions));
WriteOutput(String.Format("{11}=<Table name to restore or copy to>, valid on copy/restore (or use Configuration File)", ArgOptions));
WriteOutput(String.Format("{19}=<Table name to delete>, valid on delete (or use Configuration File)", ArgOptions));
WriteOutput(String.Format("{12}=<Name of Original Table as backed up>, valid on restore (or use Configuration File)", ArgOptions));
WriteOutput(String.Format("{13}=<Azure Root Blob> (or use Configuration File)", ArgOptions));
WriteOutput(String.Format("{14}=<Azure Blob File Name>, valid on restore only (or use Configuration File)", ArgOptions));
WriteOutput(String.Format("{15}=<Local directory with write permission> (or use Configuration File)", ArgOptions));
WriteOutput(String.Format("{16}=<Local directory for file output>, valid on backup (or use Configuration File)", ArgOptions));
WriteOutput(String.Format("{17}=<Path and Name of file to restore>, valid on restore (or use Configuration File)", ArgOptions));
WriteOutput("");
WriteOutput(String.Format("{0} and {1} are command line only, all other parms may be specified in config file as well.", ArgOptions));
//WriteOutput("see more at...");
}
public static void Main(string[] args)
{
CommandLineTool instance = new CommandLineTool();
instance.StoreArguments(args);
try
{
if (instance.IsParmOnInput(HelpParmName) || instance.IsParmOnInput(HelpParmName2))
{
instance.DisplayHelp();
}
else
{
instance.WriteOutput(String.Format("using configuration file path '{0}'", instance.GetCommandLineParameterValue(ConfigFilePathParmName, Directory.GetCurrentDirectory())));
instance.WriteOutput(String.Format("using configuration file name '{0}'", instance.GetCommandLineParameterValue(ConfigFileNameParmName, "appsettings.json")));
var builder = new ConfigurationBuilder()
.SetBasePath(instance.GetCommandLineParameterValue(ConfigFilePathParmName, Directory.GetCurrentDirectory()))
.AddJsonFile(instance.GetCommandLineParameterValue(ConfigFileNameParmName, "appsettings.json"));
IConfiguration config = builder.Build();
foreach (char c in instance.GetCommandLineParameterValue(AzureStorageConfigConnectionParmName, Microsoft.Extensions.Configuration.ConfigurationExtensions.GetConnectionString(config, AzureStorageConfigConnectionParmName)).ToCharArray())
{
instance.AzureStorageConfigConnection.AppendChar(c);
}
foreach (char c in instance.GetCommandLineParameterValue(AzureStorageConfigConnectionDestinationParmName, Microsoft.Extensions.Configuration.ConfigurationExtensions.GetConnectionString(config, AzureStorageConfigConnectionDestinationParmName)).ToCharArray())
{
instance.AzureStorageDestinationConfigConnection.AppendChar(c);
}
foreach (char c in instance.GetCommandLineParameterValue(AzureBlobStorageConfigConnectionParmName, Microsoft.Extensions.Configuration.ConfigurationExtensions.GetConnectionString(config, AzureBlobStorageConfigConnectionParmName)).ToCharArray())
{
instance.AzureBlobStorageConfigConnection.AppendChar(c);
}
string Command = instance.GetFromParmOrFile(config, CommandNameParmName);
if ("backup".Equals(Command) || "copy".Equals(Command) || "restore".Equals(Command) || "delete".Equals(Command))
{
if ("backup".Equals(Command))
{
instance.WriteOutput("Beginning backup process.");
instance.WriteOutput(String.Format("{0}", instance.Backup(instance.AzureBlobStorageConfigConnection, instance.AzureStorageConfigConnection, config)));
}
else if ("copy".Equals(Command))
{
instance.WriteOutput("Beginning copy process.");
instance.WriteOutput(String.Format("{0}", instance.Copy(instance.AzureStorageConfigConnection, instance.AzureStorageDestinationConfigConnection, config)));
}
else if ("delete".Equals(Command))
{
instance.WriteOutput("Beginning delete process.");
instance.WriteOutput(String.Format("{0}", instance.Delete(instance.AzureStorageConfigConnection, config)));
}
else
{
instance.WriteOutput("Beginning restore process.");
instance.WriteOutput(String.Format("{0}", instance.Restore(instance.AzureBlobStorageConfigConnection, instance.AzureStorageConfigConnection, config)));
}
instance.WriteOutput("AzureTableUtilities ending.");
}
else
{
instance.WriteOutput(String.Format("Valid {0} values are 'backup', 'copy, 'delete' and 'restore'", CommandNameParmName));
instance.DisplayHelp();
}
}
}
catch (Exception ex)
{
instance.DisplayHelp();
instance.WriteOutput(" ");
instance.WriteOutput(String.Format("Exception: {0}, {1}", ex.Message, ex.StackTrace));
}
}
private string GetFromParmOrFile(IConfiguration config, string ParmName)
{
return this.GetCommandLineParameterValue(ParmName, config[ParmName]);
}
private bool GetBoolFromParmOrFile(IConfiguration config, string ParmName)
{
WriteOutput(String.Format("Parsing {0}", ParmName));
return "true".Equals(GetFromParmOrFile(config, ParmName).ToLower());
}
private int GetIntFromParmOrFile(IConfiguration config, string ParmName)
{
WriteOutput(String.Format("Parsing {0}", ParmName));
return int.Parse(GetFromParmOrFile(config, ParmName));
}
private string Restore(SecureString AzureBlobStorageConfigConnection, SecureString AzureStorageConfigConnection, IConfiguration config)
{
try
{
RestoreAzureTables me = new RestoreAzureTables(AzureStorageConfigConnection, AzureBlobStorageConfigConnection);
string Target = GetFromParmOrFile(config, TargetParmName).ToLower();
if (!String.IsNullOrEmpty(Target))
{
if (Target.Contains("file"))
{
return me.RestoreTableFromFile(GetFromParmOrFile(config, DestinationTableNameParmName),
GetFromParmOrFile(config, RestoreFileNamePathParmName),
GetIntFromParmOrFile(config, TimeoutSecondsParmName));
}
else if (Target.Contains("blobdirect"))
{
return me.RestoreTableFromBlobDirect(GetFromParmOrFile(config, DestinationTableNameParmName),
GetFromParmOrFile(config, OriginalTableNameParmName),
GetFromParmOrFile(config, BlobRootParmName),
GetFromParmOrFile(config, BlobFileNameParmName),
GetIntFromParmOrFile(config, TimeoutSecondsParmName));
}
else if (Target.Contains("blob"))
{
return me.RestoreTableFromBlob(GetFromParmOrFile(config, DestinationTableNameParmName),
GetFromParmOrFile(config, OriginalTableNameParmName),
GetFromParmOrFile(config, BlobRootParmName),
GetFromParmOrFile(config, WorkingDirectoryParmName),
GetFromParmOrFile(config, BlobFileNameParmName),
GetIntFromParmOrFile(config, TimeoutSecondsParmName));
}
else
{
throw new Exception("Missing or invalid configuration for requested command.");
}
}
else
{
throw new Exception("Missing or invalid configuration for requested command.");
}
}
catch (Exception ex)
{
throw ex;
}
}
private string Backup(SecureString AzureBlobStorageConfigConnection, SecureString AzureStorageConfigConnection, IConfiguration config)
{
try
{
BackupAzureTables me = new BackupAzureTables(AzureStorageConfigConnection, AzureBlobStorageConfigConnection);
string Target = GetFromParmOrFile(config, TargetParmName).ToLower();
if (!String.IsNullOrEmpty(Target))
{
var sectionFilters = config.GetSection("Filters");
List<Filter> filters = sectionFilters.Get<List<Filter>>();
if (!Filter.AreFiltersValid(filters))
{
throw new Exception("One or more of the supplied filter cirteria is invalid.");
}
if (Target.Contains("file"))
{
return me.BackupTableToFile(GetFromParmOrFile(config, TableNameParmName),
GetFromParmOrFile(config, OutFileDirectoryParmName),
GetBoolFromParmOrFile(config, CompressParmName),
GetBoolFromParmOrFile(config, ValidateParmName),
GetIntFromParmOrFile(config, TimeoutSecondsParmName),
filters);
}
else if (Target.Contains("blobdirect"))
{
return me.BackupTableToBlobDirect(GetFromParmOrFile(config, TableNameParmName),
GetFromParmOrFile(config, BlobRootParmName),
GetBoolFromParmOrFile(config, CompressParmName),
GetIntFromParmOrFile(config, RetentionDaysParmName),
GetIntFromParmOrFile(config, TimeoutSecondsParmName),
filters);
}
else if (Target.Contains("blob"))
{
return me.BackupTableToBlob(GetFromParmOrFile(config, TableNameParmName),
GetFromParmOrFile(config, BlobRootParmName),
GetFromParmOrFile(config, OutFileDirectoryParmName),
GetBoolFromParmOrFile(config, CompressParmName),
GetBoolFromParmOrFile(config, ValidateParmName),
GetIntFromParmOrFile(config, RetentionDaysParmName),
GetIntFromParmOrFile(config, TimeoutSecondsParmName),
filters);
}
else if (Target.Contains("all"))
{
return me.BackupAllTablesToBlob(GetFromParmOrFile(config, BlobRootParmName),
GetBoolFromParmOrFile(config, CompressParmName),
GetIntFromParmOrFile(config, RetentionDaysParmName),
GetIntFromParmOrFile(config, TimeoutSecondsParmName),
filters);
}
else
{
throw new Exception("Missing or invalid configuration for requested command.");
}
}
else
{
throw new Exception("Missing or invalid configuration for requested command.");
}
}
catch (Exception ex)
{
throw ex;
}
}
private string Copy(SecureString AzureStorageSourceConfigConnection, SecureString AzureStorageDestinationConfigConnection, IConfiguration config)
{
try
{
CopyAzureTables me = new CopyAzureTables(AzureStorageSourceConfigConnection, AzureStorageDestinationConfigConnection);
string Target = GetFromParmOrFile(config, TargetParmName).ToLower();
var sectionFilters = config.GetSection("Filters");
List<Filter> filters = sectionFilters.Get<List<Filter>>();
if (!Filter.AreFiltersValid(filters))
{
throw new Exception("One or more of the supplied filter cirteria is invalid.");
}
if (String.IsNullOrEmpty(Target) || Target.Contains("table"))
{
return me.CopyTableToTable(GetFromParmOrFile(config, TableNameParmName),
GetFromParmOrFile(config, DestinationTableNameParmName),
GetIntFromParmOrFile(config, TimeoutSecondsParmName),
filters);
}
else if (Target.Contains("all"))
{
return me.CopyAllTables(GetIntFromParmOrFile(config, TimeoutSecondsParmName),
filters);
}
else
{
throw new Exception("Missing or invalid configuration for requested command.");
}
}
catch (Exception ex)
{
throw ex;
}
}
private string Delete(SecureString AzureBlobStorageConfigConnection, IConfiguration config)
{
try
{
DeleteAzureTables me = new DeleteAzureTables(AzureStorageConfigConnection);
string Target = GetFromParmOrFile(config, TargetParmName).ToLower();
if (!String.IsNullOrEmpty(Target))
{
var sectionFilters = config.GetSection("Filters");
List<Filter> filters = sectionFilters.Get<List<Filter>>();
if (!Filter.AreFiltersValid(filters))
{
throw new Exception("One or more of the supplied filter cirteria is invalid.");
}
if (Target.Contains("rows"))
{
return me.DeleteAzureTableRows(GetFromParmOrFile(config, TableNameToDeleteParmName)
, GetIntFromParmOrFile(config, TimeoutSecondsParmName)
,filters
);
}
else if (Target.Contains("table"))
{
return me.DeleteAzureTable(GetFromParmOrFile(config, TableNameToDeleteParmName)
, GetIntFromParmOrFile(config, TimeoutSecondsParmName)
);
}
else
{
throw new Exception("Missing or invalid configuration for requested command.");
}
}
else
{
throw new Exception("Missing or invalid configuration for requested command.");
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}