rsync+inotify实现文件增量实时同步
注解:rsync是数据同步,inotify是实时同步,不要搞混
项目环境
两个服务器:
服务器A: 100.102.3.133 源服务器 有目录 /data/site/
服务器b: 100.102.3.135 目标服务器 有目录 /home/application/site/
防火墙{项目环境,不是必要条件}
服务器b: 100.102.3.133
firewall-cmd --perman --zone=public --add-rich-rule=“rule family=“ipv4” source address=“100.102.3.135” accept”
firewall-cmd --reload
firewall-cmd --list-all
服务器A: 100.102.3.135
firewall-cmd --perman --zone=public --add-rich-rule=“rule family=“ipv4” source address=“100.102.3.133” accept”
firewall-cmd --reload
firewall-cmd --list-all
项目需求
1.将 100.102.3.133 中的 /data/site/ 下的文件 实时同步到100.102.3.135下边的 /home/application/site/ 文件夹中
3.开机自启动同步程序
4.记录同步数据的日志
一、rsync安装部署
1.检查是否安装rsync
rpm -qa|grep rsync #默认自带的,所以没有写安装教程
2.测试系统rsync命令
在133上执行,会将135的site 同步到133/home/site/的文件中
rsync -avH --delete root@100.102.3.135:/home/application/site/ /home/site/ --log-file=/home/application/log/aa.log
3.新建配置文件
服务器A{133}
创建或配置/etc/rsyncd.conf文件
[root@VM-12-13-centos /]# vi /etc/rsyncd.conf ###注意空格问题,否则启动不了
#进程对应的进程号文件
pid file = /var/run/rsyncd.pid
port = 873
address = 100.102.3.133
uid = root
gid = root
use chroot = yes
read only = no
hosts allow = 100.102.3.135
hosts deny=*
max connections = 10
motd file = /etc/rsyncd.motd
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
log format = %t %a %m %f %b
syslog facility = local3
timeout = 300
[igiNet]
path = /home/application/site/
list=no
ignore errors
comment = igiNet
auth users = mngUser
secrets file = /etc/rsyncd.secret
创建文件后赋权
[root@VM-12-13-centos /]# chmod 600 /etc/rsyncd.conf
创建授权账户的密码文件
[root@VM-12-13-centos /]# vi /etc/rsyncd.pwd
netPwd
创建文件后赋权
[root@VM-12-13-centos /]# chmod 600 /etc/rsyncd.pwd
[root@VM-12-13-centos /]# vi /etc/rsyncd.secret
mngUser:mngPwd
创建文件后赋权
[root@VM-12-13-centos /]# chmod 600 /etc/rsyncd.secret
服务器B{135}
创建或配置/etc/rsyncd.conf文件
pid file = /var/run/rsyncd.pid
port = 873
address = 100.102.3.135
uid = root
gid = root
use chroot = yes
read only = no
hosts allow = 100.102.3.133
hosts deny=*
max connections = 10
motd file = /etc/rsyncd.motd
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
log format = %t %a %m %f %b
syslog facility = local3
timeout = 300
[igiNet]
path = /home/application/site/
list=no
ignore errors
comment = igiNet
auth users = netUser
secrets file = /etc/rsyncd.secret
创建授权账户的密码文件
[root@VM-12-13-centos /]# vi /etc/rsyncd.pwd
mngPwd
创建文件后赋权
[root@VM-12-13-centos /]# chmod 600 /etc/rsyncd.pwd
[root@VM-12-13-centos /]# vi /etc/rsyncd.secret
netUser:netPwd
创建文件后赋权
[root@VM-12-13-centos /]# chmod 600 /etc/rsyncd.secret
4.启动【占用端口873】
rsync --daemon --config=/etc/rsyncd.conf
查看是否启动成功
ps -ef|grep rsync
netstat -ntap | grep rsync
5.关闭
kill -9 pid
关闭不掉需要执行以下操作
[root@VM-12-13-centos /] cd /var/run/
[root@VM-12-13-centos /run] cat rsyncd.pid ##查看pid文件
[root@VM-12-13-centos /run] rm -rf rsyncd.pid ##删除pid文件
[root@VM-12-13-centos /run] rsync --daemon ##服务正常启动
5.rsync命令介绍
本文章用了 avH
rsync [选项] 原始位置 目标位置
常用选项 说明
-r 递归模式,包含目录及子目录中的所有文件
-l 对于符号链接文件仍然复制为符号链接文件
-v 显示同步过程的详细信息
-z 在传输文件时进行压缩goD
-p 保留文件的权限标记
-a 归档模式,递归并保留对象属性,等同于-rlpt
-t 保留文件的时间标记
-g 保留文件的属组标记(仅超级用户使用)
-o 保留文件的属主标记(仅超级用户使用)
-H 保留硬链接文件
-A 保留ACL属性信息
-D 保留设备文件及其他特殊文件
--delete 删除目标位置有而原始位置没有的文件
--checksum 根据对象的校验和来决定是否跳过文件
二、inotify-tools安装部署
inotify-tools 下载路径
https://sourceforge.net/projects/inotify-tools/files/latest/download
# 解压inotify
tar -zxvf inotify-tools-3.13.tar.gz
# 进入解压目录
cd inotify-tools-3.13
# 编译并安装(prefix是指定安装目录,编译安装前需安装好GCC : yum install gcc)
./configure --prefix=/home/application/inotifor/
安装
make && make install
三、rsync+inotify实现文件增量实时同步
脚本说明
1.【服务器A】和【服务器B】是一样的脚本,dstdir上边rsyncd.conf配置的参数,rsyncuser 用户 rsyncpassdir用户的密码文件。
2.如果单项同步【服务器A】-【服务器B】是不需要再【服务器B】上编辑脚本的。
3.【服务器A】,【服务器B】相互同步,可以实现附件共享的作用,和(共享磁盘)的作用是一致的。
srcdir 需要同步的文件路径,路径最后面必须加入 / 否则会导致循环同步
dstdir 目标服务器的的模块名称,该名称即rsyncd.conf文件中[ ] 的内容
excludedir 需要排除的文件列表,每一行是一条记录
rsyncuser 同步使用的用户名
rsyncpassdir 同步使用的用户的密码
dstip 目标ip,多个ip以空格区分
服务器A{133}
配置文件inotify_back.sh
#!/bin/sh
srcdir=/data/site/
dstdir=igiNet
rsyncuser=netUser
rsyncpassdir=/etc/rsyncd.pwd
dstip="100.102.3.135"
for ip in $dstip
do
rsync -avH --delete $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdir
done
/home/application/inotifor/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 --delete $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdir
echo " ${file} was rsynced" >> /tmp/rsync.log 2>&1
done
done
设置脚本权限
chmod U+X inotify_back.sh
脚本执行失败
sed -i 's/\r$//' inotify_back.sh
启动inotify
nohup ./inotify_back.sh 2>&1 </dev/null | cat >> log.out &