如何在不删除符号链接的情况下复制目录结构?

koj*_*iro 31 rsync symlink mirror

我需要将一堆文件“安装”到另一个目录,保持源文件的目录结构完整。例如,如果我./foo/bar/baz.txt要去,/var/www/localhost/webroot/我希望结果是/var/www/localhost/webroot/foo/bar/baz.txt. rsync中具有此功能--relative,但是当我这样做时,我发现它对符号链接不友好:

$ ls -ald /var/www/localhost/webroot/ | grep ^l
lrwxrwxrwx  1 www-data www-data     15 2014-01-03 13:45 media -> ../static/media
lrwxrwxrwx  1 root     root         13 2014-02-24 13:47 var -> ../static/var
$ rsync -qrR . /var/www/localhost/webroot/
$ ls -ald /var/www/localhost/webroot/ | grep var
drwxr-xr-x 3 root root 4096 2014-02-24 13:52 /var/www/localhost/webroot/var
Run Code Online (Sandbox Code Playgroud)

所以你看到符号链接不再是符号链接——文件被复制到了错误的地方!

rsync也有--no-implied-dirs选项,从表面上看似乎可以做我想做的事,但它只能在执行递归 rsync时按我的意图工作,所以我必须:

find . -type f -print0 | xargs -0I{} rsync -R --no-implied-dirs {} /var/www/localhost/webroot/
Run Code Online (Sandbox Code Playgroud)

有没有更直接的方法来完成这种文件镜像而不清除中间符号链接目录(有或没有 rsync)?

ang*_*gus 43

使用rsync的选项-K( --keep-dirlinks)。从联机帮助页:

 -K, --keep-dirlinks
      This  option  causes  the  receiving side  to  treat  a
      symlink  to  a  directory  as though  it  were  a  real
      directory, but only if it matches a real directory from
      the  sender.   Without   this  option,  the  receiver’s
      symlink  would  be deleted  and  replaced  with a  real
      directory.

      For example, suppose you  transfer a directory foo that
      contains a file file, but foo is a symlink to directory
      bar  on  the  receiver.  Without  --keep-dirlinks,  the
      receiver  deletes  symlink  foo,   recreates  it  as  a
      directory,  and   receives  the   file  into   the  new
      directory.   With --keep-dirlinks,  the receiver  keeps
      the symlink and file ends up in bar.

      One note  of caution:  if you  use --keep-dirlinks, you
      must  trust all  the symlinks  in the  copy!  If  it is
      possible  for an  untrusted  user to  create their  own
      symlink to  any directory,  the user  could then  (on a
      subsequent  copy)  replace  the  symlink  with  a  real
      directory and affect the  content of whatever directory
      the  symlink references.   For backup  copies, you  are
      better off using something like a bind mount instead of
      a symlink to modify your receiving hierarchy.

      See also  --copy-dirlinks for  an analogous  option for
      the sending side.
Run Code Online (Sandbox Code Playgroud)


Ben*_*ynn 17

我想将我的符号链接保留为符号链接。为此,您可以使用 -l 选项。

    -l, --links                 copy symlinks as symlinks
Run Code Online (Sandbox Code Playgroud)

由于我在 OS X 上复制框架,我发现这很有帮助。


小智 6

请使用-a,正如上面所暗示的-l那样。但如果您想要完整的源副本,它还包含其他重要选项。

另外:据我所知,手册页-K用于接收方的符号链接。我认为这不应该是这里的正确答案。