1.rsync简介

“rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步”

2.rsync的功能和特点

1. 可以实现服务器各种资源的备份(可以夸文件系统)

2. linux-rsyncwindows-cwrsync

3. 可以做全量备份,也可以做增量备份

4. 在做备份的时候,可以排除一些特定的文件不做备份

5. 可以结合ssh实现加密传输

6. rsync支持工作在后台的模式(守护进程模式)

7. rsync可以结合inotify/sersync实现自动实时备份

8. rsync在传输文件的过程中可做限速

3.rsync的工作模式

1. 命令行模式(shell模式)

类似于cpdd命令,实现备份文件的复制(备份)

2. 远程模式(远程shell模式)

利用ssh实现数据的远程传输,类似于 scp

3. 列表模式

类似于执行ls命令,仅仅用于列出文件内容列表(不是做复制操作)

4. 后台模式(守护进程模式)

rsync是工作在后台的

4.rsync实现自动备份

rsync实时自动备份

结合inotify/sersync

inotify/sersync:监控文件和目录中的文件是否发生更改(找出新文件)

rsync:将找出的文件备份过去

rsync的用法

1.本地使用(cp

2.远程使用(scp

3.守护进程(socket

5.rsync选项详解

选项
-p:保持文件的权限属性不变
-v:显示执行过程信息
-r:目录做递归
-a:归档(包含r)
-z:压缩
-l:仅仅传输软链接自身
-L:传输软连接所指向的原始文件
-b:在备份文件的时候,如果备份文件已经存在,会将目标位置下旧的文件重命名,然后生成新的备份
--suffix=xxx        指定旧备份文件的后缀名
--backup-dir=xxxx   指定将旧备份文件移动到哪个位置下
-e:结合ssh实现加密传输
-e "ssh -p 22"
组合:-avz

6.rsync配置文件参数详解

模块:其实就是一个目录,这个模块用于保存客户端所传递过来的文件
    参数:
        pid file:指定rsync的pid文件的保存位置
        uid:指定运行rsync进程的用户id
        gid:指定运行rsync进程的用户组id
        port:指定rsync所监听的端口(默认端口873)
        path:指定模块所对应的目录的位置
        use chroot:指定将用户锁定在家目录中
        max connections:最大连接数(指定最多同时有多少个客户端在传输文件)
        log file:指定日志文件的位置
        log format:指定日志格式
        lock file:指定rsync进程的锁文件的位置
        read only:指定模块是否为只读状态
        write only:指定模块是否为只写状态
        list:指定用用户是否可以查看模块所对应的路径下的文件列表
        exclude:指定排除不做传输的文件
        exclude from:通过读取一个文件,来获取不对哪些文件做传输
        auth users:指定用于连接这个模块所要使用的匿名用户名
        secrets file:指定保存虚拟用户和密码文件
        hosts allow:指定可以做文件传输的主机
        hosts deny:黑名单
        timeout:指定客户端的超时时间

7.inotify简介

inotify查找发送了改变的文件或者新文件,可以持续检测一个目录中的文件是否发生更改。

软件inotify-tools有两个组件

inotifywait:检测一个目录下的文件所发生事件

inotifywatch:统计所触发的事件的次数

8.inotifywait的选项

    -r:递归目录
    -q:仅仅打印监控的事件信息
    -m:一直处于监控状态[组合使用-mqr]
    --excludei:排除文件或目录(不区分大小写)
    --exclude:排除文件或目录(区分大小写)
    --format:指定输出格式
        %w:显示触发监控的事件的所在路径.[%w%f组合使用就能得到文件名]
        %f:显示触发监控的事件的文件名
        %e:显示所触发的事件
        %T:显示事件的触发时间
    --timefmat:指定输出的时间的格式
    -e:指定要监控的事件

9.inotify监控的事件

    access              file or directory contents were read
    modify              file or directory contents were written
    attrib              file or directory attributes changed
    close_write          file or directory closed, after being opened in writeable mode
    close_nowrite      file or directory closed, after being opened in read-only mode
    close              file or directory closed, regardless of read/write mode
    open              file or directory opened
    moved_to          file or directory moved to watched directory
    moved_from          file or directory moved from watched directory
    move              file or directory moved to or from watched directory
    create              file or directory created within watched directory
    delete              file or directory deleted within watched directory
    delete_self          file or directory was deleted
    unmount              file system containing file or directory unmounted

10.rsync+inotify全网自动化备份实战案例

1.环境准备

centos7.5

rsync服务端IP:172.20.10.7/28

inotify服务端IP:172.20.10.8/28

2.关闭安全服务

[root@ken ~]# systemctl stop firewalld
[root@ken ~]# setenforce 0
[root@ken ~]# iptables -F

3. 配置rsync服务端

创建共享目录

[root@ken ~]# mkdir /ken

创建用于运行rsync进程的用户

[root@ken ~]# useradd -r -s /sbin/nologin -u 333 kenken

修改属主和数组

[root@ken ~]# chown -R kenken.kenken /ken

查看rsync的文件,/etc/rsyncd.conf,此文件默认不存在,需要自己手工写。

[root@ken ~]# rpm -ql rsync
/etc/rsyncd.conf
/etc/sysconfig/rsyncd
/usr/bin/rsync
...

创建rsync配置文件

[root@ken ~]# vim /etc/rsyncd.conf
pid file=/var/lock/subsys/pidfile
lock file=/var/lock/subsys/rsync
log file=/var/log/rsync
uid=333
gid=333
timeout=100
max connections=199
[ken]
path=/ken
list=yes
use chroot=yes
read only=no
auth users=user1
secrets file=/etc/rsyncd.pwd
hosts allow=172.20.10.8/28

创建虚拟用户文件

[root@ken ~]# echo "user1:123">>/etc/rsyncd.pwd

修改权限为600

[root@ken ~]# chmod 600 /etc/rsyncd.pwd 

启动rsync服务

[root@ken ~]# rsync --daemon
[root@ken ~]# ss -tnl | grep 873
LISTEN     0      5            *:873                      *:*
LISTEN     0      5           :::873                     :::*                  

 4.配置inotify服务端

 下载inotify需要配置epel仓库,复制如下代码到你的yum配置文件里面即可

[ken]
name=ken
enabled=1
gpgcheck=0
baseurl=https://mirrors.aliyun.com/epel/7Server/x86_64/

下载inotify

[root@ken ~]# yum install rsync inotify-tools -y

创建需要备份数据的目录

[root@ken ~]# mkdir /kenken

创建一个保存rsync虚拟用户和密码的文件并更改权限为600

[root@ken ~]# echo "123" >>/etc/rsync.pwd
[root@ken ~]# chmod 600 /etc/rsync.pwd

 创建自动化监控备份脚本

[root@ken ~]# vim authbak.sh
#!/bin/bash
prog="inotifywait"
events="create,delete,close_write"
opt="-mrq"
dir="/kenken"
remote_host="172.20.10.7"
anon_user="user1"
mod_name="ken"
pwd_file="/etc/rsync.pwd"
$prog $opt -e $events --format "%w%f" $dir | while read line
do
     rsync -rz --delete $dir $anon_user@$remote_host::$mod_name --password-file=$pwd_file
done

客户端运行脚本

[root@ken ~]# bash authbak.sh

5.在客户端进行测试

客户端创建文件测试

[root@ken kenken]# touch {1..10}.txt
[root@ken kenken]# ls
10.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

在rsync服务器端查看是否有相同文件

[root@ken ken]# ls
10.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

测试成功!

客户端删除文件测试

[root@ken kenken]# rm -rf 1.txt 4.txt 
[root@ken kenken]# ls
10.txt 2.txt 3.txt 5.txt 6.txt 7.txt 8.txt 9.txt

在rsync服务器端查看文件是否也已经被删除

[root@ken kenken]# ls
10.txt  2.txt  3.txt  5.txt  6.txt  7.txt  8.txt  9.txt

测试成功!

至此我们已经可以实现全网自动化, 无差异化数据备份了。

10-02 13:16