环境:
操作系统:CentOS 7u3
源服务器:192.168.245.147
目标服务器:192.168.245.148
目的:把源服务器上/home_test目录实时同步到目标服务器的/home_test下
安全相关:关闭SELINUX和防火墙
一、在目标服务器安装Rsync服务端
安装Rsync服务端软件
rhel7:
#yum install rsync
#systemctl start rsyncd
rhel6:
#yum install rsync xinetd
#vi /etc/xinetd.d/rsync
disable = no
#/etc/init.d/xinetd start
创建rsyncd.conf配置文件
#vi /etc/rsyncd.conf #7上有默认配置文件,rhel6上没有默认文件要自己创建配置文件,添加以下代码
log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.pass #用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件
motd file = /etc/rsyncd.Motd #rsync启动时欢迎信息页面文件位置(文件内容自定义)
[home_test] #同步模块名称
path = /home_test #rsync服务端数据目录路径
comment = home_test
uid = root
gid = root
port=873
use chroot = no
read only = no
list = no #不显示rsync服务端资源列表
max connections = 200
timeout = 600
auth users = user1 #执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
hosts allow = 192.168.245.147 #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny = 192.168.245.150
创建用户认证文件
#vim /etc/rsync.pass
user1:123456 #格式,用户名:密码,可以设置多个,每行一个用户名:密码
设置文件权限
chmod 600 /etc/rsyncd.conf #设置文件所有者读取、写入权限
chmod 600 /etc/rsync.pass #设置文件所有者读取、写入权限
启动rsync
rhel7:
#systemctl restart rsyncd
rhel6:
#/etc/init.d/xinetd start #启动
#service xinetd stop #停止
#service xinetd restart #重新启动
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第二部分:在源服务器192.168.245.147上操作
一、安装Rsync客户端软件
#yum install rsync xinetd
创建认证密码文件
#vim /etc/passwd.txt
123456 #密码
#chmod 600 /etc/passwd.txt #设置文件权限,只设置文件所有者具有读取、写入权限即可
测试源服务器192.168.245.147到两台目标服务器192.168.245.148之间的数据同步
#mkdir /home_test/ceshi #在源服务器上创建测试文件夹,然后在源服务器运行下面命令
#rsync -avH --port=873 --progress --delete /home_test user1@192.168.245.148::home_test --password-file=/etc/passwd.txt
运行完成后,分别在两台目标服务器192.168.245.148上查看,在/home_test目录下有ceshi文件夹,说明数据同步成功。
二、安装Inotify-tools工具,实时触发rsync进行同步
1、查看服务器内核是否支持inotify
#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
2、安装inotify-tools
#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
3、设置系统环境变量,添加软连接
#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
#ln -s /usr/local/inotify/lib/libnotifytools.so.0 /usr/lib64
4、修改inotify默认参数(inotify默认内核参数值太小)
查看系统默认参数值
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="99999999"
sysctl -w fs.inotify.max_user_watches="99999999"
sysctl -w fs.inotify.max_user_instances="65535"
#vi /etc/sysctl.conf #添加以下代码
fs.inotify.max_queued_events=99999999
fs.inotify.max_user_watches=99999999
fs.inotify.max_user_instances=65535
参数说明:
max_queued_events:
inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确
max_user_watches:
要同步的文件包含多少目录,可以用:find /home_test -type d | wc -l 统计,必须保证max_user_watches值大于统
计结果(这里/home_test为同步文件目录)
max_user_instances:
每个用户创建inotify实例最大值
5、创建脚本,实时触发rsync进行同步
#vim /usr/local/inotify/rsync.sh
#!/bin/bash
srcdir="/home_test"
dstdir="home_test"
excludedir="/usr/local/inotify/exclude.list"
rsyncuser="user1"
rsyncpassdir="/etc/passwd.txt"
dstip="192.168.245.148"
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
脚本参数说明:
excludedir=/usr/local/inotify/exclude.list
#不需要同步的目录,如果有多个,每一行写一个目录,使用相对于同步模块的路径;
#例如:不需要同步/home_test目录下的a目录和b目录下面的b1目录,exclude.list文件可以这样写
a/
b/b1/
/tmp/rsync.log #脚本运行日志记录
6、设置脚本开机自动执行
#vim /etc/rc.d/rc.local #编辑,在最后添加一行
sh /usr/local/inotify/rsync.sh & #设置开机自动在后台运行脚本
#chmod +x /etc/rc.d/rc.local
#systemctl enable rc-local
7、测试inotify实时触发rsync同步脚本是否正常运行
在源服务器192.168.245.147上创建文件inotify_rsync_ceshi
#mkdir /home_test/inotify_rsync_ceshi
重新启动源服务器:192.168.245.147
等系统启动之后,查看目标服务器192.168.245.148的/home_test下是否有inotify_rsync_ceshi文件夹
然后再在源服务器192.168.245.147创建文件夹inotify_rsync_ceshi_new
#mkdir /home_test/inotify_rsync_ceshi_new
继续查看目标服务器192.168.245.148的/home_test下是否有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 显示帮助信息
rsync+inotify
最新推荐文章于 2024-10-04 15:13:23 发布