134

I want to backup my home directory to an external drive nightly using a cron job to execute rsync. I am unsure of the exact behavior of rsync's symbolic link flags.

  1. rsync's -a flag includes the -l flag (i.e. "copy symlinks as symlinks"). Does this just mean it will copy the link or that it will follow the link and copy everything in the link-to directory? I want to avoid that because I have links to directories full of media files that would involve copying hundreds of gigabytes I don't need to back up.
  2. Fearing (but not certain) that rsync -a would copy all those media files I instead added the --no-links flag. This does not seem to be behavior I want. It just ignores copying any link which is problematic because I do have links I want copied (e.g. links to common header files from different project directories).
  3. Assuming #1 above (without the --no-links flag) is what I really want and it just copies the link without copying the linked-to files, will the links break when they are backed up? For example I may rsync source directory /home/me/projects/misc to /media/extdrive/backup/home/me/projects/misc. In this case I assume rsync is not smart enough nor does it try to correct the contents of symlinks for the relative directory changes. Is this correct? This is okay, it doesn't matter if the links are broken in the backup directories so long as they would be fixed and working if such time comes when they need to be restored.
2
  • 9
    The -L flag is probably what you want. From the rsync man page on Centos Linux: "-L, --copy-links transform symlink into referent file/dir." I recommend testing this with some sample directories and links to make sure you don't overwrite any data that you don't intend to. Edited to included warning about possible data loss. ;)
    – Mick T
    Oct 23, 2015 at 19:22
  • 5
    For clarity and completeness: The -L or --copy-links flag will "transform symlink into referent file/dir". Exactly what OP is afraid of, i.e. copying the 100gb media directories the links point to, instead of just adding the link verbatim in the new location (for that use -l or --links).
    – Erk
    Apr 2, 2021 at 19:37

2 Answers 2

154
  1. "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.

  2. It's not the behavior you want because you're misinterpreting what --links does and therefore asking for the wrong behavior (see answer #1).

  3. 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).

4
  • 5
    ummm so what's the difference between --copy-links and --links? Dec 18, 2018 at 6:57
  • 3
    --copy-links = -L, and --links = -l. See description from the documentation in my answer here: superuser.com/a/1388910/425838 Dec 30, 2018 at 7:49
  • 3
    so that it saves everyone time, not just me Dec 13, 2019 at 18:32
  • 1
    👋"It's not the behavior you want"
    – ipatch
    Dec 13, 2019 at 18:33
42

@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:

enter image description here

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:

  1. [my answer] Best settings for using rsync with FAT and exFAT filesystems
  2. rsync seems to overwrite already existing file on ExFat
  3. https://superuser.com/a/384849/425838
  4. 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

You must log in to answer this question.

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