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

Rsync 實現遠程備份與文件同步完整部署指南

編輯:Linux 教學

Rsync 核心優勢

  • 鏡像整個目錄樹,保留權限、時間、軟/硬鏈接。
  • 無需特權安裝,傳輸效率高 (增量算法)。
  • 支援 rsh/ssh/直連 socket 多種傳輸方式。
  • 支援匿名傳輸,適合鏡像站點。

安裝方式

# Debian/Ubuntu
apt-get install rsync

# RHEL/CentOS/Fedora
yum install rsync
# 或 rpm -ivh rsync.rpm

# 源碼編譯
tar xf rsync-xxx.tar.gz && cd rsync-xxx
./configure --prefix=/usr && make && make install

服務端配置 (/etc/rsyncd.conf)

核心配置示例

# 全局參數
pid file = /var/run/rsyncd.pid
port = 873
address = 192.168.1.171
uid = root
gid = root
use chroot = yes
read only = yes
hosts allow = 192.168.1.0/24 10.0.1.0/24
hosts deny = *
max connections = 5
motd file = /etc/rsyncd.motd
log format = %t %a %m %f %b
syslog facility = local3
timeout = 300

# 模組: 備份 /home (排除 easylife/, samba/)
[rhel4home]
path = /home
list = yes
ignore errors
auth users = root
secrets file = /etc/rsyncd.secrets
comment = RHEL 4 Home Data
exclude = easylife/ samba/

# 模組: 備份 /opt
[rhel4opt]
path = /opt
list = no
ignore errors
auth users = easylife
secrets file = /etc/rsyncd.secrets
comment = RHEL 4 Opt Data

關鍵參數解析

參數說明
use chroot傳輸前 chroot 到 path,增強安全,但需 root 權限且忽略符號鏈接實體
read onlyyes=只允許下載;no=允許上傳
auth users認證用戶 (需系統真實存在),多用戶逗號分隔
secrets file密碼文件路徑,格式 user:pass,權限必須 600
exclude排除模式,相對於 path

密碼與歡迎文件

# /etc/rsyncd.secrets (權限 600)
root:mike
easylife:keer

# /etc/rsyncd.motd
++++++++++++++++++++++++++++++++++++
Welcome to use rsync services!
2024-2025
++++++++++++++++++++++++++++++++++++

客戶端同步命令

# 從服務器拉取 (備份)
rsync -avzP --delete root@192.168.1.171::rhel4home /local/backup/home/

# 推送到服務器 (需服務端 read only = no)
rsync -avzP /local/data/ easylife@192.168.1.171::rhel4opt

# 通過 SSH 隧道 (更安全)
rsync -avzP -e ssh /src/ user@host:/dest/

常用參數速查

  • -a:歸檔模式 (遞歸、保留屬性、時間、鏈接等)
  • -v:詳細輸出
  • -z:傳輸壓縮
  • -P:顯示進度 + 斷點續傳
  • --delete:刪除目標端源端不存在的文件 (鏡像同步)
  • --exclude=PATTERN:排除模式
  • --bwlimit=KBPS:限制頻寬

Windows 客戶端

使用 cwRsync (含 OpenSSH) 或 DeltaCopy (圖形界面)。

總結

Rsync 以增量傳輸、豐富過濾、多協議支援成為 Linux 系統備份、同步、鏡像的標準工具。合理配置模組認證、chroot、排除規則,可構建安全高效的異地災備體系。

Copyright © Windows教程網 All Rights Reserved