• rsync实现远程同步


    一、配置rsync源服务器

    1、关于rsync

    (1)、一款快速增量备份工具

    (2)、Remote Sync,远程同步

    (3)、支持本地复制,或者与其他SSH、rsync主机同步

    (4)、官方网站:http://rsync.samba.org

    2、rsync同步源

    指备份操作的远程服务器,也称为备份源

    3、基本思路

    (1)、建立rsyncd.conf配置文件、独立的账户文件

    (2)、启用rsync的--daemon模式

    4、应用示例

    (1)、用户backuper,允许下行同步

    (2)、操作的目录为/var/www/html

    5、配置文件rsync.conf

    (1)、需手动建立,语法类似于Samba配置

    (2)、认证配置auth users、secrets file,不加则为匿名

    6、rsync账户文件

    (1)、采用“用户名:密码”的记录格式,每行一个用户记录

    (2)、独立的账户数据,不依赖于系统账号

    7、启用rsync服务

    通过--daemon独自提供服务

    二、使用rsync备份工具

    1、rsync命令的用法

    语法
    rsync [选项] 原始位置 目标位置
    常用选项
    -a:         归档模式,递归并保留对象属性,等同于-rlptgoD
    -v:         显示同步过程的详细(verbose)信息
    -z:         在传输文件时进行压缩(compress)
    -H:        保留硬连接文件
    -A:        保留ACL属性信息
    -p:         保留文件的权限标记
    -t:          保留文件的时间标记
    -g:         保留文件的属组标记
    -o:         保留文件的属主标记
    -delete:    删除目标位置有而原始位置没有的文件
    -checksum:  根据对象的校验和来决定是否跳过文件

    2、配置源的两种表示方法

    格式1
    用户名@主机地址::共享模块名
     
    格式2
    rsync://用户名@主机地址/共享模块名

    3、rsync源的免交互处理

    使用 --password-file= 密码文件

    三、rsync远程同步部署

    1、环境说明

    2、配置rsync源服务器A

    ①检查rsync是否安装

    [root@rsync ~]# rpm -q rsync
    rsync-3.0.9-18.el7.x86_64

    ②修改配置文件

    [root@rsync ~]# vi /etc/rsyncd.conf
    uid = nobody
    gid = nobody
    use chroot = yes                   
    address = 20.0.0.10             
    port 873                                    
    pid file = /var/run/rsyncd.pid       
    log file = /var/log/rsyncd.log        
    hosts allow = 20.0.0.0/24               
    dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2         
    [wwwroot]
    path = /var/www/html                   
    comment = www.xuhao.com
    read only = yes                        
    auth users = backuper
    secrets file = /etc/rsyncd_users.db     

    ③创建backuper用户的密码文件

    [root@rsync ~]# vi /etc/rsyncd_users.db
    backuper:123456

    ④给密码文件设置执行权限

    [root@rsync ~]# chmod 600 /etc/rsyncd_users.db

    ⑤启动rsync

    [root@rsync ~]# rsync --daemon
    [root@rsync ~]# netstat -antp |grep rsync
    tcp        0      0 20.0.0.10:873           0.0.0.0:*               LISTEN      14461/rsync        

    ⑥安装httpd,有一个/var/www/html的共享目录,也可以自己创建

    [root@rsync ~]# yum -y install httpd
    [root@rsync ~]# echo "this is test web" > /var/www/html/index.html

    3、客户机服务器B测试

    ①格式一

    [root@client ~]# rsync -avz backuper@20.0.0.10::wwwroot /opt
    Password:
    receiving incremental file list
    ./
    index.html
    sent 83 bytes  received 172 bytes  56.67 bytes/sec
    total size is 17  speedup is 0.07
    [root@client ~]# cat /opt/index.html         
    this is test web

    ②格式二

    删除同步的文件再测试
    [root@client ~]# rm -rf /opt/index.html
    [root@client ~]# cat /opt/index.html
    cat: /opt/index.html: 没有那个文件或目录
    [root@client ~]# rsync -avz rsync://backuper@20.0.0.10/wwwroot /opt
    Password:
    receiving incremental file list
    ./
    index.html
    sent 83 bytes  received 172 bytes  56.67 bytes/sec
    total size is 17  speedup is 0.07
    [root@client ~]# cat /opt/index.html
    this is test web

    ③免密方式登录

    创建免密登录文件
    [root@client ~]# vi /etc/server.pass 2 123456
    [root@client ~]# chmod 600 /etc/server.pass
    [root@client ~]# rm -rf /opt/index.html
    [root@client ~]# rsync -avz --delete --password-file=/etc/server.pass backuper@20.0.0.10::wwwroot /opt
    receiving incremental file list
    deleting rh/
    ./
    index.html
    sent 83 bytes  received 172 bytes  72.86 bytes/sec
    total size is 17  speedup is 0.07
    [root@client ~]# cat /opt/index.html
    this is test web

    四、rsync+inotify实时同步

    1、rsync实时同步

    (1)、定期同步的不足

    ①执行备份的时间固定,延迟明显、实时性差

    ②当同步源长期不变化时,密集的定期任务是不必要的

    (2)、实时同步的优点

    ①一旦同步源出现变化,立即启动备份

    ②只要同步源无变化,则不执行备份

    2、关于inotify

    Linux内核的inotify机制

    ①从版本2.6.13开始提供

    ②可以从监控文件系统的变动情况,并做出通知响应

    ③辅助软件:inotify-tools

    3、环境说明

     4、配置rsync源服务器A

    ①将只读模式关闭

    [root@rsync ~]# vi /etc/rsyncd.conf
    read only = no

    ②客户端更改内核参数,指定队列大小,最多监控实例数,每个实例最多监控文件数

    [root@rsync ~]# vim /etc/sysctl.conf
    fs.inotify.max_queued_events = 16384     
    fs.inotify.max_user_instances = 1024      
    fs.inotify.max_user_watches = 1048576     
    [root@rsync ~]# sysctl -p
    fs.inotify.max_queued_events = 16384
    fs.inotify.max_user_instances = 1024
    fs.inotify.max_user_watches = 1048576

    ③重新启动rsync服务,注意:kill pid号和kill -9 pid号的区别:后者不会删除pid文件,导致服务起不来

    [root@rsync ~]# cd /var/run
    [root@rsync run]# cat rsyncd.pid
    14461
    [root@rsync run]# kill 14461            
    [root@rsync run]# ll | grep rsync.pid
    [root@rsync run]# rsync --daemon
    [root@rsync run]# ll | grep rsyncd.pid
    -rw-r--r--.  1 root           root              6 11月 12 13:19 rsyncd.pid
    [root@rsync run]# cat rsyncd.pid
    54028
    [root@rsync run]# kill -9 54028        
    [root@rsync run]# ll | grep rsyncd.pid
    -rw-r--r--.  1 root           root              6 11月 12 13:19 rsyncd.pid
    [root@rsync run]# rsync --daemon
    [root@rsync run]# failed to create pid file /var/run/rsyncd.pid: File exists
    [root@rsync run]# rm -rf rsyncd.pid    
    [root@rsync run]# rsync --daemon
    [root@rsync run]# netstat -anpt | grep rsync
    tcp        0      0 20.0.0.10:873           0.0.0.0:*               LISTEN      5406
    5、客户机服务器B配置

    ①解压缩inotify并安装

    inotifywait:用于持续监控,实时输出结果

    inotifywatch:用于短期监控,任务完成后再出结果

    [root@client ~]# tar zxf inotify-tools-3.14.tar.gz
    [root@client ~]# cd inotify-tools-3.14/
    [root@client inotify-tools-3.14]# ./configure
    [root@client inotify-tools-3.14]# make && make install

    ②测试inotifywait监控命令是否正常使用

    [root@client inotify-tools-3.14]# mkdir -p /var/www/html
    [root@client inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html    

    ③执行完上面最后一条命令后处于监控状态,不能做输入操作,在开启一个新的终端设备

    另一个终端文件测试
    [root@client ~]# cd /var/www/html/
    [root@client html]# touch a.txt
    [root@client html]# vi 1.tat
    [root@client html]# rm -rf 1.txt
    [root@client html]# mv a.txt /opt
    监控端查看
    [root@client inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html
    /var/www/html/ CREATE a.txt
    /var/www/html/ CREATE 1.txt
    /var/www/html/ DELETE 1.txt
    /var/www/html/ MOVED_FROM a.txt

    ④客户端上编写脚本,将inotify监控和rsync远程同步结合起来

    [root@client inotify-tools-3.14]# vi /opt/inotify.sh
    #!/bin/bash
    INOTIFY="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html"
    RSYNC="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/  backuper@20.0.0.10::wwwroot/"
    $INOTIFY | while read DIRECTORY EVENT FILE   
    do
            if [ $(pgrep rsync | wc -l) -le 0 ];then
                $RSYNC
            fi
    done
    [root@client inotify-tools-3.14]# chmod +x /opt/inotify.sh
    [root@client inotify-tools-3.14]# cd /opt/
    注:两边的同步目录权限都设置777
    [root@rsync run]# chmod 777 /var/www/html/
    [root@client html]# chmod 777 /var/www/html/
    运行脚本
    [root@client opt]# ./inotify.sh

    ⑤在客户端/var/www/html目录下创建文件,查看源端/var/www/html目录是否同步到

    客户端/var/www/html目录下创建目录
    [root@client html]# touch a.txt
    [root@client html]# touch b.txt
    [root@client html]# touch c.txt
    [root@client html]# touch d.txt
    查看源端/var/www/html目录,新建的和原来就存在的文件都成功同步,且属主属组均为nobody用户
    [root@rsync ~]# cd /var/www/html/
    [root@rsync html]# ll
    总用量 0
    -rw-r--r--. 1 nobody nobody 0 11月 18 18:45 a.txt
    -rw-r--r--. 1 nobody nobody 0 11月 18 18:45 b.txt
    -rw-r--r--. 1 nobody nobody 0 11月 18 18:47 c.txt
    -rw-r--r--. 1 nobody nobody 0 11月 18 18:48 d.txt

    五、注意事项

    1、kill pid号和kill -9 pid号的区别

    [root@rsync ~]# cd /var/run
    [root@rsync run]# cat rsyncd.pid
    14461
    [root@rsync run]# kill 14461            
    [root@rsync run]# ll | grep rsync.pid
    [root@rsync run]# rsync --daemon
    [root@rsync run]# ll | grep rsyncd.pid
    -rw-r--r--.  1 root           root              6 11月 18 18:19 rsyncd.pid
    [root@rsync run]# cat rsyncd.pid
    54028
    [root@rsync run]# kill -9 54028       
    [root@rsync run]# ll | grep rsyncd.pid
    -rw-r--r--.  1 root           root              6 11月 18 18:19 rsyncd.pid
    [root@rsync run]# rsync --daemon
    [root@rsync run]# failed to create pid file /var/run/rsyncd.pid: File exists
    [root@rsync run]# rm -rf rsyncd.pid 
    [root@rsync run]# rsync --daemon
    [root@rsync run]# netstat -anpt | grep rsync
    tcp        0      0 20.0.0.10:873           0.0.0.0:*               LISTEN      5406
    2、同步时的注意项

    使用如下命令进行上行同步(上传)时,本地目录最后要加上/,否则会将目录同步到对方目标文件夹下,而不仅仅是同步本地目录下的文件

    ①将上面的脚本源端目录/var/www/html/改成/var/www/html

    [root@client opt]# vi /opt/inotify.sh
    #!/bin/bash
    INOTIFY="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html"
    RSYNC="rsync -azH --delete --password-file=/etc/server.pass /var/www/html  backuper@20.0.0.10::wwwroot"
    $INOTIFY | while read DIRECTORY EVENT FILE   
    do
            if [ $(pgrep rsync | wc -l) -le 0 ];then
                $RSYNC
            fi
    done
    运行脚本
    [root@client opt]# ./inotify.sh

    ②在客户端/var/www/html目录下创建文件,查看源端/var/www/html目录会发现直接同步的时目录

    客户机写入数据
    [root@client html]# touch e.txt
    rsync上查看
    [root@rsync html]# ll
    总用量 0
    -rw-r--r--. 1 nobody nobody  0 11月 18 19:45 a.txt
    -rw-r--r--. 1 nobody nobody  0 11月 18 19:45 b.txt
    -rw-r--r--. 1 nobody nobody  0 11月 18 19:47 c.txt
    -rw-r--r--. 1 nobody nobody  0 11月 18 19:48 d.txt
    drwxrwxrwx. 2 nobody nobody 71 11月 29 19:01 html            
    [root@rsync html]# cd html/
    [root@rsync html]# ll
    总用量 0
    -rw-r--r--. 1 nobody nobody 0 11月 18 19:45 a.txt
    -rw-r--r--. 1 nobody nobody 0 11月 18 19:45 b.txt
    -rw-r--r--. 1 nobody nobody 0 11月 18 19:47 c.txt
    -rw-r--r--. 1 nobody nobody 0 11月 18 19:48 d.txt
    -rw-r--r--. 1 nobody nobody 0 11月 18 20:01 e.txt
  • 相关阅读:
    面向对象-01
    网络编程-02-客户端搭建
    网络编程-01-服务端搭建
    日志-02
    日志-01
    md5加密
    shell 第五天
    shell第四天
    shell第三天
    shell
  • 原文地址:https://www.cnblogs.com/Xing88/p/13997808.html
Copyright © 2020-2023  润新知