How to use rsync between hosts with non-standard ssh ports

In order to work between hosts with non-standard ssh ports, rsync syntax should be a little bit different.

The command below is pushing all the files from MyLocalProject on the local machine to the RemoteProject directory on the remote machine.

If you want to sync from the server to the localhost, just replace their places. Here is how we do it:

rsync -vaz -e "ssh -p2222" /MyLocalProject/ username@server:/RemoteProject/

If you want to mirror the destination, use this:

rsync -vaz --delete --exclude '.*' --exclude '*.bat' -e "ssh -p2222" /mnt/d/localDir/ user@remote.tld:/home/user/public_html/project/
  • --delete delete files that don’t exist on the sending side, here comes the mirroring
  • -z--compress: compress file data during the transfer
  • --exclude=PATTERN: exclude files matching PATTERN

For more information about the switches, check put the man file.