Scheduling a Backup Script with the Windows Vista Task Scheduler

If you want to run a script automatically at a regular interval, you can schedule it as a task in the Windows Vista Task Scheduler. One such use is to make a backup copy of a database every day at a specific time. The DailyDatabaseBackup.vbs script makes a dated backup copy of the sample Northwind.mdb database every day at the specified time.

A VBS script run from the Windows Explorer can have spaces in its name, but if you intend to run a script from the Task Scheduler, its name can't contain spaces. You won't get an error when selecting the script, but when the Task Scheduler attempts to run it, you will get an error and it won't run.



Dim fso

Dim fil

Dim strPrompt

Dim strDBPath

Dim strDBName

Dim strDBNameAndPath

Dim strDBNameBackup

Dim strScriptName

Dim strScriptNameAndPath

Dim strBackupNameAndPath strScriptName = WScript.ScriptName strScriptNameAndPath = WScript.ScriptFullName

Modify the hard-coded path as needed for your system:

strDBPath = "E:\DocumentsX"

strDBName = "Northwind.mdb"

strDBNameAndPath = strDBPath & strDBName

MsgBox "Database name and path: " & strDBNameAndPath strDBNameBackup = Left(strDBName, Len(strDBName) - 4) _ & " Backup for " & FormatDateTime(Date(), 1) _ & ".mdb"

MsgBox "Backup name: " & strDBNameBackup strBackupNameAndPath = strDBPath & strDBNameBackup

Check that the database is in the specified folder, and quit if it is not found:

On Error Resume Next Set fso = _

WScript.CreateObject("Scripting.FileSystemObject") Set fil = fso.GetFile(strDBNameAndPath) 'MsgBox "Error #: " & Err

If fil Is Nothing Then strPrompt = "Can't find " & strDBName & _

" in " & strDBPath & " folder; canceling" MsgBox strPrompt, vbCritical + vbOKOnly Quit Else

Copy the database to a backup:

fso.CopyFile strDBNameAndPath, strBackupNameAndPath, _ True

MsgBox strDBName & " copied to " _

& strDBPath & " as " & strDBNameBackup

End If

You can run this script manually, but for assurance that a backup will be made every day, you can schedule the script to run from the Windows Task Scheduler. To open the Task Scheduler, first open the Control Panel, and select the Administrative Tools program group (Figure 17.18).

In the Administrative Tools program group, select the Task Scheduler, as shown in Figure 17.19. If you get a UAC message, click Continue.

Opening the Administrative Tools program group in the Control Panel.

FIGURE 17.19

Selecting the Task Scheduler in the Administrative Tools program group.

FIGURE 17.19

In the Task Scheduler window, select Create Basic Task (Figure 17.20).

FIGURE 17.20

Creating a Basic Task in the Task Scheduler.

FIGURE 17.20

Creating a Basic Task in the Task Scheduler.

Next, as displayed in Figure 17.21, enter the name and description of the task.

0 0

Post a comment