由于备份服务器每天都还原生产服务器传来的新的数据,如何查看数据库还原状态是成功还是失败?
我们将通过查看“控制面板”—〉“管理工具”—〉“事件查看器”来查看还原情况。
下面我们将设置一个邮件提醒功能,监控每天凌晨数据库运行的状态,将还原成功和错误信息都发送到邮件中。
static void Main(string[] args){ SendMailAboutCheckServerStatus(); Console.ReadKey();}
private static void SendMailAboutCheckServerStatus(){ EventLog elog = new EventLog(); elog.Log = "Application"; string emailmessage = ""; for (int i = 0; i < elog.Entries.Count; i++) { try { if (elog.Entries[i].Source == "MSSQLSERVER") { DateTime starttime=Convert.ToDateTime( DateTime.Now.ToString("yyyy-MM-dd")+" 00:00:00"); DateTime endtime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 8:30:00"); if (elog.Entries[i].TimeGenerated > starttime && elog.Entries[i].TimeGenerated < endtime) { if (elog.Entries[i].EntryType.ToString() == "Error") { emailmessage += "Message: " + elog.Entries[i].Message + "<br>" + "App: " + elog.Entries[i].Source + "<br>" + "Entry type: " + elog.Entries[i].EntryType + "<br>" + "TimeGenerated :" + elog.Entries[i].TimeGenerated + "<br>"; } if (elog.Entries[i].Message.Contains("数据库已还原")) { emailmessage += "Message: " + elog.Entries[i].Message + "<br>" + "App: " + elog.Entries[i].Source + "<br>" + "Entry type: " + elog.Entries[i].EntryType + "<br>" + "TimeGenerated :" + elog.Entries[i].TimeGenerated + "<br>"; } } } } catch { } } SendEMailInfo(emailmessage); Console.WriteLine("End!");}
private static void SendEMailInfo(string emailmessage){
try { #region 邮件 Client设置 System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(); client.Host = "mail.126.com"; client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential("namn@126.com", "password"); client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); message.From = new System.Net.Mail.MailAddress("name@126.com", "displayname", System.Text.Encoding.UTF8); #endregion
message.Subject = "数据库还原状态"; //邮件主题 message.Body = emailmessage; //邮件内容 message.To.Add("name@126.com"); //收件人 message.BodyEncoding = System.Text.Encoding.UTF8; message.IsBodyHtml = true; message.Priority = System.Net.Mail.MailPriority.High;//邮件优先级
client.Send(message);
} catch (Exception ex) { Console.WriteLine(ex.Message); }}