8.Rsync

1.rsync Service Introduction

1.1rsync Profile

属于一款实现全量及增量复制同步的软件工具

1.2rsync seven characteristics

支持拷贝普通文件与特殊文件如链接文件,设备等
支持排除指定文件或目录同步的功能,类似tar命令排除功能。
支持保持原文件或目录的所有属性信息不变
支持增量同步,既只同步变化数据,提升数据传输效率
支持使用rcp,rsh,ssh等方式来配合进行隧道加密传输文件
支持使用通过socket(守护进程方式)传输文件或目录数据信息
支持用户认证方式传输数据,提升数据同步安全性

1.3Rsync synchronous data replication principle

在同步备份数据时,默认情况下,Rsync通过其独特的“quick check”算法。
它仅同步大小或者最后修改时间发生变化的文件或目录。
当然也可根据权限,属主等属性的变化同步,但需要指定相应的参数。
甚至可以实现只同步一个文件里有变化的内容部分。
所以可以实现快速的同步备份数据,即采用增量复制方法对数据信息进行同步。
与传统cp,scp拷贝工具的全量拷贝复制截然不同,增量同步复制数据,在效率上远远高于全量复制。

Simple application of 2.rsync

2.1rsync == cp

[root@backup ~]# # rsync == cp效果
[root@backup ~]# cp -a /etc/hosts /tmp/
[root@backup ~]# ll /tmp/
total 4
-rw-r--r--. 1 root root 352 Jan 27 01:15 hosts
[root@backup ~]# rsync -a /etc/sysconfig/network /tmp/
[root@backup ~]# ll /tmp/
total 8
-rw-r--r--. 1 root root 352 Jan 27 01:15 hosts
-rw-r--r--  1 root root  31 Jan 26 18:16 network

2.2rsync == scp

说明:同步数据时,/tmp/目录后有/信息,表示将目录下面的数据内容进行备份同步
同步数据时,/tmp目录后没有/信息,表示将目录及目录下面的数据内容进行备份同步
[root@backup ~]# scp -rp /tmp/ 172.16.1.31:/tmp/
The authenticity of host '172.16.1.31 (172.16.1.31)' can't be established.
RSA key fingerprint is 5b:9b:e6:79:a9:95:4f:be:06:41:e3:bb:7a:12:ee:b4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.1.31' (RSA) to the list of known hosts.
[email protected]'s password: 
network                                                                                                                              100%   31     0.0KB/s   00:00    
hosts                                                                                                                                100%  352     0.3KB/s   00:00    
[root@backup ~]# ll /tmp/
total 8
-rw-r--r--. 1 root root 352 Jan 27 01:15 hosts
-rw-r--r--  1 root root  31 Jan 26 18:16 network
[root@backup ~]#  rsync -rp /tmp/ 172.16.1.31:/tmp/
[email protected]'s password: 
[root@backup ~]# 

2.3rsync == rm

说明:rsync实现删除目录中数据内容过程,就将一个空目录和一个有数据的目录进行同步
      最终,会将有数据的目录中的文件进行清空
[root@backup ~]# mkdir /null
[root@backup ~]# rsync --delete /null/ /tmp/
rsync: --delete does not work without -r or -d.
rsync error: syntax or usage error (code 1) at main.c(1422) [client=3.0.6]
[root@backup ~]# 
[root@backup ~]# rsync -r --delete /null/ /tmp/
[root@backup ~]# ll /tmp/
total 0

2.4rsync == ls

[root@backup ~]# ls /etc/hosts
/etc/hosts
[root@backup ~]# ls -l /etc/hosts
-rw-r--r--. 2 root root 352 Jan 27 01:15 /etc/hosts
[root@backup ~]# rsync /etc/hosts
-rw-r--r--         352 2018/01/27 01:15:59 hosts

3.rsync works

3.1 local data backup

Local:  rsync [OPTION...] SRC... [DEST]
rsync    --- 数据备份传输命令
option   --- 可以输入一下和rsync传输数据有关的参数
src      --- 要进行备份的数据(文件/目录)
dest     --- 将数据信息备份到什么位置(相应路径中)

