Use Rsync + Lsyncd For File Synchronization on Linux

File synchronization ensures that a user can create a backup for files in another location either on the local computer, remote desktop device, or on the cloud. Any changes done to the file are reconciled with the copy automatically thus reflecting the changes. A good example is how Dropbox or OneDrive syncs data between your PC and its servers. This includes files and folder permissions, renaming, adding, and deleting. This also means you can access the files from anywhere even when offline thus ideal for mobile workers.

Other than creating copies for storage and backup, file synchronization also helps in sharing files with different users. This improves collaboration as different users can view changes in real-time and work without any of the team members being left behind on any updates. File synchronization works with different means, you can use software, or command utilities (in the case of Linux) to set up file syncing. Several software solutions are available to aid in this, they include, Syncthing, DirSync Pro, FreeFileSync, GoodSync, Resilio Sync, and Raysync. Command utilities that can help set up File synchronization in Linux include; Rsync, Lsync, and FTPsync.

1. Rsync

Rsync is a pretty popular command line utility used in file synchronization. It is an open-source utility that provides fast incremental file transfer locally and remotely. It has a large number of options that control its behavior and allow flexible specifications of the set of files to be copied. It uses the delta-transfer algorithm, which reduces the amount of data sent over the network by sending only the updates between the source files and the existing files in the destination. It also checks if files exist in the destination before sending them. Rsync is specially used for backups and mirroring and as an improved copy command utility.

There are two ways in which Rsync uses to contact a remote system;

  • Remote-shell programs such as SSH. This has a single (:) colon as a separator.
  • Contacting a Rsync daemon directly via TCP port 873. This uses a double (::) colon as a separator.

Another algorithm that Rsyync uses referred to as ‘quick-check’, looks for files that have changed in size or modification time. This determines whether or not it needs to transfer a new copy, which possibly saves significant time and bandwidth. It copies data either to/from a remote host or locally on the current host but not between two remote hosts.

More features incorporated with Rsync include;

  • Supports copying links, devices, owners, groups, and permissions.
  • Exclude and exclude-from options similar to GNU tar.
  • A CVS exclude mode for ignoring the same files that CVS would ignore.
  • It does not require super-user privileges.
  • Pipelining of file transfers to minimize latency costs.
  • Supports anonymous or authenticated rsync daemons which is ideal for mirroring.

Rsync refers to the remote side as the server and to the local side as the client. It comes packaged with most Linux distributions by default so it should be easy to get started.

In this guide, I will show you various ways of using Rsync to sync files locally.

Install Rsync on Linux Systems

To install Rsync, run the following command based on your distribution.

sudo yum install rsync               #[RHEL,CentOS,Fedora]
sudo apt-get install rsync           #[Ubuntu,Debian,LinuxMint]
sudo pacman -S rsync               #[Arch Linux]
sudo zypper install rsync            #[openSUSE]

Verify the installed version

$ rsync -V
rsync  version 3.2.7  protocol version 31
Copyright (C) 1996-2022 by Andrew Tridgell, Wayne Davison, and others.
Web site: https://rsync.samba.org/
Capabilities:
    64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
    socketpairs, symlinks, symtimes, hardlinks, hardlink-specials,
    hardlink-symlinks, IPv6, atimes, batchfiles, inplace, append, ACLs,
    xattrs, optional secluded-args, iconv, prealloc, stop-at, no crtimes
Optimizations:
    SIMD-roll, no asm-roll, openssl-crypto, no asm-MD5
Checksum list:
    xxh128 xxh3 xxh64 (xxhash) md5 md4 sha1 none
Compress list:
    zstd lz4 zlibx zlib none
Daemon auth list:
    sha512 sha256 sha1 md5 md4

rsync comes with ABSOLUTELY NO WARRANTY.  This is free software, and you
are welcome to redistribute it under certain conditions.  See the GNU
General Public Licence for details.

Use Rsync on Linux

