cp -r -l in MacOS (recursive copy preserving hard links)

12

4

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.

a2800276

Posted 2009-08-30T18:25:35.423

Reputation:

using find along with cpio get around the lack of recursive cpio as described here: (http://www.mikerubel.org/computers/rsync_snapshots/#Incremental) but doesn't seem very elegant...

– None – 2009-08-30T18:48:10.417

Answers

16

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

Paul Wagland

Posted 2009-08-30T18:25:35.423

Reputation: 311

What if source and destination are in different disks? Would rsync still preserve hard links from source in the copy made in the destination? – lpacheco – 2020-02-25T11:31:03.983

7

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.

Ned Deily

Posted 2009-08-30T18:25:35.423

Reputation:

1

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).

Jonathan Leffler

Posted 2009-08-30T18:25:35.423

Reputation: 4 526

1

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.

DR

Posted 2009-08-30T18:25:35.423

Reputation:

I don't think ditto can even be told to preserve directory hard links. man page says it only does file hard links – Chris F Carroll – 2017-10-03T17:36:32.427

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 – 2011-04-20T21:49:39.267

0

+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.

Florenz Kley

Posted 2009-08-30T18:25:35.423

Reputation: 1 453