Wednesday, June 1, 2011

How to Backup/Copy Files that are "In Use" or "Locked" in Windows (Command Line)

If you’ve ever tried to copy a file that is locked by another application, you’ve probably seen an error message similar to “The process cannot access the file because another process has locked a portion of the file”. So how do you copy it anyway?
image
Since XP, Windows has supported a technology called Volume Shadow Copy, which is used to power the previous versions feature in Vista as well as System Restore and backups. What it does is take a temporary snapshot of the file or drive, and then allow an application to read from the snapshot even while other applications are accessing or modifying the file.
What we can do is use a command line utility called HoboCopy that utilizes this service to copy the file.

Understanding the Prerequisites
HoboCopy and most other backup utilities make use of two services in Windows, and you’ll need to verify that these services are not disabled:
  • Volume Shadow Copy
  • Microsoft Software Shadow Copy Provider
They can be left as Manual startup, so they don’t need to be running all the time. Hobocopy will start the two services automatically when needed, and the Volume Shadow Copy service will be turned back off after it’s done.

Using HoboCopy to Backup/Copy a Single File
The syntax is a little weird, because HoboCopy is really meant to be used for backing up an entire set of folders. We can use it to backup a single file by passing in the filename argument at the end.
Note: on Windows Vista you will need to launch an Administrator mode command prompt by right-clicking on the Command prompt in the start menu and choosing Run as Administrator.
Syntax:
hobocopy c:\directoryname\ d:\backupdirectory\
For example, I want to backup my c:\users\geek\mail\outlook.pst file to d:\backups\outlook.pst. Here’s the syntax that I’d use:
C:\> hobocopy c:\users\geek\mail\ d:\backups\ Outlook.pst
HoboCopy (c) 2006 Wangdera Corporation. hobocopy@wangdera.com
Starting a full copy from c:\users\geek\mail to d:\backups\
Copied directory
Backup successfully completed.
Backup started at 2008-03-09 01:57:28, completed at 2008-03-09 01:58:39.
1 files (606.45 MB, 1 directories) copied, 7 files skipped
Using HoboCopy to Backup an Entire Directory
A much more useful task would be to backup my entire User folder, probably to an external hard drive for safekeeping. For this, we’ll want to add a couple of command-line arguments.

/full Copy all files
/skipdenied Ignore any access denied messages because of permission errors.
/r Copy recursively
/y Don’t prompt, just copy everything

Syntax:
hobocopy /full /skipdenied /y /r c:\directoryname\ d:\backupdirectory\
Let’s go with the same example, I want to backup my entire user directory to d:\backups\, so I’d use this command:
hobocopy /full /skipdenied /y /r c:\users\geek\ d:\backups\
This command will likely take a very long time to complete, so you might want to take a nap or something. At the end you should have a nearly perfect copy of the directory… if there are any permission errors you’ll be alerted to files that didn’t copy. Realistically any files in your user directory shouldn’t have this problem.

Using HoboCopy to Incrementally Backup a Drive
Hobocopy also supports backing up files incrementally, so it will only copy the files that have changed since the last backup. This works similarly to utilities like rsync, except hobocopy stores the last backup date in a file that you need to specify on the command line.
/statefile=filename This flag specifies the file that contains the last backup information.
/incremental Only copy files that have changed since the last full copy.

Syntax:
hobocopy /incremental /statefile=filename /y /r c:\directoryname\ d:\backupdirectory\
Example:
hobocopy /incremental /statefile=d:\lastbackup.dat /y /r c:\users\geek\ d:\backups\
The first time that you run this command you will need to use /full instead of /incremental, or else you will get an error because the state file hasn’t been created yet. After that you can run the incremental backup with the /incremental switch.

This would be an excellent way to automatically backup a set of folders as part of a scheduled task.

Download HOBOCOPY

If you are having problem using the command line you may try CopySharp, which is a GUI version tool for copying open/in process files. It is inspired by robocopy and vshadow. CopySharp V1.0 requires Microsoft Windows Server 2003, Microsoft Windows XP.

No comments:

Post a Comment