Technical Implementation of Real-Time Folder Synchronization Using inotifywait and rsync

Dec 05, 2025 · Programming · 14 views · 7.8

Keywords: folder synchronization | inotifywait | rsync

Abstract: This paper explores solutions for automatic folder synchronization in Ubuntu systems, focusing on the technical implementation combining inotifywait and rsync. It details methods for real-time monitoring of file system events, achieving one-way synchronization through while loops and rsync commands to ensure timely updates from source to target folders. The paper also discusses lsyncd as an alternative, providing complete script examples and configuration advice to help build reliable real-time backup systems.

Introduction

Folder synchronization is a common and critical requirement in data management and backup strategies, especially in scenarios demanding real-time backups where traditional manual or scheduled methods fall short. Based on the specific needs outlined in the Q&A data, this paper examines how to implement automatic one-way folder synchronization on Ubuntu 12.04, ensuring that any modifications to the source folder (folder A) are promptly reflected in the target folder (folder B).

Technical Background and Requirements Analysis

The core user requirement is to establish a one-way synchronization mechanism from the source to the target folder, where any create, modify, or delete operations on the source are automatically synced to the target, without allowing changes in the target to affect the source. This design aims to provide a reliable local backup layer for cloud synchronization services like Ubuntu One, preventing data loss due to service failures.

Core Solution: Combining inotifywait and rsync

According to the best answer (Answer 1), using a combination of inotifywait and rsync is recommended for real-time synchronization. inotifywait is a command-line tool leveraging the Linux kernel's inotify mechanism to monitor file system events such as modify, create, delete, and move. By installing the inotify-tools package (via sudo apt-get install inotify-tools), users can detect changes in the source folder in real-time.

The basic implementation script is as follows:

while inotifywait -r -e modify,create,delete,move /directory; do
    rsync -avz /directory /target
done

In this script, a while loop runs continuously, with inotifywait monitoring events in /directory (the source folder), including modify, create, delete, and move. Upon detecting an event, inotifywait exits, and the rsync command in the loop body executes, synchronizing the source folder to /target (the target folder). The rsync options -avz ensure archive mode, verbose output, and compressed transfer, enhancing synchronization efficiency.

In-Depth Analysis and Optimization

To ensure reliability and performance, consider the following optimizations: First, use the --exclude option to exclude unnecessary files or directories, such as temporary files. Second, add error handling mechanisms, such as logging or sending notifications if rsync fails. Additionally, for large folders, adjust the monitoring depth of inotifywait or use the --timeout option to avoid excessive resource consumption.

Example optimized script:

#!/bin/bash
SOURCE_DIR="/path/to/folderA"
TARGET_DIR="/path/to/folderB"
LOG_FILE="/var/log/sync.log"

while true; do
    inotifywait -r -e modify,create,delete,move "$SOURCE_DIR" 2>&1 | tee -a "$LOG_FILE"
    if rsync -avz --exclude='*.tmp' "$SOURCE_DIR/" "$TARGET_DIR/" 2>&1 | tee -a "$LOG_FILE"; then
        echo "$(date): Sync successful" | tee -a "$LOG_FILE"
    else
        echo "$(date): Sync failed" | tee -a "$LOG_FILE"
    fi
done

Alternative Solution: lsyncd Tool

As supplementary reference, Answer 2 mentions the lsyncd tool, which combines inotify and rsync by aggregating events and processing them in batches for improved efficiency. lsyncd is available in the Ubuntu repository and can be installed via sudo apt-get install lsyncd. A configuration example:

settings {
    logfile = "/var/log/lsyncd.log",
    statusFile = "/var/log/lsyncd-status.log"
}

sync {
    default.rsync,
    source = "/path/to/folderA",
    target = "/path/to/folderB",
    rsync = {
        archive = true,
        compress = true
    }
}

lsyncd is suitable for scenarios requiring more complex event handling or remote synchronization but may add system complexity. In contrast, scripts based on inotifywait are lighter and more customizable.

Application Scenarios and Best Practices

This solution applies to real-time backups, development environment synchronization, or data mirroring. During implementation, it is advisable to: 1. Test the synchronization script to ensure accuracy; 2. Use system services like systemd to manage automatic startup; 3. Regularly check log files to monitor synchronization status; 4. Integrate version control systems for enhanced data security.

Conclusion

By combining inotifywait and rsync, efficient and reliable real-time one-way folder synchronization can be achieved. This method not only meets user demands for real-time backups but also offers flexibility and scalability. For more advanced needs, tools like lsyncd serve as viable alternatives. In practical deployment, parameters should be adjusted based on specific environments, and monitoring and maintenance strategies implemented to ensure continuous and stable data synchronization.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.