zoukankan      html  css  js  c++  java
  • rsync

    rsync

    简介

    远程同步(remote sync),是linux系统下的镜像备份工具。

    特性

    • 可以镜像保存整个目录树和文件系统

    • 支持同步背备份大数量的小文件

    • 保持源文件的权限时间链接等

    • 无需特殊权限(普通用户也能同步)

    • 第一次同步会复制全部内容,下一次只会传输修改过的文件(增量备份),传输过程会进行解压缩,节省带宽(远程备份,异地灾备)

    • 支持匿名传输

    远程同步

    同步系统文件需要远程主机remote认证,可选择的协议有:

    • rsync协议
    • ssh协议

    ssh认证协议

    • 和scp原理一致,通过分发key来实现免密通信

    • 使用该协议时,源服务器端不需要启动守护进程和配置rsync配置文件,直接获取远程用户密码。

      //-a宿主变化时间不变 -z压缩传输 -v显示详情
      [root@DR ~]# rsync -avz /root/anaconda-ks.cfg -e ssh root@node2:/root
      sending incremental file list
      anaconda-ks.cfg
      
      sent 96 bytes  received 47 bytes  95.33 bytes/sec
      total size is 1,179  speedup is 8.24
      
      //node2端验证
      [root@node2 /]# ll tmp/
      total 4
      -rw-------. 1 root root 1179 Apr 22 10:39 anaconda-ks.cfg
      
      

    rsync语法

    Usage: rsync [OPTION]... SRC [SRC]... DEST #本地备份
      or   rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST #远程备份
      or   rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST #远程备份(rsync协议)
      or   rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST
      or   rsync [OPTION]... [USER@]HOST:SRC [DEST]
      or   rsync [OPTION]... [USER@]HOST::SRC [DEST]
      or   rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
      
      //rsync常用选项:
        -a, --archive       //归档
        -v, --verbose       //啰嗦模式
        -q, --quiet         //静默模式
        -r, --recursive     //递归
        -p, --perms         //保持原有的权限属性
        -z, --compress      //在传输时压缩,节省带宽,加快传输速度
        --delete            //在源服务器上做的删除操作也会在目标服务器上同步
    
    • 本地同步
    //同步文件
    [root@DR ~]# rsync -avz /root/anaconda-ks.cfg /tmp/ 
    [root@DR ~]# ll /tmp/|grep ana*
    -rw-------  1 root   root   1179 Apr 22 10:39 anaconda-ks.cf
    //同步目录
    [root@DR ~]# tree a
    a
    ├── b
    └── c
    
    [root@DR ~]# rsync -avz /root/a  /tmp/A
    /tmp/A
    └── a
        ├── b
        └── c
    
    • 远程同步
    [root@DR ~]# rsync -avz /root/anaconda-ks.cfg root@node2:/tmp
    [root@node2 /]# ll tmp/|grep ana.*
    -rw-------. 1 root root    1179 Apr 22 10:39 anaconda-ks.cfg
    

    rsync和inotify组合

    引入

    相比传统cp ;tar备份,rsync具有安全性高备份迅速支持增量备份等优点。但随着系统规模深入,数据量增大,rsync每次备份需要扫描所有文件进行对比,再进行差量传输,但往往发生变化的部分只占了很小一部分,导致效率低下。同时不能实时同步,这时inotify 的出现则可以有效解决该问题。

    linux内核从2.5.13起加入了Inotify支持,通过该接口,第三方软件就能监控文件系统上下文的各种变化情况。

    基于rsync和inotify的应用实现实时同步文件

    环境:

    类型 ip 应用 系统
    源服务器(source) 192.168.94.141 rsync&inotify&脚本 rhel8
    目标服务器(dest) 192.168.94.143 rsync rhel8

    需求:

    • 同步etc下的目录到目标服务器的tmp目录下

    目标服务器端

    [root@dest ~]# systemctl stop firewalld.service 
    [root@dest ~]# setenforce 0
    [root@dest ~]# systemctl stop firewalld.service 
    [root@dest ~]# yum -y install rsync
    
    [root@dest ~]# cat /etc/rsyncd.conf 
    log file = /var/log/rsyncd.log    
    pidfile = /var/run/rsyncd.pid     
    lock file = /var/run/rsync.lock   
    secrets file = /etc/rsync.pass   
    
    [etc_from_client]    
    path = /tmp/          
    comment = sync etc from client
    uid = root        
    gid = root       
    port = 873       
    ignore errors  
    use chroot = no       
    read only = no    
    list = no     
    max connections = 200 
    timeout = 600
    auth users = admin 
    hosts allow = 192.168.94.143
    
    [root@dest ~]# echo 'admin:123456'>/etc/rsync.pass
    [root@dest ~]# cat /etc/rsync.pass
    admin:123456
    [root@dest ~]# rsync --daemon 
    [root@dest ~]# ss -antl
    State   Recv-Q  Send-Q   Local Address:Port    Peer Address:Port  
    LISTEN  0       5              0.0.0.0:873          0.0.0.0:*     
    LISTEN  0       128            0.0.0.0:22           0.0.0.0:*     
    LISTEN  0       5                 [::]:873             [::]:*     
    LISTEN  0       128               [::]:22              [::]:*
    
    

    源服务器

    [root@source ~]# yum -y install inotify-tools rsync
    [root@source ~]# echo '123456'> /etc/rsync.pass
    [root@source ~]# chmod 600 /etc/rsync.pass
    [root@source ~]# mkdir -pv /root/etc/test
    mkdir: created directory '/root/etc'
    mkdir: created directory '/root/etc/test'
    [root@source ~]# echo  'a' >etc/test/a
    [root@source ~]# rsync  -avH --port 873 --progress --delete /root/etc/ admin@192.168.94.143::etc_from_client --password-file=/etc/rsync.pass
    ...
    ./
    test/
    
    sent 77 bytes  received 3,330 bytes  6,814.00 bytes/sec
    total size is 0  speedup is 0.00
    
    
    //目标服务器验证
    [root@dest ~]# tree /tmp/test/
    /tmp/test/
    └── a
    
    0 directories, 1 file
    
    [root@DR ~]# cat /etc/rsync.pass 
    123456
    [root@DR ~]# vim scripts/inotify.sh 
    
    #!/bin/bash
    host=192.168.94.143     # 目标服务器的ip(备份服务器)
    src=/root/etc        # 在源服务器上所要监控的备份目录(此处可
    以自定义,但是要保证存在)
    des=etc_from_client     # 自定义的模块名,需要与目标服务器上>定义的同步名称一致
    password=/etc/rsync.pass        # 执行数据同步的密码文件
    user=admin          # 执行数据同步的用户名
    inotifywait=/usr/bin/inotifywait
    
    $inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src 
            | while read files;do
        rsync -avzP --delete  --timeout=100 --password-file=${password} $src $user@$host::$des
            echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
    done
    
    [root@DR ~]# nohup /bin/bash scripts/inotify.sh &
    [root@DR ~]# ps -ef|grep inotify
    root        1948    1742  0 10:54 pts/0    00:00:00 /bin/bash scripts/inotify.sh
    root        1949    1948  0 10:54 pts/0    00:00:00 /usr/bininotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /root/etc
    root        1950    1948  0 10:54 pts/0    00:00:00 /bin/bash scripts/inotify.sh
    root        2033    1742  0 11:05 pts/0    00:00:00 grep --color=auto inotify
    
    [root@DR ~]# tree etc/
    etc/
    ├── a
    └── b
    
    //模拟变化
    [root@DR ~]# touch etc/a/test
    [root@DR ~]# tree etc/
    etc/
    ├── a
    │   └── test
    └── b
    //目标服务器查看效果
    [root@dest ~]# tree /tmp/
    /tmp/
    └── etc
        ├── a
        │   └── test
        └── b
    

    基于systemctl的服务自启控制

    [root@node2 ~]# echo 'OPTIONS=""' > /etc/sysconfig/rsyncd
    [root@node2 ~]# vim /etc/systemd/system/rsyncd.service 
    
    [Unit]
    Description=rsync daemon
    ConditionPathExists=/etc/rsyncd.conf
    After=network.target remote-fs.target nss-lookup.target
    
    [Service]
    Type=simple
    ExecStart=/usr/bin/rsync --daemon --no-detach "$OPTIONS"
    ExecReload=/bin/kill -s HUP $MAINPID
    ExecStop=/bin/kill -s QUIT $MAINPID
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    
    [root@node2 ~]# systemctl daemon-reload 
    [root@node2 ~]# systemctl list-unit-files |grep rsync
    rsyncd.service                             enabled  
    
    
  • 相关阅读:
    斐波那契数列
    旋转数组的最小数字
    用两个栈实现队列
    重建二叉树
    从尾到头打印链表
    2020/01/11,人活着是为了一口气
    2020/01/11,放肆和克制,敏感层次
    2020/01/11,记忆单元
    2020/01/11,经济基础决定高层建筑和个性
    git
  • 原文地址:https://www.cnblogs.com/fangxinxin/p/14753610.html
Copyright © 2011-2022 走看看