实践练习:
[root@backup ~]# rsync -a /etc/hosts /tmp/ok.txt
[root@backup ~]# ll /tmp/ok.txt 
-rw-r--r-- 1 root root 352 Jan 27 01:15 /tmp/ok.txt

3.2 remote data backup

Access via remote shell:
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST:DEST

pull方式语法说明:
rsync    --- 数据备份传输命令
option   --- 可以输入一下和rsync传输数据有关的参数
[USER@]HOST:     --- 需要指定以什么用户身份登录到远程主机,
                     如果省略USER信息,表示以当前用户身份进行登录
                    登录主机地址或域名信息
SRC      --- 指定远程主机要传输过来到本地的数据信息
dest     --- 将数据保存到本地的什么路径中

push方式语法说明:
rsync    --- 数据备份传输命令
option   --- 可以输入一下和rsync传输数据有关的参数
[USER@]HOST:     --- 需要指定以什么用户身份登录到远程主机,
                     如果省略USER信息,表示以当前用户身份进行登录
                    登录主机地址或域名信息
SRC      --- 指定本地主机要传输到远程主机的数据
dest     --- 将本地数据保存到远端的什么路径中

[root@backup oldboy]# rsync -r  [email protected]:/oldboy/ /oldboy/
[email protected] s password: 
[root@backup oldboy]# ll
total 20
-rw-r--r-- 1 root root  352 Mar 22  2019 hosts
-rw-r--r-- 1 root root   26 Mar  7 17:10 test.data
-rw-r--r-- 1 root root   26 Mar  7 17:10 test.data.bak
-rw-r--r-- 1 root root   20 Mar  7 17:10 test.log
drwxr-xr-x 3 root root 4096 Mar  7 17:10 tmp

[root@backup oldboy]# rsync -r /oldboy/tmp [email protected]:/oldboy/
[email protected] s password: 

3.3 daemon Transfer Mode

Access via rsync daemon:
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
      rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
      rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST

pull:rsync [OPTION...] [USER@]HOST::SRC... [DEST]
[USER@]HOST::       --- 指定远程连接的认证用户
SRC                 --- 指定相应的模块信息
[DEST]              --- 将远程数据保存到本地的路径信息

Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
[USER@]HOST::       --- 指定远程连接的认证用户
SRC                 --- 指定本地要进行推送的数据信息
[DEST]              --- 远程进行保存数据的模块信息

4.rsync daemon deployment process

4.1 server deployment process

第一里程:检查软件是否安装
[root@backup ~]# rpm -qa rsync
rsync-3.0.6-12.el6.x86_64

第二里程:编写配置文件
[root@backup ~]# vim /etc/rsyncd.conf
#rsync_config
#created by HQ at 2017
##rsyncd.conf start##

uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 172.16.1.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
[backup]
comment = "backup dir by oldboy"
path = /backup
read only = false
[nfs]
comment = "backup dir by oldboy"
path = /nfs

注意:
read only = true的时候,客户端是不能把文件同步到服务端的
[root@nfs01 ~]# rsync -avz /etc/hosts [email protected]::backup --password-file=/etc/rsync.password 
sending incremental file list
ERROR: module is read only
rsync error: syntax or usage error (code 1) at main.c(866) [receiver=3.0.6]
rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(759) [sender=3.0.6]

模块中的参数只对单个模块生效,外面的参数对整个环境有效

第三个里程:创建备份目录管理用户
[root@backup ~]# useradd rsync -M -s /sbin/nologin

第四个里程:创建备份目录
[root@backup ~]# mkdir /backup
[root@backup ~]# chown -R rsync.rsync /backup

第五个里程:创建认证文件
[root@backup ~]# echo "rsync_backup:oldboy123"  >>/etc/rsync.password
[root@backup ~]# chmod 600 /etc/rsync.password

第六个里程:启动rsync服务
[root@backup ~]# rsync --daemon 
[root@backup ~]# ps -ef|grep rsync
root       2832      1  0 21:13 ?        00:00:00 rsync --daemon
root       2834   2759  0 21:13 pts/0    00:00:00 grep rsync
扩展说明:rsync启动服务扩展参数
--port         指定rsync服务端口号信息,默认是873
--config=/etc/rsyncd.conf   指定识别的rsync服务配置文件信息

