rsync同步工具学习笔记

简介:

http://www.cnblogs.com/maxincai/p/5142245.html


1、rsync介绍

rsync是一款开源的、快速的、多功能的、可实现全量及增量的本地或远程数据同步备份的优秀工具。rsync软件适用于unix/linux/windows等多种操作系统平台。

rsync和ssh带的scp命令比较相似,但又优于scp命令的功能,scp每次都是全量拷贝,而rsync可以进行增量拷贝。当然,rsync还可以在本地主机的不同分区或目录之间全量及增量的复制数据,这又类似cp命令,但同样也优于cp命令,cp每次都是全量拷贝,而rsync可以增量拷贝.利用rsync还可以实现删除文件和目录功能,这又相当于rm命令

2、rsync特性

  • 支持拷贝特殊文件如链接文件,设备等。

  • 可以有排除指定文件或目录同步的功能,相当于打包命令tar的排除功能。

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

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

  • 可以使用rcp,rsh,ssh等方式来配合传输文件(rsync本身不对数据加密)。

  • 可以通过socket传输文件和数据。

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

3、rsync工作场景

  • 两台服务器之间数据同步。

  • 把所有客户服务器数据同步到备份服务器,生产场景集群架构服务器备份方案。

  • rsync结合inotify的功能做实时的数据同步。

4、rsync命令同步参数选项

常用参数选项说明:

参数 完整参数 说明
-v --verbose 详细模式输出,传输时的进度信息
-z --compress 传输时进行压缩以提高传输效率, --compress-level=NUM可按级别压缩
-a --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rtopgDl
-r --recursive 对子目录以递归模式,即目录下的所有目录都同样传输,注意是小写r
-t --times 保持文件时间信息
-o --owner 保持文件属主信息
-p --perms 保持文件权限
-g --group 保持文件属组信息
-P --progress 显示同步的过程及传输时的进度等信息
-D --devices 保持设备文件信息
-l --links 保持软链接
-e --rsh=COMMAND 使用的信道协议,指定替代rsh的shell程序,例如ssh

--exclude=PATTERN 指定排除不需要传输的文件模式

--exclude-from=FILE 从文件中读取指定排除不需要传输的文件模式

以上参数为在生产环境中的常用参数,基本使用已足够,相关的参数还有非常多,了解更多可以man rsync。最常用的参数avz相当于vzrtopgDl,生产参数-avz或者用-vzrtopg

5、rsync工作方式

一般来说,rsync大致使用三种主要的传输数据的方式,分别为:

5.1 本地数据传输模式(local-only mode),单个主机本地之间的数据传输(此时类似于cp命令的功能)。

rsync本地传输模式的语法为:


rsync [OPTION...] SRC... [DEST]


语法说明:

  • rsync为同步的命令

  • [OPTION]为同步时的参数选项

  • SRC为源,即待同步的分区、文件或目录等

  • [DEST]为目的分区、文件或目录

实例,将/etc/hosts 同步至/tmp


1
2
3
4
5
6
7
8
MacBook-Pro:tmp maxincai$  rsync  /etc/hosts  /tmp
MacBook-Pro:tmp maxincai$ ll
total 4
drwxrwxrwt  9 root      wheel  306  1 14 15:04 ./
drwxr-xr-x@ 6 root      wheel  204  6 26  2015 ../
drwxrwxrwt  3 maxincai  wheel  102 12 31 10:46 .pd/
-rw-r--r--  1 maxincai  wheel  737  1 14 15:04 hosts
MacBook-Pro:tmp maxincai$


5.2 远程传输,借助rcp,ssh等通道来传输数据(此时类似于scp命令的功能)。

远程传输模式的语法为:

拉取: rsync [OPTION...] [USER@]HOST:SRC... [DEST]推送: rsync [OPTION...] SRC... [USER@]HOST:DEST


语法说明:

  • rsync为同步的命令

  • [OPTION]为同步时的参数选项

  • [USER@]HOST: 远程用户名及host

  • SRC为源,即待同步的分区、文件或目录等

  • [DEST]为目的分区、文件或目录

