linux服务五——rsync 服务部署详解

第1章 rsync 软件介绍

1.1 什么是rsync

rsync 是一款开源的、快速的、多功能的、可实现全量及增量的本地或远程数据同步备份的优秀工具。

1.1.1 全量及增量

全量:将全部数据,进行传输覆盖

增量:只传输差异部分的数据

1.2 实现增量复制的原理

Rsync通过其独特的“quick check”算法,实现增量传输数据

[root@iso-all ~]# man rsync

在同步备份数据时,默认情况下,Rsync通过其独特的“quick check”算法,它仅同步大小或者最后修改时间发生变化的文件或目录,当然也可根据权限,属主等属性的变化同步,但需要指定相应的参数,甚至可以实现只同步一个文件里有变化的内容部分,所以,可以实现快速的同步备份数据。

1.2.1 软件版本

[root@iso-all ~]# rsync --version
rsync  version 3.1.2  protocol version 31
Copyright (C) 1996-2015 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/
Capabilities:
    64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
    socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
    append, ACLs, xattrs, iconv, symtimes, prealloc

rsync comes with ABSOLUTELY NO WARRANTY.  This is free software, and you
are welcome to redistribute it under certain conditions.  See the GNU
General Public Licence for details.

1.3 rsync 软件功能介绍

    类似于 cp 命令 -- 实现本地备份传输数据
    类似于scp 命令 -- 远程备份传输数据
    类似于 rm 命令 -- 实现无差异同步备份
    类似于 ls 命令 -- 本地文件信息查看

1.3.1 rsync == cp

[root@iso-all test]# cp -a 1.txt /tmp
[root@iso-all test]# ls /tmp 
1.txt  a-test.sh  b-test.sh  c-test.sh  test.sh
[root@iso-all test]#  rm /tmp/1.txt  -f
[root@iso-all test]# ls /tmp 
a-test.sh  b-test.sh  c-test.sh  test.sh
[root@iso-all test]# rsync 1.txt /tmp
[root@iso-all test]# ls /tmp 
1.txt  a-test.sh  b-test.sh  c-test.sh  test.sh

1.3.2 rsync == scp

#使用scp实现

#检查对端服务器目标位置上是否有该文件

[root@10e0e0e17 ~]# ls -l
total 4
-rw-------. 1 root root 1259 Mar 26  2019 anaconda-ks.cfg

#从本地拷贝到远端服务器上

[root@iso-all ~]# scp -rp  test 10.0.0.17:/root/
1.txt                                                                                                                                    100%    0     0.0KB/s   00:00    
2.txt                                                                                                                                    100%    0     0.0KB/s   00:00    
3.txt                                                                                                                                    100%    0     0.0KB/s   00:00    
4.txt                                                                                                                                    100%    0     0.0KB/s   00:00    
5.txt                                                                                                                                    100%    0     0.0KB/s   00:00    
6.txt                                                                                                                                    100%    0     0.0KB/s   00:00    
7.txt                                                                                                                                    100%    0     0.0KB/s   00:00    
8.txt                                                                                                                                    100%    0     0.0KB/s   00:00    
9.txt                                                                                                                                    100%    0     0.0KB/s   00:00    
10.txt                                                                                                                                   100%    0     0.0KB/s   00:00    
[root@iso-all ~]# 

#检查远端服务器上的结果

[root@10e0e0e17 ~]# ls -l
total 4
-rw-------. 1 root root 1259 Mar 26  2019 anaconda-ks.cfg
[root@10e0e0e17 ~]# ls -l
total 8
-rw-------.  1 root root 1259 Mar 26  2019 anaconda-ks.cfg
drwxr-xr-x. 19 root root 4096 Sep 24 06:12 test

#使用rsync 实现

[root@iso-all ~]# rsync  -rp  test 10.0.0.17:/tmp/
[root@iso-all ~]# 

#检查远端服务器上的结果

[root@10e0e0e17 ~]# ls /tmp
test
[root@10e0e0e17 ~]# 

1.3.3 rsync == rm

环境准备

[root@iso-all test]# rm -rf {a..d}
[root@iso-all test]# 
[root@iso-all test]# rm -rf {1..5}.txt
[root@iso-all test]# 
[root@iso-all test]# 
[root@iso-all test]# ls
10.txt  6.txt  7.txt  8.txt  9.txt  [a..p]  e  f  g  h  i  j  k  l  m  n  o  p

与远端进行同步

[root@iso-all ~]# rsync -a --delete test/ 10.0.0.17:/tmp/test/

查看远端服务器目录

[root@10e0e0e17 ~]# ls /tmp/test/
10.txt  6.txt  7.txt  8.txt  9.txt  [a..p]  e  f  g  h  i  j  k  l  m  n  o  p

1.3.4 rsync == ls -l

使用rsync 可以实现与 ls 类似的功能

