---
url: /dev/ulg6keno/index.md
description: Linux 下安装配置 Rsync 远程数据同步工具，实现服务器间增量备份和文件同步。
---
Linux 下安装并使用同步工具 Rsync。

`rsync（remote synchronize）`  是一个远程数据同步工具，可以使用“rsync算法”同步本地和远程主机之间的文件。rsync的好处是只同步两个文件不同的部分，相同的部分不在传递。类似于增量备份，这使的在服务器传递备份文件或者同步文件，比起scp工具要省好多时间。

**Github地址：[WayneD/rsync](https://github.com/RsyncProject/rsync)**

## 安装

### Ubuntu

**目前大多ubuntu发行版已自带 rsync ，但其服务不是默认开启，需要手动开启：**

```shell
sudo vim /etc/default/rsync
#修改以下内容
rsync_ENABLE=true
```

## 编译安装

**如果本机没有 rsync ，则可以考虑手动编译安装。**

1. **下载rsync-3.1.3.tar.gz**
   **WayneD/rsync**
2. **安装**
   ```shell
   #解压缩
   tar -zxvf rsync-3.1.3.tar.gz
   cd rsync-3.1.3
   #配置
   ./configure --prefix=/usr/local/rsync 
   #编译和安装
   make && make install
   ```

## 配置

### 服务端配置（server）

1. 编辑`etc/rsyncd.conf` 配置文件

   ```shell
   vim /etc/rsyncd.conf
   ```

   ```shell
   #启动的用户 如 root 等等
   uid = nobody
   #启动的用户组 如 root 等等
   gid = nobody
   use chroot = no
   max connections = 30
   #这个可以不设置
   pid file = /var/run/rsyncd.pid
   # exclude = lost+found/
    transfer logging = yes
    timeout = 900
   # ignore nonreadable = yes
    dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

   [bk]
   #[bk]指定我们要同步的文件，实现免密同步
   #开启只读
           read only =yes
           #需要备份的文件路径
           path = /rsync/back
           #备注，随便起
           comment = bk
           #允许访问的主机ip，用'/'隔开
           hosts allow = 192.128.165.25/192.128.165.26
           #chaospring 为虚拟账号名称，不是linux用户，最好不要填root，否则可能会进入ssh的密码验证 可以为任意名称
           auth users = chaospring
       #免密同步的密钥文件存放处
       secrets file = /etc/rsyncd.secrets
   ```
2. **编写密钥文件**

   ```shell
   vim /etc/rsyncd.secrets
   ```

   **格式为：**`用户名:密码`

   ```shell
   chaospring:123456
   ```

   **授权**

   ```shell
   chmod 600 /etc/rsyncd.secrets
   ```

   **启动**

   ```shell
   #启动
   /usr/local/rsync/bin/rsync --daemon
   ```

### 客户端配置（client）

1. **创建数据备份目录**

   ```shell
   mkdir -p /data/backup/
   ```
2. **创建密钥文件**

   ```shell
   vim /etc/rsyncd.secrets
   ```

   **格式为：**`密码` 。 注意，这里只存放对应用户的密码！

   ```shell
   123456
   ```
3. **执行同步**

   ```shell
   rsync -vzrtopg --progress --delete --password-file=/etc/rsyncd.secrets hnrd@192.128.165.24::bk /data/backup/
   ```
