Use rsync for File Synchronization in Linux

By Raman Kumar

Updated on Jul 31, 2024

In this tutorial, we'll learn about how to use rsync for file synchronization in Linux.

rsync is a powerful utility for efficiently transferring and synchronizing files across computer systems by checking the differences between source and destination files. rsync is a widely used utility for efficiently transferring and synchronizing files and directories between different locations, whether they are on the same machine, on different machines over a network, or between a local and remote system. 

Key Features:

Incremental Transfers: rsync transfers only the parts of files that have changed, which reduces the amount of data sent over the network and speeds up synchronization processes.

Versatility: It supports local-to-local, local-to-remote, and remote-to-remote file transfers. For remote transfers, rsync can use SSH or RSH as the transport protocol.

Compression: It can compress data during transfer using the -z option, reducing the amount of data sent over the network.

Preservation of File Attributes: rsync can preserve file permissions, ownership, timestamps, and symbolic links, ensuring that the synchronized files retain their original properties.

Exclusion and Inclusion Rules: It allows fine-grained control over which files or directories to include or exclude from synchronization.

Efficient Synchronization: By comparing file checksums and timestamps, rsync ensures that only changed files or portions of files are transferred, optimizing the synchronization process.

Support for Daemon Mode: rsync can operate as a server, allowing for file synchronization over a network without needing SSH.

It stands out due to its ability to minimize data transfer by only sending differences between source and destination files, making it particularly effective for backups and mirroring. This tutorial covers the installation, configuration, and usage of rsync on Linux.

How to Use rsync or File Synchronization

Step 1: Install rsync

Most Linux distributions come with rsync pre-installed. You can check if it's already installed by running:

rsync --version

If rsync is not installed, you can install it using the package manager for your distribution.

On Debian-based systems (Ubuntu, etc.):

sudo apt update
sudo apt install rsync

On Red Hat-based systems (CentOS, AlmaLinux, etc.):

sudo dnf install rsync

Step 2: Basic rsync Usage

The basic syntax of rsync is:

rsync [options] source destination

Here are some common options:

-a : Archive mode; equivalent to -rlptgoD (recursive, preserve symlinks, permissions, timestamps, group, and owner).
-v : Verbose; shows the progress of the transfer.
-z : Compress file data during the transfer.
-h : Human-readable output.
--delete : Delete extraneous files from the destination directory.

Example 1: Synchronize a Local Directory

To synchronize files from the source directory (/source/directory/) to the destination directory (/destination/directory/):

rsync -avh /source/directory/ /destination/directory/

Example 2: Synchronize Files Over SSH

To synchronize files between a local and a remote system, use rsync over SSH:

rsync -avh -e ssh /source/directory/ user@remote_host:/destination/directory/

Step 3: Advanced rsync Usage

Synchronize in Both Directions

For bidirectional synchronization, use a tool like rsync with scripts or combine it with unison.

Exclude Files and Directories

To exclude certain files or directories, use the --exclude option:

rsync -avh --exclude 'file_or_directory_to_exclude' /source/directory/ /destination/directory/

Running rsync as a Cron Job

To automate rsync using cron, edit the crontab file:

crontab -e

Add a cron job entry, for example, to run rsync every day at 2 AM:

0 2 * * * rsync -avh /source/directory/ /destination/directory/

Step 4: Configuring rsync Daemon

To set up rsync as a daemon, follow these steps:

Edit the rsyncd.conf file:

Create or edit the /etc/rsyncd.conf file:

sudo nano /etc/rsyncd.conf

Add the following configuration:

[backup]
    path = /path/to/directory
    comment = Backup Directory
    read only = no
    uid = nobody
    gid = nogroup
    auth users = user
    secrets file = /etc/rsyncd.secrets

Create the secrets file:

sudo nano /etc/rsyncd.secrets

Add the following:

user:password

Set the correct permissions for the secrets file:

sudo chmod 600 /etc/rsyncd.secrets

Start the rsync daemon:

sudo systemctl start rsync
sudo systemctl enable rsync

Connect to the rsync daemon:

rsync -avh /source/directory/ user@remote_host::backup

Conclusion

rsync is a versatile tool for file synchronization and transfer. By leveraging its various options and configurations, you can efficiently manage data between local and remote systems, automate backups, and ensure data consistency.

Checkout our dedicated servers and KVM VPS plans.