[root@iso-all ~]# ls -l test
总用量 0
-rw-r--r-- 1 root root 0 9月  24 06:12 10.txt
-rw-r--r-- 1 root root 0 9月  24 06:12 6.txt
-rw-r--r-- 1 root root 0 9月  24 06:12 7.txt
-rw-r--r-- 1 root root 0 9月  24 06:12 8.txt
-rw-r--r-- 1 root root 0 9月  24 06:12 9.txt
drwxr-xr-x 2 root root 6 9月  24 06:11 [a..p]
drwxr-xr-x 2 root root 6 9月  24 06:11 e
drwxr-xr-x 2 root root 6 9月  24 06:11 f
drwxr-xr-x 2 root root 6 9月  24 06:11 g
drwxr-xr-x 2 root root 6 9月  24 06:11 h
drwxr-xr-x 2 root root 6 9月  24 06:11 i
drwxr-xr-x 2 root root 6 9月  24 06:11 j
drwxr-xr-x 2 root root 6 9月  24 06:11 k
drwxr-xr-x 2 root root 6 9月  24 06:11 l
drwxr-xr-x 2 root root 6 9月  24 06:11 m
drwxr-xr-x 2 root root 6 9月  24 06:11 n
drwxr-xr-x 2 root root 6 9月  24 06:11 o
drwxr-xr-x 2 root root 6 9月  24 06:11 p
[root@iso-all ~]# rsync test/
drwxr-xr-x            194 2020/09/24 06:29:44 .
-rw-r--r--              0 2020/09/24 06:12:12 10.txt
-rw-r--r--              0 2020/09/24 06:12:12 6.txt
-rw-r--r--              0 2020/09/24 06:12:12 7.txt
-rw-r--r--              0 2020/09/24 06:12:12 8.txt
-rw-r--r--              0 2020/09/24 06:12:12 9.txt
drwxr-xr-x              6 2020/09/24 06:11:29 [a..p]
drwxr-xr-x              6 2020/09/24 06:11:44 e
drwxr-xr-x              6 2020/09/24 06:11:44 f
drwxr-xr-x              6 2020/09/24 06:11:44 g
drwxr-xr-x              6 2020/09/24 06:11:44 h
drwxr-xr-x              6 2020/09/24 06:11:44 i
drwxr-xr-x              6 2020/09/24 06:11:44 j
drwxr-xr-x              6 2020/09/24 06:11:44 k
drwxr-xr-x              6 2020/09/24 06:11:44 l
drwxr-xr-x              6 2020/09/24 06:11:44 m
drwxr-xr-x              6 2020/09/24 06:11:44 n
drwxr-xr-x              6 2020/09/24 06:11:44 o
drwxr-xr-x              6 2020/09/24 06:11:44 p

