In my working day as a web application developer, I may write a couple of hundred lines of code a day for any given system; write support documents; and produce various graphics and diagrams for the same system. Invariably all of my work might be sitting on a remote machine, for example a web server attached to the network. So what happens if this system fails? Well, if I do not have another copy of all of my hard work, then I might be in REAL trouble.
In order to account for such an occurrence, however remote, I developed the following backup routine. My scheduled routine runs every workday at five minutes before I finish work, and copies the contents off whichever project folder that I happen to be working on directly onto the hard drive of my local PC. This means that, in the event of failure, I have a copy of all of my work up until yesterday to fall back on, rather than having to start again from scratch. This tutorial explains all of the tools and steps required.
Xcopy is a DOS (Disk Operating System) console command that allows you to copy entire folder structures and their contents from one location to another, including from one computer to another where network drives are shared out and mapped. To check to see that you have Xcopy, click Start->Run on Windows, then type cmd and press enter to launch the DOS console.
On a standalone computer, you should get a prompt like this:
However, on a network you might have a P or some other network drive rather than your local C. If this is the case, type C: and enter to return to the C drive. Now type xcopy /? and enter. The question mark tells DOS to display information about this command. Depending on which version of Windows you have, you should get something like this:
C:>xcopy /? 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 attributes. /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.
All of these character flags after the /slash represent various options for the Xcopy command, and as you can see from the descriptions of each one, Xcopy is a very powerful tool. For our batch operation file, we will use two of these flags: /E to copy all sub-directories and their files; and /Y to suppress all prompting. This is very important for a scheduled task, as a prompt will stop the task from being completed while it waits for user confirmation, which is no good for a backup job were a user may not even be present.
In Notepad or some other text editor, created a new file and enter in the following code:
xcopy C:\Program FilesApache Group\Apache2\htdocs C:\backup /E /Y
Now save this file as backup.bat or some other meaningful file name. A batch file (or bat file) is a file that contains valid DOS code, and will be interpreted by the system shell as such. If you double-click the bat file, for example, you will see that it calls a DOS console window to execute the commands within. In the above example, all of the files and sub-directories contained within the default Apache 2 web directory (the source) will be copied to the backup directory on the C drive (the destination). As prompting is suppressed, if the files already exist there, then they will be overwritten.
Now that we have our backup program, we now need to get Windows to run this program automatically at a time suitable for us.
All Windows versions have a very able automated Task Scheduler built in. On my system, as I am using Windows 2000, the options in Task Scheduler my differ slightly to your system if you are using some other version of Windows, but they will be broadly the same. To access Task Scheduler, click Start->Settings->Control Panel, and in the Control Panel double-click on Scheduled Tasks to launch the Task Scheduler.
Double-click Add Scheduled Task to begin the wizard, and when the dialog appears to choose a program for Windows to run, click Browse, then find your backup.bat file where you saved it, and click Open. Now give your Scheduled Task a meaningful name, like "Apache Web Root Backup", and choose a frequency for the task to be executed from the available options, for example choose Daily, then in the next dialog choose Weekdays and a time of 17:00, now your backup will run at 5:00PM every weekday from Monday to Friday, excluding weekends. Finally, you will have to tell Windows what user to run this task as, and supply the user's password (this will most likely be run as you, so enter your own Windows password here).
When you supply your network password for a scheduled task as above, remember that on some networks where you must change your password routinely every month or so, you should also go back to the Task Scheduler and update your password there. For example, if your network operates a policy where three wrongly entered passwords for a particular user account will lock-out that user account, you may find that three failed attempts by Task Scheduler to run your scheduled task will have the effect of locking-out your network account, so be careful!
Generally speaking though, this is not a problem so long as you keep this in mind. If your network does not operate this policy, or you are working on a stand-alone machine, then this is less of an issue, although your scheduled task will not run successfully if you have changed your account password but forgot to change it in the Task Scheduler. Therefore, don't always blindly assume that your back-ups are running: check back on them now and again to make sure they have not hit a problem. After all, you may find yourself needing them some day!
Updated 2020 : note that the above post may be out-of-date, given this post was originally published in 2004, but is left here for archival purposes.