Transfer files with Rsync

What is Rsync?

Rsync stands for “remote synchronization”. It is a remote and local file synchronization tool that helps you efficiently transfer files.

Rsync is faster than tools like Secure Copy Protocol (SCP) as it uses the delta-transfer algorithm that minimizes the data transfer by copying only the sections of a file that have been changed.

Some of the extra features included with Rsync:

  • Pipelines file transfers to minimize latency costs
  • Supports copying links, devices, owners, groups, and permissions

With that said, you are able to transfer files from local  to  remote, or remote  to  local. Rsync does not support remote  to  remote file transfers.


Rsync in action

Now we know what Rsync is, let’s see how it works.

Rsync works similarly to other remote server management tools like SSH and SCP.

Basic syntax of Rsync.
rsync [options] source destination

Transfer a file from your local system to a remote server. It is also called a “push” operation.
rsync local_filepath user@remote-host:remote_filepath

Transfer a file from a remote server to your local system, also called a “pull” operation.
rsync user@destination-server:destination_filepath source_filepath

Keep in mind that Rsync uses SSH for remote file-transfers by nature. Make sure you have SSH (Secure Shell) enabled on the destination system.


Rsync lets you add additional options. Let’s look at a few useful options.

Transfer recursively

A recursive file transfer can be executed if you add the -r option. This is useful when working with folders. Here is an example:

rsync -r user@remote-host:remote_folder/ local_folder

Archive option

The archive option (-a) preserves special and device files, modification times, permissions from the source folder, and is also used to preserve symbolic links while transferring files.

The archive option also syncs files recursively, so it is used more than the recursive option. Here is how you use it:

rsync -a user@remote-host:remote_folder/ local_folder

Use Compression

You can also compress files using the -z option. Compressing files will reduce the network load and speed up the file transfer.

rsync -az user@remote-host:remote_folder/ local_folder

Show the progress

For large file transfers, it is useful to see or follow the progress of the operation. You can use the -P option to know the progress of the file transfer. As Rsync is a very robust file transfer tool by nature, you can also resume file transfers if they are interrupted.

rsync -aP user@remote-host:remote_folder/ local_folder

Verbose option

The verbose option (-v) can help you understand every step of the file transfer.

rsync -av user@remote-host:remote_folder/ local_folder

To get a list of all the options, use the help command
rsync --help

How to set Rsync speed limit for bandwidth control with –bwlimit option

Syntax:

rsync --bwlimit=KBPS source destination
rsync --bwlimit=KBPS [options] source destination

Examples:

You set I/O limit in 5000 KBytes per second:

Local transfer

rsync --bwlimit=5000 source_filepath destination_filepath

Local to remote transfer

rsync --bwlimit=5000 source_filepath user@destination-server:destination_filepath

Another option is to use trickle, It is a userspace bandwidth manager.

Syntax:

trickle -u uploadLimit program
trickle -d downloadLimit rsync
trickle -u {UPLOAD_LIMIT} -d {DOWNLOAD_LIMIT} program-binary

For example:

trickle -s -d 5000 -u 5000 rsync source_filepath destination_filepath
trickle -s -d 5000 -u 5000 rsync -avr source_filepath user@destination-server:destination_filepath

Conclusion

Rsync simplifies the whole file transfer process by offering a robust, versatile, and flexible tool compared to alternatives like SCP.

Rsync is great for maintenance operations, backups, and general file operations between local and remote machines.

5 2 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments