Using NT for Scheduled Network Backups

Write a simple script to run the NT Backup utility.

Toby J. Velte

July 31, 1997

12 Min Read
ITPro Today logo

For small businesses on a budget, backing up network systems can beexpensive, time-consuming, and difficult. I developed a solution that makesnetwork backup more economical by using built-in tools on Windows NT 3.51 and4.0 systems. My solution consists of a simple backup script (batch file) thatcontains commands for running the NT backup utility--NTBACKUP. You use the NTCommand Scheduler or AT command to schedule when the script will run. Thesetools let you perform unattended scheduled backups of any network drive you canconnect to, including shared drives on Windows 95 and Windows 3.11 machines andNT systems.

Getting Ready
To use this backup method, you need Administrator privileges. Also, you mustinstall a tape backup device on the NT machine on which you'll run the backupscript.

Before writing the script, make sure you can connect to (i.e., mount)shared drives on the machines you want to back up. In a workgroup environmentthat has a separate user database for every system, you must have an accountwith sufficient privileges on each machine. I recommend creating an account forthe sole purpose of backing up the system. Give this user a name such as Backer,and assign it the User and Backup Operator groups only. If you use domains,create a user on the Primary Domain Controller (PDC) with the group settingsmentioned above; use this account to connect to all shared drives.

An easy way wto test whether a user has sufficient privileges is to try toconnect to the shared drive under File Manager, while substituting the new userin the Connect As box in the Connect Network Drive menu option. If you receivethe Network path was not found error, the share is unreachable, and youneed to verify that the share is present. If you receive the Access deniederror, you probably don't have sufficient privileges; correct this at the shareby including access to all users (if security is not a concern) or only to theuser you created to conduct the backup operations.

Before you write the script, you also must decide where to save the logfiles. I recommend an easy-to-remember yet secure location on the server that isdoing the backup. I place the log files in a directory called LogFiles, in asubdirectory of the NT system directory. If you choose to store the log files ona network drive, make sure your connection will be stable for the duration ofthe backup.

At this point, you might find it convenient to organize a list of the namesof the machines you want to back up, the shared drive names on those systems,and passwords required to connect to those systems or drives. Before you startthe backup, test whether you can connect to the shared drives and have authorityto back up all appropriate files.

Creating a Backup Script
Listing 1 shows the commands for a sample backup script (BACKUP.BAT). Thescript follows a basic format. First, you connect to a shared drive on a remoteNT machine and assign it a drive letter. Next, you use the NTBACKUP command tocopy the drive's contents to tape. When NTBACKUP has finished copying the drive,you delete the mount (i.e., disconnect the share) and start again on anothershared drive. In this way, you back up all shared drives sequentially. NT addseach shared drive's contents (i.e., backup set) in turn to the backup tape, andeach backup set has a section in the backup log.

Exploring the Backup Script
You can write the script using any text editor. Use the sample as a templatefor your script, although you will need to change some arguments for yoursystem's specific configuration. Words in angle brackets are sample text; youmust change them to fit your network environment. For example, in the firstline,

net send Backup Started

you must change the word user to an actual user who will receive themessage Backup Started when NT runs the script. The sample scriptincludes net send messages at the beginning and the end to let the userknow that the script started and ended successfully. The messages also include,by default, the times the system sent the messages. Because these timescorrespond to the start and end times of your script, you can easily calculatehow long the entire script took to run.

The statement in callout A in Listing 1 tells the system to perform severaltasks: Back up the local C drive, include a comment (/d), include the localRegistry in the backup (/b), use hardware compression (/hc:on), perform aparticular backup type (/t), write the results to a log file called backup.log(/l), and designate which tape drive you want to use (/tape). Note that the /tswitch can be followed by normal, copy, incremental,differential, or daily, depending on the type of backup you wantto do. You set another switch, /tape, to 0, if you're using the first tape driveinstalled. A value of 1 means the second tape drive, 2 the third, and so forth.

Notice also that I include a full path to the log file, starting with theenvironmental variable %windir%. This variable lets you use the scripton other machines with little modification.

The lines in callout B in Listing 1 tell the system to back up a networkshared drive on an NT system. First, you connect to the share and assign it alocal drive letter (in this case, X). This line uses the authority of userBacker in domain domainname to connect a share on a workstation. You mustinclude this user's password in the script. Because the program saves thisscript as ASCII text and anyone with sufficient privileges can read the script,a security risk exists. This risk is one reason to create a user with limitedauthority only for backing up shares.

Next, the script issues an NTBACKUP command to back up the X drive. Thisline differs from the previous backup command (A) in two ways. First, NT appendsthis backup set to the previous one (/a), so the previous backup sets aren'terased. Second, this line has no /b switch telling the program to include theRegistry with the backup. The current version of NTBACKUP lets you back up theRegistry of the local machine only. To safely back up the Registry, includingthe user database (/s), you must make a repair disk at the console for eachmachine. Of course, you can use the same Emergency Repair Disk (ERD) foridentically configured systems. You will need this disk later if you have torestore a network drive that contained a functional NT system. (For informationabout ERDs, see Michael D. Reilly, "The Emergency Repair Disk,"January 1997).

Because the number of shares you back up might change or exceed the numberof available drive letters on your system, always disconnect the share afteryou've backed it up. Doing so cleanly terminates your session with this networkshare and lets you reuse the drive letter. The command

net use x: /delete

disconnects the share and frees the X drive letter.

This script also includes commands (C) for backing up a Windows 3.11machine. Use the same syntax for Win95 machines. This system need not be in yourworkgroup or domain, but you must be able to resolve its Universal NamingConvention (UNC) name (e.g., \computername). Assuming a passwordprotects the shared drive, you connect to the share by executing the command inline 11. This statement doesn't include usernames because only passwords, notuser authorization, protect Win95 and Win 3.11 shares. However, the command toback up the share is the same as that for the NT workstation. The next linedisconnects the share. You can, of course, back up any number of differentshares, which may include other types of systems, provided you can connect totheir shares.

