Copy files *without* taking ownership

16,233

Solution 1

Turns out Robocopy can do this incredibly easily. Simply include the /B option on your command line, to copy files in Backup mode. This requires that you run as Administrator.

My command line:

robocopy /MIR /B "E:\Documents and Settings" "C:\DeadDriveBackup\Documents and Settings"

Solution 2

The given answer did not work for me; this did.

Robocopy has a /COPY argument used to specify which parts of files to copy. Available to copy are data, attributes, timestamps, security information (NTFS ACLs), ownership information, and auditing information. If you're looking to copy everything in one directory to another perfectly, use this:

robocopy /E /B /COPYALL "C:\One" "C:\Two"

Let's analyse:

  • /E copies all files, including empty directories. This works like /MIR, but doesn't delete anything in the destination.
  • /B copies in "backup mode", giving Robocopy the permission to overwrite ACLs (file permissions).
  • /COPYALL is an alias to /COPY:DATSOU, which copies all file data, including file ownership and permission (ACL) data.

Are you copying a user profile?

I was, and there are some extra caveats. Default Windows user profiles contain directory junction loops, and Robocopy cannot be configured to create new junctions rather than following them. The above Robocopy command will fail and create difficult-to-remove directory trees, so use this command which will exclude junctions.

robocopy /E /B /COPYALL /XJ "C:\One" "C:\Two"

For compatibility you should probably recreate the default directory junctions in a user profile:

mklink /J "Application Data" "AppData\Roaming"
mklink /J "Cookies" "AppData\Local\Microsoft\Windows\INetCookies"
mklink /J "Local Settings" "AppData\Local"
mklink /J "My Documents" "Documents"
mklink /J "NetHood" "AppData\Roaming\Microsoft\Windows\Network Shortcuts"
mklink /J "PrintHood" "AppData\Roaming\Microsoft\Windows\Printer Shortcuts"
mklink /J "Recent" "AppData\Roaming\Microsoft\Windows\Recent"
mklink /J "SendTo" "AppData\Roaming\Microsoft\Windows\SendTo"
mklink /J "Start Menu" "AppData\Roaming\Microsoft\Windows\Start Menu"
mklink /J "Templates" "AppData\Roaming\Microsoft\Windows\Templates"
attrib /L +S +H +I "Application Data"
attrib /L +S +H +I "Cookies"
attrib /L +S +H +I "Local Settings"
attrib /L +S +H +I "My Documents"
attrib /L +S +H +I "NetHood"
attrib /L +S +H +I "PrintHood"
attrib /L +S +H +I "Recent"
attrib /L +S +H +I "SendTo"
attrib /L +S +H +I "Start Menu"
attrib /L +S +H +I "Templates"
Share:
16,233

Related videos on Youtube

Jonathon Reinhart
Author by

Jonathon Reinhart

Professional C, asm, Python developer GitLab, FreeNas admin

Updated on September 18, 2022

Comments

  • Jonathon Reinhart
    Jonathon Reinhart almost 2 years

    I'm manually backing up data off a hard drive that has XP installed on it, because I suspect the drive is failing. I want to copy the files, but I don't want to change the owner (or any permissions) on the original drive, to be able to do so. Is there any way to do this?

  • Jonathon Reinhart
    Jonathon Reinhart over 10 years
    Good advice. I typically boot to Clonezilla for making hard drive images. In this case though, I only have the hard drive, connected via a USB2 external adapter.
  • user55325
    user55325 over 10 years
    If a drive could be failing it's usually a bad idea to write anything to it - I think Windows does this automatically (at least, Windows XP used to do things like leaving thumbs.db files everywhere, I don't know what kind of caches 7 or 8 uses). I'm not sure exactly how to tell Windows to mount a newly-connected USB drive read-only, so I tend to avoid it when dealing with suspect drives. (Also, I think the error messages tend to be less informative, and distros like Clonezilla and Parted Magic tend to have better built-in tools...)
  • Angstrom
    Angstrom over 10 years
    That's exactly the way. Some backup tools can also do this, but robocopy is the simplest option.
  • Rockallite
    Rockallite over 6 years
    /COPYALL option is useful if you need the exact same permissions on the target files/directories.