Hack 91 Back Up Individual Files from the Command Line

figs/moderate.gif figs/hack91.gif

You can't back up individual files using the ntbackup command, but there's a workaround.

The normal syntax of the ntbackup command in Windows 2000 and Windows Server 2003 lets you select specific folders to back up, but it doesn't let you select specific files. For example, to back up your C:\data folder as D:\backups\031105.bkf you would type the following at the command line (where /j indicates the descriptive name of the backup job and /f means we're backing up to file instead of to tape):

ntbackup backup C:\data /j "Nov 5 2003 backup of Data folder" /f D:\backups\031105.bkf

But what if you want to back up an individual file in the \data folder but not the entire folder? This is easy to do using the GUI version of the Backup tool. Just start the tool, switch to the Backup tab (click Advanced Mode when the wizard starts in Windows Server 2003), expand the C: drive, select the \data folder, and check off the specific files you want to back up.

However, doing this from the command line presents a problem, because the syntax of ntbackup doesn't allow you to specify files. There is, however, a workaround: you can specify the names of the specific files you want to back up in a backup selection (*.bks) file (also called a script selection file) and use the @ symbol to specify this file in your ntbackup command, as follows (where filename.bks is your backup selection file):

ntbackup backup @filename.bks /j "Nov 5 2003 backup of Data folder" /f D:\backups\031105.bkf

The problem is, you can't create a .bks file from the command line; you have to do it from the GUI.

Creating a .bks file

To create a .bks file using the Backup utility, you simply create a backup job with the selected files you want to back up and then save the job without actually running it. Then, you can copy the .bks file to another location and use ntbackup to back up the files from the command line.

For example, say the folder C:\data contains three files?products.doc, sales.doc, and reports.doc?and you want to back up only products.doc from the command line. Start the Backup utility (switch from the wizard to advanced mode in Windows Server 2003), expand the folder tree, and check the box beside products.doc, as shown in Figure 10-1.

Figure 10-1. Using Backup to create a backup selection file

Now, simply select JobsSave Selections to create your .bks file (specifying a filename like onefile.bks), and close the Backup utility. By default, any .bks files you create are stored in your user profile in the C:\Documents and Settings\username\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\Data folder. To see this hidden folder in Windows Explorer, select ToolsFolder OptionsView"Show hidden files and folders."

When you open onefile.bks using Notepad, it contains one line of text:


Now, copy onefile.bks to a directory with a shorter path, like C:\BKS, since you don't want to have to type C\Documents and Settings\...\Data at the command line. Now you can back up the single file products.doc from the command line as follows:

ntbackup backup @C:\BKS\onefile.bks /j "Nov 5 2003 backup of Data folder" /f D:\backups\


Make sure you don't forget the @ sign before your .bks file; if you do, the backup will fail without warning (a backup-job file will be created, but you won't be able to restore from it). Also, be sure to enter the absolute path for the .bks file, because relative paths aren't supported.

Hacking the .bks file

We've seen how easy it is to back up individual files using ntbackup, by first using the GUI Backup utility to create a .bks file. Once you've created such a file, it's easy to hack it using a text editor such as Notepad, because its syntax is easy to understand. In fact, its syntax is so simple you can simply create a .txt file containing the right information and then rename it with a .bks extension, instead of using Backup to create the .bks file first. In other words, it gets even easier!

Anyway, if we start with our existing file, onefile.bks, and later decide that we want to back up both the products.doc and sales.doc files, all we need to do is add a second line to the file, to make it read as follows:



You can also use your .bks file to back up entire folders or volumes by adding their paths to the file, as follows (you should include the backslash at the end of your volume or folder):



You can also back up the System State information on your server by adding the following line (make sure there is no space between the words System and State):


You can also back up shared folders by specifying their UNC path:


You can even back up a subfolder within a share (again, note the trailing backslash):


Finally, you can back up a volume or folder and exclude certain files or folders. For example, the following .bks file backs up the entire C:\data folder with the exception of products.doc:


C:\data\products.doc /exclude

Creating and customizing .bks files this way gives you a lot of flexibility for performing backups from the command line. Unfortunately, neither the ntbackup command nor .bks files support the use of wildcards. Perhaps we'll see that support in Longhorn (http://msdn.microsoft.com/longhorn/).