rsync+inotify实现实时同步、增量备份

版权声明:©来自CSDN博客作者"Debug The Life"的原创作品,如需转载,请注明出处。 https://blog.csdn.net/zhaoxixc/article/details/82079212

主机A:被备份的源主机
主机B:备份的目的主机

  • 在linux内核中,默认的inotify机制提供了三个调控参数
[root@fudanwuxi html]# uname -r
3.10.0-693.el7.x86_64
[root@fudanwuxi html]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r--. 1 root root 0 Aug 25 09:32 max_queued_events  #监控事件队列
-rw-r--r--. 1 root root 0 Aug 25 09:32 max_user_instances  #最多监控实例数
-rw-r--r--. 1 root root 0 Aug 25 09:32 max_user_watches  #每个实例最多监控文件数
[root@fudanwuxi html]# cat /proc/sys/fs/inotify/max_queued_events 
16384
[root@fudanwuxi html]# cat /proc/sys/fs/inotify/max_user_instances 
128
[root@fudanwuxi html]# cat /proc/sys/fs/inotify/max_user_watches 
8192
  • 当要监控的目录、文件数量较多或者变化较频繁时,要加大这三个参数的值
[root@fudanwuxi html]# vim /etc/sysctl.conf                      
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).

fs.inotify.max_queued_events = 32768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 305564824
~                                                    
[root@fudanwuxi html]# sysctl -p
fs.inotify.max_queued_events = 32768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 305564824
[root@fudanwuxi html]# cat /proc/sys/fs/inotify/max_queued_events  
32768
[root@fudanwuxi html]# cat /proc/sys/fs/inotify/max_user_instances  
1024
[root@fudanwuxi html]# cat /proc/sys/fs/inotify/max_user_watches
305564824
  • 安装inotify-tools-3.13.tar.gz
[root@fudanwuxi ~]# rz
rz waiting to receive.
Starting zmodem transfer.  Press Ctrl+C to cancel.
Transferring inotify-tools-3.13.tar.gz...
  100%     380 KB     380 KB/sec    00:00:01       0 Errors  
[root@fudanwuxi ~]# tar -zxvf inotify-tools-3.13.tar.gz 
[root@fudanwuxi ~]# cd inotify-tools-3.13/
[root@fudanwuxi inotify-tools-3.13]# ./configure
[root@fudanwuxi inotify-tools-3.13]# make -j 4
[root@fudanwuxi inotify-tools-3.13]# echo $?            
0
[root@fudanwuxi inotify-tools-3.13]# make install
  • ssh验证
[root@fudanwuxi ~]# ssh-keygen
[root@fudanwuxi ~]# ssh-copy-id [email protected] 
  • 编写脚本并执行
[root@fudanwuxi ~]# vim superbackup.sh
#!/bin/bash
inotifywait -mrq -e create,move,delete,modify /var/www/html/ | while read a b c
do
        rsync -azP  --delete /var/www/html/ root@192.168.10.178:/web-back

done
~                                                   
[root@fudanwuxi ~]# ./superbackup.sh
  • 测试,在主机A的/var/www/html/下创建测试文件superbackup.txt
[root@fudanwuxi html]# vim superbackup.txt 
如果你看到这个文件,说明实时同步成功
!!!are you ok
~                                                   
[root@fudanwuxi html]# echo "再次新增文件,如果看到这个文件,说明OK" > superbackup1.txt
  • 验证,在主机B查看目的备份目录/web-back/
[root@fudanwuxi002 ~]# ll /web-back/     
总用量 28
-rw-r--r--. 1 root root 31 8月  24 23:35 1.txt
-rw-r--r--. 1 root root 31 8月  24 23:43 2.txt
-rw-r--r--. 1 root root 21 8月  24 23:09 ajie.txt
-rw-r--r--. 1 root root 14 8月  24 23:15 index.txt
-rw-r--r--. 1 root root 57 8月  25 13:27 superbackup1.txt
-rw-r--r--. 1 root root 69 8月  25 13:24 superbackup.txt
-rw-r--r--. 1 root root 49 8月  24 22:36 testbackup.txt

[root@fudanwuxi002 ~]# cat /web-back/superbackup.txt  
如果你看到这个文件,说明实时同步成功
!!!are you ok
[root@fudanwuxi002 ~]# cat /web-back/superbackup1.txt 
再次新增文件,如果看到这个文件,说明OK
[root@fudanwuxi002 ~]# 

  • 优化脚本并使其开机自启

脚本V2版本下载地址,点我进行下载

[root@fudanwuxi ~]# echo '/root/superbackupV2.sh &' >> /etc/rc.local 
[root@fudanwuxi ~]# chmod +x /etc/rc.d/rc.local

查看进程

[root@fudanwuxi ~]# ps aux | grep inotify
root        976  0.0  0.0   6524   628 ?        S    14:49   0:00 inotifywait -mrq -e create,move,delete,modify,attrib /backup/super/V2
  • 测试优化的脚本
    在主机下A的/backup/super/V2目录先创建文件供测试用
[root@fudanwuxi ~]# mkdir -p /backup/super/V2
[root@fudanwuxi ~]# touch /backup/super/V2/index.php
[root@fudanwuxi ~]# touch /backup/super/V2/index.txt
[root@fudanwuxi ~]# touch /backup/super/V2/index.html

主机A重启后修改/backup/super/V2/index.php

[root@fudanwuxi ~]# echo "这是重启后修改,如果你看到这个文件,说明superbackupV2执行成功" > /backup/super/V2/index.php 
  • 验证优化的脚本
    在主机B查看备份目录/web-back
[root@fudanwuxi002 ~]# ll /web-back/V2/
总用量 4
-rw-r--r--. 1 root root  0 825 14:07 index.html
-rw-r--r--. 1 root root 86 825 14:42 index.php
-rw-r--r--. 1 root root  0 825 14:07 index.txt
[root@fudanwuxi002 ~]# cat /web-back/V2/index.php
这是重启后修改,如果你看到这个文件,说明superbackupV2执行成功
[root@fudanwuxi002 ~]# 

  • 附录1:rsync常用参数
-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
-p, --perms 保持文件权限
-P 等同于 --partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输
-q, --quiet 精简输出模式
-r, --recursive 对子目录以递归模式处理
-t, --times 保持文件时间信息
-v, --verbose 详细模式输出
-z, --compress 对备份的文件在传输时进行压缩处理
--delete 删除那些DSTSRC没有的文件
  • 附录2:inotifywait常用参数
-m, 持续监视变化
-r, 使用递归形式监视目录
-q, 减少冗余信息,只打印出需要的信息
-e, 指定要监视的事件列表

其中参数 -e 可监视的事件有

access  访问,读取文件
modify  修改,文件内容被修改
attrib  属性,文件元数据被修改
move    移动,对文件进行移动操作
create  创建,生成新文件
open    打开,对文件进行打开操作
close   关闭,对文件进行关闭操作
delete  删除,文件被删除

猜你喜欢

转载自blog.csdn.net/zhaoxixc/article/details/82079212