rsync+inotify

本文介绍如何在Ubuntu 12.04系统中利用rsync实现数据的实时同步。详细步骤包括安装配置rsync服务、设置权限及密码文件、编写同步脚本并使其随系统启动自动运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

系统:ubuntu12.04
源端:192.168.88.145
节点端:192.168.88.155
应用:数据的实时同步

rsync节点端:

sudo apt-get install rsync 
#/usr/share/doc/rsync/examples/rsyncd.conf  参考配置文件
sudo vim /etc/rsync.conf  需自行创建
-->
uid=root
gid=root
use chroot = no
max connections = 20
strict modes = yes
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log format = %t %a %m %f %b
hosts allow = 192.168.88.0/24
hosts_deny= *

[test]
path = /app/webapps
auth users = xxx    ---用户
ignore errors = yes
read only = no
write only = no
list = no
secrets file = /etc/rsync.passwd

mkdir /app/webapps -p
chown xxx.xxx /app/webapps -R
sudo vim /etc/rsync.passwd
格式  用户:密码
eg--> xxx:pass
sudo chmod 600 /etc/rsync.passwd
启动daemon:sudo rsync --daemon --config=/etc/rsync.conf
写入开机自启动:vim  /etc/rc.local
-->rsync --daemon --config=/etc/rsync.conf

源端:
sudo apt-get install rsync inotify-tools
sudo vim /etc/rsync.passwd
-->这里只需密码 如:pass
sudo chmod 600 /etc/rsync.passwd
sudo chown xxx.xxx /etc/rsync.passwd
mkdir /app/webapps -p
chown xxx.xxx /app/webapps -R

编写rsync同步脚本rsync.sh
-->
#!/bin/sh

SRC=/app/webapps
#DES=root
#USER=root

media1=192.168.88.155
#media2=192.168.88.130
#media3=192.168.88.131

#/usr/bin/inotifywait -mrq -e create,move,delete,modify $SRC | while read files
/usr/bin/inotifywait -mrq -e create,move,delete,modify $SRC | while read D E F
 
do
rsync -avzxPl --delete --password-file=/etc/rsync.passwd /app/webapps/ xxx@$media1::test
rsync -avzxPl --delete --password-file=/etc/rsync.passwd /app/webapps/ xxx@$media2::test
if [ $? -eq 0 ];then
        echo "${file} ok" >>/tmp/rsync.log 2>&1
fi
done

启动脚本:nohup /etc/rsync.sh &
注:用nohup后台启动,当终端关闭后进程不结束
写入开机自启动:vim  /etc/rc.local
--->nohup /etc/rsync.sh &

测试:略
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值