To use Rsync, you must specify a source and a destination, one of which may be remote. It is invoked using different parameters. The options available in rsync include;

  • -r Recurse through directories (as opposed to only working on files in the current directory)
  • -a archive mode
  • -l Copy symlinks as new symlinks
  • -p Preserve permissions
  • -V Print the rsync version plus other info and exit.
  • -t Preserve modification times
  • -g Preserve group ownership
  • -o Preserve user ownership (which is restricted to only superusers when dealing with other user’s files)
  • -D Copy device files
  • -v Show overall progress.
  • -d Transfer directories without recursing
  • -h show help
  • -delete delete extraneous files from destination directories.

1) Copy/Sync a File on a Local Computer

To transfer a single file from one location to another, Use the following command syntax.

$ rsync -zvh ~/Documents/file1.txt /tmp/
file1.txt

sent 80 bytes  received 35 bytes  230.00 bytes/sec
total size is 0  speedup is 0.00

To transfer multiple files from one directory to another: The following example would transfer all files from the home directory to the /tmp/ directory.

$ rsync -avz ~/Documents /tmp
sending incremental file list
rsync: [generator] chgrp "/tmp/." failed: Operation not permitted (1)
./
file1.txt
file10.txt
file2.txt
file3.txt
file4.txt
file5.txt
file6.txt
file7.txt
file8.txt
file9.txt

sent 684 bytes  received 221 bytes  1,810.00 bytes/sec
total size is 0  speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1333) [sender=3.2.3]

To copy a file with a different name

$ rsync -ai ~/Documents/file1.txt /tmp/sent.txt
>f+++++++++ file1.txt

2) Copy/Sync a Directory on a Local Computer

To copy a whole directory:

$ rsync -avzh ~/Documents ~/NewFiles
sending incremental file list
created directory /home/techviewleo/NewFiles
Documents/
Documents/file1.txt
Documents/file10.txt
Documents/file2.txt
Documents/file3.txt
Documents/file4.txt
Documents/file5.txt
Documents/file6.txt
Documents/file7.txt
Documents/file8.txt
Documents/file9.txt
Documents/AnyDesk/
Documents/NoMachine/

sent 702 bytes  received 270 bytes  1.94K bytes/sec
total size is 0  speedup is 0.00

You can also copy a directory with a different name but either make sure the destination directory exists or specify the destination path with a trailing slash:

$ rsync -ai ~/Documents ~/tmp/
cd+++++++++ Documents/
>f+++++++++ Documents/file1.txt
>f+++++++++ Documents/file10.txt
>f+++++++++ Documents/file2.txt
>f+++++++++ Documents/file3.txt
>f+++++++++ Documents/file4.txt
>f+++++++++ Documents/file5.txt
>f+++++++++ Documents/file6.txt
>f+++++++++ Documents/file7.txt
>f+++++++++ Documents/file8.txt
>f+++++++++ Documents/file9.txt

3) Set the Max Size of Files to be Transferred

This avoids transferring any file that is larger than the specified SIZE

rsync -avzhe ssh --max-size='200k' ~/Documents/ /tmp/

4) –include and –exclude Options

These options specify an exclude/include rule and do not allow the full rule-parsing syntax of normal filter rules.

rsync -avze  --include 'R*' --exclude '*' ~/Documents/ /tmp/

5) –delete option

This tells rsync to delete extraneous files from the receiving side but for synced directories only.

rsync -avz --delete file3.txt ~/Documents /tmp

2. Lsyncd

Lsyncd short for Live Syncing Daemon, is a rsync-based daemon used to synchronize directory trees continuously. Lsyncd watches local directory trees through an event monitor interface (inotify, fsevents) and continuously monitors the specified directory for changes every few seconds. It is a lightweight live mirror solution that is easy to install and its default synchronization method is rsync. It is useful in syncing data from a secure area to a not-so-secure area.

The options of Lsyncd include;

  • -delay (secs) Overrides the default delay times.
  • -help Show a help message.
  • -insist Continues startup even if a startup rsync cannot connect.
  • -log LEVEL Controls which kind of events are logged.
  • -log Category Turns on a specific debug message. E.g. -log Exec will log all processes as they are spawned.
  • -nodaemon Lsyncd will not detach from the invoker and log as well to stdout/err.
  • -pidfile FILE Lsyncd will write its process ID in FILE.
  • -runner FILE Makes the Lsyncd core load the part of Lsyncd written in Lua from FILE.
  • -version Writes version information and exits.