1.4 Rsync的特性总结(7个特性信息说明

01. 支持拷贝普通文件与特殊文件如链接文件,设备等。
02. 可以有排除指定文件或目录同步的功能,相当于打包命令tar的排除功能。
    #tar zcvf backup_1.tar.gz  /opt/data  -exclude=clsn   
    说明:在打包/opt/data时就排除了clsn命名的目录和文件。
03. 可以做到保持原文件或目录的权限、时间、软硬链接、属主、组等所有属性均不改变-p。
04. 可实现增量同步,既只同步发生变化的数据,因此数据传输效率很高(tar -N)。
    # 将备份/home 目录自 2008-01-29 以来修改过的文件
    # tar -N 2008-01-29 -zcvf /backups/inc-backup_$(date +%F).tar.gz /home
    # 将备份 /home 目录昨天以来修改过的文件
    # tar -N $(date -d yesterday "+%F") -zcvf /backups/inc-backup_$(date +%F).tar.gz /home
    # 添加文件到已经打包的文件
    # tar -rf all.tar *.gif
    说明:这条命令是将所有.gif的文件增加到all.tar的包里面去。-r是表示增加文件的意思。
05. 可以使用rcp,rsh,ssh等方式来配合进行隧道加密传输文件(rsync本身不对数据加密)
06. 可以通过socket(进程方式)传输文件和数据(服务端和客户端)*****。重点掌握
07. 支持匿名的或认证(无需系统用户)的进程模式传输,可实现方便安全的进行数据备份及镜像。

 1.5 Rsync的企业工作场景说明

    01. 两台服务器之间数据同步(定时任务cron+rsync)
        同步网站内部人员数据信息(定时任务最小周期为1分钟)
    02. 两台服务器之间数据同步(实时任务inotify/sersync/lrsyncd+rsync)
        同步网站用户人员数据信息

第2章 rsync使用方式

2.1 rsync软件工作方式

SYNOPSIS
        本地数据同步方式
       Local:  rsync [OPTION...] SRC... [DEST]
        远程数据同步方式
       Access via remote shell:
         Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
         Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
        守护进程方式同步数据
       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

2.1.1 本地数据同步方式(类似于cp

Local:  rsync [OPTION...] SRC... [DEST]

参数

含义

rsync       

数据同步命令

[OPTION...] 

rsync命令参数信息

SRC         

要同不得数据信息(文件或目录)

[DEST]      

将数据传输到什么位置

 实例演示命令:

[root@iso-all test]# cp -a 1.txt /tmp
[root@iso-all test]# ls /tmp 
1.txt  a-test.sh  b-test.sh  c-test.sh  test.sh
[root@iso-all test]#  rm /tmp/1.txt  -f
[root@iso-all test]# ls /tmp 
a-test.sh  b-test.sh  c-test.sh  test.sh
[root@iso-all test]# rsync 1.txt /tmp
[root@iso-all test]# ls /tmp 
1.txt  a-test.sh  b-test.sh  c-test.sh  test.sh

2.1.2 远程数据同步方式(类似scp)---又称为隧道传输

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

说明:需要进行交互传输数据。如果想实现免交互传输数据,需要借助ssh+key方式实现

pull拉:

[USER@]

以什么用户身份传输数据信息

HOST:    

远程主机信息(IP地址信息 主机名称信息)

SRC:     

远端要恏过来的数据信息

[dest]   

恏到本地什么位置

push:推:

SRC:    

本地要怼过去的数据信息

DEST     

怼到远端什么位置

2.1.3 【实践操作】pull

 从远端拉文件到当前目录

[root@iso-all test]# rm yhh.txt 
rm:是否删除普通文件 "yhh.txt"?y
[root@iso-all test]# 
[root@iso-all test]# 
[root@iso-all test]# 
[root@iso-all test]# rsync 10.0.0.17:/root/yhh.txt .
[root@iso-all test]# ls
10.txt  6.txt  7.txt  8.txt  9.txt  [a..p]  e  f  g  h  i  j  k  l  m  n  o  p  yhh.txt

2.1.4 【实践操作】push (目录)

使用push的格式 推整个目录(包括目录

[root@iso-all ~]# rsync -a /etc 10.0.0.17:/tmp/
[root@iso-all ~]# 

远端查看结果

[root@10e0e0e17 tmp]# ls
etc

推整个目录下的文件(不包括目录本身)

[root@iso-all ~]# rsync -a /etc/ 10.0.0.17:/tmp/
[root@iso-all ~]# 

远端查看

[root@10e0e0e17 tmp]# ls
adjtime                  csh.login                gnupg        krb5.conf                 mtab               profile             rwtab.d         sysctl.d
aliases                  dbus-1                   GREP_COLORS  krb5.conf.d               mtools.conf        profile.d           sasl2           systemd
aliases.db               default                  groff        ld.so.cache               my.cnf             protocols           securetty       system-release
alternatives             depmod.d                 group        ld.so.conf                my.cnf.d           python              security        system-release-cpe
anacrontab               dhcp                     group-       ld.so.conf.d              NetworkManager     rc0.d               selinux         tcsd.conf
asound.conf              DIR_COLORS               grub2.cfg    libaudit.conf             networks           rc1.d               services        terminfo
audisp                   DIR_COLORS.256color      grub.d       libnl                     nsswitch.conf      rc2.d               sestatus.conf   tmpfiles.d
audit                    DIR_COLORS.lightbgcolor  gshadow      libuser.conf              nsswitch.conf.bak  rc3.d               shadow          tuned
bash_completion.d        dracut.conf              gshadow-     locale.conf               openldap           rc4.d               shadow-         udev
bashrc                   dracut.conf.d            gss          localtime                 opt                rc5.d               shells          vconsole.conf
binfmt.d                 e2fsck.conf              host.conf    login.defs                os-release         rc6.d               skel            vimrc
centos-release           environment              hostname     logrotate.conf            pam.d              rc.d                ssh             virc
centos-release-upstream  ethertypes               hosts        logrotate.d               passwd             rc.local            ssl             vsftpd
chkconfig.d              exports                  hosts.allow  lvm                       passwd-            redhat-release      statetab        wgetrc
cobbler                  favicon.png              hosts.deny   machine-id                pkcs11             resolv.conf         statetab.d      wpa_supplicant
cron.d                   filesystems              httpd        magic                     pki                resolv.conf.GMZVA0  subgid          X11
cron.daily               firewalld                init.d       mailcap                   plymouth           resolv.conf.save    subuid          xdg
cron.deny                fonts                    inittab      makedumpfile.conf.sample  pm                 rpc                 subversion      xinetd.conf
cron.hourly              fstab                    inputrc      man_db.conf               polkit-1           rpm                 sudo.conf       xinetd.d
cron.monthly             gcrypt                   iproute2     mime.types                popt.d             rsyncd.conf         sudoers         yum
crontab                  gdbinit                  issue        mke2fs.conf               postfix            rsync.password      sudoers.d       yum.conf
cron.weekly              gdbinit.d                issue.net    modprobe.d                ppp                rsyslog.conf        sudo-ldap.conf  yum.repos.d
crypttab                 GeoIP.conf               kdump.conf   modules-load.d            prelink.conf.d     rsyslog.d           sysconfig
csh.cshrc                GeoIP.conf.default       kernel       motd                      printcap           rwtab               sysctl.conf

说明:

    /tmp   --表示将tmp目录本身及目录下的内容进行传输
    /tmp/  --表示只传输tmp目录下面的内容信息

2.2 守护进程方式同步数据

2.2.1 配置rsync守护进程方式(需要有服务端与客户端)

规划:

       1、iso-all服务器作为rsync服务端
       2、以rysnc客户端作为参照物,将数据推到rsync服务器上

2.2.2 配置rsync服务端(将服务端配置到 iso-all服务器上)

第一个里程碑: 软件是否存在

[root@iso-all ~]#  rpm -qa |grep rsync
rsync-3.1.2-10.el7.x86_64

第二个里程碑: 进行软件服务配置

[root@iso-all ~]# vim /etc/rsyncd.conf

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 = 10.0.0.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
[backup]
comment = "backup file by yhh.txt"
path = /test

第三个里程:创建rsync用户

[root@iso-all ~]# id rsync
id: rsync: no such user
[root@iso-all ~]# useradd -s /sbin/nologin -M rsync
[root@iso-all ~]# id rsync
uid=1003(rsync) gid=1003(rsync) 组=1003(rsync)

第四个里程碑: 创建数据备份储存目录,目录修改属主

[root@iso-all ~]# chown -R rsync.rsync test
[root@iso-all ~]# 

第五个里程碑: 创建认证用户密码文件

[root@iso-all ~]# echo "rsync_backup:123456" >>/etc/rsync.password
[root@iso-all ~]# chmod 600 /etc/rsync.password
[root@iso-all ~]# 

第六个里程碑: 启动rsync

[root@iso-all ~]# rsync --daemon

至此服务端配置完成

[root@iso-all ~]# ps -ef |grep rsync
root       7114      1  0 21:59 ?        00:00:00 rsync --daemon
root       7117   7033  0 22:00 pts/0    00:00:00 grep --color=auto rsync
[root@iso-all ~]# ss -lntp|grep rsync
LISTEN     0      5            *:873                      *:*                   users:(("rsync",pid=7114,fd=3))
LISTEN     0      5           :::873                     :::*                   users:(("rsync",pid=7114,fd=5))
[root@iso-all ~]# 

 2.2.3 配置rsync客户端(其他服务器为客户端)

第一个里程碑: 软件是否存在

[root@10e0e0e17 ~]# rpm -qa |grep rsync
rsync-3.1.2-10.el7.x86_64

第二个里程碑: 创建认证文件

   客户端的认证文件只需要有密码即可

echo "123456" >>/etc/rsync.password
chmod 600 /etc/rsync.password

第三个里程碑: 实现数据传输

交互式

[root@10e0e0e17 ~]# rsync -avzP /root/yhh.txt   rsync_backup@10.0.0.15::backup
Password: 
sending incremental file list
yhh.txt
             33 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/1)
rsync: chgrp ".yhh.txt.BQkcGt" (in backup) failed: Operation not permitted (1)

sent 124 bytes  received 126 bytes  71.43 bytes/sec
total size is 33  speedup is 0.13
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1179) [sender=3.1.2]

免交互式

[root@10e0e0e17 ~]# rsync -avzP /root/yhh.txt   rsync_backup@10.0.0.15::backup  --password-file=/etc/rsync.password
sending incremental file list
yhh.txt
             33 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/1)
