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.
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 # rsync2. 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_userCreate 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 /rsync3. 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]:/sharesAfter execution, verify on the client:
# ll /shares/
-rw-rwxr--. 1 rsync_user rsync_user 0 May 12 10:10 test.txt3.2 System‑user pull (client to server)
# rsync -avz [email protected]:/rsync /sharesCheck the synchronized files on the client:
# ls /shares/rsync/
test.txt3.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.passwdStart the services:
# systemctl start xinetd && systemctl enable xinetd
# systemctl start rsyncd && systemctl enable rsyncd
# rsync --daemon --config=/etc/rsyncd.confCreate the shared directory and test files:
# mkdir /mysql-bak
# touch /mysql-bak/test{1..5}.txtPull data from the server using the daemon module:
# rsync -avz [email protected]::image01 /tmpIf a firewall blocks the connection, stop and disable firewalld:
# systemctl stop firewalld && systemctl disable firewalldCreate the password file required by the daemon:
# echo "rsyncuser:123123" >> /etc/rsync.passwd
# chmod 600 /etc/rsync.passwd3.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/crontabSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
