zoukankan      html  css  js  c++  java
  • rsync服务及配置

    rsync介绍

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

    此工具功能类似ssh带的scp,但又优于scp命令的功能,scp每次都是全量拷贝,而rsync可以增量拷贝。

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

    rsync特性

    • 支持拷贝特殊文件如链接文件,设备等。
    • 保持源文件或目录的权限、时间、软硬链接、属主、组等所有属性均不改变
    • 可以实现增量同步,既只同步发生变化的数据,因此数据传输效率很高
    • 可以使用rcp,rsh,ssh等方式来配合传输文件(rsync本身对数据不加密)
    • 持匿名或认证(无需系统用户)的进程模式传输,可实现方便安全的进行数据备份及镜像

    注意:拷贝的时候源目录加“/”和不加“/”的区别(加“/”表示只拷贝该目录之下的文件;不加“/”表示连该目录一起拷贝)

    rsync三种工作方式

    本地文件系统上实现同步(类似copy命令)

    语法: rsync [OPTION]... SRC DEST

    [root@localhost media]# rsync share_test/file1 /tmp/
    [root@localhost media]# ls /tmp/
    file1  vmware-root_1175-3980167284
    

    实现将本地机器的内容拷贝到远程机器(类似scp,可以配合ssh进行加密使用)

    语法:rsync [OPTION]... SRC [USER@]HOST:DEST

    [root@localhost media]# rsync -avz share_test root@192.168.197.133:/media
    root@192.168.197.133's password: 
    sending incremental file list
    share_test/
    share_test/file1
    
    
    

    将远程机器的内容拷贝到本地机器

    语法:rsync [OPTION]... [USER@]HOST:SRC DEST

    [root@localhost media]# rsync -avz root@192.168.197.133:/media/123 share_test/
    root@192.168.197.133's password: 
    receiving incremental file list
    123
    
    [root@localhost media]# ls share_test/
    123  file1
    

    rsync常用选项

    • -a

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

    • -r

    对子目录以递归模式处理

    • -v, --verbose

    详细模式输出

    • -z

    对备份的文件在传输时进行压缩处理

    • --delete

    删除那些DST中SRC没有的文件

    dest端新建文件

    [root@localhost media]# touch share_test/456
    [root@localhost media]# ls share_test/
    456  file1
    

    src再次同步同一目录时,添加--delete参数,dest的456文件即将删除

    [root@localhost media]# rsync -avzr share_test --delete root@192.168.197.133:/media/
    root@192.168.197.133's password: 
    sending incremental file list
    deleting share_test/456
    share_test/
    share_test/123
    
    sent 149 bytes  received 57 bytes  82.40 bytes/sec
    total size is 0  speedup is 0.00
    
    
    [root@localhost media]# ls share_test/
    file1
    
    

    rsync+inotify

    inotify是一种文件系统事件监控机制。配合rsync可实现Daemon模式。inotify-tools为inotify的第三方软件。用来监控文件系统下文件的各种变化情况。

    Daemon,即守护进程。该模式是在一台rsync服务器上安装并运行一个rsync的服务进程,其他的客户端通过rsync命令上传文件到服务器中。该模式是rsync最常用的功能,用来做数据的定时或者实时备份

    Daemon配置过程

    服务端(dest)配置:

    root@localhost ~]# yum install -y rsync-daemon 安装Daemon工具
    ......
    
    ## 修改配置文件
    [root@localhost ~]# vim /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
    
    [share_test]
    path = /sawyer
    uid = root
    gid = root
    port = 873   服务端口873
    ignore errors
    use chroot = no
    read only = no
    list = no
    max connections = 200
    timeout = 600
    auth users = share_user   指定登录用户
    hosts allow = 192.168.197.132   允许传输文件的客户端Ip
    
    ## 创建认证用户的账号和密码,与配置文件中注意保持一致
    [root@localhost ~]# vim /etc/rsync.pass 
    
    share_user:123456
    
    [root@localhost ~]# chmod 600 /etc/rsync.pass 只让root查看并修改
    
    ## 开启服务并设定开机自动启动
    [root@localhost ~]# systemctl enable --now rsyncd
    [root@localhost ~]# ss -antl
    State     Recv-Q     Send-Q          Local Address:Port         Peer Address:Port    
    LISTEN    0          128                   0.0.0.0:22                0.0.0.0:*       
    LISTEN    0          5                     0.0.0.0:873               0.0.0.0:*       
    LISTEN    0          128                      [::]:22                   [::]:*       
    LISTEN    0          5                        [::]:873                  [::]:*
    

    客户端(src)配置

    ## 安装监听工具
    [root@localhost ~]# yum install -y inotify-tools
    
    ## 配置用户认证的密码。注意只用写密码,不需要填写用户名
    [root@localhost ~]# vim /etc/rsync.pass
    123456
    
    [root@localhost ~]# chmod 600 /etc/rsync.pass 只让root查看并修改
    
    
    ## 命令行手动验证
    [root@localhost ~]# rsync -avH --progress --delete /runtime share_user@192.168.197.133::share_test --password-file=/etc/rsync.pass 
    sending incremental file list
    runtime/
    
    sent 65 bytes  received 24 bytes  178.00 bytes/sec
    total size is 0  speedup is 0.00
    
    
    ## 在runtime目录下新建test.txt文件,再次同步目录
    [root@localhost /]# touch runtime/test.txt
    [root@localhost /]# rsync -avH --progress --delete /runtime share_user@192.168.197.133::share_test --password-file=/etc/rsync.pass 
    sending incremental file list
    runtime/
    runtime/test.txt
                  0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/2)
    
    sent 133 bytes  received 47 bytes  360.00 bytes/sec
    total size is 0  speedup is 0.00
    
    ## 服务端(dest)验证
    
    [root@localhost /]# ll sawyer/
    total 0
    drwxr-xr-x 2 root root 22 Oct 16 16:04 runtime
    [root@localhost /]# ll sawyer/runtime/
    total 0
    -rw-r--r-- 1 root root 0 Oct 16 16:04 test.txt
    
    

    编写脚本,利用inotify实时对目录进行同步

    
    
    
    [root@localhost /]# cd scripts/
    [root@localhost scripts]# vim inotify.sh
    #!/bin/bash
    
    # 变量定义
    host=192.168.197.133
    src=/runtime
    dest=share_test
    password=/etc/rsync.pass
    user=share_user
    inotify=/usr/bin/inotifywait
    
    
    
    # 脚本主体
    
    $inotify -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete
    ,create,attrib,move $src 
            | while read files;do
        rsync -avzP --delete  --timeout=100 --password-file=${password} $src $user@$
    host::$dest
            echo "${files} was rsynced" >>/var/rsync.log 2>&1
    done
    
    ## 为脚本添加执行权限
    [root@localhost scripts]# chmod +x inotify.sh 
    [root@localhost scripts]# ls
    inotify.sh
    
    ## 执行脚本
    [root@localhost scripts]# bash inotify.sh &  后台运行脚本
    
    ## 查看脚本进程
    [root@localhost scripts]# ps -ef |grep inotify
    root       2593   1717  0 16:23 pts/0    00:00:00 bash inotify.sh
    root       2594   2593  0 16:23 pts/0    00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib,move /runtime
    root       2595   2593  0 16:23 pts/0    00:00:00 bash inotify.sh
    root       2598   1717  0 16:23 pts/0    00:00:00 grep --color=auto inotify
    
    

    效果验证

    ## 删除客户端同步目录的文件
    [root@localhost runtime]# rm -rf test.txt 
    [root@localhost runtime]# sending incremental file list
    deleting runtime/test.txt
    runtime/
    
    sent 61 bytes  received 44 bytes  210.00 bytes/sec
    total size is 0  speedup is 0.00
    
    ## 服务端查看
    [root@localhost runtime]# ls
    [root@localhost runtime]# 
    
    ## 往同步目录中添加文件
    [root@localhost runtime]# touch test
    ......
    [root@localhost runtime]# ls
    test
    
    ## 服务端查看文件
    
    [root@server ~]# ls /sawyer/runtime/
    test
    
    

    设置开机自启动脚本

    为rc.local添加执行权限
    [root@client ~]# chmod +x /etc/rc.d/rc.local 
    [root@client rc.d]# vim rc.local 
    #!/bin/bash
    ......
    touch /var/lock/subsys/local
    nohup bash /scripts/inotify.sh  将脚本文件写入到此文件中
    
    
    ## 重启主机后查看进程
    [root@client rc.d]# reboot
    ......
    
    [root@client ~]# ps -ef |grep inotify
    root       1235   1219  0 16:34 ?        00:00:00 bash /scripts/inotify.sh
    root       1242   1235  0 16:34 ?        00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib,move /runtime
    root       1243   1235  0 16:34 ?        00:00:00 bash /scripts/inotify.sh
    root       1741   1712  0 16:36 pts/1    00:00:00 grep --color=auto inotify
    
  • 相关阅读:
    JavaBean对象与Map对象互相转化
    PowerDesigner V16.5 安装文件 及 破解文件
    eclipse get set 自动添加注释
    严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
    java poi excel操作 把第一列放到最后去
    java poi excel操作 下拉菜单 及数据有效性
    maven 项目编译失败
    关于TbSchedule任务调度管理框架的整合部署
    mat 使用 分析 oom 使用 Eclipse Memory Analyzer 进行堆转储文件分析
    tomcat启动问题,卡在 preparing launch delegate 100% 的解决方法
  • 原文地址:https://www.cnblogs.com/sawyer95/p/13824532.html
Copyright © 2011-2022 走看看