> For the complete documentation index, see [llms.txt](https://lack.gitbook.io/operation-and-maintenance/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lack.gitbook.io/operation-and-maintenance/yun-ji-suan/linuxtong-bu-gong-ju-rsync.md).

# linux同步工具rsync

rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步，支持本地复制，或者与其他SSH、rsync主机同步。

## rsync使用

`rsync` 命令：

介绍：linux中的文件同步工具，`yum install -y rsync`

格式：rsync \[option] \[src] \[user\@ip:]\[dest]

选项：

* -a 包含-rtplgoD
* -r 同步目录时要加上，类似cp时的-r选项
* -v 同步时显示一些信息，让我们知道同步的过程
* -l 保留软连接 -L 加上该选项后，同步软链接时会把源文件给同步
* -p 保持文件的权限属性
* -o 保持文件的属主
* -g 保持文件的属组
* -D 保持设备文件信息
* -t 保持文件的时间属性
* \--delete 删除DEST中SRC没有的文件
* \--exclude 过滤指定文件，如--exclude “logs”会把文件名包含logs的文件或者目录过滤掉，不同步
* -P 显示同步过程，比如速率，比-v更加详细
* -u 加上该选项后，如果DEST中的文件比SRC新，则不同步
* -z 传输时压缩

用法：

* rsync -av /etc/passwd /tmp/1.txt ：本地同步
* rsync -av /tmp/1.txt 192.168.188.128:/tmp/2.txt ：远程同步 （远程的机器上也要安装rsync）
* rsync -av /root/ /tmp/root/ ：同步目录，注意源目录和目标目录尾加 /

```bash
[root@localhost ~]# rsync -av /etc/passwd /tmp/passwd
sending incremental file list
passwd

sent 1,102 bytes  received 35 bytes  2,274.00 bytes/sec
total size is 1,010  speedup is 0.89
[root@localhost ~]# rsync -av /etc/passwd root@192.168.127.128:/tmp/passwd
sending incremental file list

sent 45 bytes  received 12 bytes  114.00 bytes/sec
total size is 1,010  speedup is 17.72
[root@localhost ~]# ls /tmp/passwd 
/tmp/passwd
[root@localhost ~]# rsync -av /root/ /tmp/root/
```

## rsync 以ssh的方式同步数据

* rsync -av test1/ 192.168.127.129:/tmp/test2/

  &#x20;：同步目录
* rsync -av -e "ssh -p 22" test1/ 192.168.127.129:/tmp/test2/ ：ssh 方式同步
* rsync -av -e "ssh -p 22" 192.168.127.129:/tmp/test2/

  &#x20;test1/ ：从远程机器下载

## rsync 服务方式同步

rsync 也可以使用服务方式来同步数据，但需要一些配置。

1.修改配置文件：`/etc/rsyncd.conf`

```bash
[root@localhost ~]# vim /etc/rsyncd.conf 
port=873
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
address=192.168.127.128
[test]
path=/tmp/rsync
use chroot=true
max connections=4
read only=no
list=true
uid=root
gid=root
#auth users=test
#secrets file=/etc/rsyncd.passwd
hosts allow=192.168.127.129 192.168.127.0/24
```

rsyncd.conf配置文件详解

* port：指定在哪个端口启动rsyncd服务，默认是873端口。
* log file：指定日志文件。
* pid file：指定pid文件，这个文件的作用涉及服务的启动、停止等进程管理操作。
* address：指定启动rsyncd服务的IP。假如你的机器有多个IP，就可以指定由其中一个启动rsyncd服务，如果不指定该参数，默认是在全部IP上启动。 \[]：指定模块名，里面内容自定义。
* path：指定数据存放的路径。
* use chroot true|false：表示在传输文件前首先chroot到path参数所指定的目录下。这样做的原因是实现额外的安全防护，但缺点是需要以roots权限，并且不能备份指向外部的符号连接所指向的目录文件。默认情况下chroot值为true，如果你的数据当中有软连接文件，阿铭建议你设置成false。
* max connections：指定最大的连接数，默认是0，即没有限制。
* read only ture|false：如果为true，则不能上传到该模块指定的路径下。
* list：表示当用户查询该服务器上的可用模块时，该模块是否被列出，设定为true则列出，false则隐藏。
* uid/gid：指定传输文件时以哪个用户/组的身份传输。
* auth users：指定传输时要使用的用户名。
* secrets file：指定密码文件，该参数连同上面的参数如果不指定，则不使用密码验证。注意该密码文件的权限一定要是600。格式：用户名:密码
* hosts allow：表示被允许连接该模块的主机，可以是IP或者网段，如果是多个，中间用空格隔开。

> 当设置了auth users和secrets file后，客户端连服务端也需要用用户名密码了，若想在命令行中带上密码，可以设定一个密码文件 `rsync -avL test@192.168.133.130::test/test1/ /tmp/test8/ --password-file=/etc/pass` 其中/etc/pass内容就是一个密码，权限要改为600

2.创建同步的目录

```bash
[root@localhost ~]# mkdir /tmp/rsync/
[root@localhost ~]# chmod 755 -R !$
chmod 755 -R /tmp/rsync/
[root@localhost ~]# ls -ld /tmp/rsync
drwxr-xr-x 2 root root 6 8月   8 23:03 /tmp/rsync
```

3.启动服务：`rsync --daemon`

```bash
[root@localhost ~]# rsync --daemon
[root@localhost ~]# ps aux|grep "rsync"
root       1684  0.0  0.0 114740   564 ?        Ss   23:04   0:00 rsync --daemon
root       1686  0.0  0.0 112720   984 pts/1    R+   23:04   0:00 grep --color=auto rsync
[root@localhost ~]# netstat -tnlp|grep "rsync"
tcp        0      0 192.168.127.128:873     0.0.0.0:*               LISTEN      1684/rsync
```

4.测试：在客户端192.168.127.129

```bash
## 客户端 192.168.127.129
[root@localhost ~]# mkdir /tmp/rsync
[root@localhost ~]# echo "test" >> /tmp/rsync/test
[root@localhost ~]# ls /tmp/rsync/
test
[root@localhost ~]# rsync -avP /tmp/rsync/ 192.168.127.128::test/
sending incremental file list
./
test
              5 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/2)

sent 112 bytes  received 38 bytes  14.29 bytes/sec
total size is 5  speedup is 0.03

## 服务端 192.168.127.128
[root@localhost ~]# ls /tmp/rsync/
test
```
