Tuesday, November 07, 2006

NTBackup VB Script w/ File Cleanup

The following VB script was written to allow NTBackup to run. Some of
the features are:

- Cleanup of backup files, this is set by the RetentionDays variable
- Email notification by attaching the backup log file
- Backup files are named based on server name, date, and time the backup
was executed.

To Use:
1) Save this text to a .vbs file.
2) Launch ntbackup, make all your file selections (not in the wizard,
and save the selections to c:\backup_settings.bks.
3) On the destination server (where the backup files will be sent to)
create a sub folder named the same as the server being backed up.
4) Modify the text file with your server names, email addresses, and
windows directory.
5) Configure task scheduler to launch the VB Script at the given time.

====================================================================
'Begin Full Backup Script

'Get Computer Name
Set WSHNtwk = WScript.CreateObject("WScript.Network")

'*********************************
'Set Script Variables
'*********************************
RunScriptAs = "svc-ntbackup"
LocalHost = WSHNtwk.ComputerName
BACKUP_DIR = "\\SERVERNAME\backups\" & LocALHOST & "\"
RetentionDays = 14
EnableNotification = 1

'*********************************
'Set E-Mail Notification Variables
'*********************************
Sender = "Administrator <administrator@Domain.com>"
Recipient = "Name <name@domain.com>"
CarbonCopy = "Name <name@domain.com>"
Subject = "SERVERNAME Backup Status"
Message = "SERVERNAME Backup Log is attached - Please check for errors"
Signature = ""
SMTPServer = "SMTPSERVERNAME"

'*********************************
' DO NOT MODIFY BELOW THIS LINE
'*********************************

'Get Date and Time
CurrYear = DatePart("yyyy",Date)
CurrMonth = DatePart("m",Date)
If CurrMonth < 10 then CurrMonth = "0" & CurrMonth
CurrDay = DatePart("d",Date)
If CurrDay < 10 then CurrDay = "0" & CurrDay
CurrHour = DatePart("h",Time)
If CurrHour < 10 then CurrHour = "0" & CurrHour
CurrMinute = DatePart("n",Time)
If CurrMinute < 10 then CurrMinute = "0" & CurrMinute
Timestamp = CurrYear & CurrMOnth & CurrDay & "_" & CurrHour & CurrMinute

'Delete Outdated Log Files
Dim fso, f, f1, fc
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(BACKUP_DIR)
Set fc = f.Files
For Each f1 in fc
If DateDiff("d", f1.DateLastModified, Now) > RetentionDays Then
f1.Delete
Next

'Run Windows Backup
Set WSHShell = CreateObject("WScript.Shell")
WSHShell.Run "%comspec% /c C:\Windows\system32\ntbackup.exe backup
""@c:\backup_settings.bks"" /v:yes /r:no /rs:no /hc:off /m normal /j
""Full Batched Backup"" /l:f /f " & BACKUP_DIR & LocalHost & "_Full_" &
TimeStamp & ".bkf"

'Get Latest Logfile From Backup Logs
Set NewestFile = Nothing
LOG_DIR = "C:\Documents and Settings\" & RunScriptAs & "\Local
Settings\Application Data\Microsoft\Windows NT\NTBackup\data"
Set Folder = fso.GetFolder(LOG_DIR)
CurrentDate = Now() - 7
For Each f in Folder.Files
If f.DateCreated > CurrentDate Then
Set NewestFile = f
End If
Next
On Error Resume Next
LogFile = NewestFile.Path
If EnableNotification = 1 Then SendStatus = SendLog()

'End Backup Script
WScript.Quit(0)

'E-Mail Latest Log File If Enabled
Function SendLog()
Set objEmail = CreateObject("CDO.Message")
objEmail.From = Sender
objEmail.To = Recipient
objEmail.Cc = CarbonCopy
objEmail.Subject = Subject
objEmail.Textbody = Message & Signature
objEmail.AddAttachment LogFile
objEmail.Configuration.Fields.Item
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item
("http://schemas.microsoft.com/cdo/configuration/smtpserver") =
SMTPServer
objEmail.Configuration.Fields.Item
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Send
End Function

No comments: