rsync服务的简介和配置详解

一、rsync概述

          rsync是类unix系统下的数据镜像备份工具——remote sync。与传统的cptar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份、本地复制,远程同步等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。

rsync 命令来同步系统文件之前要先登录remote 主机认证,认证过程中用到的协议有2种:ssh 协议和rsync协议。

二、rsync原理

rsync本来是用于替代rcp的一个工具,目前由rsync.samba.org维护,所以rsync.conf文件的格式类似于samba的主配置文件;Rsync可以通过rshssh使用,也能以daemon模式去运行

在以daemon方式运行时Rsync server会打开一个873 端口,等待客户端去连接。连接时,Rsync server会检查口令是否相符,若通过口令查核,则可以开始进行文件传输。第一次连通完成时,会把整份文件传输一次,以后则就只需进行增量备份

 

三、rsync特性

1、能更新整个目录和树和文件系统

2、有选择性的保持原来文件的符号链接、硬链接、文件属性、权限、设备以及时间等

3、对于安装来说,无任何特殊权限要求

4、优化的流程,文件传输效率高

5、能用rshssh或直接端口作为传输入口端口

6、支持匿名rsync同步文件,是理想的镜像工具

 

 

四、ssh模式

1、服务器本地同步

实验环境准备:一台服务器(IP地址为192.168.115.120

[root@localhost ~]# yum -y install rsync

[root@localhost ~]# mkdir -p /server/ssh

[root@localhost ~]# touch /server/ssh/{1..3}.txt

[root@localhost ~]# ls -l /server/ssh/

-rw-r--r-- 1 root root 0 2  25 18:12 1.txt

-rw-r--r-- 1 root root 0 2  25 18:12 2.txt

-rw-r--r-- 1 root root 0 2  25 18:12 3.txt

 

1、同步目录下的文件

[root@localhost ~]# rsync -av /server/ssh/ /back/

/server/ssh目录里的所有的文件同步至/back 目录(不包含/server/ssh本身)

sending incremental file list

./

1.txt

2.txt

3.txt

 

2、同步目录下的文件及目录本身

[root@localhost ~]# rsync -av  /server/ssh  /back

/server/ssh目录包括自己整个同步至/back目录

sending incremental file list

ssh/

ssh/1.txt

ssh/2.txt

ssh/3.txt

 

注意:rsync命令使用中,如果源参数的末尾有斜线,就会复制指定目录内容,而不复制目录本身;没有斜线,则会复制目录本身

2、局域网间同步

实验环境准备:一台服务器(IP地址为192.168.115.120

一台服务器(IP地址为192.168.115.130

上面已经在192.168.115.120服务器创建了目录/server/ssh,也创建好了几个文件,现在需要把这些文件同步到192.168.115.130服务器的/client/ssh目录下。

 

以下命令需要在192.168.115.130服务器上执行

[root@localhost ~]# yum -y install rsync

[root@localhost ~]# mkdir -p /client/ssh

 

1、远程文件同步到本地,需输入远程主机root密码

[root@localhost ~]# rsync -av 192.168.115.120:/server/ssh/ /client/ssh/

The authenticity of host '192.168.115.120 (192.168.115.120)' can't be established.

ECDSA key fingerprint is c7:79:b3:87:3f:cf:d9:18:34:e7:38:bd:2d:8c:db:36.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '192.168.115.120' (ECDSA) to the list of known hosts.

root@192.168.115.120's password:

receiving incremental file list

./

1.txt

2.txt

3.txt

 

2、本地文件同步到远程,需输入远程主机root密码

[root@localhost ~]# rsync -av /etc/hosts 192.168.115.120:/server/ssh

root@192.168.115.120's password:

sending incremental file list

hosts

 

3、本地整个目录同步到远程,需输入远程主机root密码

[root@localhost ~]# rsync -av /client/ssh 192.168.115.120:/server/ssh

root@192.168.115.120's password:

sending incremental file list

ssh/

ssh/1.txt

ssh/2.txt

ssh/3.txt

 

3、局域网指定用户同步

实验环境准备:一台服务器(IP地址为192.168.115.120

一台服务器(IP地址为192.168.115.130

 

192.168.115.120服务器执行以下命令

[root@localhost ~]# useradd user1

[root@localhost ~]# echo '123'|passwd --stdin user1

[root@localhost ~]# mkdir /home/user1/test

[root@localhost ~]# chown user1:user1 /home/user1/test/

 

192.168.115.130服务器执行以下命令

1、把本地文件通过远程的指定普通用户同步到远程服务器

注意:同步的远程服务器目录的用户需要对目录有相应权限,否则报错

[root@localhost ~]# rsync -av /client/ssh/ '-e ssh -l user1' 192.168.115.120:/home/user1/test

user1@192.168.115.120's password:

sending incremental file list

./

1.txt

2.txt

3.txt

 

2、把远程服务器的目录通过普通用户同步到本地的/client/ssh目录

[root@localhost ~]# rsync -av  192.168.115.120:/home/user1/test '-e ssh -l user1 -p 22' /client/ssh/

user1@192.168.115.120's password:

receiving incremental file list

test/

test/1.txt

test/2.txt

test/3.txt

 

五、daemon模式

实验环境准备:一台服务器(IP地址为192.168.115.120

一台客户机(IP地址为192.168.115.130

 

1daemon模式及配置文件的说明

1daemon模式配置文件

   rsyncdaemon方式运行的时候使用配置文件为rsyncd.conf

 

2)使用daemon模式的时候,一定要分清楚服务端和客户端

与平时理解的服务端与客户端不太一样,被同步的一端为服务端,要把文件同步到另一端的源端为客户端

 

3rsyncd.conf文件格式

     a rsyncd.conf配置文件由模块和参数组成,一个模块以写在方括号里的模块名称开始,直到下一个模块,模块里包含由“name = value”格式的参数。

     b、文件是基于行的,每一行代表一个模块名或者参数

 

4)常用参数的说明

rsync 服务运行方式需要配置 rsyncd.conf,其格式类似于 samba 的主配置文件

a全局参数

在全局参数部分也可以定义模块参数,这时该参数的值就是所有模块的默认值

address    独立运行时,用于指定服务器运行的 IP 地址,默认本地所有IP

port       指定 rsync 守护进程监听的端口号,默认 873

pid file     rsync 的守护进程将其 PID 写入指定的文件

log file     指定 rsync 守护进程的日志文件,而不将日志发送给 syslog

syslog facility   指定 rsync 发送日志消息给 syslog 时的消息级别

socket options  指定自定义 TCP 选项

lockfile   指定rsync的锁文件存放路径

timeout    超时时间

 

b模块参数

模块参数主要用于定义 rsync 服务器哪个目录要被同步。模块声明的格式必须为 [module] 形式,这个名字就是在 rsync 客户端看到的名字,类似于 Samba 服务器提供的共享名。而服务器真正同步的数据是通过 path 来指定的

 

1、基本模块参数

path       指定当前模块的同步路径,该参数是必须指定的

comment   给模块指定一个描述

 

2、模块控制参数

use chroot           在服务运行时要不要把他锁定在家目录,默认为 true

uidgid            指定rsync运行用户和用户组,默认nobody

use chroot           是否让进程离开工作目录

max connections      最大并发连接数,0为不限制

lock file             指定支持 max connections的锁文件。默认/var/run/rsyncd.lock

list                 指定列出模块列表时,该模块是否被列出。默认为 true

read only          只读选择,默认true

write only         只写选择,不让客户端从服务器上下载文件。默认false

ignore errors     忽略IO错误,默认true

ignore nonreadable 指定 rysnc 服务器完全忽略那些用户没有访问权限的文件

dont compress      用来指定那些在传输之前不进行压缩处理的文件

 

3、模块文件筛选参数

exclude     由空格隔开的多个文件或目录(相对路径),并将其添加到 exclude 列表中。

exclude from 指定包含 exclude 规则定义的文件名,服务器从中读取 exclude 列表定义

include  指定多个由空格隔开文件或目录(相对路径),并将其添加到 include 列表中。

include from  指定一个包含 include 规则定义的文件名,服务器从 中读取 include 列表定义

 

4、模块用户认证参数

auth users  执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开,默认为匿名方式

secrets file  指定一个 rsync 认证口令文件。只有在 auth users 被定义时,该文件才起作用。文件权限必须是 600

strict modes  表示是否工作在严格模式下,严格检查文件权限等相关信息,默认为true

 

5、模块访问控制参数

hosts allow  指定哪些主机客户允许连接该模块。默认值为 *

hosts deny  指定哪些主机客户不允许连接该模块

说明:

 a、二者都不出现时,默认为允许访问

 b、只出现hosts allow,定义白名单,但没有被匹配到的主机由默认规则处理,即为允许

 c、只出现hosts deny,定义黑名单,出现在名单中的都被拒绝

 d、二者同时出现,先检查hosts allow,如果匹配就allow,否则检查hosts deny,如果匹配则拒绝,如是二者都不匹配,则由默认规则处理,即为允许

 

6、模块日志参数

transfer logging 使 rsync 服务器将传输操作记录到传输日志文件。默认值为false

log format 指定传输日志文件的字段。默认为:”%o %h [%a] %m (%u) %f %l”

设置了”log file”参数时,在日志每行的开始会添加”%t [%p]“

 

可以使用的日志格式定义符如下所示:

%o 操作类型:”send” “recv”

%h 远程主机名

%a 远程IP地址

%m 模块名

%u 证的用户名(匿名时是 null

%f 文件名

%l 文件长度字符数

%p 该次 rsync 会话的 PID

%P 模块路径

%t 当前时间

%b 实际传输的字节数

%c 当发送文件时,记录该文件的校验码

 

2、服务启动方式

daemon模式启动的时候必须加参数--daemon

[root@localhost ~]# yum -y install rsync

[root@localhost ~]# rsync --daemon

[root@localhost ~]# netstat -antp|grep 873

tcp    0    0 192.168.115.120:873     0.0.0.0:*     LISTEN     11755/rsync

 

3rsync服务端配置

[root@localhost ~]# vim /etc/rsyncd.conf

# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

address = 192.168.115.120

port 873

uid = root

gid = root

use chroot = no

max connections = 5

pid file = /var/run/rsyncd.pid

log file = /var/log/rsyncd.log

#exclude = lost+found/

transfer logging = yes

timeout = 600

ignore nonreadable = yes

dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

 

[share]

        path = /server/rsync

        comment = it is a share file

        gnore errors = yes

        read only = no

        write only = no

        hosts allow = 192.168.115.0/24

        hosts deny = 192.168.100.0/24

        list = yes

        auth users = user1

        secrets file = /etc/rsync.passwd

4、创建相应的目录和密码文件,并设置密码文件的权限为600

[root@localhost ~]# mkdir /server/rsync      创建同步的目录

[root@localhost ~]# echo 'user1:123' > /etc/rsync.passwd    创建密码文件

[root@localhost ~]# chmod  600  /etc/rsync.passwd       设置密码文件权限为600

 

5、重新启动rsync服务方法

先关闭rsync进程 kill -s QUIT 进程号,再启动时会启动不成功,这时候要删除进程号的文件

 

[root@localhost ~]# netstat -antp|grep 873      查看端口已经启动

tcp        0      0 192.168.115.120:873     0.0.0.0:*   LISTEN      11755/rsync         

[root@localhost ~]# ps aux|grep rsync      查看进程号

root     11755  0.0  0.0 114644   376 ?        Ss   21:30   0:00 rsync --daemon

root     11885  0.0  0.0 112664   972 pts/0    R+   23:20   0:00 grep --color=auto rsync

[root@localhost ~]# kill -s QUIT 11755     停止进程并重启

[root@localhost ~]# rsync --daemon  

[root@localhost ~]# failed to create pid file /var/run/rsyncd.pid: File exists   重启报错

 

[root@localhost ~]# rm -f /var/run/rsyncd.pid     删除进程文件

[root@localhost ~]# rsync --daemon        再次启动就会成功了

[root@localhost ~]# netstat -antp|grep 873    查看端口是否开启

tcp        0      0 192.168.115.120:873     0.0.0.0:*     LISTEN      11892/rsync 

6、在服务器创建两个文件,下面测试会用到

[root@localhost ~]# vim /server/rsync/1.txt

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

 

[root@localhost ~]# vim /server/rsync/2.txt

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

 

7、客户端测试

1)下行同步的两种方法

rsync -avz 用户名@服务器地址::共享模块名 /本地目录

rsync -avz rsync://用户名@服务器地址/共享模块名 /本地目录

[root@localhost ~]# yum -y install rsync

[root@localhost ~]# mkdir /client/rsync

把服务器文件同步到客户端(本地)方法1

[root@localhost ~]# rsync -avz user1@192.168.115.120::share /client/rsync/

Password:

receiving incremental file list

./

1.txt

2.txt

 

sent 97 bytes  received 227 bytes  92.57 bytes/sec

total size is 130  speedup is 0.40

[root@localhost ~]# ls -l /client/rsync/

-rw-r--r-- 1 root root 64 2  25 23:38 1.txt

-rw-r--r-- 1 root root 66 2  25 23:39 2.txt

 

把服务器文件同步到客户端(本地)方法2

[root@localhost ~]# rsync -avz rsync://user1@192.168.115.120/share /client/rsync/

Password:

receiving incremental file list

./

1.txt

2.txt

 

sent 97 bytes  received 227 bytes  92.57 bytes/sec

total size is 130  speedup is 0.40

[root@localhost ~]# ls -l /client/rsync/

-rw-r--r-- 1 root root 64 2  25 23:38 1.txt

-rw-r--r-- 1 root root 66 2  25 23:39 2.txt

 

2)上行同步的两种方法

rsync -avz  /本地目录/* 用户名@服务器地址::共享模块名

rsync -avz  /本地目录/*  rsync://用户名@服务器地址/共享模块名

把客户端(本地)文件同步到服务器方法1

 

[root@localhost ~]# rsync -avz /client/rsync/* user1@192.168.115.120::share

Password:

sending incremental file list

1.txt

2.txt

 

sent 138 bytes  received 46 bytes  52.57 bytes/sec

total size is 130  speedup is 0.71

 

把客户端(本地)文件同步到服务器方法2

[root@localhost ~]# rsync -avz /client/rsync/* rsync://user1@192.168.115.120/share

Password:

sending incremental file list

 

sent 40 bytes  received 8 bytes  13.71 bytes/sec

total size is 130  speedup is 2.71

上面没有同步是因为服务器有该文件,下面我们把服务器端的文件删除发现就会同步成功。

[root@localhost ~]# rsync -avz /client/rsync/* rsync://user1@192.168.115.120/share

Password:

sending incremental file list

1.txt

2.txt

 

sent 138 bytes  received 46 bytes  52.57 bytes/sec

total size is 130  speedup is 0.71

 

rsync相关参数说明:

-a   参数,相当于-rlptgoD

-r   是递归

-l   是链接文件,意思是拷贝链接文件

-i   列出 rsync 服务器中的文件

-p  表示保持文件原有权限

-t   保持文件原有时间

-g  保持文件原有用户组

-o  保持文件原有属主

-D  相当于块设备文件

-z  传输时压缩

-P  传输进度

-v  传输时的进度等信息,和-P有点关系

 

3)让客户端与服务器文件保持完全一致

[root@localhost rsync]# rsync -avz --delete user1@192.168.115.120::share /client/rsync/

 

通过调用密码文件让客户端与服务器文件保持完全一致

[root@localhost ~]# echo "123"> /tmp/rsync.password

[root@localhost ~]# chmod 600 /tmp/rsync.password

[root@localhost ~]# rsync -avz --delete --password-file=/tmp/rsync.password user1@192.168.115.120::share /client/rsync/

 

通过定时任务让客户端自动同步

[root@localhost ~]# crontab -e

10 0 * * * rsync -avz --delete --password-file=/tmp/rsync.password user1@192.168.115.120::share /client/rsync/

[root@localhost ~]# crontab -l

 

8、免密码验证

1)在客户端生成密钥对并把公钥文件上传到服务器

[root@localhost ~]# ssh-keygen -t rsa

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

9b:da:52:44:d3:10:26:d8:55:98:4c:30:d3:72:ff:ec root@localhost.localdomain

The key's randomart image is:

+--[ RSA 2048]----+

|     o=**B.      |

|    . o=O .      |

|       + o       |

|        . .      |

|       .S  o     |

|        .o  o    |

|       .o  .     |

|      .o    E    |

|      ...        |

+-----------------+

[root@localhost ~]# ssh-copy-id root@192.168.115.120

/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys

root@192.168.115.120's password:

 

Number of key(s) added: 1

 

Now try logging into the machine, with:   "ssh 'root@192.168.115.120'"

and check to make sure that only the key(s) you wanted were added.

 

2)验证从服务器下载文件到本地是否需要密码

[root@localhost ~]# rsync -avz root@192.168.115.120:/server/rsync /client/rsync/

receiving incremental file list

rsync/

rsync/1.txt

rsync/2.txt

 

sent 53 bytes  received 179 bytes  464.00 bytes/sec

total size is 130  speedup is 0.56

可以看到,免密码验证已经成功了,从服务器下载文件到本地已经不需要密码了。