rsync: chgrp ".yhh.txt.4cZIXI" (in backup) failed: Operation not permitted (1)

sent 124 bytes  received 126 bytes  500.00 bytes/sec
total size is 33  speedup is 0.13
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1179) [sender=3.1.2]

2.3 rsync 命令同步参数选项&特殊参数

目录参数

参数说明

-v ,--verbose

详细模式输出,传输时的信息

-z,--compress

传输时进行压缩以提高传输效率

--compress-level=NUM 可按级别压缩

局域网可以不用压缩

-a,--archive (主要)

归档模式,表示以递归方式传输文件,并保持文件属性。等于 -rtopgDl

-r,--recursive  归档于-a

对子目录以递归模式,即目录下的所有目录都同样传输。小写r

-t,--times       归档于-a

保持文件时间信息

-o,--owner       归档于-a

保持文件属主信息

-p,--perms       归档于-a

保持文件权限

-g,--group       归档于-a

保持文件属组信息

-P,--progress

显示同步的过程及传输时的进度等信息(大P

-D,--devices    归档于-a

保持设备文件信息

-l,--links       归档于-a

保留软连接(小写字母l

-e,--rsh=COMMAND

使用的信道协议(remote shell),指定替代rsh的shell程序。

例如 ssh

--exclude=PATTERN

指定排除不需要传输的文件信息

--exclude-from=file

文件名所在目录文件,即可以实现排除多个文件

--bwlimit=RATE

限速功能

--delete

让目标目录SRC和源目录数据DST一致,即无差异数据同步

保持同步目录及文件属性:

这里的-avzP相当于 -vzetopdDlP,生产环境常用的参数为 -avzP

在脚本中可以报-vP去掉

--progress可以用-P代替

daemon启动扩展参数

--daemon

daemon表示以守护进程的方式启动rsync服务。

--address

绑定指定IP地址提供服务。

--config=FILE

更改配置文件路径,而不是默认的/etc/rsyncd.conf

--port=PORT

更改其它端口提供服务,而不是缺省的873端口

2.3.1 特殊参数实践

指定ip

[root@iso-all ~]# rsync --daemon --address=10.0.0.15
[root@iso-all ~]# 
[root@iso-all ~]# 
[root@iso-all ~]# ss -lntp
State       Recv-Q Send-Q                                        Local Address:Port                                                       Peer Address:Port              
LISTEN      0      5                                                 10.0.0.15:873                                                                   *:*                   users:(("rsync",pid=8950,fd=3))
LISTEN      0      128                                                       *:22                                                                    *:*                   users:(("sshd",pid=6770,fd=3))
LISTEN      0      100                                               127.0.0.1:25                                                                    *:*                   users:(("master",pid=6921,fd=13))
LISTEN      0      128                                                      :::22                                                                   :::*                   users:(("sshd",pid=6770,fd=4))
LISTEN      0      100                                                     ::1:25                                                                   :::*                   users:(("master",pid=6921,fd=14))

指定配置文件路径

[root@iso-all ~]# rsync --daemon --config=/etc/rsyncd.conf

服务端指定服务端口:

[root@iso-all ~]# rsync --daemon --port=5222
[root@iso-all ~]# ss -lntp
State       Recv-Q Send-Q                                        Local Address:Port                                                       Peer Address:Port              
LISTEN      0      128                                                       *:22                                                                    *:*                   users:(("sshd",pid=6770,fd=3))
LISTEN      0      100                                               127.0.0.1:25                                                                    *:*                   users:(("master",pid=6921,fd=13))
LISTEN      0      5                                                         *:5222                                                                  *:*                   users:(("rsync",pid=8960,fd=3))
LISTEN      0      128                                                      :::22                                                                   :::*                   users:(("sshd",pid=6770,fd=4))
LISTEN      0      100                                                     ::1:25                                                                   :::*                   users:(("master",pid=6921,fd=14))
LISTEN      0      5                                                        :::5222                                                                 :::*                   users:(("rsync",pid=8960,fd=5))

第3章 rsycn配置文件详解 rsyncd.conf

3.1 部分知识补充

3.1.1 配置文件内容参考资料

man rsyncd.conf

3.1.2 配置文件内容总结

    模块之上内容为全局变量信息

    模块之下内容为局部变量信息

说明:

无论是全局变量发生变化,还是局部变量发生变化,都建议重启rsync服务使配置生效。

3.2 守护进程多模块功能配置

第一个里程碑: 编写配置信息创建多模块

......
[test1]
comment = "test1 dir by 10e0e0e12"
path = /root/test1
[test2]
comment = "test2 dir by 10e0e0e17"
path = /root/test2 

第二个里程碑: 创建多模块指定的目录

[root@iso-all ~]# mkdir test{1..3}
[root@iso-all ~]# ls -l
drwxr-xr-x  15 rsync   rsync        209 9月  25 17:29 test
drwxr-xr-x   2 root    root           6 9月  25 17:59 test1
drwxr-xr-x   2 root    root           6 9月  25 17:59 test2
drwxr-xr-x   2 root    root           6 9月  25 17:59 test3
[root@iso-all ~]# chown -R rsync.rsync test1
[root@iso-all ~]# 
[root@iso-all ~]# chown -R rsync.rsync test2
[root@iso-all ~]# chown -R rsync.rsync test3
[root@iso-all ~]# ls -l
drwxr-xr-x  15 rsync   rsync        209 9月  25 17:29 test
drwxr-xr-x   2 rsync   rsync          6 9月  25 17:59 test1
drwxr-xr-x   2 rsync   rsync          6 9月  25 17:59 test2
drwxr-xr-x   2 rsync   rsync          6 9月  25 17:59 test3

说明:

    rsyncd.conf配置文件中,添加多模块信息,可以不用重启rsync服务,即时生效~
    全局变量参数针对所有模块生效;局部变量参数只针对指定模块生效
    read only参数默认配置为ture,即为只读模式
    全局变量发生变化,不用重启rsync服务;局部变量发生变化,需要重启rsync服务

注意:修改配置文件就重启

无论是全局变量发生变化,还是局部变量发生变化,都建议重启rsync服务使配置生效

3.3 守护进程的排除功能实践

3.3.1 排除的方式

a) --exclude=要配置的目录或文件名称

       b) --exclude-from=要排除多个目录或文件汇总文件名称

       c在配置文件中进行修改,指定要排除的信息

