Backup files using Xcopy in command line

by admin on November 3, 2010

There are lots of commercial data back up solutions available in the market. If you are a home user or a small business user of Windows OS and want to backup all your important data then this article is for you. If you can’t afford to buy a commercial backup application don’t worry! You can still backup your data with a simple windows command called Xcopy.

You can use a USB drive or an external hard disk as the backup location. Copying files to another partition on the same disk will not serve the purpose as a disk crash will cause data loss on all the partitions of that disk. Let’s say your data is in the folder D:\Data and you need to backup your data to Z:\Backup folder (Z: is mapped to some external disk storage device.). We can just copy data from D:\Data to Z:\Backup directly using any copy command or tool but it will involve copying the files even though they are not changed. I will explain this in detail. Let’s say you take data back up every week. The first time you back up the data you need to copy all the files in your disk to the backup location. In the second week, you need to copy only the files that have been changed during the last 7 days. You do not need to copy the files that have not been touched since the time you took the last back up. This will save lot of time and also decreases CPU usage and also will lead to increased lifetime for the storage devices. So a backup utility should be able to detect which files are changed and which are not. This is called incremental back up.

Now let’s see how we can do this using Windows built-in Xcopy command. Xcopy has different switches using which we can specify the conditions on what files need to be copied. We will use these switches to make Xcopy work as a backup utility.

You can run the below command to back up data.

xcopy  source  destination  /M /E /G /H /Y

This one uses archive attributes of files to distinguish the files that have not been changed since the time last back up was taken. /M switch turns off the archive attribute on the files after copying the file to the destination. This attribute will be set only if someone modifies the files after wards.  For the above example the command is given below.

Xcopy  D:\Data  Z:\Backup  /M /E /G /H /Y

Back up data using date and time stamps

We can do incremental backup by specifying the date the last back up was taken. Xcopy will copy only the files that have changed after this date. For this we need to run the below command.

xcopy source destination /E /G /H /Y /D:mm-dd-yy

Hope this post has helped you. Let me know your comments on this.

Related Posts:
Xcopy command syntax and examples

{ 2 comments… read them below or add one }

Muthu September 3, 2011 at 11:34 am

How to automate the backup operation? If I want to run backup using xcopy every Monday, how can I do it.

Reply

nerf October 10, 2011 at 1:06 pm

You can easily automate operations using Windows “Scheduled Tasks”.
Simply craft your commands in Notepad, save it as a batch file (name.bat), then create a scheduled task that executes the batch file. You’ll find the Scheduled Tasks Manager under Start>Accessories>System Tools.
It’s pretty intuitive- creating a new task is much like creating a new event in a calendar, except that ya can brows to where you saved the batch file.
need more?
jfgi ;^)

Leave a Comment

HTML tags are not allowed.

Previous post:

Next post: