[buug] backup a dir with hard links

Michael Paoli Michael.Paoli at cal.berkeley.edu
Tue Feb 13 22:42:59 PST 2007


Quoting John de la Garza <john at jjdev.com>:

> If I have a file and create a hard link to it like:
> 
> touch foo
> ln foo bar
> 
> and want to back up the directory that contains this link, it seems  
> that cp and rsync are not able to keep the link structure but if I  
> use tar it does keep it
> 
> Is it possible to keep the links with cp or rsync or must I use tar  
> or another archiving thing?  I have tried cp -RH which is not the way  
> to go...

Well, cp(1) "knows" next-to-nothing about hard links so if you're
trying to replicate the same hard link relationships, cp(1) is
probably not the way to go.  rsync(1) has some hard link
capabilities, so there may be some possibilities there (I haven't
explicitly examined/tested precise behavior and such).  Various
archive utilities e.g. tar(1), cpio(1), pax(1), etc. typically know
about hard links, ... but may not always handle such exactly as one
might expect.  E.g., some things still haven't changed[1] much:
$ ls
$ mkdir S T && echo a > S/a && ln S/a S/b && echo foo > T/a &&
> (cd S && tar -cf - a b | (cd ../T && tar -xf - b)) &&
> cat T/b; ls -li T/[ab]
foo
116874 -rw-------  2 michael users 4 Feb 13 22:05 T/a
116874 -rw-------  2 michael users 4 Feb 13 22:05 T/b
$
Note that with tar(1), with multiple hard links, the first occurrence
of the file is backed up, and the remainder are noted as hard links to
the first.  When one restores from tar(1), when non-first occurrences
are restored, hard link is made to the pathname of the first occurrence
... regardless of whether or not that data was restored.  That can
sometimes lead to rather surprising results.

I tend to think cpio handles that more reasonably[2].  If one restores
any and/or all of the occurrences of the hard linked file, one gets the
data that was backed up, and all the occurrences restored will be
hardlinked to that same restored file.

footnotes/references:

1. news:7v0cn4$37d at crl.crl.com
   or
   http://tinyurl.com/yosf8s
2. http://en.wikipedia.org/wiki/Principle_of_least_surprise
cp(1), tar(1), cpio(1), pax(1), etc.
http://www.unix.org/



More information about the buug mailing list