protected override void OnStop() { timer.Stop(); File_Read_Write.Write_File(log_path, "-----------------------------------------------------", true); File_Read_Write.Write_File(log_path, DateTime.Now.ToString() + ": Service stopped!", true); File_Read_Write.Write_File(log_path, "-----------------------------------------------------", true); }
protected override void OnStart(string[] args) { //Backup(); File_Read_Write.Write_File(log_path, "-----------------------------------------------------", true); File_Read_Write.Write_File(log_path, DateTime.Now.ToString() + ": Service Starting...", true); try { info_DB = new List <Info_DB>(); info_JobMySQL = new List <Info_MySQLJob>(); info_DB = XML_read_write.ConvertXmlStringtoObject <List <Info_DB> >(System.IO.File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "Database_Config.xml")); info_JobMySQL = XML_read_write.ConvertXmlStringtoObject <List <Info_MySQLJob> >(System.IO.File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "MySQLDatabase_Config.xml")); fTPServer.URL = ConfigurationManager.AppSettings["FTP_SERVER_URI"].ToString(); fTPServer.User = ConfigurationManager.AppSettings["FTP_USER"].ToString(); fTPServer.Pass = ConfigurationManager.AppSettings["FTP_PASSWORD"].ToString(); _Server.Server_Name = ConfigurationManager.AppSettings["Server_Name"].ToString(); _Server.User = ConfigurationManager.AppSettings["User"].ToString(); _Server.Pass = ConfigurationManager.AppSettings["Pass"].ToString(); File_Read_Write.Write_File(log_path, DateTime.Now + ": Load Config...Done!", true); timer.Start(); } catch { File_Read_Write.Write_File(log_path, DateTime.Now + ": Load Config...failed!", true); } //Running(list_job[0]); //SQLServer_Running(info_DB[0]); //MySQL_Running(info_JobMySQL[0].Instances, info_JobMySQL[0].ListDB[0]); //for(int i = 0; i< info_JobMySQL.Count; i++) //{ // Restore(info_JobMySQL[i].DBSource); //} }
bool MySQL_Running(Info_MySQL_Instansce instansce, Info_MySQL_DB DB) { string Mess = ""; Exception er = null; //gán cờ er bằng null chỉ thị ko có lỗi Thread.CurrentThread.IsBackground = true; er = DB.Download_BackupFile(fTPServer); if (er != null) { Mess = er.Message + ": " + ((er.InnerException == null) ? "" : er.InnerException.Message); File_Read_Write.Write_File(log_path, DateTime.Now + ": Error - " + DB.DBTarget + ": " + Mess, true); SendEmail.Send_Email(DB.Email, null, "[Dashboard - RTS] Clone Database error!", "Server: " + instansce.Server_Name + @"\nDatabase name: " + DB.DBTarget + "\n" + Mess, false); return(false); } else { File_Read_Write.Write_File(log_path, DateTime.Now + ": Download Backup file " + fTPServer.URL + "BackupDB_zip/" + DB.ServerSource + "/" + DateTime.Now.ToString("yyyy-MM-dd") + "/" + DB.DBSource + ".zip....done!", true); } er = DB.Excute_Restore_DB(instansce); if (er != null) { Mess = er.Message + ": " + ((er.InnerException == null) ? "" : er.InnerException.Message); File_Read_Write.Write_File(log_path, DateTime.Now + ": Error - " + DB.DBTarget + ": " + Mess, true); SendEmail.Send_Email(DB.Email, null, "[Dashboard - RTS] Clone Database error!", "Server: " + instansce.Server_Name + @"\nDatabase name: " + DB.DBTarget + "\n" + Mess, false); return(false); } else { File_Read_Write.Write_File(log_path, DateTime.Now + ": Restore " + DB.ServerSource + @"\" + DB.DBSource + " to " + instansce.Server_Name + @"\" + DB.DBTarget + " done!", true); } SendEmail.Send_Email(DB.Email, null, "[Dashboard - RTS] Clone Database succufully!", "Clone Database " + DB.ServerSource + @"\" + DB.DBSource + " to " + instansce.Server_Name + @"\" + DB.DBTarget + " succufully!", false); return(true); }
public static Exception Send_Email(List <string> ToEmail, List <string> CC, string title, string content, bool isHtml) { for (int j = 0; j < 5; j++) { try { MailMessage mail = new MailMessage(); string FromEmail = ConfigurationManager.AppSettings["Email"]; string Pass = ConfigurationManager.AppSettings["Password"]; string FromName = ConfigurationManager.AppSettings["FromName"]; SmtpClient smtpServer = new SmtpClient(ConfigurationManager.AppSettings["SMTP_Server"]); mail.From = new MailAddress(FromEmail, FromName); if (ToEmail == null || ToEmail.Count == 0) { return(new Exception("To Email is Null.")); } for (int i = 0; i < ToEmail.Count; i++) { if (string.IsNullOrEmpty(ToEmail[i])) { continue; } mail.To.Add(ToEmail[i]); // Email cần đến } if (CC != null) { for (int i = 0; i < CC.Count; i++) { if (string.IsNullOrEmpty(CC[i])) { continue; } mail.CC.Add(CC[i]); // Email cần CC } } mail.Subject = (title == "") ? "No subject" : title; mail.Body = content; mail.IsBodyHtml = isHtml; mail.Priority = MailPriority.High; smtpServer.Port = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]); smtpServer.Credentials = new NetworkCredential(FromEmail, Pass); smtpServer.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]); Disable_CertificateValidation(); smtpServer.Send(mail); File_Read_Write.Write_File(AppDomain.CurrentDomain.BaseDirectory + @"Log\" + DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString() + ": Sending email \"" + title + "\" TO " + String.Join(", ", ToEmail.ToArray()) + " and " + ((CC == null) ? "no CC" : "CC to " + String.Join(", ", CC.ToArray())) + " Succufully!", true); break; } catch (Exception er) { File_Read_Write.Write_File(AppDomain.CurrentDomain.BaseDirectory + @"Log\" + DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString() + ": Try " + (j + 1) + ": Error - Sending email \"" + title + "\" TO " + String.Join(", ", ToEmail.ToArray()) + " and " + ((CC == null) ? "no CC" : "CC to " + String.Join(", ", CC.ToArray())) + " error!" + er.Message, true); } } return(null); }