Wednesday, March 5, 2008

EXE for Monitoring Backups.... Cool Tool

Hi,

One fine day... Thought let me do somthing Coool @ work... mmmm got a idea.....

At present we have schedule for a daily backup of our MOSS 2007 Sharepoint internet facing site. And my senior team member have asked me to verify it daily. Thought why don't we automate this..... Yup i have created C# console application (Scheduled to run every day after the backup is done.) which will check the date of the backup folder & if matches the current date it will send an acknowledgment mail else if the backup failed it will be an alert for us to investigate.

Following is its code:
****************
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net.Mail;

namespace BackupChecker
{
class Program
{
static void Main(string[] args)
{
if (System.IO.File.Exists("Tivoli\\spbrtoc.xml"))
{
System.IO.FileInfo fo = new FileInfo("Tivoli\\spbrtoc.xml");
SmtpClient smtp = new SmtpClient("mail.YourSMTP.com");
smtp.UseDefaultCredentials = true;

StreamWriter sr = new StreamWriter("Checklog.log",true);
sr.WriteLine("Update on " + System.DateTime.Now.ToString());
sr.Flush();
sr.Close();

try
{
if (fo.LastWriteTime.Date.ToShortDateString() == System.DateTime.Now.ToShortDateString())
{
MailMessage msg = new MailMessage("fazuking@hotmail.com",”mohammedfauzi_vm@yahoo.com,Teammate#1,Teammate#2", "www.MySite.com Backup Done", "The www.MySite.com site backup was done Successfully. This is an Auto generated mail sent on " + System.DateTime.Now.ToLongTimeString());

smtp.Send(msg);
}

else
{
MailMessage msg = new MailMessage("fazuking@hotmail.com",”mohammedfauzi_vm@yahoo.com,Teammate#1,Teammate#2", "www.MySite.com Backup Done", "The www.MySite.com site backup was Un-Successfully. This is an Auto generated mail sent on " + System.DateTime.Now.ToLongTimeString());
smtp.Send(msg);
}
}
catch (Exception ex)
{

StreamWriter sr1 = new StreamWriter("Checklog.log",true);
sr1.NewLine = ("\r\n");
sr1.WriteLine("Error: " + ex.Message + " on " + System.DateTime.Now.ToString());
sr1.Flush();
sr1.Close();
}
}

}
}
}

***************************************

Now things are automated.... Stay Relaxed.... Let think whats next :)
Hope this should be helpful to friends who needs to keep track of specific folders.

Warm Regards
Mohammed Fauzi

No comments: