rsync:远程同步工具
相当于scp命令,但是要优于scp,属于增量备份
inotify:异步文件系统监控机制
实时监控某个文件或者目录,如果发现变更,则发出信号
环境centos6.7
centos6.7默认安装了rsync,如果没有安装,可以直接yum安装
yum install -y rsync
local
remote
daemon
由于主要用于远程实时推送,因此本文只讲解daemon模式
rsync [OPTION…] SRC… [USER@]HOST::DEST
-a
归档模式,表示以递归方式传输文件,并且保持文件属性
-z
对备份的文件在传输时进行压缩处理
-P
保留那些因故没有完全传输的文件,以是加快随后的再次传输
-v
详细模式输出,测试阶段会会用,实际中一般不用
–exclude=PATTERN
排除某目录下的文件
–exclude-from=FILE
排除单个文件
由于centos默认安装了rsync,但是没有服务端的配置文件,此处在/etc/下添加一个rsyncd.conf文件
此文件在接收推送的节点服务器上
vi /etc/rsyncd.conf
# Minimal configuration file for rsync daemon # See rsync(1) and rsyncd.conf(5) man pages for help # seting the user and group for running rsync uid = rsync gid = rsync use chroot = no # the rsync allow the max_connection max connections = 1024 # how long the connection can keep timeout = 300 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log list=false ignore errors # create a new model for a function [backup] path = /backup readonly = false hosts allow = 10.0.0.11/24 hosts deny = 0.0.0.0/32 auth users = rsync_backup secrets file = /etc/rsyncd.secrets
根据配置文件创建相应用户及接收路径
创建用户
useradd -s /sbin/nologin -M -u 65543 rsync
创建接收的路径
mkdir -p /backup
创建密码文件
echo ‘rsync_backup:123456‘ >> /etc/rsyncd.secrets
为了正常运行,需要修改密码文件的权限,以及接收路径的所属用户
chmod 600 /etc/rsyncd.secrets chown rsync:rsync /backup/
重启rsync服务
pkill rsync rsync --daemon
如果无报错说明正常运行,否则按上述步骤检查
rsync -azP /etc/hosts rsync_backup@10.0.0.6::backup
输入密码,查看文件是否推送成功
查看系统是否支持inotify
ls /proc/sys/fs/inotify/
如果出现以下三个文件说明支持inotify
max_queued_events
max_user_instances
max_user_watches
-r
递归模式监测目录
-q
查分一次,输出结果,如果查分两次将不输出结果
-m
默认第一次实践触发后推出,此参数一次出发后不退出
–format
打印适用指定的输出类似格式字符串
-e
指定监控的事件
期中-r同时监测文件数默认为8192,可以修改 /proc/sys/fs/inotify/maxuserwatches文件提高最大监测数
监听的事件类型
create
move
moved_to
delete
close_write
输出的格式类型
%w 带路径
%f 带文件名
注意inotify用于检测目录,所以安装在需要检测的节点服务器上
1.安装inotify
安装epel源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/ repo/epel-6.repo
yum安装inotify
yum install -y inotify-tools
2.测试inotify
inotifywait -mrq --format ‘%w%f‘ -e create /root
运行完命令之后,另起会话窗口,然后在root目录下添加一个文件
查看在inotifywait命令会话框时候监测到
在客户端添加请求的rsync服务端的密码文件
echo "123456" >> /etc/rsync.password
修改密码文件的权限
chmod 600 /etc/rsync.password
写脚本事件监听某目录,将某目录的变化项推送到指定服务器,根据业务修改脚本
# vi listen.sh
#!/bin/sh source /etc/profile Path=/tmp ServerUser="rsync_backup" ServerID="10.0.0.6" inotifywait -mrq --format ‘%w%f‘ -e create,close_write,delete $Path |while read line do if [ ! -e $line ];then rsync -az --delete $Path $ServerUser@$ServerID::backup --password-file=/etc/rsync.password && continue else rsync -az --delete $line $ServerUser@$ServerID::backup --password-file=/etc/rsync.password fi done
后台运行脚本
sh listen.sh &
通过在/tmp目录下创建文件,删除文件,检查结果
[root@backup etc]# cd /tmp/ [root@backup tmp]# ls [root@backup tmp]# ls /backup/ [root@backup tmp]# touch a.txt [root@backup tmp]# ls /backup/ a.txt [root@backup tmp]# touch b.txt [root@backup tmp]# ls /backup/ a.txt b.txt [root@backup tmp]# touch c.txt [root@backup tmp]# ls /backup/ a.txt b.txt c.txt
原文:http://cyn9145.blog.51cto.com/10611425/1743275