说明:
一、先安装好rsync的服务端和客户端:
二、安装,使用inotify-tools,实时同步
1、查看服务器内核是否支持inotify 1
2
3
4
5
6
| ll /proc/sys/fs/inotify #列出文件目录,出现下面的内容,说明服务器内核支持inotify
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_queued_events
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_instances
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_watches
备注:Linux下支持inotify的内核最小为2.6.13,可以输入命令:uname -a查看内核
CentOS 5.X 内核为2.6.18,默认已经支持inotify
|
2、安装inotify-tools 1
2
3
4
5
6
7
8
9
| yum install make gcc gcc-c++ #安装编译工具
inotify-tools下载地址:http://github.com/downloads/rvoi ... y-tools-3.14.tar.gz
上传inotify-tools-3.14.tar.gz到/usr/local/src目录下
cd /usr/local/src
tar zxvf inotify-tools-3.14.tar.gz #解压
cd inotify-tools-3.14 #进入解压目录
./configure --prefix=/usr/local/inotify #配置
make #编译
make install #安装
|
3、设置系统环境变量,添加软连接 1
2
3
4
| echo "PATH=/usr/local/inotify/bin:$PATH" >>/etc/profile.d/inotify.sh
source /etc/profile.d/inotify.sh #使设置立即生效
echo "/usr/local/inotify/lib" >/etc/ld.so.conf.d/inotify.conf
ln -s /usr/local/inotify/include /usr/include/inotify
|
4、修改inotify默认参数(inotify默认内核参数值太小) 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| 查看系统默认参数值
sysctl -a | grep max_queued_events
结果是:fs.inotify.max_queued_events = 16384
sysctl -a | grep max_user_watches
结果是:fs.inotify.max_user_watches = 8192
sysctl -a | grep max_user_instances
结果是:fs.inotify.max_user_instances = 128
修改参数:
sysctl -w fs.inotify.max_queued_events="65535"
sysctl -w fs.inotify.max_user_watches="65535"
sysctl -w fs.inotify.max_user_instances="65535"
vi /etc/sysctl.conf #添加以下代码
fs.inotify.max_queued_events=65535
fs.inotify.max_user_watches=65535
fs.inotify.max_user_instances=65535
:wq! #保存退出
参数说明:
max_queued_events:
inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确
max_user_watches:
要同步的文件包含多少目录,可以用:find /home/www.osyunwei.com -type d | wc -l 统计,必须保证max_user_watches值大于统计结果(这里/home/www.osyunwei.com为同步文件目录)
max_user_instances:
每个用户创建inotify实例最大值
|
5、创建脚本,实时触发rsync进行同步 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| #!/bin/sh
srcdir=/home/www.osyunwei.com/
dstdir=home_www.osyunwei.com
excludedir=/usr/local/inotify/exclude.list
rsyncuser=home_www.osyunwei.com_user
rsyncpassdir=/etc/passwd.txt
dstip="192.168.21.127 192.168.21.128"
for ip in $dstip
do
rsync -avH --port=873 --progress --delete --exclude-from=$excludedir $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdir
done
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $srcdir | while read file
do
for ip in $dstip
do
rsync -avH --port=873 --progress --delete --exclude-from=$excludedir $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdir
echo " ${file} was rsynced" >> /tmp/rsync.log 2>&1
done
done
|
chmod +x /usr/local/inotify/rsync.sh #添加脚本执行权限 脚本参数说明: 1
2
3
4
5
6
7
8
9
10
11
| srcdir=/home/www.osyunwei.com/ #源服务器同步目录
dstdir=home_www.osyunwei.com #目标服务器rsync同步目录模块名称
excludedir=/usr/local/inotify/exclude.list
#不需要同步的目录,如果有多个,每一行写一个目录,使用相对于同步模块的路径;
#例如:不需要同步/home/www.osyunwei.com/目录下的a目录和b目录下面的b1目录,exclude.list文件可以这样写
a/
b/b1/
rsyncuser=home_www.osyunwei.com_user #目标服务器rsync同步用户名
rsyncpassdir=/etc/passwd.txt #目标服务器rsync同步用户的密码在源服务器的存放路径
dstip="192.168.21.127 192.168.21.128" #目标服务器ip,多个ip用空格分开
/tmp/rsync.log #脚本运行日志记录
|
6、设置脚本开机自动执行 1
2
3
| vi /etc/rc.d/rc.local #编辑,在最后添加一行
sh /usr/local/inotify/rsync.sh & #设置开机自动在后台运行脚本
:wq! #保存退出
|
7、测试inotify实时触发rsync同步脚本是否正常运行 1
2
3
4
5
6
7
| 在源服务器192.168.21.129上创建文件inotify_rsync_ceshi
mkdir /home/www.osyunwei.com/inotify_rsync_ceshi
重新启动源服务器:192.168.21.129
等系统启动之后,查看两台目标服务器192.168.21.127,192.168.21.128的/home/www.osyunwei.com下是否有inotify_rsync_ceshi文件夹
然后再在源服务器192.168.21.129创建文件夹inotify_rsync_ceshi_new
mkdir /home/www.osyunwei.com/inotify_rsync_ceshi_new
继续查看两台目标服务器192.168.21.127,192.168.21.128的/home/www.osyunwei.com下是否有inotify_rsync_ceshi_new文件夹
|
如果以上测试都通过,说明inotify实时触发rsync同步脚本运行正常。 至此,Linux下Rsync+Inotify-tools实现数据实时同步完成。 扩展阅读: ============================================ inotify参数 -m 是保持一直监听 -r 是递归查看目录 -q 是打印出事件 -e create,move,delete,modify,attrib 是指 “监听 创建 移动 删除 写入 权限” 事件 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 显示帮助信息
|