inotify+rsync+sersync实时数据备份

第1章 rsync备份服务

1.1 rsync软件介绍

rsync软件官方链接地址:http://www.samba.org/ftp/rsync/rsync.html 

提示信息:

man rsync查看客户端说明信息

man rsyncd.conf查看服务端配置

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

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

          cp mv scp

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

      rsync

rstync适用于unix/linux/windows等多种操作系统平台

扫描二维码关注公众号,回复: 1859423 查看本文章

                                             

nfs(客户端)   (拉)<—— rsync (软件)——>(推)     backup(服务端)

1.2 rsync软件功能

1.2.1 类似于cp命令---(本地备份传输数据)

[root@backup ~]# rsync /etc/hosts /tmp/

[root@backup ~]# ls /tmp/hosts

/tmp/hosts

总结说明:利用rsync命令在备份数据目录时

备份数据的目录后面有/ 类似这样形式old_dir/ 表示oldboy_dir目录下的内容进行复制

备份数据的目录后面没有有 / 类似这样形式old_dir 表示oldboy_dir目录下的内容以及目录本身进行复制

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

[root@backup ~]# rsync -rv /old_dir/ 10.0.0.31:/tmp/

[email protected]'s password:

sending incremental file list

a

b

c

d

e

sent 253 bytes  received 107 bytes  102.86 bytes/sec

total size is 0  speedup is 0.00

[root@backup ~]# rsync -rv /old_dir 10.0.0.31:/tmp/

[email protected]'s password:

sending incremental file list

old_dir/

old_dir/a

old_dir/b

old_dir/c

old_dir/d

old_dir/e

sent 279 bytes  received 111 bytes  111.43 bytes/sec

total size is 0  speedup is 0.00

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

[root@backup tmp]# rsync -r --delete /null/  /old01/

[root@backup tmp]# ll /old01/

total 0

说明:rsync命令清空目录等内容或者清空文件内容效率比rm命令要高

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

[root@backup tmp]# rsync /etc/hosts

-rw-r--r--         371 2017/10/10 15:45:09 hosts

1.3 rsync实现增量复制的原理

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

官方增量传输算法说明:

Rsync finds files that need to be transferred using a “quick check” algorithm (by default) that looks

for files that have changed in size or in last-modified time.  Any changes  in  the  other  preserved

attributes  (as  requested by options) are made on the destination file directly when the quick check

indicates that the file’s data does not need to be updated.

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

1.4 rsync7个特性说明

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

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

    #tar zcvf backup_1.tar.gz  /opt/data  -exclude=oldboy   

    说明:在打包/opt/data时就排除了oldboy命名的目录和文件。

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工作原理

   

1.6 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

              

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

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

rsync         -- 数据同步命令

[OPTION...]   -- rsync命令参数信息

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

[DEST]        -- 将数据传输到什么位置

实例演示命令:

[root@backup test]# ll

total 4

drwxr-xr-x 2 root root 4096 Jun 29 23:21 ceshi

-rw-r--r-- 1 root root    0 Jun 29 23:21 test.txt

[root@backup test]# rsync test.txt  ceshi/

[root@backup test]# ll ceshi

total 0

-rw-r--r-- 1 root root 0 Jun 29 23:22 test.txt

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

Access via remote shell:

Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]

Push: rsync [OPTION...] SRC... [USER@]HOST:DEST

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

1.6.2.1 push:推

SRC:        本地要怼过去的数据信息

DEST         怼到远端什么位置

实践操作:push

Push: rsync [OPTION...] SRC... [USER@]HOST:DEST

服务端推:

[root@backup ~]# ll /tmp

total 0

-rw-r--r-- 1 root root 0 Jun 29 23:39 test.txt

[root@backup ~]# rsync -r /tmp 10.0.0.31:/test/

[email protected]'s password:##123456

[root@backup ~]#

客户端查看:

[root@nfs01 ~]# ll /test

total 4

drwxr-xr-x 3 root root 4096 Jun 29 23:52 tmp

服务端推:

[root@backup ~]# rsync -r /tmp/ 10.0.0.31:/test/

[email protected]'s password:

客户端查看:

