Using rsync over SSH

Tags: rsync, ssh

Among similar tools like scp and sftp, rsync provides potential advantages such as mirroring data and incremental backups1.

Local to Remote: rsync [OPTION]... [SRC]... [USER@]HOST:DEST 
Remote to Local: rsync [OPTION]... [USER@]HOST:SRC... [DEST]

Where:

  • SRC is the source directory
  • DEST is the destination directory
  • USER is the remote SSH username and
  • HOST is the remote SSH host or IP Address.

To transfer a single file from the local system to a directory on the remote system run:

rsync -a LOCAL_FILE user@DEST:REMOTE_PATH

rsync needs to be installed on both the destination and the source systems2.

Options:

  • -a = archive mode: syncs directories recursively, transfer special and block devices, preserve symbolic links, modification times, group, ownership, and permissions.
  • -r = sync directories recursively if you don’t specify -a as an option3.
  • Use the –delete option if you want to synchronize the local and remote directory. Be careful when using this option as it will delete files in the destination directory if they don’t exist in the source directory.
  • If SSH on the remote host is listening on a port other than the default 22, specify the port using the -e option. Use the -P option to show a progress bar.

If the file exists on the remote server it will be overwritten. If you want to save the file under a different name, specify the new name. To transfer data from a remote to a local machine, use the remote location as the source and the local location as destination.

It is important to know that rsync gives different treatment to the source directories with a trailing slash /. When the source directory has a trailing slash, rsync will copy only the contents of the source directory to the destination directory. When the trailing slash is omitted the source directory will be copied inside the destination directory4


  1. https://linuxize.com/post/how-to-transfer-files-with-rsync-over-ssh/)↩︎

  2. see 1 above.↩︎

  3. https://serverfault.com/questions/363555/why-is-rsync-skipping-the-main-directory)↩︎

  4. see 1 above.↩︎