> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/di-35-zhang-fu-wu-qi/di-35.1-jie-rsync-shu-ju-tong-bu.md).

# 35.1 Rsync 数据同步

Rsync（Remote Sync，远程同步）是由 Andrew Tridgell 与 Paul Mackerras 共同开发的高效文件同步工具。Rsync 采用增量传输算法，仅传输文件间的差异部分，可显著降低网络带宽消耗并缩短同步时间，是系统备份与数据镜像的常用工具。

## 环境概述

开始配置前，首先说明本节两台服务器的角色分工。

![Rsync 架构图](/files/NOZQss2o5fhBZo5RGKtB)

* 服务器 A（发起端、客户端）：**192.168.179.128**
* 服务器 B（同步源、服务端）：**192.168.179.150**

设计目标：将服务器 B 的数据同步到服务器 A，即 B（服务端）→ A（客户端），实现数据的完整镜像。

## 服务器 B（同步源）配置

首先配置同步源服务器，即服务器 B。

### 安装 rsync

在服务器 B 上安装 rsync，有两种可选安装方式：

* 使用 pkg 安装：

```sh
# pkg install rsync
```

* 或使用 Ports 安装：

```sh
# cd /usr/ports/net/rsync/
# make install clean
```

* 查看安装信息

```sh
# pkg info -D rsync
```

### 同步目录准备

```sh
# mkdir -p /home/test # 新建用于备份的文件夹 test
# chown root /home/test/ # 设置文件夹属主为 root
# cd /home/test/ # 切换路径
# touch txt001 txt002 # 在文件夹内创建测试文件
```

文件结构：

```sh
/
├── home
│   ├── test                              # 服务器 B 的同步源目录
│   │   ├── txt001                         # 测试文件 1
│   │   └── txt002                         # 测试文件 2
│   └── testBackUp                         # 服务器 A 的本地备份目录
├── usr
│   └── local
│       └── etc
│           └── rsync
│               └── rsyncd.conf           # rsyncd 服务端主配置文件
├── var
│   ├── log
│   │   └── rsyncd.log                     # rsyncd 服务日志
│   └── run
│       └── rsyncd.pid                     # rsyncd 进程 ID 文件
└── etc
    └── rsyncd_users.db                     # rsync 用户认证密码文件
```

### 服务端主配置文件

编辑 **/usr/local/etc/rsync/rsyncd.conf** 文件，写入：

```ini
# 运行服务的系统用户
uid = root

# 运行服务的系统用户组
gid = wheel

# 锁定在源目录内
use chroot = yes

# 监听地址
address = 192.168.179.150

# 监听端口，默认为 873
port = 873

# 日志文件路径
log file = /var/log/rsyncd.log

# PID 文件路径
pid file = /var/run/rsyncd.pid

# 允许访问的客户端地址
hosts allow = 192.168.179.128

# 共享模块名称，可自定义
[testcom]

# 同步的目录路径，必须与 uid/gid 指定的用户与组匹配
path = /home/test

# 模块说明
comment = testcombackup

# 是否只读
read only = yes

# 传输时不压缩的文件类型
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# 授权账户
auth users = root

# 用户认证密码文件路径
secrets file = /etc/rsyncd_users.db
```

> **技巧**
>
> 上述示例中的 **192.168.179.128**、**192.168.179.150** 为占位符，需要替换为实际的值。

### 创建用于授权备份账户认证的密码文件（服务端）

* 编辑 **/etc/rsyncd\_users.db** 文件，写入：

```sh
root:12345678   # 支持多个用户，每行一个
```

> **注意**
>
> 服务端的密码文件应包含用户名和密码的映射关系。格式为 `授权账户用户名:密码`。

* 限制数据文件权限，否则将导致错误：

```sh
# chmod 600 /etc/rsyncd_users.db
```

设置 rsync 用户数据库文件的权限为仅属主可读写。

### 服务启动配置

```sh
# service rsyncd enable   # 设置 rsync 服务开机自动启动
# service rsyncd start    # 启动 rsync 服务
```

### 服务端口验证

查看 rsync 服务使用的网络端口和对应进程：

```sh
# sockstat | grep rsync
root     rsync       1198 5   tcp4   192.168.179.150:873   *:*
```

## 服务器 A（发起端）配置

请参照上文步骤安装 rsync。

### 本地备份目录配置

创建本地备份目录 **/home/testBackUp/**，并设置相应权限：

```sh
# mkdir -p /home/testBackUp                 # 创建备份目录
# chown root:wheel /home/testBackUp/       # 设置目录所有者为 root，组为 wheel
```

### 同步操作（密码输入方式）

将文件同步到本地备份目录 **/home/testBackUp/**，需手动输入密码。

```sh
# rsync -avz root@192.168.179.150::testcom /home/testBackUp  # 用 rsync 从远程 testcom 模块同步文件到本地备份目录
Password: # 输入服务器 B 设置的密码
receiving incremental file list
./
txt001
txt002

sent 65 bytes  received 151 bytes  86.40 bytes/sec
total size is 0  speedup is 0.00
```

`testcom` 是在 **/usr/local/etc/rsync/rsyncd.conf** 文件中定义的同步模块名称，对应服务器上的目录。

#### 附录：指定密码文件方式

创建授权备份账户认证的密码文件（客户端）。

* 编辑客户端的 **/etc/rsyncd\_users.db** 文件，仅写入密码：

```sh
12345678
```

* 限制权限，否则将引发错误 `ERROR: password file must not be other-accessible`。

```sh
# chmod 600 /etc/rsyncd_users.db
```

设置 rsync 用户数据库文件权限为仅属主可读写。

> **注意**
>
> 客户端密码文件应仅包含密码本身，每行一个。

执行同步命令。

使用 rsync 将远程 `testcom` 模块同步到本地备份目录，显示同步进度，并指定密码文件。

```sh
# rsync -auvz --progress --password-file=/etc/rsyncd_users.db root@192.168.179.150::testcom /home/testBackUp
receiving incremental file list
./
txt001
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=1/3)
txt002
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=0/3)

sent 65 bytes  received 151 bytes  432.00 bytes/sec
total size is 0  speedup is 0.00
```

### 同步结果验证

列出本地备份目录中的详细文件信息：

```sh
# ls -l  /home/testBackUp
total 1
-rw-r--r--  1 root wheel 0 Apr 17 18:33 txt001
-rw-r--r--  1 root wheel 0 Apr 17 18:33 txt002
```

## 参考文献

* Tridgell A, Mackerras P. The rsync algorithm\[EB/OL]. (1998-11-09)\[2026-04-17]. <https://rsync.samba.org/tech_report/>. rsync 算法的原始技术报告，由两位作者共同发表。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/di-35-zhang-fu-wu-qi/di-35.1-jie-rsync-shu-ju-tong-bu.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