[root@nfs01 ~]# ll /test

total 0

-rw-r--r-- 1 root root 0 Jun 29 23:53 test.txt

说明:/tmp   -- 表示将tmp目录下面数据内容及目录本身都进行传输

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

 

 

远程隧道加密传输方式

[root@backup tmp]# rsync -vzrtopgP  -e 'ssh -p 22' [email protected]:/tmp/ /tmp/

[root@backup tmp]# rsync -vzrtopgP  -e 'ssh -p 22' /tmp/  [email protected]:/tmp

1.6.2.2 pull:拉

Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]

[USER@] :  以什么用户身份传输数据信息

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

SRC:          远端要恏过来的数据信息

[dest]        恏到本地什么位置

nfs01服务器作为本地,要获取rsync服务器上的数据,自然就成拉的概念了

[oldboy@nfs01 ~]$ rsync -rv [email protected]:/tmp /tmp/

[email protected]'s password:

receiving incremental file list

tmp/

tmp/a

tmp/b

tmp/c

tmp/d

tmp/e

rsync: send_files failed to open "/tmp/yum.log": Permission denied (13)

tmp/.ICE-unix/

tmp/oldboy_dir/

sent 137 bytes  received 459 bytes  170.29 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(1505) [generator=3.0.6]

[oldboy@nfs01 ~]$ ls  /tmp/

tmp

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

1.7 rsync软件在企业中的应用场景

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

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

一般是网站内部人员存储的数据信息,用定时同步

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

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

一般是网站外部人员存储的数据信息,用实时同步

1.8 rsync守护进程模式部署

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

01. backup服务器作为rsync服务端

02.rsync客户端服务器作为参照服务器,将数据推到rsync服务端

     

1.8.1 第一部分:配置rsync服务端(将服务端配置到backup服务器上)

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

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

rsync-3.0.6-12.el6.x86_64

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

vim /etc/rsyncd.conf  (默认没有这个配置文件)

cat >/etc/rsyncd.conf<<EOF

#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

[backup]

comment = "backup dir by baoge"

path = /backup

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

EOF

配置文件说明

###以上为配置文件的描述信息

#rsync_config

#created by baoge at 2017

##rsyncd.conf start##

###以上为配置文件的全局配置

uid = rsync

##用户 远端的命令使用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

##日志文件 显示出错信息等

###配置文件模块配置

[backup]                             ##模块名称

comment = "backup dir by baoge"  ##描述信息

path = /backup                      ##模块对应的位置  

ignore errors                        ##忽略错误程序

read only = false                    ##是否只读

list = false                           ##是否可以列表

hosts allow = 172.16.1.0/24         ##准许访问rsync服务器的范围(白名单)

hosts deny = 0.0.0.0/32             ##禁止访问rsync服务器的范围(黑名单)

auth users = rsync_backup          ##不存在的用户,只用于认证(开门)

secrets file = /etc/rsync.password   ##不存在的用户进行认证时的密钥文件

第三个里程碑:创建rsync服务管理用户

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

[root@backup ~]# id  rsync

uid=501(rsync) gid=501(rsync) groups=501(rsync)

第四个里程碑:创建数据备份存储目录

mkdir -p /backup

chown -R rsync.rsync /backup/

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

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

[root@backup ~]# chmod 600 /etc/rsync.password

[root@backup ~]# cat  /etc/rsync.password

rsync_backup:test123

[root@backup ~]# ll /etc/rsync.password

-rw------- 1 root root 23 Nov 28 11:24 /etc/rsync.password

第六个里程碑:启动rsync服务

[root@backup ~]# rsync --daemon

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

root      25120      1  0 11:27 ?        00:00:00 rsync --daemon

root      25122  24773  0 11:27 pts/1    00:00:00 grep rsync

[root@backup ~]# netstat -lntup|grep rsync  ##查看服务端口

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

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

至此:服务端配置完成

客户端进行推送数据测试

[root@nfs01 ~]# rsync -avz /etc/hosts [email protected]::backup

Password:

sending incremental file list

hosts

sent 201 bytes  received 27 bytes  11.69 bytes/sec

total size is 371  speedup is 1.63

服务端查看:

