We help IT Professionals succeed at work.

Script to email latest backup log

Medium Priority
299 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.

Comment
Watch Question

Commented:
what software you using ?

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

Author

Commented:
Hi Dear i'm using windows NTBackup on windows server 2003 which don't have option to send e-mail
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
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.

Author

Commented:
yes we have SMTP server but any information about the script
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
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.

Author

Commented:
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
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

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

Regards,

Rob.

Author

Commented:
ok i see thanks it will be Useful not only for me but in general.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
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.

Author

Commented:
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.


CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
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

Author

Commented:
yes it's great many thanks.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Great. Thanks for the grade.

Regards,

Rob.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.