Wednesday, March 10, 2010

Using Windows Scripts to Backup Files to External Hard Disk

By: John Andersen

One of the more routine processes that can be performed with a Windows script is backing up files and folders. Often times you just want to copy files onto a backup USB type jump drive or other external hard drive that can be removed and stored safely in case of emergency and can also be easily moved from system to system.

The first step in building this script is you need to assess what exactly needs to be backed up. Is it just a hand full of files or are there complete directory structures and folders that need to be saved in case of a disaster. If you are just copying files straight across to a backup drive you can use the copy command and some wild cards in place of file names along the lines of "copy *.*" which will copy all files from a source to a destination regardless of name or file extension.

There are additional switches you want to be aware of for use with the copy command. The V switch will verify all of the files copied with the original and can come in handy if you are copying files to a floppy disk using a questionable or flaky disk drive. If you want to create a script that can run in the background without user intervention then you will make use of the Y switch. The Y switch suppresses prompting when the copy command overwrites files in to the destination. For example if every day you backed up the same set of files to a backup drive you would want the copy to overwrite the files instead of prompting you to confirm each one.

Now if you want to keep a directory structure along with folders and sub-folders intact or copy them over verbatim, you will need to use the xcopy command. Xcopy is like the copy command only on steroids with many more features including the ability to copy whole folders and sub folders containing files. Executing this command would look like "xcopy source destination" with the E switch which will copy all files and folders. Just like copy the xcopy command supports the Y switch to suppress overwrite prompts.

When it comes to the xcopy command I could cover many more features which can be quite powerful when building Windows backup scripts for your workstations and servers. With this cool command you have the ability to copy over ownership and security information, file system attributes, and so much more. With either the xcopy or copy commands you also have the ability to pipe the detailed job log information into an output file using the > character followed by the file name. You can then go back and review the detailed log files to make sure all of the backups are running smoothly.

Once you have a backup script created you can take it a bit further by looking at ways we can schedule it to run automatically and then freeing up your time to do more important things. Automating a script like this can be easily accomplished using the task scheduler that comes pre-installed with Windows. You simply specify what days and time you want the script to run and your off to the races.