[root@backup ~]# ls /backup

hosts

1.8.2 第二部分:配置rsync客户端

架构中其他服务器称为rsync客户端

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

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

rsync-3.0.6-12.el6.x86_64

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

[root@nfs01 ~]# echo "test123"  >/etc/rsync.password

[root@nfs01 ~]# chmod 600 /etc/rsync.password

测试:

[root@nfs01 baoge_rsync]# touch {a,b,c}.txt

[root@nfs01 baoge_rsync]# ll

total 0

-rw-r--r-- 1 root root 0 Jun 29 17:39 a.txt

-rw-r--r-- 1 root root 0 Jun 29 17:39 b.txt

-rw-r--r-- 1 root root 0 Jun 29 17:39 c.txt

[root@nfs01 baoge_rsync]# rsync -avz /baoge_rsync/{a,b,c}.txt  [email protected]::backup --password-file=/etc/rsync.password

password file must not be other-accessible

continuing without password file

Password:

sending incremental file list

a.txt

b.txt

c.txt

sent 154 bytes  received 65 bytes  16.22 bytes/sec

total size is 0  speedup is 0.00

[root@backup backup]# ll

total 0

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:39 a.txt

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:39 b.txt

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:39 c.txt

数据推送正常

免交互式推送数据(设置环境变量)

[root@nfs01 ~]# export RSYNC_PASSWORD=test123       ###临时

echo  'export RSYNC_PASSWORD=test123' >>/etc/profile   ##永久

[root@backup backup]# ll

total 0

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:47 1.txt

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:47 2.txt

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:47 3.txt

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:39 a.txt

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:39 b.txt

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:39 c.txt

第三个里程碑:测试传输

守护进程方式推拉

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 -avz /etc/hosts  [email protected]::backup

非交互式:rsync -avz /etc/hosts  [email protected]::backup --password-file=/etc/rsync.password

实践测试:

推:

[root@nfs01 ~]# rsync -avz /tmp/push.txt [email protected]::backup

sending incremental file list

push.txt

sent 65 bytes  received 27 bytes  184.00 bytes/sec

total size is 0  speedup is 0.00

对端查看:

[root@backup backup]# ll

total 0

-rw-r--r-- 1 rsync rsync 0 Jun 30 12:39 push.txt

-rw-r--r-- 1 root  root  0 Jun 30 12:22 rsynctest.txt

拉:

[root@nfs01 ~]# rsync -avz [email protected]::backup/rsynctest.txt /tmp

receiving incremental file list

rsynctest.txt

sent 83 bytes  received 142 bytes  450.00 bytes/sec

total size is 0  speedup is 0.00

[root@nfs01 ~]# ll /tmp

total 0

-rw-r--r-- 1 root root 0 Jun 30 12:22 rsynctest.txt

##常见问题:

[root@nfs01 tmp]# rsync -avz /etc/hosts  [email protected]::backup

Password:

sending incremental file list

hosts

rsync: mkstemp ".hosts.U5OCyR" (in backup) failed: Permission denied (13)

sent 200 bytes  received 27 bytes  13.76 bytes/sec

total size is 371  speedup is 1.63

rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]

说明:备份目录权限设置不正确

1.8.3 第三部分:rsync服务的重启—kill

1.     kill 进程pid

kill -9 强制杀死

说明:需要知道进程号信息,且进程杀死后会有说明信息

2.killall 进程名

说明:进程杀死后有提示说明

3.pkill 进程名(模糊杀死)

说明:进程杀死后没有提示说明

1.9 rsync参数

命令参数

参数说明

-v,--verbose

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

-z,--compress

传输时进行压缩以提高传输效率,--compress可以按级别压缩。局域网可以不用压缩

-a,--archive重要

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

-r,--recursive   归类于-a

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

-t,--times    归类于-a

保持文件时间信息

-o,--owner   归类于-a

保持文件属主信息

-p,--perms   归类于-a

保持文件权限

-g,--group   归类于-a

保持文件属组信息

-D,--devices 归类于-a

保持设备文件信息

-l,--links    归类于-a

