-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
550 lines (465 loc) · 23.4 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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.IO;
using System.IO.Compression;
using System.Net.Mail;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Auth;
namespace WebServerBackUp
{
class Program
{
private static String _resultmsg = "";
static void Main(string[] args)
{
try
{
_resultmsg = "";
BackupDB();
BackupWebSites();
IISSettings();
IISlogFiles();
CopyToAzure();
if (_resultmsg == "") _resultmsg = "OK," + DateTime.UtcNow.ToString();
ResultFile(_resultmsg);
CheckBackup();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
ResultFile("ERROR," + ex.ToString());
}
}
static String GetSetting(string key)
{
try
{
var appSettings = ConfigurationManager.AppSettings;
string result = appSettings[key] ?? "";
return result;
}
catch (ConfigurationErrorsException)
{
Console.WriteLine("Error reading app settings");
return "";
}
}
private static void IISlogFiles()
{
var clearIISLogFiles = GetSetting("ClearIISLogFiles");
if (clearIISLogFiles.ToLower() == "true")
{
Console.WriteLine("IIS Log Files");
var logFileFolder = GetSetting("LogFileFolder");
var logFileRetensionDays = GetSetting("LogFileRetensionDays");
ExecuteCommandSync("forfiles /p \"" + logFileFolder + "\" /s /m *.* /c \"cmd /c Del @path\" /d -" + logFileRetensionDays);
}
}
private static void IISSettings()
{
var iisSettingsBackup = GetSetting("IISSettingsBackup");
var webZipFolder = GetSetting("WebZipFolder");
if (webZipFolder != "" && iisSettingsBackup.ToLower() == "true")
{
Console.WriteLine("IIS Settings");
if (File.Exists(webZipFolder + "\\iis_settings.zip"))
{
File.Delete(webZipFolder + "\\iis_settings.zip");
}
ExecuteCommandSync("%windir%\\system32\\inetsrv\\appcmd list apppool/config/xml > " + webZipFolder + "\\iis_apppools.xml");
ExecuteCommandSync("%windir%\\system32\\inetsrv\\appcmd list site/config/xml > " + webZipFolder + "\\iis_sites.xml");
ZipArchive zip = ZipFile.Open(webZipFolder + "\\iis_settings.zip", ZipArchiveMode.Create);
var file = webZipFolder + "\\iis_apppools.xml";
zip.CreateEntryFromFile(file, Path.GetFileName(file), CompressionLevel.Optimal);
File.Delete(file);
file = webZipFolder + "\\iis_sites.xml";
zip.CreateEntryFromFile(file, Path.GetFileName(file), CompressionLevel.Optimal);
File.Delete(file);
zip.Dispose();
}
}
static void BackupDB()
{
var dataBaseBackup = GetSetting("DataBaseBackup");
if (dataBaseBackup.ToLower() == "true")
{
Console.WriteLine("DB Backup");
var sqlServer = GetSetting("SqlServer");
var sqlAdmin = GetSetting("SqlAdmin");
var sqlAdminPass = GetSetting("SqlAdminPass");
var backupDBFolder = GetSetting("BackupDBFolder");
var backupDBZipPathName = GetSetting("BackupDBZipPathName");
var compressDBBackup = GetSetting("CompressDBBackup");
// remove any existing backup files.
var files1 = System.IO.Directory.GetFiles(backupDBFolder, "*.bak");
foreach (string file in files1)
{
File.Delete(file);
}
Server myServer = new Server(sqlServer);
myServer.ConnectionContext.LoginSecure = false;
myServer.ConnectionContext.Login = sqlAdmin;
myServer.ConnectionContext.Password = sqlAdminPass;
myServer.ConnectionContext.Connect();
foreach (Database myDatabase in myServer.Databases)
{
if (myDatabase.ID > 4 && myDatabase.Status == DatabaseStatus.Normal)
{
Backup bkpDBFull = new Backup();
/* Specify whether you want to back up database or files or log */
bkpDBFull.Action = BackupActionType.Database;
bkpDBFull.Database = myDatabase.Name;
bkpDBFull.Devices.AddDevice(backupDBFolder.TrimEnd('\\') + "\\" + myDatabase.Name + ".bak", DeviceType.File);
bkpDBFull.SqlBackup(myServer);
}
}
if (myServer.ConnectionContext.IsOpen) myServer.ConnectionContext.Disconnect();
if (compressDBBackup.ToLower() == "true" && backupDBZipPathName.ToLower().EndsWith(".zip"))
{
File.Delete(backupDBZipPathName);
// zip .bak files.
var files = System.IO.Directory.GetFiles(backupDBFolder, "*.bak");
ZipArchive zip = ZipFile.Open(backupDBZipPathName, ZipArchiveMode.Create);
foreach (string file in files)
{
zip.CreateEntryFromFile(file, Path.GetFileName(file), CompressionLevel.Optimal);
File.Delete(file);
}
zip.Dispose();
}
}
}
static void BackupWebSites()
{
var websiteBackup = GetSetting("WebsiteBackup");
if (websiteBackup.ToLower() == "true")
{
Console.WriteLine("Website Backup");
var webRootFolder = GetSetting("WebRootFolder");
var webZipFolder = GetSetting("WebZipFolder");
// remove any existing backup files.
var files1 = System.IO.Directory.GetFiles(webZipFolder, "*.zip");
foreach (string file in files1)
{
File.Delete(file);
}
if (webZipFolder != "" && webRootFolder != "")
{
var lp = 1;
var websitelist = GetListOfWebsite(webRootFolder, new List<string>());
foreach (var webfolder in websitelist)
{
Console.WriteLine(webfolder);
var parentfolder = "";
var zipfolders = webfolder.Split('\\');
if (zipfolders.Count() > 2) parentfolder = zipfolders[zipfolders.Count() - 2] + "_";
var zipfilename = webZipFolder + "\\" + parentfolder + webfolder.Split('\\').Last() + ".zip";
if (File.Exists(zipfilename))
{
_resultmsg += "WARNING, Website zip file '" + parentfolder + webfolder.Split('\\').Last() + ".zip' already exists. 2 or more web installations have the same 'install folder' and 'parent folder' names, you need to rename the parent or install folder.";
}
else
{
ZipArchive zip = ZipFile.Open(zipfilename, ZipArchiveMode.Create);
zip = ZipFilesInDirRecusive(webfolder, "", zip);
zip.Dispose();
lp += 1;
}
}
}
}
}
private static ZipArchive ZipFilesInDirRecusive(String searchFolderPath,String ZipFolderPath, ZipArchive zip)
{
var filelist = Directory.GetFiles(searchFolderPath, "*.*");
if (filelist.Any())
{
foreach (var f in filelist)
{
try
{
zip.CreateEntryFromFile(f, ZipFolderPath + Path.GetFileName(f), CompressionLevel.Optimal);
}
catch (Exception ex)
{
// Ignore file in error, files may be locked.
// there is always the Search write.lock file which throws a lock error.
Console.WriteLine("ERROR: " + f);
if (!ex.ToString().Contains("write.lock"))
{
_resultmsg += "WARNING, Unknown zip error: " + ex.ToString();
}
}
}
}
var dirlist = Directory.GetDirectories(searchFolderPath);
foreach (var d in dirlist)
{
zip = ZipFilesInDirRecusive(d, ZipFolderPath + d.Split('\\').Last() + "\\", zip);
}
return zip;
}
private static List<String> GetListOfWebsite(String searchFolderPath, List<String> websitelist)
{
var filematchname = GetSetting("filematchname");
var filelist = Directory.GetFiles(searchFolderPath, "*" + Path.GetExtension(filematchname));
if (filelist.Any())
{
foreach (var f in filelist)
{
if (f.ToLower().EndsWith(filematchname.ToLower()))
{
websitelist.Add(searchFolderPath);
return websitelist;
}
}
}
var dirlist = Directory.GetDirectories(searchFolderPath);
foreach (var d in dirlist)
{
websitelist = GetListOfWebsite(d, websitelist);
}
return websitelist;
}
private static void CopyToAzure()
{
var copytoazure = GetSetting("copytoazure");
if (copytoazure.ToLower() == "true")
{
Console.WriteLine("Copy to Azure");
var storageConnectionString = GetSetting("StorageConnectionString");
var storageContainer = GetSetting("StorageContainer");
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(storageContainer);
// Create the container if it doesn't already exist.
container.CreateIfNotExists();
// copy DB backup file to webfolder so it's also copied to Azure
var compressDBBackup = GetSetting("CompressDBBackup");
var webZipFolder = GetSetting("WebZipFolder");
if (compressDBBackup.ToLower() == "true")
{
var backupDBZipPathName = GetSetting("BackupDBZipPathName");
var copiedfilenamepath = webZipFolder.Trim('\\') + "\\" + Path.GetFileName(backupDBZipPathName);
if (copiedfilenamepath != backupDBZipPathName)
{
if (File.Exists(copiedfilenamepath)) File.Delete(copiedfilenamepath);
File.Copy(backupDBZipPathName, copiedfilenamepath);
}
}
var retensionminutes = GetSetting("retensionminutes");
var filelist = Directory.GetFiles(webZipFolder, "*.zip");
if (filelist.Any())
{
foreach (var f in filelist)
{
Console.WriteLine("Copy Azure: " + DateTime.UtcNow + " " + f);
// Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference(Path.GetFileName(f));
// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(f))
{
blockBlob.UploadFromStream(fileStream);
}
blockBlob.CreateSnapshot();
}
// purge snapshots
var bloblist = container.ListBlobs(null, true, BlobListingDetails.Snapshots);
foreach (IListBlobItem item in bloblist)
{
//you must cast this as a CloudBlockBlob
// because blobItem does not expose all of the properties
CloudBlockBlob theBlob = item as CloudBlockBlob;
//Call FetchAttributes so it retrieves the metadata.
theBlob.FetchAttributes();
if (theBlob.IsSnapshot)
{
if (theBlob.SnapshotTime.Value.AddMinutes(Convert.ToInt32(retensionminutes)) < DateTime.UtcNow)
{
if (theBlob.SnapshotTime.Value.Day == 1)
{
if (theBlob.SnapshotTime.Value.AddDays(32) < DateTime.UtcNow)
{
theBlob.Delete();
}
}
else
{
theBlob.Delete();
}
}
}
}
}
}
}
private static void ResultFile(String resultmsg)
{
var webZipFolder = GetSetting("WebZipFolder");
var copytoazure = GetSetting("copytoazure");
if (copytoazure.ToLower() == "true" && webZipFolder != "")
{
Console.WriteLine("Send Result File");
var resultfile = webZipFolder.Trim('\\') + "\\resultfile.txt";
if (File.Exists(resultfile)) File.Delete(resultfile);
File.WriteAllText(resultfile, resultmsg);
var storageConnectionString = GetSetting("StorageConnectionString");
var storageContainer = GetSetting("StorageContainer");
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(storageContainer);
// Create the container if it doesn't already exist.
container.CreateIfNotExists();
Console.WriteLine("Azure: " + DateTime.UtcNow + " " + resultfile);
// Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference(Path.GetFileName(resultfile));
// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(resultfile))
{
blockBlob.UploadFromStream(fileStream);
}
}
}
private static void CheckBackup()
{
var checkresultfile = GetSetting("checkresultfile");
if (checkresultfile.ToLower() == "true")
{
var smtp = GetSetting("smtp");
var smtpuser = GetSetting("smtpuser");
var smtppassword = GetSetting("smtppassword");
var email = GetSetting("email");
var checkcontainer = GetSetting("checkcontainer");
var resultfilehours = GetSetting("resultfilehours");
var isOK = true;
var emailMsg = "";
var storageConnectionString = GetSetting("StorageConnectionString");
var storageContainer = GetSetting("StorageContainer");
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
IEnumerable<CloudBlobContainer> containers = blobClient.ListContainers();
foreach (CloudBlobContainer item in containers)
{
// read result file
try
{
var resultBlob = item.GetBlobReference("resultfile.txt");
var source = resultBlob.OpenRead();
var resultdata = "ERROR, Invalid resultfile data";
if (resultBlob.Properties.Length < 32000) // resultfile should always be less than this.
{
byte[] buffer = new byte[resultBlob.Properties.Length];
source.Read(buffer, 0, Convert.ToInt32(resultBlob.Properties.Length));
resultdata = Encoding.UTF8.GetString(buffer);
}
var resultarray = resultdata.Split(',');
if (resultarray.Length >= 1)
{
if (resultarray[0] != "OK")
{
isOK = false;
emailMsg += item.Name + ": " + resultdata + Environment.NewLine;
}
else
{
//Status OK, check date
try
{
if (resultBlob.Properties.LastModified.Value < DateTime.Now.AddHours(Convert.ToInt32(resultfilehours) * -1))
{
isOK = false;
emailMsg += item.Name + ": backup out of date, " + resultBlob.Properties.LastModified.Value.ToString() + ". Expected minimum date: " + DateTime.Now.AddHours(Convert.ToInt32(resultfilehours) * -1) + Environment.NewLine;
}
else
{
emailMsg += item.Name + ": " + resultdata + Environment.NewLine;
}
}
catch (Exception e)
{
emailMsg += item.Name + ": " + e.ToString() + Environment.NewLine;
isOK = false;
}
}
}
else
{
isOK = false;
emailMsg += item.Name + ": ERROR - Invalid Result File." + Environment.NewLine;
}
}
catch (Exception e)
{
isOK = false;
emailMsg += item.Name + ": " + e.ToString() + Environment.NewLine;
Console.WriteLine(e.Message);
}
}
Console.WriteLine(emailMsg);
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient(smtp);
mail.From = new MailAddress(email);
mail.To.Add(email);
if (isOK)
mail.Subject = "WebServerBackup: OK";
else
mail.Subject = "WebServerBackup: FAILED";
mail.Body = emailMsg;
SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential(smtpuser, smtppassword);
SmtpServer.EnableSsl = false;
SmtpServer.Send(mail);
}
catch (Exception e)
{
Console.WriteLine("Email Error : " + e.ToString() + Environment.NewLine);
}
}
}
/// <span class="code-SummaryComment"><summary></span>
/// Executes a shell command synchronously.
/// <span class="code-SummaryComment"></summary></span>
/// <span class="code-SummaryComment"><param name="command">string command</param></span>
/// <span class="code-SummaryComment"><returns>string, as output of the command.</returns></span>
private static void ExecuteCommandSync(object command)
{
// create the ProcessStartInfo using "cmd" as the program to be run,
// and "/c " as the parameters.
// Incidentally, /c tells cmd that we want it to execute the command that follows,
// and then exit.
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();
// Display the command output.
Console.WriteLine(result);
}
}
}