rsync同步工具学习笔记

rsync同步工具

一、rsync介绍

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

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

二、rsync特性

  • 支持拷贝特殊文件如连接文件,设备等。
  • 能够有排除指定文件或目录同步的功能,至关于打包命令tar的排除功能。
  • 能够作到保持原文件或目录的权限、时间、软硬连接、属主、组等全部属性均不改变。
  • 可实现增量同步,便可同步发生变化的数据,所以数据传输效率很高。
  • 可使用rcp,rsh,ssh等方式来配合传输文件(rsync自己不对数据加密)。
  • 能够经过socket传输文件和数据。
  • 支持匿名的或认证(无需系统用户)的进程模式传输,可实现方便安全的进行数据备份及镜像。

三、rsync工做场景

  • 两台服务器之间数据同步。
  • 把全部客户服务器数据同步到备份服务器,生产场景集群架构服务器备份方案。
  • rsync结合inotify的功能作实时的数据同步。

四、rsync命令同步参数选项

经常使用参数选项说明:vim

参数 完整参数 说明
-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或者用-vzrtopgwindows

五、rsync工做方式

通常来讲,rsync大体使用三种主要的传输数据的方式,分别为:centos

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

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

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

语法说明:bash

  • rsync为同步的命令
  • [OPTION]为同步时的参数选项
  • SRC为源,即待同步的分区、文件或目录等
  • [DEST]为目的分区、文件或目录

实例,将/etc/hosts 同步至/tmp服务器

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
5.2 远程传输,借助rcp,ssh等通道来传输数据(此时相似于scp命令的功能)。

远程传输模式的语法为:架构

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

语法说明:ssh

  • rsync为同步的命令
  • [OPTION]为同步时的参数选项
  • [USER@]HOST: 远程用户名及host
  • SRC为源,即待同步的分区、文件或目录等
  • [DEST]为目的分区、文件或目录

推送实例

[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]#

拉取实例

[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

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

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

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

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

[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]# 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 /]# mkdir -p /data1
[root@vagrant-centos65 /]# chown -R rsync.rsync /data1
# 这里报错了,由于没有相应的用户,咱们须要建立
chown: invalid user: `rsync.rsync'
# 建立一个不须要登陆的系统用户
[root@vagrant-centos65 /]# useradd rsync -s /sbin/nologin
[root@vagrant-centos65 /]# chown -R rsync.rsync /data1

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

# rsync_backup是用户名,maxincai是密码
[root@vagrant-centos65 /]# echo "rsync_backup:maxincai" >/etc/rsync.password
[root@vagrant-centos65 /]# cat /etc/rsync.password
rsync_backup:maxincai
# 因为咱们存放的是密码文件,为了安全,修改权限为600,同时这也是rsync自己的要求
root@vagrant-centos65 /]# chmod 600 /etc/rsync.password
# 同时咱们须要关闭防火樯
[root@vagrant-centos65 /]# /etc/init.d/iptabls stop
-bash: /etc/init.d/iptabls: No such file or directory
[root@vagrant-centos65 /]# getenforce
Disabled

下面配置客户端,将密码保存在密码配置文件,同是为了与服务端统一,咱们使用至关的文件名,注意这里咱们只须要放入密码便可

[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端的目录结构

[root@vagrant-centos65 data1]# pwd
/data1
[root@vagrant-centos65 data1]# tree
.
├── dir1
│   └── test3
├── dir2
├── test1
└── test2

3 directories, 2 files
[root@vagrant-centos65 data1]#

开始拉取

[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 ~]# 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

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

[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

在客户端进行推送实例

# 建立10个文件
[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]#  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]# tree
.
├── 1
├── 10
├── 2
├── 3
├── 4
├── 5
├── 6
├── 7
├── 8
├── 9
├── dir1
│   └── test3
├── dir2
├── test1
└── test2

3 directories, 12 files

其余的语法

# 推送
[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]#  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

小结:

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
  • 整个部署流程总体考虑排查
  • 操做习惯看成一个大事

排除单个文件

[root@vagrant-centos65 data1]#  rsync -avz  rsync://rsync_backup@192.168.35.2/data1 /data1/ --exclude=1 --password-file=/etc/rsync.password

排除多个文件

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

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

# 排除连续的
rsync -avz  rsync://rsync_backup@192.168.35.2/data1 /data1/ --exclude={a..g} --password-file=/etc/rsync.password

# 从文件中读取排除规则
rsync -avz  rsync://rsync_backup@192.168.35.2/data1 /data1/ --exclude-from=paichu.log --password-file=/etc/rsync.password

服务端排除参数,在/etc/rsyncd.conf中修改:

# 注意是用空格去分隔
exclude=a b test/1.txt
相关文章
相关标签/搜索