4.2 client deployment process

第一个里程:确认软件是否安装
[root@nfs01 tmp]#rpm -qa rsync
rsync-3.0.6-12.el6.x86_64

第二个里程:创建认证密码文件
[root@nfs01 tmp]#echo "oldboy123" >>/etc/rsync.password
[root@nfs01 tmp]#chmod 600 /etc/rsync.password

第三个里程:进行数据备份测试/etc/hosts
[email protected]::backup其中backup为/etc/rsyncd.conf中的模块名

[root@nfs01 tmp]# rsync -avz /etc/hosts [email protected]::backup --password-file=/etc/rsync.password 
sending incremental file list
hosts

sent 189 bytes  received 27 bytes  432.00 bytes/sec
total size is 352  speedup is 1.63

8.Rsync

5.rsync parameters introduced

8.Rsync
8.Rsync

6. server configuration Parameter Description

8.Rsync
8.Rsync

7. Common error

7.1rsync server open iptables firewall

【客户端的错误】
   No route to host
  【错误演示过程】
   [root@nfs01 tmp]# rsync -avz /etc/hosts [email protected]::backup
   rsync: failed to connect to 172.16.1.41: No route to host (113)
   rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6]
  【异常问题解决】
   关闭rsync服务端的防火墙服务(iptables)
   [root@backup mnt]# /etc/init.d/iptables stop
   iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
   iptables: Flushing firewall rules:                         [  OK  ]
   iptables: Unloading modules:                               [  OK  ]
   [root@backup mnt]# /etc/init.d/iptables status
   iptables: Firewall is not running.

7.2rsync client performs rsync command error

【客户端的错误】
   The remote path must start with a module name not a / 
  【错误演示过程】
   [root@nfs01 tmp]# rsync -avz /etc/hosts [email protected]::/backup
   ERROR: The remote path must start with a module name not a /
   rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
  【异常问题解决】
   rsync命令语法理解错误,::/backup是错误的语法,应该为::backup(rsync模块)

7.3rsync service user authentication failed *****

 【客户端的错误】
   auth failed on module oldboy
  【错误演示过程】
   [root@nfs01 tmp]# rsync -avz /etc/hosts [email protected]::backup
   Password: 
   @ERROR: auth failed on module backup
   rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
  【异常问题解决】
   1. 密码真的输入错误,用户名真的错误
   2. secrets file = /etc/rsync.password指定的密码文件和实际密码文件名称不一致
   3. /etc/rsync.password文件权限不是600
   4. rsync_backup:123456密码配置文件后面注意不要有空格
   5. rsync客户端密码文件中只输入密码信息即可,不要输入虚拟认证用户名称

7.4rsync Service Location module error

【客户端的错误】
   Unknown module 'backup'   
  【错误演示过程】  
   [root@nfs01 tmp]# rsync -avz /etc/hosts [email protected]::backup
   @ERROR: Unknown module 'backup'
   rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
  【异常问题解决】
   1. /etc/rsyncd.conf配置文件模块名称书写错误

7.5rsync service permissions prevent problems

【客户端的错误】
   Permission denied
  【错误演示过程】 
   [root@nfs01 tmp]# rsync -avz /etc/hosts [email protected]::backup
   Password: 
   sending incremental file list
   hosts
   rsync: mkstemp ".hosts.5z3AOA" (in backup) failed: Permission denied (13) 
   sent 196 bytes  received 27 bytes  63.71 bytes/sec
   total size is 349  speedup is 1.57
   rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]   
  【异常问题解决】
   1. 备份目录的属主和属组不正确,不是rsync
   2. 备份目录的权限不正确,不是755

7.6rsync abnormal backup directory service

【客户端的错误】
   chdir failed   
  【错误演示过程】   
   [root@nfs01 tmp]# rsync -avz /etc/hosts [email protected]::backup
   Password: 
   @ERROR: chdir failed
   rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
  【异常问题解决】  
   1. 备份存储目录没有建立
   2. 建立的备份存储目录和配置文件定义不一致
   说明:如果没有备份存储目录

7.7rsync service invalid user information

