Synchronizing Files Between Two Linux Servers Using Lsyncd

Synchronizing Files Between Two Linux Servers Using Lsyncd

Maintaining real-time file synchronization between servers is a critical requirement for high availability, backup strategies, and distributed workloads. While traditional tools like cron and rsync are excellent for scheduled transfers, they lack the ability to sync files instantly as changes occur. This is where Lsyncd (Live Syncing Daemon) shines. By monitoring local directories for changes using inotify and aggregating them for a brief period before spawning rsync, Lsyncd provides an efficient, real-time synchronization solution. In this guide, we will walk you through setting up Lsyncd to synchronize directory contents between a master and slave Linux server.

Server Preparation

Before configuring the synchronization daemon, we need to prepare the servers by installing the necessary packages. Ensure you have root or sudo access on both machines.

  1. Enable EPEL Release:

    yum -y install epel-release
    

    This command enables the Extra Packages for Enterprise Linux (EPEL) repository, which provides additional software packages not included in the default CentOS/RHEL repositories.

  2. Install Lsyncd:

    yum -y install lsyncd
    

    This command installs the Lsyncd package on both servers to ensure consistency across your environments.

Configure SSH Key-based Authentication

Lsyncd uses SSH to securely transfer files to the remote target. To automate this process without requiring manual password prompts, we must establish key-based authentication between the master and slave servers.

  • Generate SSH Key on Master Server:

    ssh-keygen -t rsa
    

    Accept the default file locations and press Enter to leave the passphrase blank for seamless background automation.

  • Copy Public Key to Slave Server:

    ssh-copy-id root@<slave_server_ip>
    

    Replace <slave_server_ip> with the actual IP address of the slave server. You will be prompted to enter the slave server’s root password once.

  • Configure SSH Config (Optional):
    Create a configuration file in ~/.ssh/config on the master server for easier connection pooling and host identification:

    Host dest_host
    Hostname <slave_server_ip>
    User root
    IdentityFile ~/.ssh/id_rsa
    

Configure Lsyncd on Master Server

Now that the connection between the servers is secured, we can configure our synchronization parameters on the master server.

  • Create Configuration File:

    nano /etc/lsyncd.conf
    

    Add the following configuration to the file:

    settings {
        logfile = "/var/log/lsyncd/lsyncd.log",
        statusFile = "/var/log/lsyncd/lsyncd.status",
        statusInterval = 1,
    }
    
    sync {
        default.rsync,
        delay = 1,
        source = "/shared",
        target = "root@<slave_server_ip>:/shared",
        rsync = {
            archive = true,
            compress = true,
            update = true
        }
    }
    

    Replace <slave_server_ip> with the IP address of the slave server. Make sure to adjust the source and target paths to match the directories you wish to sync.

Start and Enable Lsyncd Service

With the configuration file saved, you can now start the Lsyncd service and configure it to run automatically on system boot.

  • Start Lsyncd:

    systemctl start lsyncd
    
  • Check Lsyncd Status:

    systemctl status lsyncd
    
  • Enable Lsyncd on Boot:

    systemctl enable lsyncd
    

Monitoring Lsyncd Status

To verify that files are syncing correctly and to troubleshoot any connection issues, you can monitor Lsyncd’s active status report.

  • Check Lsyncd Status Log:

    tail /var/log/lsyncd/lsyncd.status
    

Explanation of Configuration Parameters

To help you customize your replication environment, here is an explanation of the directives used in the /etc/lsyncd.conf directory block:

  • settings: Defines general performance and administrative logging behaviors.

    • logfile: Tracks generic operational messages and errors.
    • statusFile: Keeps a live-updated status report of synchronization queues.
    • statusInterval: Defines how frequently (in seconds) the status file is rewritten.
  • sync: Establishes the parameters for a specific synchronization path.

    • default.rsync: Utilizes default, optimized local-to-remote rsync actions.
    • delay: Buffer window (in seconds) that Lsyncd waits for continuous writes to finish before triggering a sync.
    • source: The path to the directory on the local master server to be monitored.
    • target: The destination server ssh-string and absolute directory path.
    • rsync: Explicit flags translated directly to the backend rsync engine.
      • archive: Keeps file owners, permissions, groups, symlinks, and timestamps preserved.
      • compress: Minimizes bandwidth usage by compressing data streams in transit.
      • update: Skips files on the target that are already up-to-date or newer than files on the source.

By leveraging Lsyncd alongside SSH key-based authentication, you can run an automated, secure, and lightweight file replication system across your cloud architecture. This setup ensures that your backup directories, static assets, and user uploads are consistently up to date across your compute nodes with minimal administrative overhead.

Leave a Reply

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