How to use rsync for Remote and Local Data Synchronization and Transfer in a Linux System

How to use rsync for Remote and Local Data Synchronization and Transfer in a Linux System

Introduction

Rsync is a lightweight yet powerful utility used to synchronize directories and folders in a Linux system. It’s a fast and flexible application written as a replacement for SCP and RCP utilities.

The utility allows webmasters to upload files to a remote server, and is essential for reducing data transfer when maintaining copies of local files on the remote server.

Rsync facilitates the transfer of the difference between the local files and the remote destination to provide an expedient, incremental file transfer. This tutorial will help you transfer files and folders using the Rsync utility.

Before you start, spin up a VPS or dedicated server at good Linux hosting service.

Ready? Let’s go!

Step 1 –
Installing The Rsync Utility

Rsync is a readily available utility that is preinstalled on almost every Linux system. To confirm the version of Rsync deployed on your Linux system, run the command below:

$ sudo rsync --version

This will give you an output similar to the one below:

rsync  version 3.1.2  protocolversion 31

In case, this utility is not deployed on your Linux system, run the command below to install it:

For Debian and Ubuntu:

$ sudo apt install rsync

For CentOS systems:

$ sudo yum install rsync

That is it! By running the commands Rsync will be installed automatically on your Linux system.

Step 2 –
The Command Syntax

The Rsync Comand has the following structure:

LocaltoLocal:  rsync [OPTION]... SRC [SRC]... DEST
LocaltoRemote: rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
RemotetoLocal: rsync [OPTION]... [USER@]HOST:SRC [DEST]

Let’s review this syntax to help you understand the entire Rsync command:

  • The OPTION shows the available Rysnc options
  • SRC is the original directory
  • DEST is the destination folder
  • USER represents the remote username
  • HOST represents the remote host

Now, the Rsync utility provides multiple options that manage its behavior. Here, we’ll discuss the most commonly used Rsync options, which include:

  • -a, –archive, this mode is the same as  -rlptgoD and is commonly used to sync directories recursively, preserve symlinks, group, modification tiles, privileges, and ownership.
  • -p, –perms is an option used to preserve privileges
  • -z, –compress is an option used to compress data and files during the transfer process. It’s highly effective when your connection to a remote server is slow.
  • –delete, this option is used to expunge the extraneous files and data from the destination directory.
  • -q, –quiet is a command used to suppress a non-error message.
  • -e, –rsh=COMMAND, is an option used to stipulate the remote shell to be used for file and data transfer.
  •  -T, –temp-dir=DIR is an option used to generate new temporary files in a specific directory.
  • –progress is a helpful option that displays the progress during file transfer.

Step 3 –
Using The Rsync Utility

Basic usage

To transfer one file from a local location to another, issue the command:

$ sudo rsync -a /opt/filename.zip /tmp/

If you omit the name of the file from your destination directory, Rsync will be forced to copy the file featuring the currently set name. To save a file under its unique name, you should specify its new name. For instance:

$ sudo rsync -a /opt/filename.zip /tmp/newfilename.zip

Next, to create a backup for your website files on your a machine, run the command below:

$ sudo rsync -a /var/www/domain.com/public_html/ /var/www/domain.com/public_html_backup/

If the target location is not available, then, Rsysnc utility will generate it automatically.

Note: Rsync handles an original source directory featuring the trailing slash (/). When the slash is added to the source/original directory, the rysnc will transfer only the content of this directory to your destination folder. On the other hand, when the slash is eliminated, rysnc copies the source file inside your destination folder.

Synchronizing Data To/from A Remote System

As aforementioned, Rysnc can be used to synchronize data to or from your remote server. To accomplish this, you must install this utility on both your source machine and destination system.

Note: The modern versions of the Rsync utility are designed to utilize the Secure Shell (SSH) as the preselected remote shell.

To transfer a single directory from your local machine to the remote system, run the command below:

$ sudo rsync -a /opt/media/ remote_user@remote_host_or_ip:/opt/media/

Next, to move data from one remote machine to your local machine, run the command below:

$ sudo rsync -a remote_user@remote_host_or_ip:/opt/media/ /opt/media/

If the Secure Shell (SSH) implemented on your remote system is only listening on another port and not port 22, you should add the -e flag in the command to specify the new port:

$ sudo rsync -a -e "ssh -p 2322" /opt/media/ remote_user@remote_host_or_ip:/opt/media/

Note: if you intend to transfer large files or amount of data, utilize the -P option or execute all rysnc commands in a different screen session.

Excluding Directories And Files

To flawlessly exclude a file or a directory, you must utilize the relative path for each. Rysnc provides two options that can be used to exclude directories and files:

  • The first is the –exclude option which is used to specify the directories and files to be excluded on a command line. For example,
$ sudo rsync -a --exclude=node_modules --exclude=tmp /src_directory//dst_directory/

In the command above, you will exclude the tmp and node_modules directories which are hosted in the src_directory.

  • The –exclude-from is the alternative used to specify directories and files to be excluded in a specific file. For example:
$ sudo rsync -a --exclude-from='/exclude-file.txt'/src_directory//dst_directory/

In the above command, you will exclude tmp and node_modules from /exclude-file.txt.

Conclusion

This tutorial has shown you how to transfer and synchronize directories and files using the Rsync utility. Visit the official Rysnc User’s manual website page to learn more about this utility.

Check out these top 3 VPS services:

Kamatera
$4.00 /mo
Starting price
Visit Kamatera
Rating based on expert review
  • User Friendly
    3.5
  • Support
    3.0
  • Features
    3.9
  • Reliability
    4.0
  • Pricing
    4.3
Hostinger
$2.99 /mo
Starting price
Visit Hostinger
Rating based on expert review
  • User Friendly
    4.7
  • Support
    4.7
  • Features
    4.8
  • Reliability
    4.8
  • Pricing
    4.7
Webdock
$1.05 /mo
Starting price
Visit Webdock
Rating based on expert review
  • User Friendly
    3.8
  • Support
    4.5
  • Features
    4.5
  • Reliability
    4.3
  • Pricing
    4.3

How to Install cPanel on a CentOS 7 VPS or Dedicated Server

This article provides step by step instructions on how to install cPanel on a Vi
less than a minute
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How to Install a Let’s Encrypt Certificate on your Ubuntu 18.04 Dedicated Server or VPS

If you are hosting your website on a VPS server running Ubuntu 18.04, we will sh
less than a minute
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester

How to Install Own Cloud On an an Ubuntu 18.04 Dedicated Server or VPS

You can create your own self-hosted cloud storage services by installing the ope
less than a minute
Idan Cohen
Idan Cohen
Marketing Expert

How to Install and Configure Linux Malware Detect on CentOS 7

This tutorial will help you install and configure Linux Malware Detect (LMD) on
less than a minute
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top