rsync+notify 同步(异步方式)文件

同步文件,多个主机。可以做图片服务同步,代码管理同步等。通过异步方式同步,监控到文件的变化。同步更新变化的内容,效率比较好。

环境说明

服务类型IP地址应用操作系统
源服务器192.168.217.151rsync inotify-tools 脚本centos7/redhat7
目标服务器192.168.217.150rsynccentos7/redhat7

在目标服务器上做以下配置

1.关闭防火墙与SELINUX
# systemctl stop firewalld
# systemctl disable firewalld
# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
# getenforce 0

2.安装rsync服务端软件
#yum -y install rsync
3.设置rsyncd.conf配置文件(前后不要有空格)test为同步的文件夹
# vim /etc/rsyncd.conf

log file = /var/log/rsyncd.log
//日志文件位置,启动rsync后自动产生,无需提前创建
pidfile = /var/run/rsyncd.pid
//pid文件存放位置
lock file = /var/run/rsync.lock
//支持max connections参数的锁文件
secrets file = /etc/rsync.pass
//用户认证配置文件,里面存放用户名称和密码,必须手动创建这个文件
   
[etc_from_client]
//自定义同步名称
path = /test/
//rsync服务端存放路径,客户端的数据将同步到此目录
comment = sync etc from client
uid = root
//设置rsync运行权限为root
gid = root
//设置rsync运行权限为root
port = 873
//默认端口为873
ignore errors
//表示出现错误忽视错误
use chroot = no
//默认为true ,修改为no,增加对目录软链接的备份
read only = no
//设置rsync服务端为读写权限
list = no
//不显示rsync服务端资源列表
max connections = 200
//最大连接数
timeout = 600
//设置超时时间
auth users = admin
//执行数据同步的用户名,可以设置多个,用英文逗号隔开
hosts allow = 192.168.217.151
//允许进行数据同步的IP地址,可以设置多个,用英文逗号隔开

4.创建存放路径目录
# mkdir /test
5.创建用户认证文件
echo 'admin:111' > /etc/rsync.pass
cat /etc/rsync.pass

6.设置文件权限
chmod 600 /etc/rsync* 
ll /etc/rsync*

7.启动rsync服务并设置开机自启动
# systemctl start rsyncd
# systemctl enable rsyncd


在源服务器上做以下部署:

1.关闭防火墙与SELINUX
#  systemctl stop firewalld
# systemctl disable firewalld
#  sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
# setenforce 0 
2.安装rsync服务端软件

 yum -y install rsync

3.创建认证密码文件
echo '111' > /etc/rsync.pass
# cat /etc/rsync.pass
4.设置文件权限,只设置文件所有者具有读取、写入的权限
# chmod 600 /etc/rsync.pass
# ll /etc/rsync.pass

5.在源服务器上创建测试目录,然后在源服务器上运行以下命令
rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.217.150::etc_from_client --password-file=/etc/rsync.pass

6.运行完成后在目标服务器上查看,在/test/目录下有test目录,说明数据同步成功
ls /test

7.安装inotify-tools工具,实时触发rsync同步
检查服务器内核是否支持inotify,如果有这三个max开头的文件则表示服务器内核支持inotify
ll /proc/sys/fs/inotify/
yum -y install make gcc gcc-c++ inotify-tools


8.写同步脚本

# mkdir /scripts

# touch /scripts/inotify.sh
# chmod 755 /scripts/inotify.sh
# ll
 # vim /scripts/inotify.sh host=192.168.217.150 //目标服务器的ip(备份服务器) src=/test //在源服务器上所要监控的备份目标 des=etc_from_client //自定义的模块名,需要与目标服务器上的定义名称同步 password=/etc/rsync.pass //执行数据同步的密码文件 user=admin //执行数据同步的名 inotifywait=/usr/bin/inotifywait $inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \ | while read files ; do rsync -avzP --delete --timeout=100 --password-file=${password} $src $user@$host::$des echo "${files} was rsynced" >>/tmp/rsync.log 2>&1 done

//检查脚本
# bash -x /scripts/inotify.sh


//启动脚本

nohup bash /scripts/inotify.sh &
# ps -ef|grep inotify
9.设置脚本开机自动启动
# chmod +x /etc/rc.d/rc.local
ll /etc/rc.d/rc.local
echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local
tail /etc/rc.d/rc.local

10.谁便在根目录test里边建点文件,到目标服务器上查看是否把新生成的文件自动传上去了

 
 
 
 
 


 

 
 
 


 


 



 
 

转载于:https://www.cnblogs.com/cx558/p/9547398.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值