inotify配合rsync实现文件同步

一.slave端rsync设置
在此部署rsync服务和rsync daemon

1.安装rsync

 

2.配置rsyncd.conf文件
#vi /etc/rsyncd.conf
配置文件
uid = root
gid = root
use chroot = yes
max connections = 4
strict modes = yes
port = 873
log file = /var/log/rsync.log

[static]
path = /path/路径/ #上传于本slave的目录路径
comment = welcome to website!
ignore errors
read only = no
list = yes
auth users = user #允许的用户名
secrets file = /etc/server-rsync.pas #所需的密码文件,该文件格式为 用户名:密码,该文件的权限应该为600,否则会报error starting client-server protocol (code 5) at main.c
hosts allow = ***.**.**.** #允许的ip


3.以daemon启动rsync服务
#rsync --daemon


查看
#ps -ef | grep rsync

root     16596     1  0  2015 ?        00:00:00 rsync --daemon --config=/etc/rsyncd.conf
root     24919 24786  0 15:58 pts/1    00:00:00 grep rsync

查看端口及监听
#netstat -anop | grep rsync
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      16596/rsync         off (0.00/0/0)
tcp        0      0 :::873                      :::*                        LISTEN      16596/rsync         off (0.00/0/0)


二.master配置

 

1.rsync配置及测试
使用以下命令同步文件到slave上
/usr/bin/rsync -vzrtopg --progress --exclude-from=/other-config/exclude.conf /path/dir/ user@SLAVEIP::static --password-file=/etc/rsync.pas

以上表示将master下/path/dir/的文件同步到slave服务器上(受slave模块static限制),使用user用户,使用user用户对应的密码(在/etc/rsync.pas指定),使用--exclude-from排除不同步的目录和文件

注解:
--exclude-from表示从一个文件中排除不同步的目录,该文件必须使用相对路径,支持通配符
如:
api #排除api目录
.* #排除隐藏文件


/etc/rsync.pas密码文件说使用的密码和slave中的static模块secrets file文件中指定的密码一致,用户名也和statid中指定的一致


rsync参数的具体解释如下:
-v, --verbose 详细模式输出
-q, --quiet 精简输出模式
-c, --checksum 打开校验开关,强制对文件传输进行校验
-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
-r, --recursive 对子目录以递归模式处理
-R, --relative 使用相对路径信息
-b, --backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用--suffix选项来指定不同的备份文件前缀。
--backup-dir 将备份文件(如~filename)存放在在目录下。
-suffix=SUFFIX 定义备份文件前缀
-u, --update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件)
-l, --links 保留软链结
-L, --copy-links 想对待常规文件一样处理软链结
--copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链结
--safe-links 忽略指向SRC路径目录树以外的链结
-H, --hard-links 保留硬链结
-p, --perms 保持文件权限
-o, --owner 保持文件属主信息
-g, --group 保持文件属组信息
-D, --devices 保持设备文件信息
-t, --times 保持文件时间信息
-S, --sparse 对稀疏文件进行特殊处理以节省DST的空间
-n, --dry-run现实哪些文件将被传输
-W, --whole-file 拷贝文件,不进行增量检测
-x, --one-file-system 不要跨越文件系统边界
-B, --block-size=SIZE 检验算法使用的块尺寸,默认是700字节
-e, --rsh=COMMAND 指定使用rsh、ssh方式进行数据同步
--rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息
-C, --cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件
--existing 仅仅更新那些已经存在于DST的文件,而不备份那些新创建的文件
--delete 删除那些DST中SRC没有的文件
--delete-excluded 同样删除接收端那些被该选项指定排除的文件
--delete-after 传输结束以后再删除
--ignore-errors 及时出现IO错误也进行删除
--max-delete=NUM 最多删除NUM个文件
--partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输
--force 强制删除目录,即使不为空
--numeric-ids 不将数字的用户和组ID匹配为用户名和组名
--timeout=TIME IP超时时间,单位为秒
-I, --ignore-times 不跳过那些有同样的时间和长度的文件
--size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间
--modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0
-T --temp-dir=DIR 在DIR中创建临时文件
--compare-dest=DIR 同样比较DIR中的文件来决定是否需要备份
-P 等同于 --partial
--progress 显示备份过程
-z, --compress 对备份的文件在传输时进行压缩处理
--exclude=PATTERN 指定排除不需要传输的文件模式
--include=PATTERN 指定不排除而需要传输的文件模式
--exclude-from=FILE 排除FILE中指定模式的文件
--include-from=FILE 不排除FILE指定模式匹配的文件
--version 打印版本信息
--address 绑定到特定的地址
--config=FILE 指定其他的配置文件,不使用默认的rsyncd.conf文件
--port=PORT 指定其他的rsync服务端口
--blocking-io 对远程shell使用阻塞IO
-stats 给出某些文件的传输状态
--progress 在传输时显示传输过程
--log-format=formAT 指定日志文件格式
--password-file=FILE 从FILE中得到密码
--bwlimit=KBPS 限制I/O带宽,KBytes per second
-h, --help 显示帮助信息