Schedule the Script
After you create a backup script for automated network backup, test thescript commands by entering them one line at a time at the command prompt. Ifthe commands execute as planned, run the entire script from the command promptby entering the script name. If the script works as intended, you're ready toschedule it to run automatically using either the Command Scheduler (WINAT.EXE)included with the Microsoft NT Resource Kit or the AT command (AT.EXE).The Command Scheduler is a graphical interface for the AT command. Although theAT command isn't as elegant, if you don't have the resource kit, you can use itto fulfill all your scheduling needs. In either case, by default, onlyAdministrators can submit commands with these utilities.

To use either the Command Scheduler or the AT command, start the Schedulerservice from Control Panel, Services. You need Administrator privileges to startthis service. Note, however, that AT jobs don't run under the security contextof the user requesting the job. Rather, they use the security context of theSchedule service, which is typically in the context of the operating system.Make sure you set the startup mode to Automatic so your scheduled jobs willsurvive a reboot.

Let's look at how to schedule a command to run via the Command Schedulerand via the AT command. The NT resource kit (highly recommended for anyNT-phile) includes the Command Scheduler in the Configuration menu. Once youstart this program, the program asks you whether you want to start the Schedulerservice, if it isn't already started. Answer yes, and then select Edit, Add toschedule your new script. You'll see a window similar to Screen 1. Enter yourscript filename (use the full path if the script isn't in a directory listed inthe path variable) and the parameters describing when you want it to run. Checkthe Interactive box only if you want to require a user to enter informationduring the script execution. After you click OK, you see the Command Schedulerwindow, shown in Screen 2, which lists AT jobs, including your script file withthe appropriate times. If an error appears in the Error column after the timescheduled for your AT job to execute, make sure you've placed the script file ina directory listed in the path, or provide the full path to the script file.When you're sure the scheduling information is correct, simply close the CommandScheduler, and your script file is ready to go.

If you don't have the Resource Kit, you can use the AT command to schedulethe backup script. First, enter

AT/?

at the command prompt to learn more about the AT command's switches andarguments. Then, at the command line enter a line similar to

AT \ 1:00 /EVERY:Saturday "backup.bat"

This command schedules the script file for the same settings you used forthe Command Scheduler.

Restoring Data
Although the backup script automates network backup, it performs no restorefunctions. To restore data from tape, you use the NTBACKUP program's restorefunction. To perform a restore, select the Tapes option from the Windows menuand double-click the tape icon to display the backup set list on the tape. Fromthe set list, you can select entire drives, directories, or individual files forrestoration, by clicking the boxes preceding their names. Click Restore. On thewindow you see in Screen 3, designate where you want to place the restoredfiles, if not in the original location. If you intend to restore the files totheir initial location, make sure you have re-established the original networkconnections.

Restoring drives containing NT system files requires more work. If thedrive you want to restore was local to the system that created the backup andyou used the /b option to back up that system's Registry to the tape, you're inluck. Place the backup tape drive on an NT system that has access to the driveyou want to restore. If this system is another machine, you physically move thehard drive from this machine back to the original machine after you haverestored NT. You can also use another installation of NT on your system if thesecond version of NT is on a different hard drive. Find the disk in the tape'sbackup set list, select the entire drive, and click Restore. Next, check theRestore Local Registry box, and click OK. After the program has restored filesto disk, you have to return the drive to the exact hardware configuration it wasin when it last successfully ran NT. You might have to remove the tape devicefrom your other system and add it back to your original machine. The systemstartup files expect the same environment that existed when you made the backup.

If the drive you want to restore contains an NT system and was a remotedrive, you need an ERD to successfully return the system to its previous state(you should have already made a repair disk before backing up the system). Torestore this drive, select the entire disk from the backup set list and restoreit to a replacement drive. Install the replacement drive in the NT machine andbegin the NT installation. Choose to repair an installation when prompted, andinsert the ERD. Select Inspect Registry Files, and continue. The programpresents you with a list of Registry keys that the system can restore; selectall of them. When the program has successfully restored the Registry, rebootyour system.

Missing Features
Although writing a script and using the AT command or Command Scheduler is aflexible solution, the solution lacks some features that you might find in acommercial NT network backup product. The most obvious shortcoming is theinability to back up Registries on remote drives. However, as I've alreadymentioned, you can address this limitation by creating an ERD for each machineyou back up.

Another pitfall is that all shares you back up must fit on one tape;otherwise you must manually change tapes, which defeats the purpose of anunattended backup. In this situation or others that require user input, you mustrun the script in Interactive mode or the NT
BACKUP process will hang, andyou'll have to reboot and restart the script.

But don't worry. If the script tries to connect to a share on a machinethat's turned off or otherwise debilitated, the script will skip this commandand move to the next one and back up other shares.

Want More?
You can find more information In the NT backup program's Help file. Lookunder the Backup disk files to tape book and then under Using batchfiles to do backups. Or enter

NTBACKUP/?

at the command prompt to list the command-line switches for the NTBACKUPcommand. To obtain more information about connecting and disconnecting networkshares, enter

NETHELPUSE

at the command prompt.

The NT Resource Kit includes commands that let a script pause in itsexecution or pause for user input (see SLEEP /? and TIMEOUT /?, respectively).In addition to command-line Help, you can find more information about thecommands in this article in the resource kit documentation. For more informationon backing up your system, see Bob Chronister, "System and Enterprise-wideBackup Software," April 1996.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like