Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript to backup server with NTBackup.exe

Status
Not open for further replies.

itopman

IS-IT--Management
Mar 23, 2004
20
0
0
GB
I am new to scripting and need help to create a vbscript to run a daily full backup of c: and d: drives saving log to backup.log

i know this is very simple to people that use VBScripts all the time, so if any one can help a sample script that just needs editing would be ideal.

Thank you
ITOpMan
 
I thought with the quality of the othe rposts this one would be easy!
I have manage with help from a friend to do the following which works to the point where the back up completes and verifies the data then fails at the point of log file line 'Set LogFile = WSHFileSystem.OpenTextFile("c:\backup\backup.log", 1, True, True)

This is on a windows 2000 server.
Please help

option explicit

Public Const VerifyBackup = True
Public Const UseHWCompression = True
Public Const MediaType = "DLT"

Public ComputerName
Public LogPath
Public Date
Public TapeName
Public BackupCommandLine

Public WSHNetwork
Public WSHShell
Public WSHEnvironment
Public WSHFileSystem

Call Backup()

Sub Backup()

dim intResult
dim CommandLine
dim LogFile
dim CDOMessage


SetConstants()

' delete any existing logs
CommandLine = "cmd /c del " & LogPath & "*.* /q"
intResult = WshShell.Run(CommandLine,, -1)
if intResult <> 0 then WshShell.LogEvent 1, "Brians Backup Script - Unexpected error clearing old backup logs. Error Code: " & intResult

' running backup
intResult = WshShell.Run(BackupCommandLine, , -1)

' Log event if error
if intResult <> 0 then
WshShell.LogEvent 1, "Brians Backup Script - NTBackup returned status code of: " & intResult

' emailing error message to logbook
Set CDOMessage = Wscript.CreateObject("CDO.Message")
CDOMessage.From = "my_email_ address@hotmail.com"
CDOMessage.To= SendLogTo
CDOMessage.Subject = ComputerName & " Backup Failed - " & Date
CDOMessage.TextBody = "Backup failed. Unexpected Error. Please check event log for details."
CDOMessage.Send

Exit Sub
End If

' rename logfile to backup log
CommandLine = "cmd /c ren """ & LogPath & "*.*"" backup.log"

intResult = WshShell.Run(CommandLine,, -1)
if intResult <> 0 then WshShell.LogEvent 1, "Brians Backup Script - Unexpected error renaming backup log. Error Code: " & intResult

' copy it to backup folder
set LogFile = WSHFileSystem.GetFile(LogPath & "backup.log")
LogFile.Copy("c:\Backup\Backup.log")

' Delete original file
LogFile.Delete

emailing backup log to logbook
Set LogFile = WSHFileSystem.OpenTextFile("c:\backup\backup.log", 1, True, True)
Set CDOMessage = Wscript.CreateObject("CDO.Message")
CDOMessage.From = "my_email_address@hotmail.com"
CDOMessage.To= SendLogTo
CDOMessage.Subject = ComputerName & " Backup Log - " & Date
CDOMessage.TextBody = LogFile.ReadAll
CDOMessage.Send

End Sub


Function MakeBackupCommand()
dim strTemp

' Basic Backup command. Pick file selections from c:\easy\backup\selection.bks file
strTemp = "NTBACKUP.EXE backup ""@C:\Backup\Selection.bks"" "

' Tape Name
strTemp = strTemp & " /n """ & TapeName & " - " & Date & """"

' Description
strTemp = strTemp & " /d ""Brians Backup " & TapeName & " daily backup - " & Date & """"

If VerifyBackup Then
strTemp = strTemp & " /v:Yes "
Else
strTemp = strTemp & " /v:No "
End If

If UseHWCompression Then
strTemp = strTemp & " /hc:On "
Else
strTemp = strTemp & " /hc:Off "
End If

' Backup Type
strTemp = strTemp & " /m normal "

' Job Name
strTemp = strTemp & " /j ""Brians Backup"" "

' Log type
strTemp = strTemp & " /l:s "

