Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rsync - copy symlinks as directories, but skip if they point to file

Tags:

backup

rsync

I have the following structure:

/source/link1/->/dir1
/source/link2/->/dir2
/source/link1/file2 
/source/link1/link11->/dir2/file21

link=symbolic link, file=real file.

At the target (remote) directory links in the "root" of source folder should be copied as directories, and links under /source/link1, /source/link2 folders should not be copied, or they should be copied only as links (to avoid duplicate structure). Target structure is the following:

/target/link1 - directory
/target/link2 - directory
/target/link1/link11->/target/link2/file21
/target/link1/file2

How to maintain that with rsync? It is needed for the daily sync - every day target and source folder should be synced, and rsync should be used with --delete command.

like image 957
Zvezda Avatar asked Dec 24 '22 01:12

Zvezda


1 Answers

Rsync has a range of options for dealing with links (from the man page):

    -l, --links                 copy symlinks as symlinks
    -L, --copy-links            transform symlink into referent file/dir
        --copy-unsafe-links     only "unsafe" symlinks are transformed
        --safe-links            ignore symlinks that point outside the tree
        --munge-links           munge symlinks to make them safer
    -k, --copy-dirlinks         transform symlink to dir into referent dir
    -K, --keep-dirlinks         treat symlinked dir on receiver as dir

If it so happens that the top-level links are "unsafe" and the other links are all "safe", then --copy-unsafe-links will Just Work, for you.

Otherwise, I'd recommend that you use a loop to sync the top-level directories one at a time:

rsync -a --delete /source/link1/ /target/link1/

Note that -a implies --links, so any links inside the directories will be copied as links, verbatim. The top-level link1 will be treated as a directory if you use the trailing / to dereference it (i.e. it's outside the tree rsync is scanning).

like image 53
ams Avatar answered Mar 05 '23 16:03

ams