Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

CentOS 7 Rsync 伺服器端與客戶端完整部署教學

編輯:關於伺服器

架構概覽

Rsync 為增量同步神器,採用 C/S 架構。本文涵蓋伺服器端(模組模式)與客戶端完整配置,含認證、防火牆、定時同步等生產級細節。

一、伺服器端部署

1. 安裝與啟用 xinetd

yum -y install xinetd rsync
systemctl enable xinetd --now

2. 配置 xinetd 啟用 rsync

vi /etc/xinetd.d/rsync
# 將 disable = yes 改為 disable = no
systemctl restart xinetd

3. 防火牆開放 873 埠

firewall-cmd --permanent --add-port=873/tcp
firewall-cmd --reload
# 或 iptables
iptables -A INPUT -p tcp --dport 873 -j ACCEPT

4. 建立 rsyncd.conf 核心配置

vi /etc/rsyncd.conf
# Global Settings
uid = root
gid = root
use chroot = no
max connections = 20
secrets file = /etc/rsyncd.secrets
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock

[backup]
path = /data/www/repos
auth users = rsync
read only = no
hosts allow = 192.168.3.0/24
hosts deny = *
list = true

5. 建立認證檔並設權限(關鍵:必須 600)

echo "rsync:StrongPassw0rd!" > /etc/rsyncd.secrets
chmod 600 /etc/rsyncd.secrets
chown root:root /etc/rsyncd.secrets

二、客戶端同步操作

1. 安裝 rsync

yum -y install rsync

2. 互動式同步測試

rsync -avz --progress rsync@192.168.3.191::backup /www

3. 非互動式(腳本/排程)同步

# 僅存放密碼,無使用者名
echo "StrongPassw0rd!" > /etc/rsyncd.pas
chmod 600 /etc/rsyncd.pas

# 完整指令
rsync -vzrtopgu --progress --delete \
  --password-file=/etc/rsyncd.pas \
  rsync@192.168.3.191::backup /www/

參數解析: -a=保留屬性 -z=壓縮 -u=僅更新 --delete=鏡像刪除

4. 寫入 crontab 定時同步(每 3 分鐘)

crontab -e
*/3 * * * * rsync -avz --password-file=/etc/rsyncd.pas rsync@192.168.3.191::backup /www/ &>/dev/null

三、常用指令速查

需求指令範例
列出遠端模組rsync rsync@192.168.3.191::
本地目錄同步rsync -av --delete /src/ /dst/
SSH 加密傳輸rsync -avz -e ssh /src/ user@host:/dst/
排除特定目錄rsync -av --exclude='cache/' /src/ /dst/

避坑提醒

  • 密碼檔權限必須 600,否則拒絕連線
  • 雙端時間需同步(NTP),否則 -u 判斷失效
  • 大量小檔案同步建議加 -W (整檔傳輸) 提升效能
  • 生產環境建議搭配 SSH 通道或 VPN 使用
Copyright © Windows教程網 All Rights Reserved