rsync服务部署笔记


一、rsync介绍

1.1 什么是rsync

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

1.2 全量及增量

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

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

1.3 rsync 软件功能介绍 

类似于 cp 命令 -- 实现本地备份传输数据

类似于scp 命令 -- 远程备份传输数据

类似于 rm 命令 -- 实现无差异同步备份

类似于 ls 命令 -- 本地文件信息查看

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

1. 支持拷贝普通文件与特殊文件如链接文件,设备等。

2. 可以有排除指定文件或目录同步的功能

3. 可以做到保持原文件或目录的权限、时间、软硬链接、属主、组等所有属性均不改变-p。

4. 可实现增量同步,既只同步发生变化的数据,因此数据传输效率很高。

5. 可以使用rcp,rsh,ssh等方式来配合进行隧道加密传输文件(rsync本身不对数据加密)

6. 可以通过socket(进程方式)传输文件和数据(服务端和客户端)。重点掌握

7. 支持匿名的或认证(无需系统用户)的进程模式传输,可实现方便安全的进行数据备份及镜像。

1.5 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端口

1.6 Rsync的企业工作场景说明

1. 两台服务器之间数据同步(定时任务cron+rsync)

    同步网站内部人员数据信息(定时任务最小周期为1分钟)

2. 两台服务器之间数据同步(实时任务inotify/sersync/lrsyncd+rsync)

   同步网站用户人员数据信息

二、rsync的使用

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

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

参数

含义

rsync       

数据同步命令

[OPTION...] 

rsync命令参数信息

SRC         

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

[DEST]      

将数据传输到什么位置

命令演示:

[root@web01 ~]# rsync /etc/profile /tmp/

[root@web01 ~]# ls /tmp/profile

2.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     

传输到远端什么位置

pull拉:

[root@web01 ~]# rsync 192.168.231.143:/etc/profile ./

root@192.168.231.143's password:

push推(包括目录):

[root@web01 ~]# rsync -r scripts 192.168.231.143:/root/

root@192.168.231.143's password:

192.168.231.143主机查看:

(不包括目录)

[root@web01 ~]# rsync -r scripts/ 192.168.231.143:/tmp/

root@192.168.231.143's password:

远程主机查看:

说明:

scripts   --表示将scripts目录本身及目录下的内容进行传输

scripts/    --表示只传输scripts目录下面的内容信息

2.3 守护进程方式同步数据

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

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

客户端:192.168.231.142 服务端:192.168.231.143

配置rsync服务端

1.检查软件是否存在

[root@web02 ~]# rpm -qa |grep rsync

rsync-3.0.6-12.el6.x86_64

2.进行软件服务配置

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

有些版本的系统上文件可能默认不存在 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 = 192.168.231.142

hosts deny = 0.0.0.0/32

auth users = rsync_backup

secrets file = /etc/rsync.password

[backup]

comment = "test"

path = /backup

 

3.创建rsync用户

[root@web02 ~]# useradd -s /sbin/nologin -M rsync

创建数据库备份存储目录,并修改属主,属组

mkdir /backup

chown -R rsync.rsync /backup

4.创建认证用户密码文件并修改权限:

[root@web02 ~]# echo"rsync_backup:123456">>/etc/rsync.password

chmod 600 /etc/rsync.password

5.启动rsync服务

rsync   --daemon

6.服务端配置完成(查看端口及进程):

[root@web02 ~]# ps -ef | grep rsync

root      10802      1  0 16:13 ?        00:00:00 rsync --daemon

root      10932  10900  0 17:14 pts/1    00:00:00 grep rsync

[root@web02 ~]# netstat -lntup | grep rsync

tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      10802/rsync         

tcp        0      0 :::873                      :::*                        LISTEN      10802/rsync   

实现数据传输

交互模式(需输入密码)

 [root@web01 ~]# rsync -avzP /etc/hosts rsync_backup@192.168.231.143::backup

Password:

sending incremental file list

hosts

         224 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)

 

sent 155 bytes  received 27 bytes  72.80 bytes/sec

total size is 224  speedup is 1.23

/etc/hosts是要传输的文件,rsync_backup时认证用户,::后面是创建的数据备份存储目录,就是服务端配置文件所写

免交互模式(不用输入密码)

[root@web01 ~]# rsync -avzP 1.txt rsync_backup@192.168.231.143::backup --password-file=/etc/rsync.password

sending incremental file list

1.txt

           4 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)

 

sent 70 bytes  received 27 bytes  194.00 bytes/sec

total size is 4  speedup is 0.04

 

三、 rsycn配置文件详解 rsyncd.conf 

3.1 配置示例及详解

######### 全局配置参数 ##########

port=888    # 指定rsync端口。默认873

uid = rsync    # rsync服务的运行用户,默认是nobody,文件传输成功后属主将是这个uid

gid = rsync     # rsync服务的运行组,默认是nobody,文件传输成功后属组将是这个gid

use chroot = no     # rsync daemon在传输前是否切换到指定的path目录下,并将其监禁在内