【客户端的错误】
   invalid uid rsync
  【错误演示过程】    
   [root@nfs01 tmp]# rsync -avz /etc/hosts [email protected]::backup
   Password: 
   @ERROR: invalid uid rsync
   rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
  【异常问题解决】  
   rsync服务对应rsync虚拟用户不存在了

7.8 The client has been configured password file, but avoid secret key login, still you need to enter a password

【客户端的错误】
   password file must not be other-accessible
  【错误演示过程】 
   [root@nfs01 tmp]# rsync -avz /etc/hosts [email protected]::backup --password-file=/etc/rsync.password
   password file must not be other-accessible
   continuing without password file
   Password: 
   sending incremental file list
   sent 26 bytes  received 8 bytes  5.23 bytes/sec
   total size is 349  speedup is 10.26
  【异常问题解决】  
   rsync客户端的秘钥文件也必须是600权限

7.9rsync slow client connection issues

```IP === 域名 反向DNS解析
【错误日志信息】
错误日志输出
2017/03/08 20:14:43 [3422] params.c:Parameter() - Ignoring badly formed line in configuration file: ignore errors
2017/03/08 20:14:43 [3422] name lookup failed for 172.16.1.31: Name or service not known
2017/03/08 20:14:43 [3422] connect from UNKNOWN (172.16.1.31)
2017/03/08 20:14:43 [3422] rsync to backup/ from rsync_backup@unknown (172.16.1.31)
2017/03/08 20:14:43 [3422] receiving file list
2017/03/08 20:14:43 [3422] sent 76 bytes received 83 bytes total size 349
正确日志输出
2017/03/08 20:16:45 [3443] params.c:Parameter() - Ignoring badly formed line in configuration file: ignore errors
2017/03/08 20:16:45 [3443] connect from nfs02 (172.16.1.31)
2017/03/08 20:16:45 [3443] rsync to backup/ from rsync_backup@nfs02 (172.16.1.31)
2017/03/08 20:16:45 [3443] receiving file list
2017/03/08 20:16:45 [3443] sent 76 bytes received 83 bytes total size 349
【异常问题解决】
查看日志进行分析,编写rsync服务端hosts解析文件
IP === 域名 反向DNS解析
【错误日志信息】
错误日志输出
2017/03/08 20:14:43 [3422] params.c:Parameter() - Ignoring badly formed line in configuration file: ignore errors
2017/03/08 20:14:43 [3422] name lookup failed for 172.16.1.31: Name or service not known
2017/03/08 20:14:43 [3422] connect from UNKNOWN (172.16.1.31)
2017/03/08 20:14:43 [3422] rsync to backup/ from rsync_backup@unknown (172.16.1.31)
2017/03/08 20:14:43 [3422] receiving file list
2017/03/08 20:14:43 [3422] sent 76 bytes received 83 bytes total size 349
正确日志输出
2017/03/08 20:16:45 [3443] params.c:Parameter() - Ignoring badly formed line in configuration file: ignore errors
2017/03/08 20:16:45 [3443] connect from nfs02 (172.16.1.31)
2017/03/08 20:16:45 [3443] rsync to backup/ from rsync_backup@nfs02 (172.16.1.31)
2017/03/08 20:16:45 [3443] receiving file list
2017/03/08 20:16:45 [3443] sent 76 bytes received 83 bytes total size 349
【异常问题解决】
查看日志进行分析,编写rsync服务端hosts解析文件

## 7.10rsync服务没有正确启动

【错误日志信息】
Connection refused (111)
【错误演示过程】
[root@oldboy-muban ~]# rsync -avz /etc/hosts [email protected]::backup
rsync: failed to connect to 172.16.1.41: Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6]
【异常问题解决】
[root@oldboy-muban ~]# rsync --daemon
[root@oldboy-muban ~]# ss -lntup |grep rsync
tcp LISTEN 0 5 :::873 ::: users:(("rsync",1434,5))
tcp LISTEN 0 5
:873 : users:(("rsync",1434,4))
[root@oldboy-muban ~]# rsync -avz /etc/hosts [email protected]::backup
Password:
sending incremental file list
hosts
sent 196 bytes received 27 bytes 49.56 bytes/sec
total size is 349 speedup is 1.57

# 8.扩展功能
## 8.1守护进程多模块功能配置

第一步:修改配置文件
vim /etc/rsyncd.conf
[backup01]
comment = "backup dir by oldboy"
path = /backup
[backup02]
comment = "backup dir by oldboy"
path = /backup02

第二步:创建多模块目录
mkdir /backup02
chown -R rsync.rsync /backup02

第三步:重启服务程序
killall rsync
rsync --daemon

第四步:进行测试检查

## 8.2守护进程的排除功能实践

[root@nfs01 ~]# mkdir /test_dir
[root@nfs01 ~]# cd /test_dir/
[root@nfs01 test_dir]# touch {a..d}
[root@nfs01 test_dir]# ll
total 0
-rw-r--r-- 1 root root 0 Mar 7 23:07 a
-rw-r--r-- 1 root root 0 Mar 7 23:07 b
-rw-r--r-- 1 root root 0 Mar 7 23:07 c
-rw-r--r-- 1 root root 0 Mar 7 23:07 d
第一种数据备份排除方式:--exclude
rsync -avz /test_dir/ --exclude=b --exclude=d [email protected]::backup01 --password-file=/etc/rsync.password
rsync -avz /test_dir/ --exclude={b,d} [email protected]::backup01 --password-file=/etc/rsync.password
rsync -avz /test_dir/ --exclude={b..d} [email protected]::backup01 --password-file=/etc/rsync.password
第二种数据备份排除方式:--exclude-from=file
rsync -avz /test_dir/ --exclude-from=./exclude_file.txt [email protected]::backup01 --password-file=/etc/rsync.password
cat exclude_file.txt
a
b

注意:排除的文件名书写的是相对路径

## 8.3守护进程来创建备份目录

客户端同步命令
[root@nfs01 test_dir]# rsync -avz /etc/hosts --exclude-from=./exclude_file.txt [email protected]::backup01/sa/ --password-file=/etc/rsync.password
[root@nfs01 test_dir]# rsync -avz /etc/hosts --exclude-from=./exclude_file.txt [email protected]::backup01/dev/ --password-file=/etc/rsync.password
[root@nfs01 test_dir]# rsync -avz /etc/hosts --exclude-from=./exclude_file.txt [email protected]::backup01/dba/ --password-file=/etc/rsync.password
服务端查看
[root@backup backup01]# ll
total 12
drwxr-xr-x 2 rsync rsync 4096 Mar 7 23:09 dev
drwxr-xr-x 2 rsync rsync 4096 Mar 7 23:09 sa
[root@backup backup01]# cd sa/
[root@backup sa]# ll
total 4
-rw-r--r-- 1 rsync rsync 0 Mar 7 23:07 b
-rw-r--r-- 1 rsync rsync 0 Mar 7 23:07 c
-rw-r--r-- 1 rsync rsync 0 Mar 7 23:07 d
-rw-r--r-- 1 rsync rsync 3 Mar 7 23:09 exclude_file.txt

## 8.4守护进程的访问控制配置

三种情况:

  1. 只有白名单,白名单网段或主机信息允许。其余阻止
  2. 只有黑名单,黑名单网段或主机信息阻止,其余允许
  3. 有黑名单也要白名单,白名单网段或主机信息允许,黑名单网段或主机信息阻止,其余允许
    建议只选择前两种方式配置
    hosts allow = 172.16.1.0/24
    hosts deny = 0.0.0./32
    ![](https://s1.51cto.com/images/blog/201908/01/a67d53ad6c67021d3202749dea8e4989.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
    ## 8.5守护进程无差异同步配置

    我有的,你也有;我没有的,你也不能有
    rsync -avz /test_dir/ --delete [email protected]::backup01 --password-file=/etc/rsync.password
    说明:一定要谨慎使用,否则可能会清空备份目录;
    如果要快速清空目录数据,也可以使用无差异同步清空

    ## 8.6守护进程的列表功能配置

    list = true
    : Indicates whether to display a list of all modules rsync server information
    [root @ nfs01 test_dir] # rsync [email protected] ::
    backup01 "Backup dir by Oldboy"
    backup02 "Backup dir by Oldboy"
    is false does not display
    [ root @ nfs01 test_dir] # rsync [email protected] ::

Guess you like

Origin blog.51cto.com/10983441/2425585