Since the backup server restores new data from the production server every day, how can I check whether the database restore status is successful or failed?
We will view the restoration by viewing "Control Panel"> "Administrative Tools"> "Event Viewer.
Next we will set up an email reminder function to monitor the database running status in the early morning of every day, and send the restored success and error messages to the mail.
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 ("database restored "))
{
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 email client settings
System. net. Mail. smtpclient client = new system. net. Mail. smtpclient ();
Client. Host = "mail.126.com ";
Client. usedefacrecredentials = 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 = "database restoration status"; // Email Subject
Message. Body = emailmessage; // mail content
Message. to. Add ("name@126.com"); // recipient
Message. bodyencoding = system. Text. encoding. utf8;
Message. isbodyhtml = true;
Message. Priority = system. net. Mail. mailpriority. High; // mail priority
Client. Send (Message );
}
Catch (exception ex)
{
Console. writeline (ex. Message );
}
}