rsync - How to transfer or synchronise files between servers over the Internet

What is rsync?

rsync is a software tool for efficiently transferring data over a network or synchronising files between two locations. Rsync compares what is already the same between locations (to avoid needing to transfer data unnecessarily) and uses compression and other techniques to minimise the amount of resources needed to transfer files between network locations.

How do I install rsync?

Usually Rsync is installed as standard on Linux. If it is not you can install it using:

On Ubuntu / Debian based distributions:

sudo apt install rsync

On Red Hat / Centos based distributions:

sudo yum install rsync

Practical Rsync Examples

Below are a couple of practical examples that are common ways of using rsync.

'Push' (Transfer files to a remote server from the one you are logged into):

Preparation:

For the following examples you need to log into a machine via SSH. If you are unfamiliar with this process you can see our guide on how to log in via SSH. It is also worth being reasonably familiar with the Linux command line too.

In this example we have logged into a machine via SSH (this is the machine on the left) and we are ‘pushing’ the folder ‘htdocs’ complete with all the files and folders contained within it to another server (the one on the right in the animation above).

First we confirm a directory where we are going to transfer the files and folders to actually exists. On the right (in this case receiving server) we check the directory we are in with the command:

pwd

This seems fine to use as a receiving directory for this demonstration, you will want to change this to a directory on your own (receiving) machine based on your own needs.

Next we find the folder we are transferring and confirm its path with the command:

realpath htdocs

So we now know the full (absolute) path of the folder we are transferring is /home/ub1/htdocs and the full (absolute) path of the folder we are transferring to is /home/ub1. We will take a note of these for use in our rsync command.

The actual command (push):

rsync -vhzaP /home/ub1/htdocs [email protected]:/home/ub2

Breakdown of this command:

'Pull' (Transfer files from remote server to the one you are logged into):

In this example we have logged into a machine via SSH (this is the machine on the right) and we are ‘pulling’ the folder ‘htdocs’ complete with all the files and folders contained within it from another server (the one on the left in the animation below).

The command is very similar to the Push command previously, really the only difference is that the locations are reversed in order.

The actual command (pull):

rsync -vhzaP [email protected]:/home/ub1/htdocs /home/ub2