3.3.2 排除测试

第一个里程碑: 创建模拟测试环境

[root@10e0e0e17 test]# ls -l
total 0
-rw-r--r--. 1 root root 0 Sep 24 06:12 10.txt
-rw-r--r--. 1 root root 0 Sep 24 06:12 1.txt
-rw-r--r--. 1 root root 0 Sep 24 06:12 2.txt
-rw-r--r--. 1 root root 0 Sep 24 06:12 3.txt
-rw-r--r--. 1 root root 0 Sep 24 06:12 4.txt
-rw-r--r--. 1 root root 0 Sep 24 06:12 5.txt
-rw-r--r--. 1 root root 0 Sep 24 06:12 6.txt
-rw-r--r--. 1 root root 0 Sep 24 06:12 7.txt
-rw-r--r--. 1 root root 0 Sep 24 06:12 8.txt
-rw-r--r--. 1 root root 0 Sep 24 06:12 9.txt
drwxr-xr-x. 2 root root 6 Sep 24 06:11 a
drwxr-xr-x. 2 root root 6 Sep 24 06:11 [a..p]
drwxr-xr-x. 2 root root 6 Sep 24 06:11 b
drwxr-xr-x. 2 root root 6 Sep 24 06:11 c
drwxr-xr-x. 2 root root 6 Sep 24 06:11 d
drwxr-xr-x. 2 root root 6 Sep 24 06:11 e
drwxr-xr-x. 2 root root 6 Sep 24 06:11 f
drwxr-xr-x. 2 root root 6 Sep 24 06:11 g
drwxr-xr-x. 2 root root 6 Sep 24 06:11 h
drwxr-xr-x. 2 root root 6 Sep 24 06:11 i
drwxr-xr-x. 2 root root 6 Sep 24 06:11 j
drwxr-xr-x. 2 root root 6 Sep 24 06:11 k
drwxr-xr-x. 2 root root 6 Sep 24 06:11 l
drwxr-xr-x. 2 root root 6 Sep 24 06:11 m
drwxr-xr-x. 2 root root 6 Sep 24 06:11 n
drwxr-xr-x. 2 root root 6 Sep 24 06:11 o
drwxr-xr-x. 2 root root 6 Sep 24 06:11 p

