Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

July 12, 2004 12:00 AM

Use Scripts to Automate Windows Utilities

Automate backups and restore Dfs roots
Windows IT Pro
InstantDoc ID #43027
Rating: (4)
Downloads
43027.zip

In Reader to Reader, March 2001, InstantDoc ID 19831, Stephen Kiss provided the Backup.vbs script, which automates backups by running the Windows NT Server 4.0 NTBackup utility. However, Backup.vbs doesn't work on Windows 2000 and later. Inspired by Backup.vbs, I created the BackXP.vbs script, which you run from a command line and which automates a daily backup of a Windows XP client to a tape drive by executing the ntbackup.exe and rsm.exe Windows utilities. I also created the DFSRootBackup.vbs script, which uses the Dfscmd utility (dfscmd.exe) to produce a batch script that restores your Dfs roots. These two examples demonstrate how you can use scripting and Windows Script Host (WSH) to enhance your control over built-in and third-party Windows utilities.

Preliminaries
To use BackXP.vbs, you need to perform two preliminary tasks. First, you must create a backup selection script (.bks) file, which contains a list of folders and files that you want to back up. The .bks file is simply a text file that stores directory paths to back up, one line at a time. For example, if you want to back up the entire contents of drives C, D, and F, your .bks file should contain the following lines:

C:
D:
F:

If you want to back up only certain directories, the .bks file will look something like this:

C:\accelerator
F:\great missenden

Although you can create the .bks file manually, it's easier to do so by using the GUI version of NTBackup to schedule a backup job. There are several ways to run NTBackup, one of which is to select Start, Run. Then, type

ntbackup

in the Run dialog box and click OK.

To create the .bks file, you run NTBackup in Advanced (not wizard) mode, select the Backup tab, and select all the check boxes for the paths that you want to back up. Then, click either Job, Save Selections or Job, Save Selections As to create a .bks file that contains the paths you've chosen to back up.

Your second preliminary task is to use NTBackup to format and label all the backup tapes you'll use. To format the tapes, on NTBackup's Backup tab, click Tools, MediaTools. Label your backup tapes sequentially (e.g., Day One, Day Two, Day Three).

How BackXP.vbs Works
Now that you've taken care of the preliminaries, let's examine BackXP.vbs. The heart of BackXP.vbs is a Select Case statement, which the code at callout A in Listing 1 shows. The Select Case statement identifies the backup tape to use on the current day by checking for seven cases, each of which corresponds to a day of the week. The Select Case statement begins with the passing of two parameters to the DatePart function: the current date on which the script is run, which the script obtains through the Date function, and the w parameter, which tells the DatePart function to return a value from 1 to 7 that corresponds to the day of the week (1 means Sunday, 2 means Monday, and so on). The Select Case statement assigns the predetermined tape names based on the value and day of the week that DatePart returns. As long as you've inserted the correct tape into the tape drive each day before the script runs, BackXP.vbs should execute properly.

The five strParam variables contain some standard NTBackup parameters that you can apply to a backup in your own environment. (NTBackup's Help lists these and other parameters.) The /j parameter uses the Now function to return the job name for the backup log file, including the weekday string and the current date and time. The /t and /n parameters specify the existing tape name and the new tape name, respectively. I use the same values for the tape name to ensure that I overwrite only the same tape name each week; any other tape in the drive will cause the backup job to fail. This is just a small failsafe in case I put the wrong tape in the drive. The /d parameter specifies the job's description.

The strParam5 variable includes six parameters. The /v parameter indicates whether NTBackup should verify the backup. You can specify yes or no; you typically specify yes, assuming you check the backup logs daily for errors. You use the /r parameter to indicate whether only administrators are permitted to access the data on the tape. Like the /v parameter, the /r parameter options are yes and no. The /l parameter is the type of log file. You can specify f (full), s (summary), or n (none). The /m parameter indicates the backup type. You can specify normal (full backup, which is the default), copy (full backup, except the archive bits aren't cleared), differential (backup that copies files created or changed since the last full or incremental backup), incremental (similar to a differential backup, except the archive bits are cleared), or daily (which copies only files that have changed that day and doesn't clear the archive bits). The /rs parameter tells NTBackup whether (specify yes) or not (specify no) to back up the removable storage database. Setting the /hc parameter to on enables hardware compression if it's available. Setting this parameter to off disables it.

Next, BackXP.vbs builds two commands to execute. The strCmd variable contains the command to run the complete NTBackup command with all its parameters. The strRSMCmd variable contains the rsm.exe command that will eject the backup medium after the backup has completed.

The script executes the NTBackup command by using the WshShell object's Run method, as the code at callout B shows. The Run method's SHOW_WINDOW parameter tells the script to display a window that shows the backup is taking place. The Run method's WAIT_ON_RETURN parameter tells the script to pause execution until NTBackup has finished its work. When NTBackup completes successfully, it returns a value of 0; otherwise, it returns a nonzero value. The script stores the returned value in the intResult variable.

Related Content:

ARTICLE TOOLS

Comments
  • Karen
    4 years ago
    Oct 24, 2008

    Hi kevystyles,

    Are you not able to see the entire article? (There are two pages.) Or can you see the entire article but need more information?

    Please let me know and I'll be glad to help with either situation.

    Thanks for commenting on this article!

    Karen Bemowski, senior editor
    Windows IT Pro, SQL Server Magazine

  • Kevin
    4 years ago
    Oct 20, 2008

    need more instructions

  • ryan
    4 years ago
    Jun 02, 2008

    need more instructions

  • Piotr
    5 years ago
    Mar 02, 2007

    as

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.