In this guide, I will show you how to Lsyncd to synchronize local directories

1) Install Lsyncd

The Lsyncd package is available in most Linux operating systems. Install it with the following command.

#[Ubuntu,Debian,LinuxMint]
sudo apt update
sudo apt install lsyncd -y           

#[RHEL,CentOS,Fedora]
sudo yum install epel-release
sudo yum install lsyncd                   

#[openSUSE]
sudo zypper install lsyncd

For Arch Linux, the package is available in the AUR repository.

Verify the installed version

$ lsyncd --version
Version: 2.2.3

2) Configure Lsyncd

We are going to configure a local syncing with lsyncd.

Create the source directory containing the files.

mkdir -p $HOME/source

Then populate the directory with files using the touch command.

touch $HOME/source/File{1..10}0

Create the target directory:

mkdir -p $HOME/target

Next, create the log files:

sudo mkdir -p /var/log/lsyncd
sudo touch /var/log/lsyncd/lsyncd.{log,status}

Create a directory for Lsyncd:

sudo mkdir -p /etc/lsyncd

Now create a configuration file.

sudo nano /etc/lsyncd/lsyncd.conf.lua

Put the following code. Change the source and target directories to their absolute path.

settings  {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status",
 statusInterval = 20,
 nodaemon   = false,
}

sync {
default.rsync,
source = "/home/cloudspinx/source/",
target = "/home/cloudspinx/target",
delay = 10,
}

Save and exit the file then Start and enable Lsyncd.

sudo systemctl start lsyncd
sudo systemctl enable lsyncd

Chcek the status of the service.

You can check the Lsyncd log for more details.

$ tail -f /var/log/lsyncd/lsyncd.log
Fri Jan 24 22:05:55 2025 Normal: --- Startup, daemonizing ---
Fri Jan 24 22:05:55 2025 Normal: recursive startup rsync: /home/cloudspinx/source/ -> /home/cloudspinx/target/
Fri Jan 24 22:05:55 2025 Normal: Startup of /home/cloudspinx/source/ -> /home/cloudspinx/target/ finished.

Or check the syncing status with the following command.

$ tail -f /var/log/lsyncd/lsyncd.status
Lsyncd status report at Fri Jan 24 22:06:15 2025

Sync1 source=/home/cloudspinx/source/
There are 0 delays
Filtering:
  nothing.


Inotify watching 1 directories
  1: /home/cloudspinx/source/

Use Rsync with Lsyncd

You can also use rsync with lsyncd to copy directories using the following command. This process works in the background.

$ lsyncd -rsync ~/source/ ~/target/
22:10:42 Normal: --- Startup, daemonizing ---

To see the process working at the front, use the following command

$ lsyncd -nodaemon -rsync ~/source/ ~/target/
22:11:01 Normal: --- Startup ---
22:11:01 Normal: recursive startup rsync: /home/cloudspinx/source/ -> /home/cloudspinx/target/
22:11:01 Normal: Startup of /home/cloudspinx/source/ -> /home/cloudspinx/target/ finished.

Exit the process with Ctrl + C once you see it is finished.

Wrap Up

Using Rsync and Lsyncd provides an economical and efficient way to synchronize your files and directories. Other than synchronizing files locally, it can also be done remotely by using SSH. Rsync provides several options that can be used for synchronizing files while Lsyncd provides a number of parameters that can be customized to fit your needs. Rsync and Lsyncd do not hamper local file system performance.

More articles:

Your IT Journey Starts Here!

Ready to level up your IT skills? Our new eLearning platform is coming soon to help you master the latest technologies.

Be the first to know when we launch! Join our waitlist now.

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Post

Debian 12(Bookworm) is the latest stable release of Debian that comes with a lot more software than its predecessor bullseye. […]

Virtualization allows for a better and efficient compute resources utilization. One of the most used open source virtualization solutions available […]

In this article we show you how to create a private and virtual network bridge on Proxmox with NAT configured […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.