[buug] backup a dir with hard links

Michael Paoli Michael.Paoli at cal.berkeley.edu
Thu Feb 15 23:16:28 PST 2007


> Scp -rp somedir/foo host:/somedir/bar
>   r is for recursive, while p is for preserve.
> 
> Works for me. As for Hardlinks, it really shouldn't matter. You shell might
> have something to do with it. 

No, ... that won't preserve the hard links.  And it's not a matter of
shell, either.

$ d=`pwd -P`
$ mkdir s t
$ >s/a
$ ln s/a s/t
$ scp -pq s/? localhost:$d/t
$ ls -l [st]/?
-rw-------  2 michael users 0 Feb 15 23:01 s/a
-rw-------  2 michael users 0 Feb 15 23:01 s/t
-rw-------  1 michael users 0 Feb 15 23:01 t/a
-rw-------  1 michael users 0 Feb 15 23:01 t/t
$
Note that we end up with two distinct target files (each with link
count of 1), and not a single file with a link count of 2.

But if we use tar or cpio, things end up much better:
$ rm t/?
$ (cd s && tar -cf - ?) | ssh -x localhost "cd $d/t && tar -xf -"
$ ls -l [st]/?
-rw-------  2 michael users 0 Feb 15 23:01 s/a
-rw-------  2 michael users 0 Feb 15 23:01 s/t
-rw-------  2 michael users 0 Feb 15 23:01 t/a
-rw-------  2 michael users 0 Feb 15 23:01 t/t
$ rm t/?
$ (cd s && ls -d ? | cpio -o -H newc) |
> ssh -x localhost "cd $d/t && cpio -imu -H newc"
1 block
1 block
$ ls -l [st]/?
-rw-------  2 michael users 0 Feb 15 23:01 s/a
-rw-------  2 michael users 0 Feb 15 23:01 s/t
-rw-------  2 michael users 0 Feb 15 23:01 t/a
-rw-------  2 michael users 0 Feb 15 23:01 t/t
$ ssh-add -d

> >>>-----Original Message-----
> >>>From: buug-bounces at weak.org [mailto:buug-bounces at weak.org] On Behalf Of
> >>>Michael Paoli
> >>>Sent: Tuesday, February 13, 2007 10:43 PM
> >>>To: John de la Garza
> >>>Cc: buug at weak.org
> >>>Subject: Re: [buug] backup a dir with hard links
> >>>
> >>>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/
> >>>_______________________________________________
> >>>Buug mailing list
> >>>Buug at weak.org
> >>>http://www.weak.org/mailman/listinfo/buug



More information about the buug mailing list