2.查看master是否支持inotify
#ll /proc/sys/fs/inotify/
rw-r--r-- 1 root root 0 4月  22 14:56 max_queued_events
-rw-r--r-- 1 root root 0 4月  22 14:56 max_user_instances
-rw-r--r-- 1 root root 0 4月  22 14:56 max_user_watches

出现以上信息表示支持

/proc/sys/fs/inotify/max_queued_evnets      

表示调用inotify_init时分配给inotify instance中可排队的event的数目的最大值,超出这个值的事件被丢弃,但会触发IN_Q_OVERFLOW事件。

    /proc/sys/fs/inotify/max_user_instances

表示每一个real user ID可创建的inotify instatnces的数量上限。

    /proc/sys/fs/inotify/max_user_watches

表示每个inotify instatnces可监控的最大目录数量。如果监控的文件数目巨大,需要根据情况,适当增加此值的大小。

例如: echo 30000000 > /proc/sys/fs/inotify/max_user_watches


3.编写inotify脚本:

#!/bin/bash
#param
host01=192.168.1.84  #inotify-slave的ip地址
src=/path/dir/        #本地监控的目录
dst=static         #inotify-slave的rsync服务的模块名
user=user      #inotify-slave的rsync服务的虚拟用户
rsync_passfile=/etc/rsync.pas   #本地调用rsync服务的密码文件
inotify_home=/usr    #inotify的安装目录
inotify_exclude = /other-config/inotify-exclude.conf #intofiy排除的目录
rsync_check = /other-config/inotify-check.conf #intofiy检测的目录
rsync_path = /path/dir #rsync 同步目录
#judge
if [ ! -e "$src" ]
|| [ ! -e "${rsync_passfile}" ]
|| [ ! -e "${inotify_home}/bin/inotifywait" ]
|| [ ! -e "/usr/bin/rsync" ];
then
echo "Please Check File and Folder"
exit 9
fi
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib --fromfile='${rsync_check}' 
| while read file
do
/usr/bin/rsync -vzrtopg --progress --exclude-from=${rsync_exclude} {$rsync_path} $user@$host01::$dst --password-file=${rsync_passfile}
done
exit 0

保存以上脚本为inotify-rsync.sh
#chmod +x inotify-rsync.sh


注解:
--fromfile标示从一个指定文件中载入监控和排除的目录
该文件中使用绝对路径
如:
/path/dir/about
@/path/dir/my

以上表示监控/path/dir/about,不监控/path/dir/my


4.以后台方式运行
#/bin/bash inotify-rsync.sh &

5.加入开机启动
#vi /etc/rc.d/rc.local
在最后加入
/bin/bash inotify-rsync.sh &

:wq
保存退出

至此inotify+rsync同步完成

原文地址:https://www.cnblogs.com/itfenqing/p/5401407.html