private static BackupExistResult IsBackupExist(string backupDirectory) { BackupExistResult directoryExistResult = new BackupExistResult(); DirectoryInfo directoryInfo = new DirectoryInfo(backupDirectory); Regex backupDirDateRegex = new Regex(ConfigHelper.GetSetting <string>("BkpFolderDatePatternRegex")); // ^\d{2}-\d{2}-\d{4}(_(\d+))?$ IOrderedEnumerable <DirectoryInfo> topDirResult = directoryInfo.GetDirectories("*", SearchOption.TopDirectoryOnly) .Where(D => backupDirDateRegex.IsMatch(D.Name)) .OrderByDescending(O => O.CreationTimeUtc); DirectoryInfo todaysBackupDir = topDirResult.FirstOrDefault(D => D.CreationTime.Date == DateTime.Now.Date); if (todaysBackupDir != null) { Match mtchRslt = backupDirDateRegex.Match(todaysBackupDir.Name); int todaysBackupNo = mtchRslt.Groups[2].Success ? Convert.ToInt32(mtchRslt.Groups[2].Value) : 1; directoryExistResult = new BackupExistResult(todaysBackupDir.Name, todaysBackupNo); } directoryExistResult.ExistingBackupList = topDirResult.Select(D => D.FullName); return(directoryExistResult); }
static void Main(string[] args) { List <ConfigModel> configList = null; string backupDirectory = string.Empty; try { if (applicationData == null) { applicationData = AppData.Load(); } applicationData.NoOfTimesAppRan = ++applicationData.NoOfTimesAppRan; applicationData.LastRanTime = DateTime.Now.ToString("dd-MMM-yyyy hh:mm:ss tt"); PrintDescriptionAndAuthorInfo(); backupDirectory = ConfigHelper.GetSetting <string>("BackupFolder"); CreateRootBackupDirIfNotExist(backupDirectory); BackupExistResult bkpExistRslt = IsBackupExist(backupDirectory); string newBackupDirName = bkpExistRslt.GetNewBackupDirName(ConfigHelper.GetSetting <string>("BkpFolderDatePattern")); if (bkpExistRslt.IsTodaysBackupExist) { Console.WriteLine($"Backup Already Exist At {bkpExistRslt.TodaysBackupDirName}. Creating New Backup At {newBackupDirName}\n"); } configList = LoadBackupConfigFile(); string todaysBackupDir = Path.Combine(backupDirectory, newBackupDirName); TakeBackup(configList, todaysBackupDir); applicationData.NoOfFilesBackedUp += fileCount; applicationData.NoOfDirectoriesBackedUp += dirCount; applicationData.Save(); // Delete backup after taking backup if (ConfigHelper.GetSetting <bool>("IsDelOldBackups")) { DeleteExistingBackups(bkpExistRslt.ExistingBackupList); } Console.WriteLine($"\nBackup Completed. Copied {fileCount} files and {dirCount} folders"); } catch (Exception ex) { File.AppendAllText("ExceptionDetail.txt", $"{DateTime.Now:dd/MM/yyyy hh:ss:ss tt}\r\n{ex}\r\n"); SendErrorMail(ex); } if (ConfigHelper.GetSetting <bool>("SendBackupSuccessMail")) { string recepiant = ConfigHelper.GetSetting <string>("BackupSuccessMailRecepiants"); if (string.IsNullOrEmpty(recepiant)) { return; } string backupSuccessSubject = $"{ConfigHelper.GetSetting<string>("BackupSuccessMailSubject")} - {DateTime.Now:dd-MM-yyyy}"; IEnumerable <ConfigModel> srcDirCnt = configList.Where(C => C.ConfigPattern == ConfigPatterns.SourceFolder); var foldersOrFolderTxt = srcDirCnt.Count() > 1 ? "folders" : "folder"; StringBuilder mailBody = new StringBuilder(string.Empty); mailBody.AppendLine($"Backup of below {foldersOrFolderTxt} successfully created at {backupDirectory} on machine {Environment.MachineName}"); foreach (var item in srcDirCnt) { mailBody.AppendLine(item.Value); } if (ConfigHelper.GetSetting <bool>("IsDelOldBackups")) { mailBody.AppendLine($"\"Delete Previous Backups\" setting is enabled and only keeping last {ConfigHelper.GetSetting<int>("DelOldBackupGreaterThan")} backups."); } SendMail(recepiant, backupSuccessSubject, mailBody.ToString()); } if (ConfigHelper.GetSetting <bool>("ShowConsoleAfterComplete")) { Console.ReadKey(); } }