[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Script to email latest backup log

Posted on 2009-02-08
14
Medium Priority
?
280 Views
Last Modified: 2012-05-06
Dear All

Currently Im running daily system state backup on around 35 servers using dedicated backup administrator user account I need script that
will do the following:

1- check the last updated log file.
2- check the status of the log file (Success / Fail)
3- copy that log file to other location (that mean 35 log file in one folder)
4- send me e-mail containing the folder that have all 35 log file  

I check many scrips that may do some of the past request but its not working so please any one can help me on this.

0
Comment
Question by:Dlala
  • 7
  • 6
14 Comments
 
LVL 5

Expert Comment

by:Jaymz_R
ID: 23587762
what software you using ?

all backup software i've seen has an option to email the backup log to the administrator.
0
 

Author Comment

by:Dlala
ID: 23587798
Hi Dear i'm using windows NTBackup on windows server 2003 which don't have option to send e-mail
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 23593814
Do you have an SMTP server that you can send your emails through, that is contactable by the server that will be running the script?

Regards,

Rob.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:Dlala
ID: 23597403
yes we have SMTP server but any information about the script
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 23597617
Yeah, we'll write something shortly...just getting some info....

Will the script
a) be run from one computer, which will then pull the log files from each of the servers, or
b) run on each PC, and only copy the latest log from itself, to a central location?

I'm assuming it would be option A, so that you can empty the target folder before starting to copy the latest files?

When you say you want the email to contain the folder, do you just mean you want the folder path in the body of the email?

Regards,

Rob.
0
 

Author Comment

by:Dlala
ID: 23597776
ok dear Rob

yes it's option A for example it will be on one central server for example SMTP server the script will do the following

1- check the last update backup log file on each of 35 server (OR) i can configure other script to just Xcopy the last log from each server then it will cleare all the logs then move to the central server

2- after all logs arrive to the central folder from the 35 server the folder that contain all log will be zipped and be attaced as send to me via e-mail
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 23597811
OK, makes more sense now...I'll write a script tomorrow...it's late afternoon here...

Regards,

Rob.
0
 

Author Comment

by:Dlala
ID: 23597818
ok i see thanks it will be Useful not only for me but in general.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 23607602
OK, finally got there....

This includes everything except for checking the status of the log file.  If you want me to include that as well, can you please upload a sample backup log that has each of the status that you expect.

You will need to modify all of the variables at the top to suit your environment.

Regards,

Rob.
' Computer name array
arrComputers = Array( _
	"SERVER1", _
	"SERVER2", _
	"SERVER3" _
	)
 
' Backup log variables
' This should be the local path on each server that the logs are written to
strPathToLogs = "C:\NTBackup\Logs"
' This should be the UNC path of the location to copy the latest logs to
strCopyLogsTo = "\\Server\Share\LatestLogs"
 
' Provide the path to the Ultimate Zip command line tool here
strUZip = "\\server\share\UltimateZip Command Line Tool\uzcomp.exe"
' This is the name of the zip file that you want to create
strZipFile = "\\Server\Share\LatestLogs\LatestBackupFiles.zip"
 
' Email variables:
strServer = "smtp_server_name"
strTo = "recipient@domain.com"
strFrom = "sender@domain.com"
strSubject = "Compressed Backup Logs"
strBody = "Please find attached the compressed backup logs." & VbCrLf
 
If Right(strCopyLogsTo, 1) <> "\" Then strCopyLogsTo = strCopyLogsTo & "\"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
If objFSO.FolderExists(strCopyLogsTo) = True Then
	' This line will delete all of the logs inthe destination folder before starting to copy the latest ones
	objFSO.DeleteFile strCopyLogsTo & "*", True
	' Now go through each of the computers log paths
	For Each strComputer In arrComputers
		strLogPath = "\\" & strComputer & "\" & Replace(strPathToLogs, ":", "$")
		If Right(strLogPath, 1) <> "\" Then strLogPath = strLogPath & "\"
		If objFSO.FolderExists(strLogPath) = True Then
			dteLatestDate = CDate("01/01/1901")
			strLatestFile = ""
			For Each objLogFile In objFSO.GetFolder(strLogPath).Files
				If objLogFile.DateLastModified > dteLatestDate Then
					dteLatestDate = objLogFile.DateLastModified
					strLatestFile = strLogPath & objLogFile.Name
				End If
			Next
			If strLatestFile <> "" Then
				objFSO.CopyFile strLatestFile, strCopyLogsTo, True
			End If
		Else
			MsgBox "Could not find " & strLogPath
		End If
	Next
	
	' Now compress the log files
	Set objShell = CreateObject("WScript.Shell")
	strUZip = objFSO.GetFile(strUZip).ShortPath
	If objFSO.FileExists(strZipFile) = True Then objFSO.DeleteFile strZipFile, True
	strCommand = "cmd /c " & strUZip & " -m -c2 -w """ & strZipFile & """ """ & strCopyLogsTo & "*.*"""
	objShell.Run strCommand, 0, True
	
	' Now send the file
	SendEmail strServer, strTo, strFrom, strSubject, strBody, strZipFile
	MsgBox "Email has been sent."
Else
	MsgBox "Could not find " & strCopyLogsTo
End If
 
