Backing up your data is vital, whether it’s your holiday photos, or your business, and there are many products out there that all do the same thing: backup your data.
For the average home user an expensive backup solution is not the best solution, or due to budget requirements it may not be possible to purchase software. But did you know that Windows comes with its own software?
In this article I will show you how to use the command line tool XCOPY to create backups of your data and to schedule the task to run every day.
First, lets bring up the command prompt and look at xcopy.
C:\Documents and Settings\....>xcopy /?
Copies files and directory trees.
XCOPY source [destination] [/A | /M] [/D[:date]] [/P] [/S [/E]] [/V] [/W]
[/C] [/I] [/Q] [/F] [/L] [/G] [/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. Each string
should be in a separate line in the files. 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.
/G Allows the copying of encrypted files to destination that does
not support encryption.
/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.
C:\Documents and Settings\...>
Looks pretty intimidating doesn’t it? For this example we will assume that my documents are located in C:/My Documents/ and that I want to back them up to another drive.
xcopy "c:\my documents\*.*" "j:\my backups\"
When you run this command you will get the message “Does J:\my backups\ specify a file name or directory name on the target?”. This is because the command can’t tell if you want to copy to a folder or a file called my backup. This can be
remedied using the /I switch to force the destination as a folder.
This will now copy all the files in C:\My Documents\ to J:\My Backups\. All well and good, but this will not copy sub directories, to do this we need to add /E to the end of the command. It would also be a good idea to verify the data we are coping to be sure it hasn’t been corrupted by adding /V to the command. We can then put this command into a batch file that we can run to create a backup
Create a new text file on the desktop and call it “backup.bat”. Open it in a text editor (right-click then Edit should work).
Add in our new command:
xcopy "c:\my documents\*.*" "j:\my backups\" /I/E/V
When run, this batch file will copy the entire contents of C:\My Documents\ to J:\My Backups\ including sub directories. When run again however you will get a message stating “File creation error - Cannot create a file when that file already exists.” What we need to do now is tell the command to overwrite existing files, so add /Y and /R to the command.
There are a few other switches that we are going to use, /C will continue, even id there is an error. This prevents the backups from halting all together. /H will copy any hidden and system files such as thumbs.db (Windows Explorer Thumbnails), /K will also copy over file
attributes such as Read-Only, Hidden etc… /D and /M will copy files if they have been modified since the last backup.
Our command should now look like this:
xcopy "c:\my documents\*.*" "j:\my backups\" /C/E/H/R/K/D/M/Y
What we have here is a differential backup batch file using xcopy.
You can add multiple lines to the batch file to backup more than one folder.
Here is my backup batch file, the first line will close Microsoft Outlook before backing up my emails.
taskkill /f /im outlook.exe xcopy "f:\my work\*.*" "j:\my work\" /c/e/h/r/k/d/m/y xcopy "C:\Documents and Settings\...\Application Data\Thunderbird\Profiles\*.*" "j:\thunderbird\" /c/e/h/r/k/d/m/y xcopy "C:\Documents and Settings\...\Local Settings\Application Data\Microsoft\Outlook\*.*" "j:\outlook\" /c/e/h/r/k/d/m/y xcopy "C:\Documents and Settings\...\Application Data\Microsoft\Outlook\*.*" "j:\outlook\" /c/e/h/r/k/d/m/y xcopy "d:\websites\*.*" "j:\websites\" /c/e/h/r/k/d/m/y xcopy "d:\My Programming\*.*" "j:\My Programming\" /c/e/h/r/k/d/m/y xcopy "d:\My Pictures\*.*" "j:\My Pictures\" /c/e/h/r/k/d/m/y



