Using rsync command in Linux to transfer or synchronize files locally and remotely.


  1. Linux machine


Step 1: Installing Rsync

Install rsync by using the following commands.

For Debian/Ubuntu Linux Distributions

sudo apt-get install -y rsync

For Fedora/CentOS Linux Distributions

sudo yum install -y rsync

Step 2: Rsync Command Syntax

In short, rsync can be used to synchronize files either local-to-local or local-to-remote. Listed below are the basic syntax of this command.

Local-to-Local:  rsync [option]... [src]... [dest]
Local-to-Remote: rsync [option]... [src]... [user@host:dest]
Remote-to-Local: rsync [option]... [user@]host:src]... [dest]

option : rsync options
src : source directory
dest : destination directory
user : remote username
host : remote hostname or IP address

rsync options
-a or –archive, specifies rsync to sync directories while also transferring special and block devices, preserving symbolic links, modification times, groups, ownership, and permissions.
-z or –compress, forces rsync to compress the data that is sent to the destination machine.
-P or –partial-progress, shows progress bar and tells rsync to keep partially transferred files.
–delete, deletes irrelevant files in the destination location. Usually used for mirroring purposes.
-q or –quiet, silences non-error messages.
-e, allows rsync to use a different shell, by default rsync uses ssh.

Step 3: Rsync Command Usage (Local-to-Local)

The most basic usage of rsync is copying a file to another location, as shown in the example below.

rsync -a /opt/myfile.txt /home/

Next, you can also change the file name while performing the copying operation, simply by specifying the new file name in the directory section, as shown below.

rsync -a /opt/myfile.txt /home/myfile_newname.txt

Finally, you can synchronize an entire directory (i.e. to do backup) and copy it to another directory by using the following command.

rsync -a /var/www/html/mywebsite/ /var/www/html/mywebsite_backup/

Step 4: Rsync Command Usage (Local-to-Remote)

To use rsync for synchronizing data between a local and remote machine, both machines must have rsync installed. By default, SSH is used as the remote shell for file transfer.

The usage of rsync to transfer data from local to remote is similar to that of local-to-local operations, but with the addition of the remote user and IP address.

To transfer data from a local machine to remote machine, use the following command.

rsync -a /home/user/myfolder/ [remote_user]@[remote_host_or_ip]:/opt/myfolder 

To transfer data from a remote machine to a local machine, use the following command.

rsync -a [remote_user]@[remote_host_or_ip]:/opt/myfolder /home/user/myfolder/

Step 5: Set Rsync Bandwidth Speed Limit

Sometimes, the rsync utility would use up all the bandwidth and I/O capabilities of the machine, causing it to slow down significantly. To prevent this, an extra step is needed when formulating the command.

To set rsync transfer speed limit, include the –bwlimit flag in your command.
This flag specifies the maximum transfer speed in kilobytes, and its usage is shown in the following examples.

The following example sets a local rsync operation to have a transfer speed limit of 5,000 Kbps.

rsync -a --bwlimit=5000 /var/www/html/mywebsite/ /var/www/html/mywebsite_backup/

The following example sets a local rsync operation to have a transfer speed limit of 8,000 Kbps.

rsync -a --bwlimit=8000 /home/user/myfolder/ [remote_user]@[remote_host_or_ip]:/opt/myfolder 

Step 6: Using Rsync on a Non-Standard SSH Port

By default, rsync uses SSH for its remote shell, which is on port 22. However, most instances tend to modify their SSH port number due to security concerns. You can specify the remote shell using the -e flag, as shown in the syntax below.

rsync -a -e 'ssh -p [port-number]' /home/user/myfolder/ [remote_user]@[remote_host_or_ip]:/opt/myfolder

For example, if the SSH port is changed to 2211, then the command would look like the following.

rsync -a -e 'ssh -p 2211' /home/user/myfolder/ [remote_user]@[remote_host_or_ip]:/opt/myfolder