Sub SendEmail(strServer, strTo, strFrom, strSubject, strBody, strAttachment)
	Dim objMessage
	
	Set objMessage = CreateObject("CDO.Message")
	objMessage.To = strTo
	objMessage.From = strFrom
	objMessage.Subject = strSubject
	objMessage.TextBody = strBody
 
	objMessage.AddAttachment strAttachment
 
	'==This section provides the configuration information for the remote SMTP server.
	objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
	'Name or IP of Remote SMTP Server
	objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strServer
	'Server port (typically 25)
	objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25	
	objMessage.Configuration.Fields.Update
	'==End remote SMTP server configuration section==
 
	objMessage.Send
	Set objMessage = Nothing
End Sub

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 23607613
Oh, I forgot to mention, for the zipping of the logs, this requires that you have the UltimateZip Command Line Tool available from here:
http://www.ultimatezip.com/download.htm

Regards,

Rob.
0
 

Author Comment

by:Dlala
ID: 23608526
Thanks Dear Rob

i think it will work i will modify it and let you know one more thing can you just modify that after it collect the log file can it clear the hall folder in

C:\Documents and Settings\user\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\

so i will be sure that it collect the last log on each server.


0
 
LVL 65

Expert Comment

by:RobSampson
ID: 23608607
It doesn't really need to, but it could.....It will always find the file that has the latest modified date.

In any case, this will now also copy the latest log file to the central location, then delete all log files, except that one, from the local folder on each machine.

Regards,

Rob.
' Computer name array
arrComputers = Array( _
	"SERVER1", _
	"SERVER2", _
	"SERVER3" _
	)
 
' Backup log variables
' This should be the local path on each server that the logs are written to
strPathToLogs = "C:\NTBackup\Logs"
' This should be the UNC path of the location to copy the latest logs to
strCopyLogsTo = "\\Server\Share\LatestLogs"
 
' Provide the path to the Ultimate Zip command line tool here
strUZip = "\\server\share\UltimateZip Command Line Tool\uzcomp.exe"
' This is the name of the zip file that you want to create
strZipFile = "\\Server\Share\LatestLogs\LatestBackupFiles.zip"
 
' Email variables:
strServer = "smtp_server_name"
strTo = "recipient@domain.com"
strFrom = "sender@domain.com"
strSubject = "Compressed Backup Logs"
strBody = "Please find attached the compressed backup logs." & VbCrLf
 
If Right(strCopyLogsTo, 1) <> "\" Then strCopyLogsTo = strCopyLogsTo & "\"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
If objFSO.FolderExists(strCopyLogsTo) = True Then
	' This line will delete all of the logs inthe destination folder before starting to copy the latest ones
	objFSO.DeleteFile strCopyLogsTo & "*", True
	' Now go through each of the computers log paths
	For Each strComputer In arrComputers
		strLogPath = "\\" & strComputer & "\" & Replace(strPathToLogs, ":", "$")
		If Right(strLogPath, 1) <> "\" Then strLogPath = strLogPath & "\"
		If objFSO.FolderExists(strLogPath) = True Then
			dteLatestDate = CDate("01/01/1901")
			strLatestFile = ""
			For Each objLogFile In objFSO.GetFolder(strLogPath).Files
				If objLogFile.DateLastModified > dteLatestDate Then
					dteLatestDate = objLogFile.DateLastModified
					strLatestFile = strLogPath & objLogFile.Name
				End If
			Next
			If strLatestFile <> "" Then
				objFSO.CopyFile strLatestFile, strCopyLogsTo, True
				' Now delete all of the log files from that folder, except the latest one.
				For Each objLogFile In objFSO.GetFolder(strLogPath).Files
					If LCase(objLogFile.Name) <> LCase(Mid(strLatestFile, InStrRev(strLatestFile, "\") + 1)) Then
						objFSO.DeleteFile strLogPath & objLogFile.Name, True
					End If
				Next				
			End If
		Else
			MsgBox "Could not find " & strLogPath
		End If
	Next
	
	' Now compress the log files
	Set objShell = CreateObject("WScript.Shell")
	strUZip = objFSO.GetFile(strUZip).ShortPath
	If objFSO.FileExists(strZipFile) = True Then objFSO.DeleteFile strZipFile, True
	strCommand = "cmd /c " & strUZip & " -m -c2 -w """ & strZipFile & """ """ & strCopyLogsTo & "*.*"""
	objShell.Run strCommand, 0, True
	
	' Now send the file
	SendEmail strServer, strTo, strFrom, strSubject, strBody, strZipFile
	MsgBox "Email has been sent."
Else
	MsgBox "Could not find " & strCopyLogsTo
End If
 
Sub SendEmail(strServer, strTo, strFrom, strSubject, strBody, strAttachment)
	Dim objMessage
	
	Set objMessage = CreateObject("CDO.Message")
	objMessage.To = strTo
	objMessage.From = strFrom
	objMessage.Subject = strSubject
	objMessage.TextBody = strBody
 
	objMessage.AddAttachment strAttachment
 
	'==This section provides the configuration information for the remote SMTP server.
	objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
	'Name or IP of Remote SMTP Server
	objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strServer
	'Server port (typically 25)
	objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25	
	objMessage.Configuration.Fields.Update
	'==End remote SMTP server configuration section==
 
	objMessage.Send
	Set objMessage = Nothing
End Sub

Open in new window

0
 

Author Comment

by:Dlala
ID: 23608898
yes it's great many thanks.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 23615211
Great. Thanks for the grade.

Regards,

Rob.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article covers five tools all IT professionals should know about, as they up productivity by a great deal!
MS Outlook undoubtedly is the most widely used email client.Its user-friendliness, cost effectiveness, and availability with Microsoft Office Suite make it the most popular email application.  Its compatibility with Microsoft applications like Exch…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Suggested Courses
Course of the Month18 days, 8 hours left to enroll

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question