max connections = 200     # 指定最大连接数量,0表示没有限制

timeout = 300         # 确保rsync服务器不会永远等待一个崩溃的客户端,0表示永远等待

motd file = /var/rsyncd/rsync.motd   # 客户端连接过来显示的消息

pid file = /var/run/rsyncd.pid       # 指定rsync daemon的pid文件

lock file = /var/run/rsync.lock      # 指定锁文件

log file = /var/log/rsyncd.log       # 指定rsync的日志文件,而不把日志发送给syslog

dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2    # 指定哪些文件不用进行压缩传输

 

###########下面指定模块,并设定模块配置参数,可以创建多个模块###########

[backup]        # 模块ID

path = /backup/   # 指定该模块的路径,该参数必须指定。启动rsync服务前该目录必须存在。rsync请求访问模块本质就是访问该路径。

ignore errors      # 忽略某些IO错误信息

read only = false  # 指定该模块是否可读写,即能否上传文件,false表示可读写,true表示可读不可写。所有模块默认不可上传

write only = false   # 指定该模式是否支持下载,设置为true表示客户端不能下载。所有模块默认可下载

list = false       # 客户端请求显示模块列表时,该模块是否显示出来,设置为false则该模块为隐藏模块。默认true

hosts allow =192.168.231.143/24    # 指定允许连接到该模块的机器,多个ip用空格隔开或者设置区间

hosts deny = 0.0.0.0/32   # 指定不允许连接到该模块的机器

auth users = rsync_backup    # 指定连接到该模块的用户列表,只有列表里的用户才能连接到模块,用户名和对应密码保存在secrts file中, 这里使用的不是系统用户,而是虚拟用户。不设置时,默认所有用户都能连接,但使用的是匿名连接

secrets file = /etc/rsyncd.password # 保存auth users用户列表的用户名和密码,每行包含一个username:passwd。

配置文件内容总结

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

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

说明:

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

3.2 利用xinetd服务,管理启动rsync服务

安装xinetd软件

yun install xinetd -y

修改配置文件

vim /etc/xinetd.d/rsync

修改disable=no

service rsync

{

        disable = no

        flags           = IPv6

        socket_type     = stream

        wait            = no

        user            = root

        server          = /usr/bin/rsync

        server_args     = --daemon

        log_on_failure  += USERID

}

重启xinetd服务

/etc/init.d/xinetd restart

传输测试

[root@web01 ~]# rsync -avz /etc/profile rsync_backup@192.168.231.143::backup --password-file=/etc/rsync.password

sending incremental file list

profile

 

sent 1081 bytes  received 27 bytes  2216.00 bytes/sec

total size is 2368  speedup is 2.14

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

编写配置信息创建多模块

服务端:

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

[backup]

comment = "test"

path = /backup/test1

[backup2]

comment = "test2"

path = /backup/test2

创建多模块指定目录

mkdir -p /backup/{test1,test2}

修改目录权限

chown rsync.rsync /backup/{test1,test2}

客户端:

[root@web01 ~]# rsync -avz /etc/profile rsync_backup@192.168.231.143::backup2 --password-file=/etc/rsync.password

sending incremental file list

profile

说明:

rsyncd.conf配置文件中,添加多模块信息,可以不用重启rsync服务,即时生效~

全局变量参数针对所有模块生效;局部变量参数只针对指定模块生效

read only参数默认配置为ture,即为只读模式

全局变量发生变化,不用重启rsync服务;局部变量发生变化,需要重启rsync服务

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

排除的方式

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

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

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

a)

排除测试

创建文件进行测试
[root@web01 test]# mkdir {a..d}

[root@web01 test]# tree

.

├── a

│   ├── 1.txt

│   ├── 2.txt

│   └── 3.txt

├── b

│   ├── 1.txt

│   ├── 2.txt

│   └── 3.txt

├── c

│   ├── 1.txt

│   ├── 2.txt

│   └── 3.txt

└── d

    ├── 1.txt

    ├── 2.txt

    └── 3.txt

 

4 directories, 12 files

--exclude参数测试:

需求:不要a中的1.txt,不要b、c目录

[root@web01 ~]# rsync -avz test/ --exclude=a/1.txt --exclude={b,c} rsync_backup@192.168.231.143::backup --password-file=/etc/rsync.password

sending incremental file list

./

a/

a/2.txt

a/3.txt

d/

d/1.txt

d/2.txt

d/3.txt

 

sent 300 bytes  received 114 bytes  828.00 bytes/sec

total size is 0  speedup is 0.00

 b)利用--exclude-from 方式进行排除 

还是利用上面的目录,文件

利用--exclude-from参数,测试排除功能:

[root@web01 ~]# vim /root/test.txt

a/2.txt

b

c

进行排除测试

[root@web01 ~]# rsync -avz test/  --exclude-from=test.txt rsync_backup@192.168.231.143::backup2 --password-file=/etc/rsync.password

sending incremental file list

./

a/

a/1.txt

