Operations 9 min read

Mastering rsync: Efficient Data Backup and Synchronization on Linux

This guide explains what rsync is, compares it with SCP, shows how to install and configure it on CentOS, demonstrates push and pull synchronization using system and non‑system users, and provides scripts for automated scheduled backups.

Open Source Linux
Open Source Linux
Open Source Linux
Mastering rsync: Efficient Data Backup and Synchronization on Linux

1. rsync Introduction

rsync (remote synchronize) is an open‑source tool for remote data backup and synchronization. Unlike SCP, which cannot efficiently handle large data sets, rsync copies, computes checksums, and compares files on the fly, transferring only changed files after the initial full copy.

It supports two synchronization modes: push (the rsync server pushes data to clients) and pull (clients pull data from the server). The default listening port is 873.

# grep rsync /etc/services
rsync           873/tcp   # rsync
rsync           873/udp   # rsync

2. rsync Installation

On a CentOS 7.7 host, install the required packages: # yum -y install xinetd rsync Create a dedicated user for rsync and set its password:

# useradd rsync_user
# echo 123 | passwd --stdin rsync_user

Create a shared directory and a test file, then grant the rsync_user read/write/execute permissions using ACL:

# mkdir /rsync
# touch /rsync/test.txt
# setfacl -R -m user:rsync_user:rwx /rsync

3. Using rsync for Data Transfer

Two main methods are demonstrated:

System‑user based transfer (both push and pull).

Non‑system‑user transfer using a daemon configuration.

3.1 System‑user push (server to client)

# rsync -avz /rsync/ [email protected]:/shares

After execution, verify on the client:

# ll /shares/
-rw-rwxr--. 1 rsync_user rsync_user 0 May 12 10:10 test.txt

3.2 System‑user pull (client to server)

# rsync -avz [email protected]:/rsync /shares

Check the synchronized files on the client:

# ls /shares/rsync/
test.txt

3.3 Non‑system‑user synchronization (daemon mode)

Create and edit /etc/rsyncd.conf to define a module and authentication:

# vim /etc/rsyncd.conf
uid = root
gid = root
address = 192.168.1.252
port = 873
hosts allow = 192.168.1.0/24
use chroot = yes
max connections = 5
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
motd file = /etc/rsyncd.motd
[image01]
    path = /mysql-bak
    comment = used for mysql-data
    read only = false
    list = yes
    auth users = rsyncuser
    secrets file = /etc/rsync.passwd

Start the services:

# systemctl start xinetd && systemctl enable xinetd
# systemctl start rsyncd && systemctl enable rsyncd
# rsync --daemon --config=/etc/rsyncd.conf

Create the shared directory and test files:

# mkdir /mysql-bak
# touch /mysql-bak/test{1..5}.txt

Pull data from the server using the daemon module:

# rsync -avz [email protected]::image01 /tmp

If a firewall blocks the connection, stop and disable firewalld:

# systemctl stop firewalld && systemctl disable firewalld

Create the password file required by the daemon:

# echo "rsyncuser:123123" >> /etc/rsync.passwd
# chmod 600 /etc/rsync.passwd

3.4 Automating backups with cron

Write a simple shell script to run the rsync command and schedule it via crontab:

#!/bin/bash
rsync -avz [email protected]::image01 /tmp --password-file=/etc/rsync.mypasswd
# chmod 755 /root/bak.sh
# echo "0 2 * * * sh /root/bak.sh" >> /etc/crontab
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linuxcrondata backupfile synchronizationxinetd
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.