rsync and symbolic links
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Future Grid Looping
--
Chapters
00:00 Rsync And Symbolic Links
01:30 Accepted Answer Score 155
03:02 Answer 2 Score 43
04:58 Thank you
--
Full question
https://superuser.com/questions/799354/r...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#linux #rsync #symboliclink
#avk47
ACCEPTED ANSWER
Score 155
"Copy symlinks as symlinks" means exactly what it says: If rsync sees a symlink in the source directory, it will create an identical symlink in the destination. Nothing more.
(A few lines down in the manual page, a different option,
--copy-links
, describes the opposite behavior (always copying the data) which you described as undesirable.)See also the section "Symbolic links":
If
--links
is specified, then symlinks are recreated with the same target on the destination.It's not the behavior you want because you're misinterpreting what
--links
does and therefore asking for the wrong behavior (see answer #1).By default, it copies the destination exactly.
That is, if the link pointed to an absolute path (e.g.
/home/me/projects
), it'll continue pointing to the same path; it won't break, it'll just continue pointing to a file in your home directory rather than the one in your backup.Meanwhile, if the link pointed to a relative path (e.g.
../../projects
), it'll also continue pointing to the same path, but since it's relative to the symlink's location, the symlink in your backup will also be pointing to a file in your backup.Unfortunately there doesn't seem to be any option to translate absolute symlinks for their new base (only an option to break them entirely). To avoid problems, you should change existing symlinks to relative ones (which is a good idea generally, for links inside $HOME).
ANSWER 2
Score 43
@grawity has a great answer, but here's a screenshot of some relevant info from the documentation. Here's the exact wording of the -l
(lowercase letter L, not a one) and -L
options, for instance, which seem to be the most relevant ones in question:
Source: https://linux.die.net/man/1/rsync, or man rsync
manual pages on Linux.
Note also that the -a
(--archive
) option also includes the -l
option within it, which is awesome, since I really like using the -l
option to preserve my symlinks from the source as symlinks on the destination. From the man pages (man rsync
):
-a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
And here are the meanings of -rlptgoD
, also from man rsync
:
--recursive, -r recurse into directories
--links, -l copy symlinks as symlinks
--perms, -p preserve permissions
--times, -t preserve modification times
--group, -g preserve group
--owner, -o preserve owner (super-user only)
-D same as --devices --specials
--devices preserve device files (super-user only)
--specials preserve special files ["such as named sockets and fifos"]
Important!:
If using rsync
on exFAT or FAT filesystems, however, they do not support symlinks nor POSIX owners, groups, and permissions, so remove -lpgo
, and use -rtD
instead of -a
! You may add -v
for "verbose" if desired too, and use -vrtD
.
See more here:
- [my answer] Best settings for using rsync with FAT and exFAT filesystems
- rsync seems to overwrite already existing file on ExFat
- https://superuser.com/a/384849/425838
- https://learn.microsoft.com/en-us/windows/win32/fileio/filesystem-functionality-comparison
Note, for my favorite rsync
commands and instructions, including copying, mirroring, showing total progress...
...doing dry runs, logging stderr and stdout to (separate) files, showing stats, using an include path file and an exclude file, etc, see my full answer here under the 2nd section, titled "2. rsync Command-line tool (Linux, Windows with Cygwin)": https://superuser.com/a/1464264/425838