7

I want to sync a big folder hierarchy between two machines. I want to copy the contents of the symlinks if they are absolute, for eg it points to /some/folder/someFile I want to retain the symlinks if they are relative, for eg, it points to ../../somefolder How do I do this?

Edit: Basically I try to avoid recursion. I tried --copy-unsafe-links option, it copies absolute links but skips internal or relative links, does not retain the link.

2 Answers 2

6

I think you should be able to use the --copy-unsafe-links option on rsync to copy file data where the symlink points outside of the synchronized folder space but sends just the symlink when the file pointed to is also being synced. This is in addition to --links (-l, it's included in -a) to copy relative symlinks inside the copied hierarchy as symlinks.

If that isn't fine tuned enough, you will probably have to create a list of links you do and don't want to send using find and pass those as input to rsync in the form of include/exclude lists.

4
  • it temporarily worked for my problem. Basically I try to avoid recursion but some files have absolute symlink to the synchronized folder. This causes recursion still. :(. It would have been great if there was some option to select symlinks based on pattern just like we are able to select files based on --exclude
    – balki
    Jun 17, 2011 at 15:43
  • Sounds like you should consider asking the rsync developers directly. Jun 17, 2011 at 15:45
  • Sorry, this didn't work, it copies external links but skips links which point to synchronized folder(relative links). It does not copy/create the symlink.
    – balki
    Jun 17, 2011 at 15:54
  • 1
    Did you combine this with --links to preserve symlinks? It should work but you have to use both arguments. If that's not enough control, you'll have to make your lists of includes and excludes to pass to rsync manually.
    – Caleb
    Jun 17, 2011 at 16:11
0

Use this rsync command:

rsync -vrtil --copy-unsafe-links --log-file=(logfile) (source_dir) (dest_dir)

to:

  1. keep symlinks on the local system relative

  2. for absolute links (i.e., starting with "/"), copy the file/dir that the symlink is pointing to (that is, the "referent") to the destination.

Example of #1: In this example, there's 2 files on the source side, the symlink and the file being linked to (that is, the "referent", which is docs_home.htm):

(prompt): ls -al docs*
-rwxr-sr-x 1 (owner) (group) 8853 Jul 31  2019 docs_home.htm
lrwxrwxrwx 1 (owner) (group)   13 May 31  2019 docs_home.html -> docs_home.htm

On the destination (aka target) side, after running the above rsync command, you get:

(prompt): ls -al docs*
-rwxr-sr-x 1 (owner) (group) 8853  Jul 31  2019 docs_home.htm
lrwxrwxrwx 1 (owner) (group)   13 May 31  2019 docs_home.html -> docs_home.htm

Example of #2: On the source side, there's a link that looks like this:

lrwxrwxrwx 1 (owner) (group) 59 Sep 14  2011 Document13.pdf -> /projects/proj_name/proj_subdir/Document13.pdf

On the destination ("target") side, after running the rsync command above, the file itself has been copied, as this snippet of the result of running "ls -al" shows:

-rwxrwxr-x  1 (owner) (group) 45874 Sep 14  2011 Document13.pdf

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .