Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to copy the entire file system hierarchy from one drive to another..i.e contents of each directory as well as regular files in Linux platform. Would be gratefull to know the best way to do that with possibly Linuxes in-built functions. The file system is a ext family.

Thanks

share|improve this question

migrated from stackoverflow.com Jul 7 '11 at 9:07

This question came from our site for professional and enthusiast programmers.

add comment

5 Answers

up vote 8 down vote accepted

I often use

> cp -ax / /mnt

Presuming /mnt is the new disk mounted on /mnt and there are no other mounts on /.

the -x keeps it on the one filesystem.

This of course needs to be done as root or using sudo.

This link has some alternatives, including the one above

http://linuxdocs.org/HOWTOs/mini/Hard-Disk-Upgrade/copy.html

share|improve this answer
add comment

What you want is rsync.

This command can be used to synchronize a folder, and also resume copying when it's aborted half way. The command to copy one disk is:

rsync -avxHAX --progress / /new-disk/

The options are:

-a  : all files, with permissions, etc..
-v  : verbose, mention files
-x  : stay on one file system
-H  : preserve hard links (not included with -a)
-A  : preserve ACLs/permissions (not included with -a)
-X  : preserve extended attributes (not included with -a)

To improve the copy speed, add -W (--whole-file), to avoid calculating deltas/diffs of the files. This is the default when both the source and destination are specified as local paths, since the real benefit of rsync's delta-transfer algorithm is reducing network usage.

Also consider adding --numeric-ids to avoid mapping uid/gid values by user/group name.

share|improve this answer
    
Can you be more specific on what rsync does? –  Hello71 Jul 7 '11 at 14:42
    
rsync copies the contents of one path to another, and has lots of options that can control its behavior (like compression, excluding certain file patterns, optionally deleting the originals after transfer, optionally deleting the target files before transfer, etc.). –  Michael Aaron Safyan Jul 8 '11 at 3:16
    
Genius, thank you. Btw, I ended up using rsync -avxHAWX --numeric-ids --progress / mnt/ but I should have done rsync -avxHAWX --numeric-ids --progress / mnt/ > ~/rsync.out. I suspect pouring the output to the terminal slowed the process. :D –  Chris K Jan 15 at 10:15
add comment

For a one shot local copy from one drive to another, I guess cp suffices as described by Wolfmann here above.

For bigger works like local or remote backups for instance, the best is rsync.

Of course, rsync is significantly more complex to use.

Why rsync :

  • this allows you to copy (synchronized copy) all or part of your drive A to drive B, with many options, like excluding some directories from the copy (for instance excluding /proc).

  • Another big advantage is that this native tool monitors the file transfer: eg for massive transfers, if the connection is interrupted, it will continue from the breakpoint.

  • And last but not least, rsync uses ssh connection, so this allow you to achive remote synchronized secured "copies". Have a look to the man page as well as here for some examples.

share|improve this answer
add comment

Adding two useful bits to the thread re rsync: changing cypher, and using --update:

As per Wolfman's post, cp -ax is elegant, and cool for local stuff.

However, rsync is awesome also. Further to Michael's answer re -W, changing the cypher can also speed things up (read up on any security implications though).

rsync --progress --rsh="ssh -c blowfish" / /mnt/dest -auvx

There is some discussion (and benchmarks) around the place about a slow CPU being the actual bottleneck, but it does seem to help me when machine is loaded up doing other concurrent things.

One of the other big reasons for using rsync in a large, recursive copy like this is because of the -u switch (or --update). If there is a problem during the copy, you can fix it up, and rsync will pick up where it left off (I don't think scp has this). Doing it locally, cp also has a -u switch.

(I'm not certain what the implications of --update and --whole-file together are, but they always seem to work sensibly for me in this type of task)

I realise this isn't a thread about rsync's features, but some of the most common I use for this are:

  • --delete-after etc (as Michael mentioned in follow-up), if you want to sync the new system back to the original place or something like that. And,
  • --exclude - for skipping directories/files, for instances like copying/creating a new system to a new place whilst skipping user home directories etc (either you are mounting homes from somewhere else, or creating new users etc).

Incidentally, if I ever have to use windows, I use rsync from cygwin to do large recursive copies, because of explorer's slightly brain-dead wanting to start from the beginning (although I find Finder is OS X even worse)

share|improve this answer
add comment

Like Michael Safyan suggests above, I've used rsync for this purpose. I suggest using some additional options to exclude directories that you probably don't want to copy.

This version is fairly specific to Gnome- and Debian/Ubuntu-based systems, since it includes subdirectories of users' home directories which are specific to Gnome, as well as the APT package cache.

The last line will exclude any directory named cache/Cache/.cache, which may be too aggressive for some uses:

rsync -WavxHAX --delete-excluded --progress \
  /mnt/from/ /mnt/to/
  --exclude='/home/*/.gvfs' \
  --exclude='/home/*/.local/share/Trash' \
  --exclude='/var/run/*' \
  --exclude='/var/lock/*' \
  --exclude='/lib/modules/*/volatile/.mounted' \
  --exclude='/var/cache/apt/archives/*' \
  --exclude='/home/*/.mozilla/firefox/*/Cache' \
  --exclude='/home/*/.cache/chromium'
  --exclude='home/*/.thumbnails' \
  --exclude=.cache --exclude Cache --exclude cache
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.