zoukankan      html  css  js  c++  java
  • Rsync实时备份工具

    Rsync实时备份工具使用

    一、实时备份介绍

    1.1.1什么是实时备份:

    实时备份就是利用实时工具对重要数据和文件变化进行一个实时的监控与备份,一发生变化,我就备份。

    1.1.2 企业中常用的备份工具:

    有inotify和sersync(推荐使用) 两种。

    1.1.3. 企业为什么要做实时备份:

    1)防止数据的丢失。
    2)保证数据的一致性。
    3) 实时备份数据适合用户上传的普通文件(文档,视频,压缩包等)。

    二、实时复制实践(inotify-tools)

    2.1.1 inotify实时备份实战

    1. 检查内核版本
    [root@nfs01 ~]# uname -r
    3.10.0-1062.18.1.el7.x86_64
    # 注意inotify需要2.6.13以上的内核
    
    1. 检查是否有inotify的三个参数文件
    [root@nfs01 ~]# ls -l /proc/sys/fs/inotify/
    total 0
    -rw-r--r-- 1 root root 0 May  1 16:53 max_queued_events
    -rw-r--r-- 1 root root 0 May  1 16:53 max_user_instances
    -rw-r--r-- 1 root root 0 May  1 16:53 max_user_watches
    #这三个参数是优化的关键
    
    1. 安装inotify
    [root@nfs01 ~]# yum install inotify-tools -y
    
    1. 查看是否安装
    [root@nfs01 ~]# rpm -ql inotify-tools|head -2
    /usr/bin/inotifywait
    /usr/bin/inotifywatch
    [root@nfs01 ~]# rpm -qa inotify-tools
    inotify-tools-3.14-9.el7.x86_64
    

    2.1.2 inotifywait重要命令说明

    1. 重要参数说明
    参数 说明
    -m(monitor) 始终保持监听状态
    -d (daemon) 类似于-m参数,之时间命令放到后台运行
    -r 递归监控目录数据信息变化
    q 减少输出
    -e 监听指定的事件,如果省略,默认监听所有。
    1. -e 后面接的参数说明
    参数 说明
    close_write 文件或目录关闭,在写入模式打开之后关闭
    create 文件或目录创建在监控的目录中
    delete 文件或目录被删除在监控目录中
    1. 实战
    [root@nfs01 ~]# inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create /data
    
    1. 结果
    [root@nfs01 ~]# cd /data/
    [root@nfs01 /data]# touch a.txt
    [root@nfs01 /data]# touch b.txt
    [root@nfs01 ~]# inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create /data
    01/05/20 17:23 /data/a.txt
    01/05/20 17:23 /data/b.txt
    
    1. 编写监控脚本
    [root@nfs01 ~]# cat /server/scripts/monitor.sh
    #!/bin/bash 
    #auto by  chenhj date 2019-1-13 
    #monitor rsync data bash file
    CMD="/usr/bin/inotifywait"
    $CMD -mrq --format '%w%f' -e close_write,delete /data |
    while read line
    do
    	[ ! -e $line ] && cd /data && 
    	rsync -az --delete ./  rsync_backup@172.16.1.41::data --password-file=/etc/rsync.password && continue
    	rsync -az --delete $line rsync_backup@172.16.1.41::data --password-file=/etc/rsync.password
    done 
    
    1. 测试
    [root@nfs01 ~]# cd /data/
    [root@nfs01 /data]# touch {1..100}.txt
    [root@nfs01 /data]# ls -lht
    total 0
    -rw-r--r-- 1 root root 0 May  1 18:45 100.txt
    -rw-r--r-- 1 root root 0 May  1 18:45 91.txt
    -rw-r--r-- 1 root root 0 May  1 18:45 92.txt
    -rw-r--r-- 1 root root 0 May  1 18:45 93.txt
    -rw-r--r-- 1 root root 0 May  1 18:45 94.txt
    -rw-r--r-- 1 root root 0 May  1 18:45 95.txt
    
    

    7.在backup上查看

    [root@backup ~]# ls -lrt /data/
    total 0
    -rw-r--r-- 1 rsync rsync 0 May  1 18:45 1.txt
    -rw-r--r-- 1 rsync rsync 0 May  1 18:45 9.txt
    -rw-r--r-- 1 rsync rsync 0 May  1 18:45 8.txt
    -rw-r--r-- 1 rsync rsync 0 May  1 18:45 7.txt
    -rw-r--r-- 1 rsync rsync 0 May  1 18:45 6.txt
    -rw-r--r-- 1 rsync rsync 0 May  1 18:45 5.txt
    -rw-r--r-- 1 rsync rsync 0 May  1 18:45 4.txt
    
    1. 放在后台执行
    [root@nfs01 ~]# /bin/sh /server/scripts/monitor.sh &
    [1] 1506
    
    1. 设置开机自动启动
    [root@nfs01 ~]# vim /etc/rc.local #monitor shell
    /bin/sh /server/scripts/monitor.sh &
    
    1. 授予执行权限
    [root@nfs01 ~]# chmod +x /etc/rc.local 
    [root@nfs01 ~]# chmod +x /etc/rc.d/rc.local 
    

    2.1.3 inotify 优化

    [root@nfs01 ~]# echo '50000000'> /proc/sys/fs/inotify/max_queued_events 
    #设置可监听文件的数量
    [root@nfs01 ~]# echo '50000000'> /proc/sys/fs/inotify/max_user_watches
    #设置inotify实例事件(event)队列可容纳的事件数量
    [root@nfs01 ~]# cat < /proc/sys/fs/inotify/max_user_instances 
    128
    #设置每个用户可以运行的命令进程数,默认就行。
    

    上述优化开机可能失效,可以把命令放到/etc/rc.local里面开机自启。

    三、sersync的使用

    3.1.1 sersync介绍

    1. sersync 是在inotify的基础上开发的,其功能比inotify更丰富,更实用,支持多线程。
    1. 安装部署sersync
    [root@nfs01 /server/tools]# tar zxvf sersync_64bit_binary_stable_final.tar.gz 
    GNU-Linux-x86/
    GNU-Linux-x86/sersync2
    GNU-Linux-x86/confxml.xml
    #解压即用
    

    3.移动到软件安装目录下

    [root@nfs01 /server/tools]# mv GNU-Linux-x86/ /application/
    
    1. 重命名
    [root@nfs01 /application]# mv GNU-Linux-x86 sersync
    [root@nfs01 /application]# ll
    total 0
    drwxr-xr-x 2 root root 41 Oct 26  2011 sersync
    
    1. 备份并编辑配置文件
    root@nfs01 /application/sersync]# cp confxml.xml{,.bak}
    [root@nfs01 /application/sersync]# ll
    total 1776
    -rwxr-xr-x 1 root root    2214 Oct 26  2011 confxml.xml
    -rwxr-xr-x 1 root root    2214 May  1 22:47 confxml.xml.bak
    -rwxr-xr-x 1 root root 1810128 Oct 26  2011 sersync2
    #修改如下:
     23     <sersync>
     24         <localpath watch="/data">
     25             <remote ip="172.16.1.41" name="backup"/>
     26             <remote ip="172.16.1.41" name="data"/>
     27             <!--<remote ip="192.168.8.39" name="tongbu"/>-->
     28             <!--<remote ip="192.168.8.40" name="tongbu"/>-->
     29         </localpath>
     30         <rsync>
     31             <commonParams params="-artuz"/>
     32             <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
     33             <userDefinedPort start="false" port="874"/><!-- port=874 -->
     34             <timeout start="true" time="100"/><!-- timeout=100 -->
     35             <ssh start="false"/>
    
    1. 启动服务
    [root@nfs01 /application/sersync]# ./sersync2 -d -o /application/sersync/confxml.xml
    set the system param
    execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
    execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
    parse the command param
    option: -d 	run as a daemon
    option: -r 	rsync all the local files to the remote servers before the sersync work
    option: -o 	config xml name:  /application/sersync/confxml.xml
    daemon thread num: 10
    parse xml config file
    host ip : localhost	host port: 8008
    daemon start,sersync run behind the console 
    use rsync password-file :
    user is	rsync_backup
    passwordfile is 	/etc/rsync.password
    config xml parse success
    please set /etc/rsyncd.conf max connections=0 Manually
    sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) 
    Max threads numbers is: 32 = 12(Thread pool nums) + 20(Sub threads)
    please according your cpu ,use -n param to adjust the cpu rate
    ------------------------------------------
    rsync the directory recursivly to the remote servers once
    working please wait...
    execute command: cd /data && rsync -artuz -R --delete ./  --timeout=100 rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password >/dev/null 2>&1 
    run the sersync: 
    watch path is: /data
    #参数 -d -r -o的顺序一定不要错。否则会报错,坑坑啊!!
    #在工作中尽量不要加-r ,-r(参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍),我们启动sersync服务的时候手工推送一遍测试一下就行了,因为工作环境数据时好几十个G如果你加上-r的话,很长一段时间你都无法实时推送数据,因为要等那十几个G的数据推送玩。
    
    1. 参数详解
    [root@nfs01 /application/sersync]# ./sersync2 -h
    set the system param
    execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
    execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
    parse the command param
    _______________________________________________________
    参数-d:启用守护进程模式
    参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
    c参数-n: 指定开启守护线程的数量,默认为10个
    参数-o:指定配置文件,默认使用confxml.xml文件
    参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
    参数-m:单独启用其他模块,使用 -m socket 开启socket模块
    参数-m:单独启用其他模块,使用 -m http 开启http模块
    不加-m参数,则默认执行同步程序
    ________________________________________________________________
    
    1. 查看进程
    [root@nfs01 /application/sersync]# ps -ef | grep sersync
    root       2187      1  0 23:45 ?        00:00:00 ./sersync2 -d -r -o /application/sersync/confxml.xml
    root       2231   2142  0 23:57 pts/1    00:00:00 grep --color=auto sersync
    

    扩展命令 :pstree 查看服务有多少个线程

    [root@nfs01 /application/sersync]# pstree
    systemd─┬─agetty
            ├─crond
            ├─dbus-daemon
            ├─gssproxy───5*[{gssproxy}]
            ├─lvmetad
            ├─rpc.idmapd
            ├─rpc.mountd
            ├─rpc.statd
            ├─rpcbind
            ├─rsyslogd───2*[{rsyslogd}]
            ├─2*[sersync2───11*[{sersync2}]]
            ├─sshd───sshd───bash───pstree
            ├─systemd-journal
            ├─systemd-logind
            └─systemd-udevd
    
    1. 测试一下效果就行了
    [root@nfs01 /data]# ls
    a.txt  c.txt  e.txt  g.txt  i.txt  k.txt  m.txt  o.txt  q.txt  s.txt  u.txt  w.txt  y.txt
    b.txt  d.txt  f.txt  h.txt  j.txt  l.txt  n.txt  p.txt  r.txt  t.txt  v.txt  x.txt  z.txt
    
    [root@backup /data]# ls /backup/
    a.txt  c.txt  e.txt  g.txt  i.txt  k.txt  m.txt  o.txt  q.txt  s.txt  u.txt  w.txt  y.txt
    b.txt  d.txt  f.txt  h.txt  j.txt  l.txt  n.txt  p.txt  r.txt  t.txt  v.txt  x.txt  z.txt
    
    1. 添加开机自启动
    [root@nfs01 ~]# vim /etc/rc.local [root@nfs01 ~]# tail -2 /etc/rc.local 
    # sersync monitor server
    /application/sersync/sersync2 -d -o /application/sersync/confxml.xml
    

    完美

  • 相关阅读:
    链表问题----反转部分单向链表
    HTTP请求详解
    链表问题----删除链表的中间节点和a/b处的节点
    链表问题----删除倒数第K个节点
    栈和队列----最大值减去最小值小于等于num的子数组的数量
    栈和队列----求最大子矩阵的大小
    TCP/IP、Http、Socket的区别
    栈和队列----生成窗口的最大值数组
    linux根文件系统制作,busybox启动流程分析
    linux 内核启动流程分析,移植
  • 原文地址:https://www.cnblogs.com/woaiyunwei/p/12892785.html
Copyright © 2011-2022 走看看