第二个里程碑 利用 --exclude参数测试排除功能

[root@10e0e0e17 test]# rsync -avzP  /root/test --exclude=3.txt --exclude=5.txt  rsync_backup@10.0.0.15::test1  --password-file=/etc/rsync.password
sending incremental file list
rsync: chgrp "test" (in test1) failed: Operation not permitted (1)
test/
test/1.txt
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=24/26)
test/10.txt
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=23/26)
test/2.txt
              0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=22/26)
test/4.txt
              0 100%    0.00kB/s    0:00:00 (xfr#4, to-chk=21/26)
test/6.txt
              0 100%    0.00kB/s    0:00:00 (xfr#5, to-chk=20/26)
test/7.txt
              0 100%    0.00kB/s    0:00:00 (xfr#6, to-chk=19/26)
test/8.txt
              0 100%    0.00kB/s    0:00:00 (xfr#7, to-chk=18/26)
test/9.txt
              0 100%    0.00kB/s    0:00:00 (xfr#8, to-chk=17/26)
rsync: chgrp "test/[a..p]" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/a" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/b" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/c" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/d" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/e" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/f" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/g" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/h" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/i" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/j" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/k" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/l" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/m" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/n" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/o" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/p" (in test1) failed: Operation not permitted (1)
test/[a..p]/
test/a/
test/b/
test/c/
test/d/
test/e/
test/f/
test/g/
test/h/
test/i/
test/j/
test/k/
test/l/
test/m/
test/n/
test/o/
test/p/
rsync: chgrp "test/.1.txt.ZUDib0" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/.10.txt.AihsiP" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/.2.txt.gmtCpE" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/.4.txt.Fn5Mwt" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/.6.txt.5n6XDi" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/.7.txt.58taL7" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/.8.txt.j8fnSW" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/.9.txt.MyKAZL" (in test1) failed: Operation not permitted (1)

sent 693 bytes  received 2,250 bytes  5,886.00 bytes/sec
total size is 0  speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1179) [sender=3.1.2]
[root@iso-all test]# ls
10.txt  1.txt  2.txt  4.txt  6.txt  7.txt  8.txt  9.txt  a  [a..p]  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p

3.3.3 利用--exclude-from 方式进行排除

第一个里程碑: 创建模拟测试环境

[root@iso-all test1]# rm test/ -rf
[root@iso-all test1]# 
[root@iso-all test1]# 
[root@iso-all test1]# ls -l
总用量 0

第二个里程碑:利用--exlude-from参数,测试排除功能

[root@10e0e0e17 ~]#  vim /tmp/paichu.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
a
b
                    

第三个里程碑:进行排除

[root@10e0e0e17 ~]# rsync -avzP  /root/test --exclude-from=/tmp/paichu.txt   rsync_backup@10.0.0.15::test1  --password-file=/etc/rsync.password
sending incremental file list
rsync: chgrp "test" (in test1) failed: Operation not permitted (1)
test/
test/1.txt
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=18/20)
test/10.txt
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=17/20)
test/2.txt
              0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=16/20)
rsync: chgrp "test/[a..p]" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/b" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/c" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/d" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/e" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/f" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/g" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/h" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/i" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/j" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/k" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/l" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/m" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/n" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/o" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/p" (in test1) failed: Operation not permitted (1)
test/[a..p]/
test/b/
test/c/
test/d/
test/e/
test/f/
test/g/
test/h/
test/i/
test/j/
test/k/
test/l/
test/m/
test/n/
test/o/
test/p/
rsync: chgrp "test/.1.txt.xmOHk0" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/.10.txt.WBMC1X" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/.2.txt.8rQyIV" (in test1) failed: Operation not permitted (1)

sent 448 bytes  received 1,653 bytes  4,202.00 bytes/sec
total size is 0  speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1179) [sender=3.1.2]
[root@iso-all test]# ll
总用量 0
-rw------- 1 rsync rsync 0 9月  25 18:21 10.txt
-rw------- 1 rsync rsync 0 9月  25 18:21 1.txt
-rw------- 1 rsync rsync 0 9月  25 18:21 2.txt
drwx------ 2 rsync rsync 6 9月  24 06:11 [a..p]
drwx------ 2 rsync rsync 6 9月  24 06:11 b
drwx------ 2 rsync rsync 6 9月  24 06:11 c
drwx------ 2 rsync rsync 6 9月  24 06:11 d
drwx------ 2 rsync rsync 6 9月  24 06:11 e
drwx------ 2 rsync rsync 6 9月  24 06:11 f
drwx------ 2 rsync rsync 6 9月  24 06:11 g
drwx------ 2 rsync rsync 6 9月  24 06:11 h
drwx------ 2 rsync rsync 6 9月  24 06:11 i
drwx------ 2 rsync rsync 6 9月  24 06:11 j
drwx------ 2 rsync rsync 6 9月  24 06:11 k
drwx------ 2 rsync rsync 6 9月  24 06:11 l
drwx------ 2 rsync rsync 6 9月  24 06:11 m
drwx------ 2 rsync rsync 6 9月  24 06:11 n
drwx------ 2 rsync rsync 6 9月  24 06:11 o
drwx------ 2 rsync rsync 6 9月  24 06:11 p

