centos7.2 rsync+inotify安装使用 实时备份

来源:互联网 发布:php删除相同前缀文件 编辑:程序博客网 时间:2024/05/19 22:06

rsync可以与其他ssh、rsync主机进行同步数据。
inotify.监控文件系统操作。比如读取、写入、创建等。反应特别灵敏,
rsync+inotify就是监控系统中文件。进行快速的备份操作。

准备环境:
虚拟机centos7 两台:
192.168.51.20 server端
192.168.51.15 client端
(server,client)表示客户端和服务端都需配置

环境配置:(server\client)
1、关闭firewalled和selinux
systemctl stop firewalld.service
vim /etc/sysconfig/selinux
将selinux=enable修改为selinux=disable
2、查看是否支持inotify
ll /proc/sys/fs/inotify/
总用量 0
-rw-r–r–. 1 root root 0 11月 15 05:29 max_queued_events
-rw-r–r–. 1 root root 0 11月 15 05:29 max_user_instances
-rw-r–r–. 1 root root 0 11月 15 05:29 max_user_watches
有这几个文件表示支持。

一、安装rsync (server.client)
yum install rsync -y

1、配置rsync (server)
vi /etc/rsyncd.conf

……….
uid = root
gid = root
use chroot = no
max connections = 10
strict modes = yes
port = 873 默认端口
address = 192.168.51.20 服务器地址
[test]
path = /test 检测的路径
comment = mirror for test
ignore errors
read only = no
list = no
auth users = user 使用的用户
secrets file = /etc/rsync.pas 存储用户名和密码
hosts allow = *
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log

启动rsync

rsync –daemon –config=/etc/rsyncd.conf
开机启动:
vim /etc/rc.local
/usr/bin/rsync –daemon

安装inotify (server client)
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

tar zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure –prefix=/usr/local/inotify
make && make install

配置inotify (client)

vi /etc/rc.d/inotify.sh
该脚本在做客户端目录下文件若发生变化,则向服务端做同步上传操作,也就是保持客户端目录文件发生变化,服务端也相应改变。

  1. #!/bin/bash
    src=/test
    des=test
    ip=192.168.51.20
    /usr/local/inotify/bin/inotifywait -mrq –timefmt ‘%d/%m/%y/%H:%M’
    –format ‘%T%w%f’ -e modify,delete,create,attrib src|whilereadfiledorsyncvzrtopgdeleteprogresssrc user@ip::des
    –password-file=/etc/rsync.pas &&
    echo “$src has been resynced”
    done

赋予执行权限
chmod +x /etc/rc.d/inotify.sh
执行脚本并做开机启动
/etc/rc.d/inotify.sh
echo “/etc/rc.d/inotify.sh”>>/etc/rc.local
记得将rc.local赋予执行权限。实验时候没看权限,重启总是不启动,折腾了半天,还以为这个文件有问题的
这个脚本的作用就是通过inotify监控文件目录的变化。进行触发rsync进行操作。

创建需求文件(server)
mkdir /test
vi /etc/rsync.pas

user:123456 用户及密码

创建需求文件(client)

mkdir /test
vi /etc/rsync.pas

123456 只填写密码

客户端测试:
cd /test
touch 111
sent 485 bytes received 139 bytes 416.00 bytes/sec
total size is 0 speedup is 0.00
/test/ has been resynced
sending incremental file list

去服务器端查看
ll /test
有刚才创建的文件即为成功。