//Email, Local Folder, AWS S3 private void LoadOtherSettings() { try { AWSS3Helper awsHelper = new AWSS3Helper(); AWSS3 = awsHelper.GetConfig(); if (AWSS3 == null) { AWSS3 = new AWSS3Setting(); } EmailHelper emailHelper = new EmailHelper(); Email = emailHelper.GetConfig(); if (Email == null) { Email = new EmailSetting(); } GeneralSettingHelper generalHelper = new GeneralSettingHelper(); General = generalHelper.GetConfig(); if (General == null) { General = new GeneralSetting(); } } catch (Exception ex) { string message = Functions.GetErrorFromException(ex); MessageBox.Show("A problem occurred while loading settings. \n" + message, "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); } }
/// <summary> /// Saves Configuration on File /// </summary> public bool SaveConfig(AWSS3Setting setting) { try { string configFile = Path.Combine(EnVar.AppWorkingPath, "awss3.config"); File.WriteAllText(configFile, JsonConvert.SerializeObject(setting)); return(true); } catch //(Exception ex) { return(false); } }
/// <summary> /// Returns config if saved /// </summary> /// <returns></returns> public AWSS3Setting GetConfig() { try { string configFile = Path.Combine(EnVar.AppWorkingPath, "awss3.config"); if (!File.Exists(configFile)) { return(null); } string fileText = File.ReadAllText(configFile); AWSS3Setting setting = JsonConvert.DeserializeObject <AWSS3Setting>(fileText); return(setting); } catch //(Exception ex) { return(null); } }
/// <summary> /// Returns config if saved /// </summary> /// <returns></returns> public AWSS3Setting GetConfig() { try { string configFile = Path.Combine(EnVar.AppWorkingPath, "awss3.config"); if (!File.Exists(configFile)) { return(null); } string fileText = File.ReadAllText(configFile); AWSS3Setting setting = JsonConvert.DeserializeObject <AWSS3Setting>(fileText); return(setting); } catch (Exception ex) { LogHelper.LogMessage("error", "Unable to load AWS S3 Settings | " + Functions.GetErrorFromException(ex)); return(null); } }
private void GenerateSummary() { AwsS3SettingColor = "LightGray"; EmailSettingColor = "LightGray"; ServiceColor = "LightGray"; try { MSSQLHelper mssqlHelper = new MSSQLHelper(); List<MSSQLBackup> sqlitems = mssqlHelper.GetConfigList(); MSSQLCount = sqlitems != null ? sqlitems.Count() : 0; MYSQLHelper mysqlHelper = new MYSQLHelper(); List<MYSQLBackup> mysqlitems = mysqlHelper.GetConfigList(); MySQLCount = mysqlitems != null ? mysqlitems.Count() : 0; FolderHelper folderHelper = new FolderHelper(); List<FolderBackup> folderitems = folderHelper.GetConfigList(); FolderCount = folderitems != null ? folderitems.Count() : 0; AWSS3Helper awsHelper = new AWSS3Helper(); AWSS3Setting awsSetting = awsHelper.GetConfig(); if (awsSetting == null) { AwsS3SettingStatus = "AWS S3 settings not defined"; } else if (awsSetting.IsValid == false) { AwsS3SettingStatus = "AWS S3 settings are not valid"; } else if (awsSetting.IsActive == false) { AwsS3SettingStatus = "AWS S3 settings are not active"; } else { AwsS3SettingStatus = "AWS S3 settings are valid & active"; AwsS3SettingColor = "Green"; } EmailHelper emailHelper = new EmailHelper(); EmailSetting emailSetting = emailHelper.GetConfig(); if (emailSetting == null) { EmailSettingStatus = "E-Mail settings are not defined"; } else if (emailSetting.IsValid == false) { EmailSettingStatus = "E-Mail settings are not valid"; } else { EmailSettingStatus = "Email settings are valid & active"; EmailSettingColor = "Green"; } ServiceStatus = "Background service is not installed"; GeneralSettingHelper generalHelper = new GeneralSettingHelper(); GeneralSetting generalSetting = generalHelper.GetConfig(); if (generalSetting != null && generalSetting.ServiceInstalled == true) { ServiceStatus = "Background service is installed"; ServiceColor = "Green"; btnInstallService.Visibility = Visibility.Collapsed; btnUnInstallService.Visibility = Visibility.Visible; } } catch (Exception ex) { string message = Functions.GetErrorFromException(ex); MessageBox.Show("An error occurred. \n" + message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
public async Task ExecuteBackups() { //Read current configuration //As user may have updated settings try { string tmpPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); EnVar.AppWorkingPath = Path.Combine(tmpPath, "Runtime Backup Manager"); AWSS3Helper awsHelper = new AWSS3Helper(); awsS3Setting = awsHelper.GetConfig(); EmailHelper emailHelper = new EmailHelper(); emailSetting = emailHelper.GetConfig(); GeneralSettingHelper generalHelper = new GeneralSettingHelper(); generalSetting = generalHelper.GetConfig(); MSSQLHelper mssqlHelper = new MSSQLHelper(); mssqlBackups = mssqlHelper.GetConfigList(); MYSQLHelper mysqlHelper = new MYSQLHelper(); mysqlBackups = mysqlHelper.GetConfigList(); FolderHelper folderHelper = new FolderHelper(); folderBackups = folderHelper.GetConfigList(); } catch (Exception ex) { LogHelper.LogMessage("Error", "Unable to load Backup settings." + Functions.GetErrorFromException(ex)); return; } try { int Hour = DateTime.Now.Hour; int Minute = DateTime.Now.Minute; if (generalSetting == null || string.IsNullOrWhiteSpace(generalSetting.LocalFolder) || !Directory.Exists(generalSetting.LocalFolder)) { return; } EmailHelper emailHelper = null; if (emailSetting != null && emailSetting.IsValid == true) { emailHelper = new EmailHelper(emailSetting.DisplayName, emailSetting.EmailAddress, emailSetting.Username, emailSetting.Password, emailSetting.SmtpServer, emailSetting.SmtpPort, emailSetting.SmtpSsl); } //Backup MSSQL if (mssqlBackups != null && mssqlBackups.Count > 0) { foreach (MSSQLBackup backup in mssqlBackups) { if (backup.BackupTime.Hour == Hour && backup.BackupTime.Minute == Minute) { MSSQLHelper sqlBackupHelper = new MSSQLHelper(backup.ServerName, backup.DatabaseName, backup.Username, backup.Password); string fileName = Path.Combine(generalSetting.LocalFolder, DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + "_" + backup.DatabaseName + ".bak"); bool Result = await sqlBackupHelper.ExceuteCommand("BACKUP DATABASE [" + backup.DatabaseName + "] TO DISK=@filename", new System.Data.SqlClient.SqlParameter[] { new System.Data.SqlClient.SqlParameter { ParameterName = "@filename", Value = fileName } }); if (Result == true) { //log message LogHelper.LogMessage("Info", "MSSQL database '" + backup.DatabaseName + "' backed up successfully"); //email config if (emailHelper != null && emailSetting.LocalSuccess) { string body = "Dear Admin, \n\n" + "This is to notify you that following scheduled task has completed successfully:\n" + "Backup Type: MSSQL\n" + "Database Name: " + backup.DatabaseName + "\n" + "Backup Location: Local\n" + "Backup Time: " + DateTime.Now.ToString("MMM dd, yyyy HH:mm:ss", DateTimeFormatInfo.InvariantInfo) + ".\n\n" + "Runtime Backup Manager"; bool emailSuccess = await emailHelper.SendEmail(emailSetting.RecipientEmail, "Success: Backup for MSSQL '" + backup.DatabaseName + "'", body); if (emailSuccess == false) { LogHelper.LogMessage("Error", "E-Mail sending failed. " + emailHelper.ResultMessage); } } } else { //log message LogHelper.LogMessage("Error", "MSSQL backup failed for " + backup.DatabaseName + ". " + sqlBackupHelper.Message); //email config if (emailHelper != null && emailSetting.LocalFailure) { string body = "Dear Admin, \n\n" + "This is to notify you that following scheduled task has failed:\n" + "Backup Type: MSSQL\n" + "Database Name: " + backup.DatabaseName + "\n" + "Backup Location: Local\n" + "Backup Time: " + DateTime.Now.ToString("MMM dd, yyyy HH:mm:ss", DateTimeFormatInfo.InvariantInfo) + ".\n" + "Error Message: " + sqlBackupHelper.Message + "\n\n" + "Runtime Backup Manager"; bool emailSuccess = await emailHelper.SendEmail(emailSetting.RecipientEmail, "Failure: Backup for MSSQL '" + backup.DatabaseName + "'", body); if (emailSuccess == false) { LogHelper.LogMessage("Error", "E-Mail sending failed. " + emailHelper.ResultMessage); } } } } } } //Backup MySQL if (mysqlBackups != null && mysqlBackups.Count > 0) { foreach (MYSQLBackup backup in mysqlBackups) { if (backup.BackupTime.Hour == Hour && backup.BackupTime.Minute == Minute) { MYSQLHelper helper = new MYSQLHelper(backup.ServerName, backup.DatabaseName, backup.Username, backup.Password); string fileName = Path.Combine(generalSetting.LocalFolder, DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + "_" + backup.DatabaseName + ".sqldump"); bool Result = await helper.BackupDatabase(fileName); if (Result == true) { //log message LogHelper.LogMessage("Info", "MySQL database '" + backup.DatabaseName + "' backed up successfully"); //email config if (emailHelper != null && emailSetting.LocalSuccess) { string body = "Dear Admin, \n\n" + "This is to notify you that following scheduled task has completed successfully:\n" + "Backup Type: MySQL\n" + "Database Name: " + backup.DatabaseName + "\n" + "Backup Location: Local\n" + "Backup Time: " + DateTime.Now.ToString("MMM dd, yyyy HH:mm:ss", DateTimeFormatInfo.InvariantInfo) + ".\n\n" + "Runtime Backup Manager"; bool emailSuccess = await emailHelper.SendEmail(emailSetting.RecipientEmail, "Success: Backup for MySQL '" + backup.DatabaseName + "'", body); if (emailSuccess == false) { LogHelper.LogMessage("Error", "E-Mail sending failed. " + emailHelper.ResultMessage); } } } else { //log Message LogHelper.LogMessage("Error", "MySQL backup failed for " + backup.DatabaseName + ". " + helper.Message); //email config if (emailHelper != null && emailSetting.LocalFailure) { string body = "Dear Admin, \n\n" + "This is to notify you that following backup task has failed:\n" + "Backup Type: MySQL\n" + "Database Name: " + backup.DatabaseName + "\n" + "Backup Location: Local\n" + "Backup Time: " + DateTime.Now.ToString("MMM dd, yyyy HH:mm:ss", DateTimeFormatInfo.InvariantInfo) + ".\n" + "Error Message: " + helper.Message + "\n\n" + "Runtime Backup Manager"; bool emailSuccess = await emailHelper.SendEmail(emailSetting.RecipientEmail, "Failure: Backup for MySQL '" + backup.DatabaseName + "'", body); if (emailSuccess == false) { LogHelper.LogMessage("Error", "E-Mail sending failed. " + emailHelper.ResultMessage); } } } } } } //Backup a folder if (folderBackups != null && folderBackups.Count > 0) { foreach (FolderBackup backup in folderBackups) { if (!Directory.Exists(backup.FolderName)) { LogHelper.LogMessage("Warning", "Backup folder not found: " + backup.FolderName); continue; } if (backup.BackupTime.Hour == Hour && backup.BackupTime.Minute == Minute) { try { string backupFolderName = Path.GetFileName(backup.FolderName); string destinationPath = Path.Combine(generalSetting.LocalFolder, DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + "_" + backupFolderName + ".zip"); ZipFile.CreateFromDirectory(backup.FolderName, destinationPath, CompressionLevel.Optimal, true); //log Message LogHelper.LogMessage("Info", "Folder '" + backup.FolderName + "' backed up successfully at '" + destinationPath + "'"); //email config if (emailHelper != null && emailSetting.LocalSuccess) { string body = "Dear Admin, \n\n" + "This is to notify you that following backup task has completed successfully:\n" + "Backup Type: Folder\n" + "Folder Name: " + backup.FolderName + "\n" + "Backup Location: Local\n" + "Backup Time: " + DateTime.Now.ToString("MMM dd, yyyy HH:mm:ss", DateTimeFormatInfo.InvariantInfo) + ".\n\n" + "Runtime Backup Manager"; bool emailSuccess = await emailHelper.SendEmail(emailSetting.RecipientEmail, "Success: Backup for Folder '" + backup.FolderName + "'", body); if (emailSuccess == false) { LogHelper.LogMessage("Error", "E-Mail sending failed. " + emailHelper.ResultMessage); } } } catch (Exception ex) { string error = Functions.GetErrorFromException(ex); //log Message LogHelper.LogMessage("Error", "Folder backup failed for '" + backup.FolderName + "'. " + error); //email config if (emailHelper != null && emailSetting.LocalFailure) { string body = "Dear Admin, \n\n" + "This is to notify you that following backup task has failed:\n" + "Backup Type: Folder\n" + "Folder Name: " + backup.FolderName + "\n" + "Backup Location: Local\n" + "Backup Time: " + DateTime.Now.ToString("MMM dd, yyyy HH:mm:ss", DateTimeFormatInfo.InvariantInfo) + ".\n\n" + "Runtime Backup Manager"; bool emailSuccess = await emailHelper.SendEmail(emailSetting.RecipientEmail, "Failure: Backup for Folder '" + backup.FolderName + "'", body); if (emailSuccess == false) { LogHelper.LogMessage("Error", "E-Mail sending failed. " + emailHelper.ResultMessage); } } } } } } //Upload to AWS S3 if (awsS3Setting != null && awsS3Setting.BackupTime.Hour == Hour && awsS3Setting.BackupTime.Minute == Minute) { if (generalSetting != null && Directory.Exists(generalSetting.LocalFolder)) { AWSS3Helper helper = new AWSS3Helper(awsS3Setting.AccessKeyId, awsS3Setting.AccessSecretKey, awsS3Setting.AWSRegion, awsS3Setting.BucketName, awsS3Setting.FolderName); foreach (var file in Directory.GetFiles(generalSetting.LocalFolder)) { bool Result = await helper.UploadToS3Async(file, awsS3Setting.CompressBeforeUpload, awsS3Setting.DeleteAfterBackup); if (Result == true) { //log Message LogHelper.LogMessage("Info", "File uploaded to S3: " + file); //email config if (emailHelper != null && emailSetting.RemoteSuccess) { string body = "Dear Admin, \n\n" + "This is to notify you that following backup task has completed successfully:\n" + "File Name: " + file + "\n" + "Backup Location: Remote\n" + "Backup Time: " + DateTime.Now.ToString("MMM dd, yyyy HH:mm:ss", DateTimeFormatInfo.InvariantInfo) + ".\n\n" + "Runtime Backup Manager"; bool emailSuccess = await emailHelper.SendEmail(emailSetting.RecipientEmail, "Success: Remote Backup to Amazon S3", body); if (emailSuccess == false) { LogHelper.LogMessage("Error", "E-Mail sending failed. " + emailHelper.ResultMessage); } } //Move this file to 'Uploaded' folder else it will be uploaded again on next run //if there is no delete policy in place or delete after > 1 day //call only if immediate delete is not enabled if (awsS3Setting.DeleteAfterBackup == false) { try { string uploadedFolder = Path.Combine(generalSetting.LocalFolder, "Uploaded"); if (!Directory.Exists(uploadedFolder)) { Directory.CreateDirectory(uploadedFolder); } File.Move(file, Path.Combine(uploadedFolder, Path.GetFileName(file))); } catch (Exception ex) { LogHelper.LogMessage("Error", "Failed to move file to 'Uploaded' folder. " + Functions.GetErrorFromException(ex)); } } } else { //log Message LogHelper.LogMessage("Error", "File upload to S3 Failed. " + helper.Message); //email config if (emailHelper != null && emailSetting.RemoteFailure) { string body = "Dear Admin, \n\n" + "This is to notify you that following backup task has failed:\n" + "File Name: " + file + "\n" + "Backup Location: Remote\n" + "Backup Time: " + DateTime.Now.ToString("MMM dd, yyyy HH:mm:ss", DateTimeFormatInfo.InvariantInfo) + ".\n\n" + "Runtime Backup Manager"; bool emailSuccess = await emailHelper.SendEmail(emailSetting.RecipientEmail, "Failure: Remote Backup to Amazon S3", body); if (emailSuccess == false) { LogHelper.LogMessage("Error", "E-Mail sending failed. " + emailHelper.ResultMessage); } } } } } } //Delete local folder files after specified days //run at 5:00 AM everyday //add recommendation in docs to schedule backup tasks between before 5:00 AM everyday if (Hour == 5 && Minute == 0) { if (generalSetting != null && generalSetting.AutoDelete == true && generalSetting.DeleteAfterDays != null) { DateTime dateTimeBefore = DateTime.Now.AddDays((int)generalSetting.DeleteAfterDays * -1); foreach (string file in Directory.GetFiles(generalSetting.LocalFolder)) { DateTime filetime = File.GetCreationTime(file); if (filetime < dateTimeBefore) { try { File.Delete(file); LogHelper.LogMessage("Info", "File deletion failed for '" + file + "'"); } catch (Exception ex) { LogHelper.LogMessage("Error", "File deletion failed for '" + file + "' \n" + Functions.GetErrorFromException(ex)); } } } foreach (string file in Directory.GetFiles(Path.Combine(generalSetting.LocalFolder, "Uploaded"))) { DateTime filetime = File.GetCreationTime(file); if (filetime < dateTimeBefore) { try { File.Delete(file); LogHelper.LogMessage("Info", "File deletion failed for '" + file + "'"); } catch (Exception ex) { LogHelper.LogMessage("Error", "File deletion failed for '" + file + "' \n" + Functions.GetErrorFromException(ex)); } } } } } } catch (Exception ex) { string message = Functions.GetErrorFromException(ex); MessageBox.Show("An error occurred. \n" + message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }