I have a folder structure that I want to copy using rsync, in which the structure is a series of backups from which I only want to copy the latest (which can be identified using a symbolic link), like so:
/backups
/foo
/2019-05-01
/2019-06-01
/latest -> 2019-06-01
/bar
/2019-05-20
/2019-06-20
/latest -> 2019-06-20
And so-on; what I would like to do is, using a single rsync command, copy only the latest
backup from each folder, while otherwise mimicking the structure. To this end I came up with the following command:
rsync -rptgoDLm --include '*/' --include '/*/latest/**' --exclude '*' user@remote:/backups /some/local/path
Basically an archive with --copy-links
and some include/exclude trickery to select only the /*/*/latest
branches (plus -m
to avoid a bunch of empty directory structures). This works fine except for one problem; if any of these branches contains symbolic links, then these are also copied as their targets, rather than just as plain symbolic links.
What I really need to be able to do is only use --copy-links
behaviour with the latest
symbolic links, while using another behaviour (e.g- that of --links
) for any others that are encountered.
Is such a thing a possible with a single rsync command? My aim is to be able to run this without having to know what the immediate contents of /backups
is, so that if I add anything new (e.g- /backups/baz
) then it will copy automatically.
Update: To clarify, the resulting directory structure I'd like to see on the destination would be:
/backups
/foo
/latest
/bar
/latest
i.e- I only need the latest version of each backup on the target (the target itself will handle the backup history in another way).
foo/2019-05-01
andbar/2019-05-20
in your example?/some/local/path
?latest
symbolic links as directories and copy only them to the destination, without resolving any other links contained within.latest
symbolic links as directories and copy only them to the destination, but without resolving any symbolic links contained within.