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'm trying to copy a directory tree recursively preserving hardlinks to the file. Using gnu cp, this would work with the -l flag. cp would then recreate the directory structure, but wouldn't need to copy the contents of each file.

This is preliminary to a backup, first I want to make a cheap (hardlinked) copy of the previous backup and then rsync the source directory over this copy. Roughly:

 cp -r -l yesterdays_backup todays_backup
 rsync -a source_dir todays_backup

Unfortunately, OSX's cp doesn't support the -l flag, as far as I can tell, cpio doesn't support recursive copying. The other alternative is pax, but that leads to the entire directory structure being copied:

 pax -rw backups/yesterdays_backup backups/todays_backup

transforms:

 yesterdays_backup
 |
  \source_dir (...)

to:

 todays_backup
 |
  \backups
          \yesterdays_backup
                            \source_dir(...)

There should be an easy/obvious way to do this, but I'm currently stumped... Any alternatives to cpio and pax? I'd like to avoid having to install gnu cp.

I'm aware of Timemachine, but that won't properly back up encrypted directories incrementally.

share|improve this question

migrated from stackoverflow.com Sep 1 '09 at 0:48

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

    
using find along with cpio get around the lack of recursive cpio as described here: (mikerubel.org/computers/rsync_snapshots/#Incremental) but doesn't seem very elegant... –  Anonymous Aug 30 '09 at 18:48

5 Answers 5

+1 for Pauls's answer. ditto would be an option, but only if used in conjunction with find - it does preserve hard links, but only inside directories.

share|improve this answer

It is easy enough to install cp from MacPorts, however, if you don't want to, or want to create a portable script, then you have three options:

rsync

rsync --archive --link-dest=../yesterdays_backup backups/yesterdays_backup\
   backups/todays_backup

cpio

mkdir backups/todays_backup
cd backups/yesterdays_backup
find . -print | cpio -p -al ../todays_backup

pax

mkdir backups/todays_backup
cd backups/yesterdays_backup
pax -rwl . ../todays_backup
share|improve this answer

I believe what you want can also be achieved with ditto yesterdays_backup todays_backup. By default, ditto does a recursive copy that preserves hard links, ACLs, and extended attributes.

share|improve this answer
    
the problem is that the copy is not cheap any more when the objective is to create hard links across directory scope. ditto can preserve them when they exist, but it can't be told to link instead of creating a duplicate. –  Florenz Kley Apr 20 '11 at 21:49

Although cpio does not support recursive copying directly, you can feed the exact list of files you want to copy into its standard input, and achieve the effect of a recursive copy using find piped into cpio. This is directly analogous to using GNU tar with the options -T - (which means read the list of files to back up from standard input).

share|improve this answer

It's easy to install the coreutils package from MacPorts which contains the GNU cp command renamed to gcp.

But even better, newer versions of rsync, including the one in OS X 10.5 at least, support the --link-dest option which should eliminate the need for the initial cp -al. See here. It's good practice to use the -E option, too, to copy extended attributes, ACLs, etc.

share|improve this answer

Your Answer

 
discard

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