a/3.txt

d/

d/1.txt

d/2.txt

d/3.txt

 

sent 300 bytes  received 114 bytes  276.00 bytes/sec

total size is 0  speedup is 0.00

说明:

1. 排除文件中,需要利用相对路径指定排除信息(不能利用绝对路径)

2. 相对路径指的是相对同步的目录信息而言,是对rsync -avz /data/ 后面的data目录进行相对

c)在配置文件中修改要排除的文件

修改服务端配置文件:

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

comment = "test2"

path = /backup/test2

exclude=a/3.txt b c

重启rsync服务:

killall rsync && sleep 1 && rsync --daemon

因之前用利用xinetd服务启动rsync服务,这里可以用:

[root@web02 ~]# /etc/init.d/xinetd restart

传输测试:

[root@web01 ~]# rsync -avz test/ rsync_backup@192.168.231.143::backup2 --password-file=/etc/rsync.password

sending incremental file list

./

a/

a/1.txt

a/2.txt

skipping daemon-excluded file "a/3.txt"

skipping daemon-excluded directory "b"

*** Skipping any contents from this failed directory ***

skipping daemon-excluded directory "c"

*** Skipping any contents from this failed directory ***

d/

d/1.txt

d/2.txt

d/3.txt

 

sent 399 bytes  received 116 bytes  1030.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(1039) [sender=3.0.6]

3.5 守护进程来创建备份目录中的子目录

[root@web01 ~]# rsync -avzP /etc/profile rsync_backup@192.168.231.143::backup/test3/ --password-file=/etc/rsync.password

sending incremental file list

created directory test3

profile

        2368 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)

 

sent 1081 bytes  received 27 bytes  2216.00 bytes/sec

total size is 2368  speedup is 2.14

服务端查看:

[root@web02 ~]# tree /backup/test1

/backup/test1

└── test3

    └── profile

 

1 directory, 1 file

在backup模块中的备份目录/backup/test1/下创建了test3目录并且传输profile文件

说明:

a 目标目录名称后要加上 "/", 表示创建目录,否则变为修改传输文件名称了

b 利用客户端创建服务备份子目录时,只能创建一级子目录。

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

注意:在服务端配置,编写白名单策略和黑名单策略只能取其一

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

#hosts allow = 192.168.231.142

hosts deny = 192.168.231.142

关于访问控制的说明:

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

    2. 白名单单一存在时,默认控制策略为不匹配的传输数据信息全部禁止

    3. 黑名单单一存在时,默认控制策略为不匹配的传输数据信息全部放行

重启服务

[root@web02 ~]# /etc/init.d/xinetd restart

客户端测试:

[root@web01 ~]# rsync -avz /etc/services rsync_backup@192.168.231.143::backup2 --password-file=/etc/rsync.password

@ERROR: Unknown module 'backup2'

rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]

修改配置为允许并进行测试:

[root@web01 ~]# rsync -avz /etc/services rsync_backup@192.168.231.143::backup2 --password-file=/etc/rsync.password

sending incremental file list

services

 

sent 127417 bytes  received 27 bytes  254888.00 bytes/sec

total size is 641020  speedup is 5.03

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

什么是无差异:

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

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

    两端数据要保持一致

创建实验环境:

[root@web01 test]# tree

.

├── a

│   ├── 1.txt

│   ├── 2.txt

│   └── 3.txt

├── b

│   ├── 1.txt

│   ├── 2.txt

│   └── 3.txt

├── c

│   ├── 1.txt

│   ├── 2.txt

│   └── 3.txt

└── d

    ├── 1.txt

    ├── 2.txt

    └── 3.txt

 

4 directories, 12 files

进行第一次数据同步

[root@web01 ~]# rsync -avz --delete test/ rsync_backup@192.168.231.143::backup/ --password-file=/etc/rsync.password

上边一直用--passwrod-file,有点麻烦,直接添加环境变量可以进入免交互模式

[root@web01 ~]# export RSYNC_PASSWORD=123456

删除指定目录,测试无差异功能:

[root@web01 test]# rm -rf a/

[root@web01 ~]# rsync -avz --delete test/ rsync_backup@192.168.231.143::backup/

sending incremental file list

./

deleting a/3.txt

deleting a/2.txt

deleting a/1.txt

deleting a/

 

sent 173 bytes  received 14 bytes  374.00 bytes/sec

total size is 0  speedup is 0.00

无差异同步方法的应用

1、实现存储数据与备份数据完全一致

2、快速删除大文件数据

mkdir /null 创建一个空文件

rsync -avz --delete /null /bigdata/ 删除效率高于 rm -rf /bigdata

3.8 守护进程的列表功能配置

服务端:

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

list = ture

重启服务

/etc/init.d/xinetd restart

客户端查看服务端模块信息

[root@web01 ~]# rsync rsync_backup@192.168.231.143::

backup          "test"

backup2         "test2"

说明:

为了提升备份服务器安全性,建议关闭list列表功能

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

real向往

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值