Search code examples
rsync

rsync: how to replicate full directory structure


I would like to use rsync to sync some directories from a remote server to my local machine. In order to keep the files organized i would like rsync to create/copy the whole path with all the parent directories.

I am using ssh with certificate files so no manual login is required.

If i use it like following it copies the file.crt but does not create the parent directories:

rsync -ave ssh [email protected]:/opt/dir/data/file.crt /home/user1/

If i try to use the -R --recursive option it fails with the error below:

 rsync -ave --recurisive ssh [email protected]:/opt/dir/data/file.crt /home/user1/

or with -R

 rsync -aveR ssh [email protected]:/opt/dir/data/file.crt /home/user1/

Unexpected remote arg: [email protected]:/opt/dir/data/file.crt

rsync error: syntax or usage error (code 1) at main.c(1361) [sender=3.1.2]

Thanks for any help in advance


Solution

  • I see two problems here: first, the -R and --recurisive options are not the same (and "recurisive" is misspelled). -R is the short form of --relative, which is what you want (it includes the full path of the source). There is a --recursive option (short form: -r), but that's implied by -a, and also irrelevant here because you're copying a single file, not a directory.

    Second, when you add the new option, you're putting it between -ave and ssh, but ssh is there as a parameter to the -e option, so it must follow immediately after -e. The way you're writing it, rsync is interpreting -R or whatever as a parameter to -e, and ssh as a completely separate argument, and therefore getting completely confused.

    But you shouldn't need -e ssh anyway -- that's almost always the default, so you should be able to just leave it off. Therefore, try this:

    rsync -avR [email protected]:/opt/dir/data/file.crt /home/user1/
    

    If that doesn't work, try this instead:

    rsync -avR -e ssh [email protected]:/opt/dir/data/file.crt /home/user1/