说明:

    01. 排除文件中,需要利用相对路径指定排除信息(不能利用绝对路径)
    02. 相对路径指的是相对同步的目录信息而言,是对rsync -avz /data/ 后面的data目录进行相对

3.4 守护进程来创建备份目录

通过客户端命令创建服务端备份目录中子目录

[root@10e0e0e17 ~]# rsync -avzP  /etc/hosts   rsync_backup@10.0.0.15::test1/10e0e0e17/  --password-file=/etc/rsync.password
sending incremental file list
created directory 10e0e0e17
rsync: change_dir#2 "/10e0e0e17" (in test1) failed: Permission denied (13)
rsync error: errors selecting input/output files, dirs (code 3) at main.c(675) [Receiver=3.1.2]

检查结果:

[root@iso-all test1]# ls
10e0e0e17  test

说明:

    a 目标目录名称后要加上 "/", 表示创建目录,否则变为修改传输文件名称了
    b 利用客户端创建服务备份子目录时,只能创建一级子目录。

3.5 守护进程的访问控制配置

第一个里程碑:在服务端配置文件,编写白名单策略或黑名单策略(只能取其一)

    vim /etc/rsyncd.conf
    hosts allow = 10.0.0.12/32
    #hosts deny = 0.0.0.0/32

关于访问控制的说明:

01. 白名单和黑名单同时存在时,默认控制策略为不匹配的传输数据信息全部放行
02. 白名单单一存在时,默认控制策略为不匹配的传输数据信息全部禁止
03. 黑名单单一存在时,默认控制策略为不匹配的传输数据信息全部放行

 全局变量修改控制策略信息,rsync服务必须重启

第二个里程碑:客户端进行测试

#10e0e0e12
[root@10e0e0e12 ~]# rsync -avzP  /etc/hosts   rsync_backup@10.0.0.15::test1/10e0e0e12/ 
Password: 
sending incremental file list
created directory 10e0e0e12
rsync: change_dir#2 "/10e0e0e12" (in test1) failed: Permission denied (13)
rsync error: errors selecting input/output files, dirs (code 3) at main.c(675) [Receiver=3.1.2]
#10e0e0e17
[root@10e0e0e17 ~]# rsync -avzP  /etc/hosts   rsync_backup@10.0.0.15::test1/10e0e0e17/  --password-file=/etc/rsync.password
@ERROR: Unknown module 'test1'
rsync error: error starting client-server protocol (code 5) at main.c(1649) [sender=3.1.2]

3.6 守护进程无差异同步配置

3.6.1 什么是无差异:

    推模式:我有什么,你就有什么;我没有,你也不能有

    拉模式:你有什么,我就有什么;你没有,我也不能有

    总结:服务端客户端数据完全一致(一模一样)

3.6.2 实现无差异同步方法

第一个里程碑: 创建实验环境

[root@10e0e0e17 ~]# rsync -avzP  /root/test --exclude=3.txt --exclude=5.txt  rsync_backup@10.0.0.15::test1  --password-file=/etc/rsync.password

第二个里程:删除指定目录,并添加指定文件,测试无差异功能