推送实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@vagrant-centos65 ~] # mkdir data
[root@vagrant-centos65 ~] # ll
total 8
drwxr-xr-x 2 root root 4096 Jan 14 07:26 data
drwxr-xr-x 3 root root 4096 Jan 11 11:08 workspace
[root@vagrant-centos65 ~] # cd data
[root@vagrant-centos65 data] # ll
total 0
[root@vagrant-centos65 data] # ll
total 0
[root@vagrant-centos65 data] # vim 1.txt
[root@vagrant-centos65 data] # rsync -avzP -e 'ssh -p 22' /root/data 
root@192.168.35.2: /root
The authenticity of host  '192.168.35.2 (192.168.35.2)'  can't be established.
RSA key fingerprint is 0c:5f:f6:c7:a6:27:4e:a0:e6:7c:99:8a:db:2e:41: df .
Are you sure you want to  continue  connecting ( yes /no )?  yes
Warning: Permanently added  '192.168.35.2'  (RSA) to the list of known hosts.
root@192.168.35.2's password:
sending incremental  file  list
data/
data /1 .txt
            4 100%    0.00kB /s     0:00:00 (xfer #1, to-check=0/2)
sent 104 bytes  received 35 bytes  25.27 bytes /sec
total size is 4  speedup is 0.03
[root@vagrant-centos65 data] #



拉取实例


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@vagrant-centos65 data] #rsync -avzP -e 'ssh -p 22' root@192.168.35.2:/root/data1 
/root
root@192.168.35.2's password:
receiving incremental  file  list
data1/
data1 /2 .txt
            4 100%    3.91kB /s     0:00:00 (xfer #1, to-check=0/2)
sent 34 bytes  received 106 bytes  56.00 bytes /sec
total size is 4  speedup is 0.03
[root@vagrant-centos65 data] # ll
total 4
-rw-r--r-- 1 root root 4 Jan 14 07:26 1.txt
[root@vagrant-centos65 data] # cd ..
[root@vagrant-centos65 ~] # ll
total 12
drwxr-xr-x 2 root root 4096 Jan 14 07:27 data
drwxr-xr-x 2 root root 4096 Jan 14 07:30 data1
drwxr-xr-x 3 root root 4096 Jan 11 11:08 workspace
[root@vagrant-centos65 ~] #



5.3 以守护进程(socket)的方式传输数据,这个是rsync自身的重要功能。


我们实验的机器如下:

server: 192.168.35.2

client: 192.168.36.2

先新建配置文件,请注意rsyncd.conf配置文件只用在服务端新建就好。

vim /etc/rsyncd.conf

以下只是配置文件中最常用的部份,更多的请使用命令man rsyncd.conf查看。


# 运行rsync的用户和组id

uid = rsync

gid = rsync

# bug信息的处理,一种安全方式

use chroot = no

# 最大的连接数

max connections = 200

# 超时时间

timeout = 300

# pid文件

pid file = /var/run/rsyncd.pid

# 锁文件

lock file = /var/run/rsync.lock

# 日志文件

log file = /var/log/rsyncd.log

# 需要同步的模块,这是其中一个,可以有多个

[data1]

# 同步的根目录

path = /data1/

# 忽略错误

ignore errors

# 只读falsh 表示可读可写

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


以守护进程的方式启动rsync

1
[root@vagrant-centos65 data1] # rsync --daemon


如果需要了解启动更详情的信息,建议查看帮助,这里就不具体列出来了

1
[root@vagrant-centos65 data1] # rsync --daemon --help


查看是否启动成功,rsyncd的默认端口是873

1
2
3
4
5
6
7
[root@vagrant-centos65 data1] # netstat -lntup | grep 873
tcp        0      0 0.0.0.0:873             0.0.0.0:*                   LISTEN      2580 /rsync
tcp        0      0 :::873                :::*                        LISTEN      2580 /rsync
[root@vagrant-centos65 data1] # ps -ef | grep rsync
root      2580     1  0 08:19 ?     00:00:00  rsync  --daemon
root      2585  2116  0 08:30 pts /0     00:00:00  grep  rsync
[root@vagrant-centos65 data1] #


如果启动出错,我们就需要查看一下系统日志,我们这里日志显示正常启动

1
2
3
[root@vagrant-centos65 data1] # cat /var/log/rsyncd.log
2016 /01/15  08:19:12 [2580] rsyncd version 3.0.6 starting, listening on port 873
[root@vagrant-centos65 data1] #


下面我们创建需要同步的目录,并给予相应的权限

1
2
[root@vagrant-centos65 /] # mkdir -p /data1
[root@vagrant-centos65 /] # chown -R rsync.rsync /data1


# 这里报错了,因为没有相应的用户,我们需要创建

chown: invalid user: `rsync.rsync'

# 创建一个不需要登录的系统用户

1
2
[root@vagrant-centos65 /] # useradd rsync -s /sbin/nologin
[root@vagrant-centos65 /] # chown -R rsync.rsync /data1


将用户名和密码重定义输出到我们的密码存放文件


# rsync_backup是用户名,maxincai是密码

1
2
3
[root@vagrant-centos65 /] # echo "rsync_backup:maxincai" >/etc/rsync.password
[root@vagrant-centos65 /] # cat /etc/rsync.password
rsync_backup:maxincai


# 由于我们存放的是密码文件,为了安全,修改权限为600,同时这也是rsync本身的要求

1
root@vagrant-centos65 /] # chmod 600 /etc/rsync.password


# 同时我们需要关闭防火樯

1
2
3
4
[root@vagrant-centos65 /] # /etc/init.d/iptabls stop
- bash /etc/init .d /iptabls : No such  file  or directory
[root@vagrant-centos65 /] # getenforce
Disabled


下面配置客户端,将密码保存在密码配置文件,同是为了与服务端统一,我们使用相当的文件名,注意这里我们只需要放入密码即可


1
2
3
4
[root@vagrant-centos65 ~] # echo "maxincai" >/etc/rsync.password
[root@vagrant-centos65 ~] # chmod 600 /etc/rsync.password
[root@vagrant-centos65 ~] # cat /etc/rsync.password
maxincai


通过daemon方式远程传输的语法为:

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


在客户端进行拉取实例:

先看看server端的目录结构


1
2
3
4
5
6
7
8
9
10
11
[root@vagrant-centos65 data1] # pwd
/data1
[root@vagrant-centos65 data1] # tree
.
├── dir1
│   └── test3
├── dir2
├── test1
└── test2
3 directories, 2 files
[root@vagrant-centos65 data1] #


开始拉取


1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@vagrant-centos65 ~] # rsync -avz rsync_backup@192.168.35.2::data1 /data1
Password:
# 需要输入密码,输入之前的定义的密码maxincai
receiving incremental  file  list
created directory  /data1
./
test1
test2
dir1/
dir1 /test3/
dir2/
sent 116 bytes  received 289 bytes  90.00 bytes /sec
total size is 0  speedup is 0.00
[root@vagrant-centos65] #


# 可以看到通过成功,看看同步之后的目录结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@vagrant-centos65 ~] # cd /data1
[root@vagrant-centos65 data1] # ll
total 8
drwxr-xr-x 3 root root 4096 Jan 15 09:11 dir1
drwxr-xr-x 2 root root 4096 Jan 15 09:11 dir2
-rw-r--r-- 1 root root    0 Jan 15 09:10 test1
-rw-r--r-- 1 root root    0 Jan 15 09:11 test2
[root@vagrant-centos65 data1] # tree
.
├── dir1
│   └── test3
├── dir2
├── test1
└── test2
3 directories, 2 files
[root@vagrant-centos65 data1] #


在看看拉取的时候需要输入密码,不方便我们在rcontab中进行定义,所以我们需要让他自动读取密码,参数--password-file=/etc/rsync.password


1
2
3
4
5
6
[root@vagrant-centos65 data1] # rsync -avz rsync_backup@192.168.35.2::data1 /data1 
--password- file = /etc/rsync .password
receiving incremental  file  list
sent 66 bytes  received 205 bytes  108.40 bytes /sec
total size is 0  speedup is 0.00
[root@vagrant-centos65 data1] #


在客户端进行推送实例


# 创建10个文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@vagrant-centos65 data1] # touch {1..10}
[root@vagrant-centos65 data1] # ll
total 8
-rw-r--r-- 1 root root    0 Jan 15 09:25 1
-rw-r--r-- 1 root root    0 Jan 15 09:25 10
-rw-r--r-- 1 root root    0 Jan 15 09:25 2
-rw-r--r-- 1 root root    0 Jan 15 09:25 3
-rw-r--r-- 1 root root    0 Jan 15 09:25 4
-rw-r--r-- 1 root root    0 Jan 15 09:25 5
-rw-r--r-- 1 root root    0 Jan 15 09:25 6
-rw-r--r-- 1 root root    0 Jan 15 09:25 7
-rw-r--r-- 1 root root    0 Jan 15 09:25 8
-rw-r--r-- 1 root root    0 Jan 15 09:25 9
drwxr-xr-x 3 root root 4096 Jan 15 09:11 dir1
drwxr-xr-x 2 root root 4096 Jan 15 09:11 dir2
-rw-r--r-- 1 root root    0 Jan 15 09:10 test1
-rw-r--r-- 1 root root    0 Jan 15 09:11 test2
[root@vagrant-centos65 data1] #


# 开始推送

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@vagrant-centos65 data1] # rsync -avz /data1/ rsync_backup@192.168.35.2::data1 
--password- file = /etc/rsync .password
sending incremental  file  list
./
1
10
2
3
4
5
6
7
8
9
sent 558 bytes  received 204 bytes  1524.00 bytes /sec
total size is 0  speedup is 0.00
[root@vagrant-centos65 data1] #


# 查看服务端的同步情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@vagrant-centos65 data1] # tree
.
├── 1
├── 10
├── 2
├── 3
├── 4
├── 5
├── 6
├── 7
├── 8
├── 9
├── dir1
│   └── test3
├── dir2
├── test1
└── test2
3 directories, 12 files
[root@vagrant-centos65 data1] #


其他的语法


# 推送

1
2
3
4
5
6
[root@vagrant-centos65 data1] #rsync -avz /data1/ rsync://rsync_backup@192.168.35.2/data1
  --password- file = /etc/rsync .password
sending incremental  file  list
sent 195 bytes  received 11 bytes  412.00 bytes /sec
total size is 0  speedup is 0.00
[root@vagrant-centos65 data1] #


# 拉取

1
2
3
4
5
6
[root@vagrant-centos65 data1] # rsync -avz  rsync://rsync_backup@192.168.35.2/data1 
/data1/  --password- file = /etc/rsync .password
receiving incremental  file  list
sent 96 bytes  received 317 bytes  826.00 bytes /sec
total size is 0  speedup is 0.00
[root@vagrant-centos65 data1] #


#排除单个文件

1
2
rsync  -avz  rsync : //rsync_backup @192.168.35.2 /data1  /data1/  --exclude=1 --password- file =
/etc/rsync .password



#排除多个文件

1
2
rsync  -avz   rsync : //rsync_backup @192.168.35.2 /data1  /data1/  --exclude={1,2} 
--password- file = /etc/rsync .password


或者

1
2
rsync  -avz   rsync : //rsync_backup @192.168.35.2 /data1  /data1/  --exclude=a --exclude=b
--password- file = /etc/rsync .password



#排除连续的

1
2
rsync  -avz   rsync : //rsync_backup @192.168.35.2 /data1  /data1/  --exclude={a..g}
--password- file = /etc/rsync .password

#从文件中读取排除规则

1
2
rsync  -avz   rsync : //rsync_backup @192.168.35.2 /data1  /data1/  --exclude-from=paichu.log
--password- file = /etc/rsync .password

服务端排除参数

1
/etc/rsyncd .conf中修改:



#注意是用空格去分隔

1
exclude=a b  test /1 .txt


5.4 rsync限速同步

  有时候服务端业务比较繁忙,我们进行拉取或者推送会造成带宽更加紧张,这里可以通过一个参数设置带宽:

--bwlimit,如:限制为 1000k Bytes/s;值是多少k Bytes/s

如果我们将带宽设置为1M,则--bwlimit=1000KBps

1
2
rsync  -auvz --progress  --bwlimit=1000 远程文件  本地文件
rsync  -auvz --progress  --bwlimit=1000 本地文件  远程文件



小结:

rsync server:

  • vim /etc/rsyncd.conf(用户,目录,模块,虚拟用户及密码文件)

  • 创建共享目录 /data1

  • 创建rsync用户,并且授权访问/data1

  • 创建密码文件,复制配置文件里的路径,然后添加密码内容,内容虚拟用户名:密码

  • 密码文件权限600

  • rsync --daemon然后放入/etc/rc.local

  • tail /var/log/rsyncd.log

rsync client(多个)

  • 密码文件和服务端没任何关系,命令时 --password-file=/etc/rsync.password 内容:密码

  • /etc/rsync.password 600

  • 同步:
    推、拉

排错:

  • 防火墙和selinux

  • /var/log/rsyncd.log

  • 整个部署流程整体考虑排查

  • 操作习惯当作一个大事




      本文转自027ryan  51CTO博客,原文链接:http://blog.51cto.com/ucode/1951011,如需转载请自行联系原作者







相关文章
|
17天前
|
Shell 开发工具 数据安全/隐私保护
Rsync 文件同步原理及部署
Rsync 文件同步原理及部署
12 0
|
7月前
|
监控 算法 Unix
rsync远程同步
rsync远程同步
97 0
|
监控 算法 Linux
使用 Rsync 实现远程同步,以及实时同步
Rsync 全称为:Remote Sync(远程同步),是一款开源的快速备份工具,可以在不同主机间镜像同步整个目录树,支持增量备份,保持链接和权限,采用优化的同步算法,传输前执行压缩,适用于异地备份,镜像服务器等应用(Linux 系统默认安装)
1076 0
使用 Rsync 实现远程同步,以及实时同步
|
监控 Linux 网络安全
|
消息中间件 缓存 监控
rsync远程同步(下)
一、rsync介绍 1.1 rsync简介 rsync(Remote Sync,远程同步)是一个开源的快速备份工具,可以在不同主机之间镜像同步整个目录树,支持增量备份,并保持链接和权限,且采用优化的同步算法,传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用。
127 0
|
监控 算法 安全
rsync远程同步(上)
一、rsync介绍 1.1 rsync简介 rsync(Remote Sync,远程同步)是一个开源的快速备份工具,可以在不同主机之间镜像同步整个目录树,支持增量备份,并保持链接和权限,且采用优化的同步算法,传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用。
264 0
|
监控 安全 数据安全/隐私保护
|
安全
Rsync远程同步配置
# rsync服务端配置 # 安装rsync软件包 yum -y install rsync # 生成rsyncd.
868 0
|
算法 Shell 网络安全