Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Real-time File Synchronization Using rsync and inotify for Continuous Backup

Tech May 18 2

SSH Key Authentication Setup

Generate SSH keys without password protection:

ssh-keygen

Press enter to accept all defaults.

Copy the public key to the target server:

ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.1.221

Enter the password for 192.168.1.221 when prompted.

Installing Inotify Tools

Verify kernel version meets minimum requirements (2.6.13 or higher):

uname -r

Add EPEL repository and install inotify tools:

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum -y install inotify-tools
rpm -qa inotify-tools

Automated Real-time Backup Script

Create the following script to enable continuous file synchronization:

#!/bin/bash
# Script Name: syncfiles.sh
# Created: 2020-1-13

# This script implements real-time backup using rsync over SSH with inotify monitoring

LOG_PATH="/var/log/sync_activity.log"
TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")

log_message() {
    local show_log=$1
    local message="$TIMESTAMP $2"
    
    if [ $show_log -ne 0 ]; then
        echo "$message"
        echo "$message" >> $LOG_PATH
    else
        echo "$message" >> $LOG_PATH
    fi  
}

verify_success() {
    local result=$?
    if [ ${result} -ne 0 ]; then
        log_message 1 "#[FAILURE] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
        log_message 1 "#[FAILURE] Error occurred: $1"
        log_message 1 "#[FAILURE] <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
        exit 1
    fi  
}

output_colored_text() {
    if [ $1 -eq 0 ]; then
        echo -e "\033[41;37m ${2} \033[0m"
        return 0
    fi

    if [ $1 -eq 1 ]; then
        echo -e "\033[43;37m ${2} \033[0m"
        return 0
    fi

    if [ $1 -eq 2 ]; then
        echo -e "\033[47;30m ${2} \033[0m"
        return 0
    fi
}

log_completion() {
    log_message 1 "<<<<<<<<<<<<<<<<<<<<END<<<<<<<<<<<<<<<<<<<<<<<<<<"
}

log_message 1 "<<<<<<<<<<<<<<<<<<<<Start<<<<<<<<<<<<<<<<<<<<<<<<<<"

# Configuration parameters
TARGET_SERVER=192.168.1.221
SOURCE_DIR=/root/aa_inotify
DESTINATION_DIR=/test_inotify

# Initial sync with delete option
echo "Starting initial synchronization..."
rsync -Rraz --delete -e ssh $SOURCE_DIR root@${TARGET_SERVER}:${DESTINATION_DIR}

# Monitor directory changes and sync in real-time
/usr/bin/inotifywait -mrq --format '%w%f' -e create,close_write,delete $SOURCE_DIR | while read filepath
do
    rsync -Rraz --delete -e ssh ${filepath} root@${TARGET_SERVER}:${DESTINATION_DIR}
done

log_completion

To enable automatic startup, add the script to /etc/rc.local:

# Add this line before the exit statement
/path/to/syncfiles.sh &

Ensure the script has execute permissions:

chmod +x /path/to/syncfiles.sh

The system will now manitain real-time synchronization between the source and destination directories, automatically propagating file changes as they occur.

Tags: rsync

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.