保留软链接(小写L

-P,--progress

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

-e

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

例如:ssh

-exclude=PATTENRN

指定排除不需要传输的文件信息(类似于tar中)

--exclude-from=file

文件所在的目录,即可以实现排除多个文件(类似于tar中)

--bwlimit=RATE

传输文件时限速

limit I/O   bandwidth;KBbytes per cecond

limit socket I/O   bandwidth限速功能

案例:某DBA做数据同步,带宽占满,导致用户无法访问网站

--delete

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

注:

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

这里的-avzP相当于-vzrtopgDlP(还多了Dl功能),生产环境常用的参数选项为-avzP-vzrtopgP

如果放入脚本中,也可以把-v-P去掉。这里的--progress可以用 -P代替

提示信息:

以上参数还可以使用man rsync或者参考资料地址。

thhp://www.samba.org/ftp/rsync/rsync.html OPTIONS SUMMARY

生产参数:-avz或者-vzrtopg

更多参数:http://www.samba.org/ftp/rsync/rsync.html

1.9.1 案例:某DBA做数据同步,带宽占满,导致用户无法访问网站

1.10 利用xinetd超级守护进程启动rsync服务

第一个里程碑:安装xinetd服务

       yum install -y xinetd

第二个里程碑:让rsync服务可以被xinetd服务所管理

       [root@backup xinetd.d]# vim /etc/xinetd.d/rsync

    # default: off

    # description: The rsync server is a good addition to an ftp server, as it \

    #     allows crc checksumming etc.

    service rsync

    {

           disable  = no                 <-- 把默认yes修改为no

           flags             = IPv6

           socket_type     = stream

           wait            = no

           user            = root

           server          = /usr/bin/rsync

           server_args     = --daemon

           log_on_failure  += USERID

    }

第三个里程碑:停止rsync服务,利用xinetd服务重启启动

       [root@backup ~]# killall rsync

       [root@backup ~]# /etc/init.d/xinetd start

    Starting xinetd:                                           [  OK  ]

       [root@backup ~]# netstat -lntup|grep 873

    tcp        0      0 :::873                      :::*                        LISTEN      4833/xinetd

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

[root@nfs01 ~]# rsync -av /etc/hosts [email protected]::backup

sending incremental file list

   

sent 26 bytes  received 8 bytes  68.00 bytes/sec

total size is 378  speedup is 11.12

1.11 rsync服务的扩展功能

1.11.1 设置rsync软件开机启动(仅服务端)

1.     配置/etc/rc.local文件

echo 'rsync --daemon' >>/etc/rc.local

2.     配置/etc/init.d/目录(需要自己编写启动脚本)

3.     xinetd服务启动rsync

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

第一个里程碑:编写配置文件添加多模块

[root@backup ~]# cat  /etc/rsyncd.conf

#rsync_config

#created by baoge 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 baoge"

path = /backup

[backup_dev]

comment = "backup dir by Baohong"

path = /backup_dev

第二个里程碑:重启rsync服务

killall rsync

rsync --daemon

创建新模块的备份目录

mkdir -p /backup_dev

chown -R rsync.rsync /backup_dev/

实践测试

[root@nfs01 ~]# rsync -avz /etc/hosts  [email protected]::backup_dev --password-file=/etc/rsync.password

sending incremental file list

hosts

sent 201 bytes  received 27 bytes  456.00 bytes/sec

total size is 371  speedup is 1.63

[root@backup ~]# ls /backup_dev

hosts

[root@nfs01 ~]# rsync -avz /etc  [email protected]::backup_dev

[root@backup ~]# ll /backup_dev/

total 8

drwxr-xr-x 79 rsync rsync 4096 Jun 29 17:25 etc

-rw-r--r--  1 rsync rsync  337 Jun 28 01:38 hosts

1.11.3 rsync排除功能实践

创建模拟环境

[root@nfs01 ~]# mkdir -p /paichu

[root@nfs01 ~]# mkdir  /paichu/{a..d}

[root@nfs01 ~]# touch  /paichu/{a..d}/{1..4}

[root@nfs01 ~]# tree  /paichu

/paichu

├── a

   ├── 1

   ├── 2

   ├── 3

   └── 4

├── b

   ├── 1

   ├── 2

   ├── 3

   └── 4

├── c

   ├── 1

   ├── 2

   ├── 3

   └── 4

└── d

    ├── 1

    ├── 2

    ├── 3

    └── 4

4 directories, 16 files

法一:

[root@nfs01 ~]# cd   /paichu

[root@nfs01 paichu]#

[root@nfs01 paichu]# ll

total 16

drwxr-xr-x 2 root root 4096 Jun 30 14:57 a

drwxr-xr-x 2 root root 4096 Jun 30 14:57 b

drwxr-xr-x 2 root root 4096 Jun 30 14:57 c

drwxr-xr-x 2 root root 4096 Jun 30 14:57 d

rsync -av --exclude=a --exclude=b --exclude=d/4 /paichu/ [email protected]::backup_dev

[root@nfs01 paichu]# rsync -av --exclude=a --exclude=b --exclude=d/4 /paichu/ [email protected]::backup_dev

sending incremental file list

./

c/

c/1

c/2

c/3

c/4

d/

d/1

d/2

d/3

sent 387 bytes  received 152 bytes  1078.00 bytes/sec

total size is 0  speedup is 0.00

服务端查看:

[root@backup ~]# ll /backup_dev

total 8

drwxr-xr-x 2 rsync rsync 4096 Jun 30 14:57 c

drwxr-xr-x 2 rsync rsync 4096 Jun 30 14:57 d

[root@backup ~]# tree /backup_dev

/backup_dev

├── c

   ├── 1

   ├── 2

   ├── 3

   └── 4

└── d

    ├── 1

    ├── 2

    └── 3

2 directories, 7 files

法二:

事先编写排除文件

[root@nfs01 paichu]# cat exclude.txt  -A

c$

d$

a/1$

exclude.txt$

推送数据测试:

[root@nfs01 paichu]# rsync -av  --exclude-from=exclude.txt /paichu/ [email protected]::backup_dev

sending incremental file list

./

a/

a/2

a/3

a/4

b/

b/1

b/2

b/3

b/4

sent 387 bytes  received 152 bytes  1078.00 bytes/sec

total size is 0  speedup is 0.00

服务端查看

[root@backup ~]# rm -rf /backup_dev/*

[root@backup ~]# ll /backup_dev

total 0

[root@backup ~]# ll /backup_dev

total 8

drwxr-xr-x 2 rsync rsync 4096 Jun 30 14:57 a

drwxr-xr-x 2 rsync rsync 4096 Jun 30 14:57 b

[root@backup ~]# tree /backup_dev

/backup_dev

├── a

   ├── 2

   ├── 3

   └── 4

└── b

    ├── 1

    ├── 2

    ├── 3

    └── 4

2 directories, 7 files

##可以不用事先在服务端创建备份目录 但是不可以创建多级目录   

服务端也可以/etc/rsyncd.conf里配置排除

1.11.4 分类备份

o  备份时针对不同的人员将数据分别备份至期对应目录下的实践:

人员身份:            分类备份目录

运维                   sa

开发                   dev

DBA                    dba

要求说明:

1./fenlei/a目录备份至运维备份目录/backup/sa

2./fenlei/b目录备份至开发备份目录/backup/dev

3./fenlei/c目录备份至DBA备份目录/backup/dba

实践操作:

服务端:

[root@backup backup]# ll

total 0

[root@backup backup]#

客户端:

[root@nfs01 fenlei]# ll

total 12

drwxr-xr-x 2 root root 4096 Jun 30 14:57 a

drwxr-xr-x 2 root root 4096 Jun 30 14:57 b

drwxr-xr-x 2 root root 4096 Jun 30 14:57 c

运维人员数据备份

[root@nfs01 fenlei]# rsync -av /fenlei/a [email protected]::backup/sa

sending incremental file list

created directory sa

a/

a/1

a/2

a/3

a/4

sent 222 bytes  received 88 bytes  620.00 bytes/sec

total size is 0  speedup is 0.00

开发人员数据备份

[root@nfs01 fenlei]# rsync -av /fenlei/b [email protected]::backup/dev

sending incremental file list

created directory dev

b/

b/1

b/2

b/3

b/4

sent 222 bytes  received 88 bytes  620.00 bytes/sec

total size is 0  speedup is 0.00

DBA人员数据备份

[root@nfs01 fenlei]# rsync -av /fenlei/c [email protected]::backup/dba

sending incremental file list

created directory dba

c/

c/1

c/2

c/3

c/4

sent 222 bytes  received 88 bytes  620.00 bytes/sec

total size is 0  speedup is 0.00

服务端查看备份数据:

[root@backup backup]# ll

total 12

drwxr-xr-x 3 rsync rsync 4096 Jun 30 16:37 dba

drwxr-xr-x 3 rsync rsync 4096 Jun 30 16:36 dev

drwxr-xr-x 3 rsync rsync 4096 Jun 30 16:36 sa

[root@backup backup]# tree ./

./

├── dba

   └── c

       ├── 1

       ├── 2

       ├── 3

       └── 4

├── dev

   └── b

       ├── 1

       ├── 2

       ├── 3

       └── 4

└── sa

    └── a

        ├── 1

        ├── 2

        ├── 3

        └── 4

6 directories, 12 files

说明:在客户端做备份时,不必先在服务端创建对应下的目录,只需要在客户端备份命令的相应模块后面加上相应的目录名就行了

rsync -av /fenlei/b [email protected]::backup/dev

    注意:这种方式只能创建一级目录,不能创建多级目录

1.11.5 守护进程的访问权限控制配置

hosts allow = 172.16.1.0/24  (只允许172.16.1.0/24这个网段的主机访问)

hosts deny = 0.0.0.0/32      (禁止0.0.0.0/32这个网段的主机访问)

因为0.0.0.0/32这个地址不存在,那么把黑名单转换成白名单,就是没有禁止访问的网段,所以它的权限大于白名单的权限

说明:白名单和黑名单同时存在是,权限为大着优先使用

实践证明:将nfs服务器/fenlei目录下的数据备份至rsync服务器的/backup目录下,走公网

设想:不能备份

###服务端:

###[root@backup backup]# ll

###total 0

客户端备份

[root@nfs01 fenlei]# rsync -av /fenlei/ [email protected]::backup

sending incremental file list

./

a/

a/1

a/2

a/3

a/4

b/

b/1

b/2

b/3

b/4

c/

c/1

c/2

c/3

c/4

sent 631 bytes  received 251 bytes  1764.00 bytes/sec

total size is 0  speedup is 0.00

服务端查看:

[root@backup backup]# ll

total 12

drwxr-xr-x 2 rsync rsync 4096 Jun 30 14:57 a

drwxr-xr-x 2 rsync rsync 4096 Jun 30 14:57 b

drwxr-xr-x 2 rsync rsync 4096 Jun 30 14:57 c

结论:白名单和黑名单同时存在是,权限为大着优先使用

o  设计只让内网地址为172.16.1.0/24这个网段的主机备份

hosts allow = 172.16.1.0/24

#hosts deny = 0.0.0.0/32

[root@backup backup]# killall rsync

[root@backup backup]# killall rsync

rsync: no process killed

[root@backup backup]# rsync --daemon

[root@backup backup]# ps -ef|grep [r]sync

root       1710      1  0 17:11 ?        00:00:00 rsync --daemon

[root@backup backup]# netstat -lnupt|grep [r]sync

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

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

再次测试:

删除之前向服务端备份的数据:

[root@backup backup]# rm -rf *

[root@backup backup]# ll

total 0

客户端备份数据:

[root@nfs01 fenlei]# rsync -av /fenlei/ [email protected]::backup

@ERROR: Unknown module 'backup'

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

###可以发现已经有权限限制

客户端也没有新的备份数据

[root@backup backup]# ll

total 0

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

首先确保,服务端与客户端数据信息一致

1.11.6.1          推方式

说明:客户端/fenlei目录与服务端/backup无差异同步

服务端数据查看

[root@backup backup]# ll

total 4

drwxr-xr-x 2 rsync rsync 4096 Jun 30 14:57 a

客户端数据查看

[root@nfs01 fenlei]# ll

total 12

drwxr-xr-x 2 root root 4096 Jun 30 14:57 a

drwxr-xr-x 2 root root 4096 Jun 30 14:57 b

drwxr-xr-x 2 root root 4096 Jun 30 14:57 c

客户端执行无差异同步命令

[root@nfs01 fenlei]# rsync -avz --delete /fenlei/ [email protected]::backup

sending incremental file list

./

b/

b/1

b/2

b/3

b/4

c/

c/1

c/2

c/3

c/4

sent 452 bytes  received 172 bytes  1248.00 bytes/sec

total size is 0  speedup is 0.00

服务端再次查看数据,是否与客户端数据一致

[root@backup backup]# ll

total 12

drwxr-xr-x 2 rsync rsync 4096 Jun 30 14:57 a

drwxr-xr-x 2 rsync rsync 4096 Jun 30 14:57 b

drwxr-xr-x 2 rsync rsync 4096 Jun 30 14:57 c

        推方式数据无差异同步:本地没有的数据信息,远程服务端也不能有/ 本地有的数据信息,远程服务端也必须有

1.11.6.2          拉方式

服务端/backup_dev目录客户端/baoge_rsync目录与无差异同步

服务端数据查看

[root@backup backup_dev]# ll

total 0

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:47 1.txt

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:47 2.txt

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:47 3.txt

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:39 a.txt

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:39 b.txt

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:39 c.txt

 

客户端数据查看

[root@nfs01 baoge_rsync]# ll

total 0

-rw-r--r-- 1 root root 0 Jun 29 17:47 1.txt

-rw-r--r-- 1 root root 0 Jun 29 17:47 2.txt

-rw-r--r-- 1 root root 0 Jun 29 17:47 3.txt

客户端执行无差异同步命令

[root@nfs01 baoge_rsync]# rsync -avz --delete  [email protected]::backup_dev /baoge_rsync/

receiving incremental file list

./

a.txt

b.txt

c.txt

sent 137 bytes  received 309 bytes  892.00 bytes/sec

total size is 0  speedup is 0.00

客户端查看数据有没有同步

[root@nfs01 baoge_rsync]# ll

total 0

-rw-r--r-- 1 501 501 0 Jun 29 17:47 1.txt

-rw-r--r-- 1 501 501 0 Jun 29 17:47 2.txt

-rw-r--r-- 1 501 501 0 Jun 29 17:47 3.txt

-rw-r--r-- 1 501 501 0 Jun 29 17:39 a.txt

-rw-r--r-- 1 501 501 0 Jun 29 17:39 b.txt

-rw-r--r-- 1 501 501 0 Jun 29 17:39 c.txt

拉方式数据无差异同步:远端没有的数据信息,本地客户端也不能有/ 远端有的数据信息,本地客户端也必须有

   注意:无差异同步数据备份功能慎用

此命令还有一个特殊用途:快速清空大文件

服务端大文件

[root@backup backup_dev]# ll

total 4

-rw-r--r-- 1 root root 1275 Jun 30 20:59 big.txt

客户端执行无差异同步数据命令

客户端的空文件

[root@nfs01 baoge_rsync]# ll

total 0

-rw-r--r-- 1 root root 0 Jun 30 21:00 null.txt

[root@nfs01 baoge_rsync]# rsync -avz --delete /baoge_rsync/null.txt [email protected]::backup_dev/big.txt --password-file=/etc/rsync.passwordsending incremental file list

null.txt

sent 69 bytes  received 39 bytes  216.00 bytes/sec

total size is 0  speedup is 0.00

查看服务端大文件的大小

[root@backup backup_dev]# ll

total 0

-rw-r--r-- 1 rsync rsync 0 Jun 30 21:00 big.txt

结论:已经快速清空了服务端大文件中的数据

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

  rsync服务端配置文件中

  list = false    --- 表示关闭显示模块信息列表功能

  list = true     --- 表示开启显示模块信息列表功能

  

客户端查看模块信息命令

  rsync [email protected]::

参数功能说明:再执行同步数据命令的时候会把服务端配置文件里面的所有模块信息显示出来,再无它用。


猜你喜欢

转载自blog.51cto.com/13131196/2135814