[root@10e0e0e17 test]# rm 4.txt -f
[root@10e0e0e17 test]# 
[root@10e0e0e17 test]# 
[root@10e0e0e17 test]# rm a -rf 
[root@10e0e0e17 test]# 
[root@10e0e0e17 test]# rm c -rf 
[root@10e0e0e17 test]# 
[root@10e0e0e17 test]# 
[root@10e0e0e17 test]# rsync -avzP  /root/test --delete   rsync_backup@10.0.0.15::test1  --password-file=/etc/rsync.password
sending incremental file list
rsync: chgrp "test" (in test1) failed: Operation not permitted (1)
deleting test/c/
deleting test/a/
deleting test/4.txt
test/
test/1.txt
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=23/25)
test/10.txt
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=22/25)
test/2.txt
              0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=21/25)
test/3.txt
              0 100%    0.00kB/s    0:00:00 (xfr#4, to-chk=20/25)
test/5.txt
              0 100%    0.00kB/s    0:00:00 (xfr#5, to-chk=19/25)
test/6.txt
              0 100%    0.00kB/s    0:00:00 (xfr#6, to-chk=18/25)
test/7.txt
              0 100%    0.00kB/s    0:00:00 (xfr#7, to-chk=17/25)
test/8.txt
              0 100%    0.00kB/s    0:00:00 (xfr#8, to-chk=16/25)
test/9.txt
              0 100%    0.00kB/s    0:00:00 (xfr#9, to-chk=15/25)
rsync: chgrp "test/[a..p]" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/b" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/d" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/e" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/f" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/g" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/h" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/i" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/j" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/k" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/l" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/m" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/n" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/o" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/p" (in test1) failed: Operation not permitted (1)
test/[a..p]/
test/b/
test/d/
test/e/
test/f/
test/g/
test/h/
test/i/
test/j/
test/k/
test/l/
test/m/
test/n/
test/o/
test/p/
rsync: chgrp "test/.1.txt.quqP4m" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/.10.txt.C1PuVi" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/.2.txt.C2AbMe" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/.3.txt.zuBTCa" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/.5.txt.vr5Ct6" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/.6.txt.q4Qnk2" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/.7.txt.8Ap9aY" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/.8.txt.YsIV1T" (in test1) failed: Operation not permitted (1)
rsync: chgrp "test/.9.txt.Q9NISP" (in test1) failed: Operation not permitted (1)

sent 726 bytes  received 2,236 bytes  1,184.80 bytes/sec
total size is 0  speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1179) [sender=3.1.2]

查看结果:

[root@iso-all test]# ls
10.txt  1.txt  2.txt  3.txt  5.txt  6.txt  7.txt  8.txt  9.txt  [a..p]  b  d  e  f  g  h  i  j  k  l  m  n  o  p

 第4章 常见问题(本人配置遇到问题)

 4.1 客户端的错误现象:No route to host

 rsync服务端开启的iptables防火墙

[root@iso-all ~]# systemctl status iptables
● iptables.service - IPv4 firewall with iptables
   Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled)
   Active: active (exited) since 六 2020-09-26 19:41:24 CST; 2s ago
  Process: 10062 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS)
 Main PID: 10062 (code=exited, status=0/SUCCESS)

9月 26 19:41:24 iso-all systemd[1]: Starting IPv4 firewall with iptables...
9月 26 19:41:24 iso-all iptables.init[10062]: iptables: Applying firewall rules: [  确定  ]
9月 26 19:41:24 iso-all systemd[1]: Started IPv4 firewall with iptables.

关闭防火墙

[root@iso-all ~]# systemctl stop  iptables
[root@iso-all ~]# systemctl status iptables
● iptables.service - IPv4 firewall with iptables
   Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled)
   Active: inactive (dead) since 六 2020-09-26 19:42:16 CST; 5s ago
  Process: 10108 ExecStop=/usr/libexec/iptables/iptables.init stop (code=exited, status=0/SUCCESS)
  Process: 10062 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS)
 Main PID: 10062 (code=exited, status=0/SUCCESS)

9月 26 19:41:24 iso-all systemd[1]: Starting IPv4 firewall with iptables...
9月 26 19:41:24 iso-all iptables.init[10062]: iptables: Applying firewall rules: [  确定  ]
9月 26 19:41:24 iso-all systemd[1]: Started IPv4 firewall with iptables.
9月 26 19:42:16 iso-all systemd[1]: Stopping IPv4 firewall with iptables...
9月 26 19:42:16 iso-all iptables.init[10108]: iptables: Setting chains to policy ACCEPT: filter [  确定  ]
9月 26 19:42:16 iso-all iptables.init[10108]: iptables: Flushing firewall rules: [  确定  ]
9月 26 19:42:16 iso-all systemd[1]: Stopped IPv4 firewall with iptables.

4.2 ERROR: The remote path must start with a module name not a /

 rsync客户端执行rsync命令错误:

   客户端的错误现象: 

[root@10e0e0e17 ~]#rsync -avz /etc/hosts rsync_backup@10.0.0.15::/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模块)

4.3 @ERROR: auth failed on module backup

3. @ERROR: auth failed on module oldboy

   客户端的错误现象:

[root@10e0e0e17 ~]#rsync -avz /etc/hosts rsync_backup@10.0.0.15::/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客户端密码文件中只输入密码信息即可,不要输入虚拟认证用户名称

 

原文地址:https://www.cnblogs.com/huihuangyan/p/13722652.html