inotify + rsync实时同步
1、准备工作
1)内核版本必须在2.6.13以上
[root@nfs-31 ~]
3.10.0-862.el7.x86_64
2)看是否存在3个文件系统,存在才支持
[root@nfs-31 ~]
total 0
-rw-r--r-- 1 root root 0 Apr 20 20:08 max_queued_events
-rw-r--r-- 1 root root 0 Apr 20 20:08 max_user_instances
-rw-r--r-- 1 root root 0 Apr 20 20:08 max_user_watches
系统文件解释
max_user_watches: 设置inotifywait或inotifywatch命令可以监视的文件数量(单进程)
默认只能监控8192个文件
max_user_instances: 设置每个用户可以运行的inotifywait或inotifywatch命令的进程数
默认每个用户可以开启inotify服务128个进程
max_queued_events: 设置inotify实例事件(event)队列可容纳的事件数量
默认监控事件队列长度为16384
2、安装inotifty-tools工具
[root@nfs-31 ~]
检查生成的软件命令
[root@nfs-31 ~]
/usr/bin/inotifywait
/usr/bin/inotifywatch
3、编辑脚本
[root@nfs-31 /afei]
path=/afei
backup_server=172.16.1.41
export RSYNC_PASSWORD=111111
/usr/bin/inotifywait -mrq -e modify,delete,create,attrib,move /afei | while read line
do
echo ${line}
rsync -az --delete /afei/ rsync_user@${backup_server}::data
done
4、执行脚本就行测试
[root@nfs-31 /afei]

sersync + rsync实时同步
1、在nfs-31服务器下载sersync程序
cd /opt && wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz
2、解压
[root@nfs-31 /opt]
GNU-Linux-x86/
GNU-Linux-x86/sersync2
GNU-Linux-x86/confxml.xml
3、重命名,查看配置文件
[root@nfs-31 /opt]
[root@nfs-31 /opt]
4、该软件就俩文件,一个二进制,一个配置文件
[root@nfs-31 /opt/sersync-2-5-4]
confxml.xml sersync2
[root@nfs-31 /opt/sersync-2-5-4]
sersync2: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, for GNU/Linux 2.6.15, BuildID[sha1]=c41f3d73f3d4fe1d6931d93a4e64fe805769b28f, stripped
[root@nfs-31 /opt/sersync-2-5-4]
confxml.xml: XML 1.0 document, ASCII text
5、修改配置文件
1)备份原来的
[root@nfs-31 /opt/sersync-2-5-4]
[root@nfs-31 /opt/sersync-2-5-4]
confxml.xml confxml.xml.bak sersync2
2)改配置文件
<sersync>
<localpath watch="/afei">
<remote ip="172.16.1.41" name="data"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-az"/>
<auth start="true" users="rsync_user" passwordfile="/etc/rsync.passwd"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
6、启动程序
[root@nfs-31 /opt/sersync-2-5-4]
7、进入/afei目录
9、在nfs-31输入数据
[root@nfs-31 /afei]
10、在rsync-41上监测
[root@rsync-41 /data]

lsyncd+rsync实时同步
1、下载lsyncd服务
[root@nfs-31 /afei]
2、保存原来配置文件
[root@nfs-31 ~]
3、修改配置文件(监控一个目录)
[root@nfs-31 ~]
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status",
inotifyMode = "CloseWrite",
maxProcesses = 8,
}
sync {
default.rsync,
source = "/afei/",
target = "rsync_user@10.0.0.41::data",
delete= true,
exclude = {".*"},
delay = 1,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
verbose = true,
password_file = "/etc/rsync.passwd",
_extra = {"--bwlimit=200"}
}
}
4、nfs-31放入密码文件
[root@nfs-31 ~]
111111
修改权限
[root@nfs-31 ~]
[root@nfs-31 ~]
-rw------- 1 root root 7 Apr 25 21:01 /etc/rsync.passwd
5、启动
[root@nfs-31 ~]
6、查看状态
[root@nfs-31 ~]
7、测试
nfs-31服务器
[root@nfs-31 /afei]
rsync-41服务器
[root@rsync-41 /data]

web-7 (1.web-7的网页根目录数据来自于nfs共享目录/nfs-nginx-data/ ,要求该目录下的所有用户映射为www(uid=11111),允许读写)
nfs-31 (2.在web-7的nginx网站根目录下创建首页文件后,触发实时同步,备份到rsync-41机器上)
rsync-41 (3.数据备份服务器)
nfs-31
[root@nfs-31 ~]
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
yum install nfs-utils rpcbind -y
yum install rsync -y
systemctl restart rpcbind
systemctl restart nfs-server
systemctl is-enabled nfs
systemctl enable rpcbind nfs
mkdir -p /nfs-nginx-data
useradd www -u 11111 -M -s /nfs-nginx-data
cat > /etc/exports <<EOF
/nfs-nginx-data 172.16.1.7(rw,sync,all_squash,anonuid=11111,anongid=11111)
EOF
exportfs -r
chown -R www:www /nfs-nginx-data
yum install lsyncd -y
cp /etc/lsyncd.conf{,.bak}
cat > /etc/lsyncd.conf << EOF
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status",
inotifyMode = "CloseWrite",
maxProcesses = 8,
}
sync {
default.rsync,
source = "/nfs-rsync-data",
target = "rsync_user@10.0.0.41::tantan01",
delete= true,
exclude = {".*"},
delay = 1,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
verbose = true,
password_file = "/etc/rsync.pwd",
_extra = {"--bwlimit=200"}
}
}
EOF
echo "111111" > /etc/rsync.pwd
chmod 600 /etc/rsync.pwd
systemctl start lsyncd
rsync-41
[root@rsync-41 ~]
yum install rsync -y
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
cat > /etc/rsyncd.conf << 'EOF'
uid = afei
gid = afei
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_user
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
[tantan01]
comment = yuchaoit.cn about rsync
path = /tantan01
[tantan02]
path = /tantan02
EOF
useradd -u 2000 -M -s /sbin/nologin afei
mkdir /tantan01
mkdir /tantan02
chown -R afei:afei /tantan01/
chown -R afei:afei /tantan02/
echo 'rsync_user:111111' > /etc/rsync.passwd
chmod 600 /etc/rsync.passwd
systemctl start rsyncd
web-7
[root@web-7 ~]
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
yum install nginx -y
useradd www -M -s /sbin/nologin
sed -i 's#user nginx#user www#g' /etc/nginx/nginx.conf
systemctl start nginx
yum install nfs-utils -y
systemctl start rpcbind
mount -t nfs 172.16.1.31:/nfs-nginx-data /usr/share/nginx/html
cd /usr/share/nginx/html && echo '人生无常,大肠包小肠' > index.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?