24 May 2010

batch files!!!

The key to automated backup is the use of a batch file. This is a text file that contains commands that are executed whenever the file is run. Creating a batch file is easy. In Windows, you can create one using Notepad. The thing to remember is that it must be a pure ASCII text file. Using a Word Processor, even Wordpad, will not produce a pure text file unless you are very careful. I recommend sticking to Notepad to be sure.

Microsoft first introduced batch files in DOS, where they used one (autoexec.bat) as one of the main startup files for booting DOS. Since then, the graphical environment of Windows has taken over, and a lot of people either forgot about batch files or never learned about them. But they still have their uses, and backing up files is one of them. I will do an example that assumes you have a Zip drive that is the G: drive on your system. If you want to backup to a different hard drive, or to a network drive, you will need to adjust this accordingly.

Start by opening Notepad. Type the following command:

copy c:\temp g:

That is all, just the one line. Save it to your Desktop, but be careful to name it Test.bat. You need to have the *.bat extension for it to work as a batch file. Now, if you look at the icon on your desktop, you will see that it is not the usual text file icon. It will have gear on it. That is the sign that it is recognized as a batch file. If you have a C:\Temp directory, make sure it has a couple of files in it so you can see how this works. If you don’t have such a directory, create one and put a couple of files in it. Now, just double-click on your batch file, and you should see a MS-DOS window open, with a black background, and you will see your files being copied. Each file will be listed, and at the end if will say “X” file(s) copied, where “X” is the number of files in your C:\Temp directory. Now, go to your G: drive (or whichever drive you used for this test), and verify that your files were indeed copied. Congratulations! You have just written and run a batch file.

Now, this is the essence of how to use a batch file to backup important files. But we need to refine it a little. First, the “copy” command is just not powerful enough for our purposes. Fortunately, there is another command, called “xcopy”, that will do very nicely. You can get information on this command by opening an MS-DOS window (also known as a Command Prompt in Windows NT/2000), and entering the command:

help xcopy

Here is what you get back in Windows 2000, for instance:

****************************************************

Copies files and directory trees.

XCOPY source [destination] [/A | /M] [/D[:date]] [/P] [/S [/E]] [/V] [/W] [/C] [/I] [/Q] [/F] [/L] [/H] [/R] [/T] [/U] [/K] [/N] [/O] [/X] [/Y] [/-Y] [/Z] [/EXCLUDE:file1[+file2][+file3]…]

source Specifies the file(s) to copy.
destination Specifies the location and/or name of new files.
/A Copies only files with the archive attribute set, doesn’t change the attribute.
/M Copies only files with the archive attribute set, turns off the archive attribute.
/D:m-d-y Copies files changed on or after the specified date. If no date is given, copies only those files whose source time is newer than the destination time.
/EXCLUDE:file1[+file2][+file3]… Specifies a list of files containing strings. When any of the strings match any part of the absolute path of the file to be copied, that file will be excluded from being copied. For example, specifying a string like \obj\ or .obj will exclude all files underneath the directory obj or all files with the .obj extension respectively.
/P Prompts you before creating each destination file.
/S Copies directories and subdirectories except empty ones.
/E Copies directories and subdirectories, including empty ones. Same as /S /E. May be used to modify /T.
/V Verifies each new file.
/W Prompts you to press a key before copying.
/C Continues copying even if errors occur.
/I If destination does not exist and copying more than one file, assumes that destination must be a directory.
/Q Does not display file names while copying.
/F Displays full source and destination file names while copying.
/L Displays files that would be copied.
/H Copies hidden and system files also.
/R Overwrites read-only files.
/T Creates directory structure, but does not copy files. Does not include empty directories or subdirectories. /T /E includes empty directories and subdirectories.
/U Copies only files that already exist in destination.
/K Copies attributes. Normal Xcopy will reset read-only attribute
/N Copies using the generated short names.
/O Copies file ownership and ACL information.
/X Copies file audit settings (implies /O).
/Y Suppresses prompting to confirm you want to overwrite an existing destination file.
/-Y Causes prompting to confirm you want to overwrite an existing destination file.
/Z Copies networked files in restartable mode.

The switch /Y may be preset in the COPYCMD environment variable. This may be overridden with /-Y on the command line.

****************************************************

Now, this may be a bit much when you first look at it, but in most cases you won’t need all of these options in your batch file. Here is a copy of my own backup batch file, which I call “backup.bat”

xcopy /e /v /y E:\Agent \\NTSERVER\NTServerC\KevinBackup\Agent
xcopy /e /v /y E:\Agent M:\Backup\Agent
xcopy /e /v /y J:\Netscape\Users\KOB1 \\NTSERVER\NTServerC\KevinBackup\Netscape
xcopy /e /v /y J:\Netscape\Users\KOB1 M:\Backup\Netscape
xcopy /e /v /y F:\MyDocuments \\NTSERVER\NTServerC\KevinBackup\MyDocuments
xcopy /e /v /y F:\MyDocuments M:\Backup\MyDocuments
xcopy /e /v /y F:\Eudora \\NTSERVER\NTServerC\KevinBackup\Eudora
xcopy /e /v /y F:\Eudora M:\Backup\Eudora

Let us take this a piece at a time. First, the command “xcopy”. This is followed by three “switches” as they are called. The first, /e, says to copy all of the subdirectories and their contents as well. So I only need to name a directory, and all of the contents get copied. The second switch, /v, tells the computer to verify the copy it makes. The third switch, /y, tells the computer to go ahead and overwrite the remote copy of the file without asking me if I am sure. Following these switches, first comes the source, followed by the destination. So in line one, all of the contents of the E:\Agent directory are being copied to a place on the Server. In my case, I am actually doing two backups here. After copying to the server, I also make a copy to a second hard drive (Drive M: on my machine).

So, take your Test file, and try creating an xcopy command to back up one of your directories. When you know it works, just create additional xcopy commands for as many directories as you think you need to backup. I think you will find that creating a batch file to do your backups is not hard at all. And next I will show you how to make it automatic.

##############———-##############

Use ECHO %DATE% at a prompt and it returns the
data–you can also use %date% in a .cmd file with just about any
other command. You just have to remember, depending on the
regional settings of the machines, to replace / ir you’re
gonna use it in a file name

No comments:

Post a Comment