How to Set Up Real-Time NFS Backup with Inotify and Rsync on Linux
This step‑by‑step guide shows how to configure Linux servers for real‑time NFS static resource backup using inotify and rsync, covering host preparation, rsync daemon setup, user and password management, inotify script creation, and verification of synchronized files across multiple machines.
Task Requirements
1. Real‑time backup of static resources on an NFS server using inotify + rsync.
Host List
# External IP Internal IP Hostname
192.168.122.207 172.16.1.207 web-test-209
192.168.122.231 172.16.1.231 nfs-test-231
192.168.122.241 172.16.1.241 rsync-test-241Architecture Diagram
1. Set up rsync on rsync-test-241
1.1 Install rsync
# yum install rsync -y1.2 Modify configuration file /etc/rsyncd.conf
uid = rsync
gid = rsync
fake super = yes
use chroot = no
max connections = 200
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 172.16.1.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
[backup-nfs]
comment = This is nfs backup!
path = /backup/uid / gid : run rsync daemon as the rsync user.
use chroot = no : disable chroot for internal network sync.
max connections = 200 : maximum concurrent connections.
read only = false : allow read‑write access.
hosts allow / deny : whitelist and blacklist IP ranges.
auth users : virtual user for authentication.
secrets file : password file for the virtual user.
path : directory to be backed up.
1.3 Create user, directory and authentication file
# useradd rsync -s /sbin/nologin -M
# id rsync
# mkdir /backup
# chown -R rsync.rsync /backup/
# echo "rsync_backup:mima666" > /etc/rsync.password
# chmod 600 /etc/rsync.password1.4 Start rsync service and enable at boot
# systemctl start rsyncd
# systemctl enable rsyncd1.5 Verify rsync service
# systemctl status rsyncd
# ps -ef|grep rsync
# netstat -tnlp | grep rsync2. Install rsync on nfs-test-231
2.1 Install rsync
# yum install rsync -y2.2 Create password file
# echo 'mima666' > /etc/rsync.password
# chmod 600 /etc/rsync.password2.3 Test rsync data sync
# rsync -avzP network_init.sh [email protected]::backup-nfs --password-file=/etc/rsync.passwordFlags: -a preserve attributes, -v verbose, -z compress, -P show progress.
3. Deploy inotify on nfs-test-231
3.1 Install inotify‑tools
# yum install inotify-tools -y3.2 Write script rsync_nginx.sh
#!/bin/bash
/usr/bin/inotifywait -mrq -e modify,delete,create,attrib,move /nfs-web-share/ | while read line
do
rsync -a --delete /nfs-web-share/ [email protected]::backup-nfs --password-file=/etc/rsync.password
echo "`date +%F\ %T`出现事件$line" >> /var/log/rsync.log 2>&1
done3.3 Run script in background
# bash rsync_nginx.sh &4. Verify synchronization
4.1 Update file on web-test-207
# cd /usr/share/nginx/html/
# vim index.html
# cat index.html4.2 Check log on nfs-test-231
# tail -f /var/log/rsync.log4.3 Check backup on rsync-test-241
# cd /backup/
# ls
# cat index.htmlIf the process fails, common reasons include firewall blocks, incorrect NFS permissions, or network connectivity issues; consult error messages for further troubleshooting.
Signed-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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