' Media Pool
strTemp = strTemp & " /p """ & MediaType & """ "

' Use any tape available
strTemp = strTemp & " /um "

MakeBackupCommand = strTemp

End Function


Sub SetConstants()

Set WSHShell = WScript.CreateObject("WScript.Shell")
Set WSHEnvironment = WSHShell.Environment("Process")
Set WSHNetwork = WScript.CreateObject("WScript.Network")
Set WSHFileSystem = CreateObject("Scripting.FileSystemObject")

ComputerName = WSHNetwork.ComputerName
LogPath = WSHEnvironment("UserProfile") & "\LOCALS~1\APPLIC~1\Microsoft\WINDOW~1\NTBackup\data\"
Date = cstr(year(now())) & "/" & right( "0" & cstr( month(now())),2) & "/" & right( "0" & cstr( day(now())),2)
TapeName = DayOfWeek()
BackupCommandLine = MakeBackupCommand()

End Sub


Function DayOfWeek()

Select Case WeekDay(Now())
Case 1
DayOfWeek = "Weekend"
Case 2
DayOfWeek = "Monday"
Case 3
DayOfWeek = "Tuesday"
Case 4
DayOfWeek = "Wednesday"
Case 5
DayOfWeek = "Thursday"
Case 6
DayOfWeek = "Weekend"
Case 7
DayOfWeek = "Weekend"
end select

End Function

 
Any chance you could post the exact error message ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Hi.. the error is file not found on line

Set LogFile = WSHFileSystem.OpenTextFile("c:\backup\backup.log", 1, True, True)

Script c:\backup\backup.vbs
Line 66
Error File not found
Code 800A0035
Source Microsoft VBScript runtime error

 
you have one to many TRUES
object.OpenTextFile(filename[, iomode[, create[, format]]])

only one is required if you want to create the file if it doesnt alrady exist
 
you should prob set your LogFile = Nothing before reusing it, shouldnt make any difference but its nice to get in the habit, like you would if you reused a str variable, you would do strX = "" before reusing it etc

' copy it to backup folder
set LogFile = WSHFileSystem.GetFile(LogPath & "backup.log")
LogFile.Copy("c:\Backup\Backup.log")

' Delete original file
LogFile.Delete
SET LOGFILE = NOTHING
emailing backup log to logbook
Set LogFile = WSHFileSystem.OpenTextFile("c:\backup\backup.log", 1, True, True)
 
saying that you prob dont want to create the text file if it isnt already there so perhaps you shousl change it to
Set LogFile = WSHFileSystem.OpenTextFile("c:\backup\backup.log", 1)

 
Hi...Mrmovie, Thanks for the help much apreciated. I have been able to create this back up script but its not through knowing what I'm doing but through chopping and changing a few old NT scripts that we used to use a couple of years ago.

So you help in explaining the functions are great. will try these later today and see how I get on.

The other question I have, is that the server that this script was used on was on a network that had a exchange server so mailing back to the logbook was set for that. The server I am trying to use it on now is a remote web server will the mailing part still work or is there a better way of getting it to send this log back to the office?

In the mean time if their is a better way to do this script after looking how I have slapped it together then your comments / help would be apreciated.

Great site and some good tips for other projects. keep up the good work.

All the best

ITOpMan
 
im not sure about your use of

intResult = WshShell.Run(CommandLine,, -1)

the -1 is the bWaitOnReturn

object.Run (strCommand, [intWindowStyle], [bWaitOnReturn])

of the Wshshell.Run method. this means if the script will or wont wait for the process it starts, ie ntbackup.exe to finish or not. You will want the script to wait for it to finsih, otherwise you will always return 0 and never see the real result of NTBackup.exe, thats if NTBackup.exe returns an Non zero exit code if something fails, who knows i would hope so.
Anyhow i have never tried -1, I always explicitly use a boolean, like True or False, you would want to use True.

As for mailing back it looks like you are relying on the server having a mail account of some description setup on it, i.e. outlook or something. question: have you got an smtp mail server available to use? if so, then i would go for that option, unless the CDO is working (if it aint broke dont fix it) you will find a number of examples of sending SMTP mail on this site, look at the FAQ or do a word search on it. if you dont have an smtp mail server available then if your box in question is a webserver it might already have smtp setup for its own use or it should be a simple case of enabling it, it comes with IIS i think.

your use of the eventlog is good, im not sure why more peopole dont write application eventlogs for reference, after all thats what the eventlog is there for. its pretty simple to write an eventlog to a remote machine. you might consider writing events to an audit machine locally and monitoring the results that way? would avoid the mail option altogether. saying that its nice to get a mail, people tend to respond to that.
hoo hum ,good luck

i like the way Friday is classed as a weekend!!!

only other thing is your use of the SetConstants sub
its nice to split things up logically and its a good wat to go.
the only thing with objects etc is that it is good practice to destroy them before your script terminates, i know vbscipt trash collection will do it for you, in this case, but sometimes that is not always so.
so, really before your script ends you should
Set WshShell = Nothing
Set WshShellFSO = Nothing etc
it shows others you are not only thinking about getting the job done but you are also cleaning up after yourself
 
Hi...Your comments are noted and will do a bit of editing shortly, found the fault on the run error, it was the path to the log file under userprofile. changed this by pasting in the fullpath displayed in explorer and it worked with no error (althought the only thing different was a capital letter!. just updated the selection to a full backup and runnig test now.

copying the file to backup also worked fine once the path changed.

Saying its going to be hard to get this to be mailed would like to add somethng like a dialog box when script completes, something simple like backup job successful or failed with a close button that opens the c:\backup folder so we can check the log.

How would this need to be done?

Cheers for all the work

ITOpMan
 
thanks for this, the test worked fine so will add this message box and jobs complete, again thanks for all your help. [2thumbsup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top