rsync实时同步

15 篇文章 0 订阅

一:配置rsync源服务器(服务器A)

在这里插入图片描述

注意事项 : 可以在最开始就把每台服务器上的防火墙关闭,当然也可以开放防火墙
这里是关闭防火墙以及Linux内核

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
注意 每台服务器都可以这样配置 我已经试过了 可以实现  注意改动IP地址
[root@localhost ~]# yum -y install rsync xinetd

[root@localhost ~]# vim /etc/rsyncd.conf //rsync的配置文件

uid = nobody
gid = nobody
use chroot = yes
address = 192.168.10.101
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.10.0/24

[www]                                    //同步站点的名字  (可以多个)
    path = /var/www/html                 //同步的目录 
    comment = Document Root of www1.benet.com
    read only = no                       //需要no掉  yes是只读 no是可写
    dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z
    auth users = bdqn
secrets file = /etc/rsyncd_users.db

[root@localhost ~]# vim /etc/rsyncd_users.db //创建文件 添加一下内容

bdqn:pwd123
[root@localhost ~]# chmod 600 /etc/rsyncd_users.db    //限制权限
[root@localhost ~]# chmod -R 777 /var/www/html/     //给同步目录权限
[root@localhost ~]# rsync –daemon            //以守护进程方式启动  想要关闭需要kill
[root@localhost ~]# netstat -auntp | grep rsync
tcp        0      0 192.168.10.101:873      0.0.0.0:*      LISTEN      57018/rsync   

二:配置rsync目标服务器(服务器B)

这是服务器的内核参数: (都是默认的)
[root@localhost ~]# vi /proc/sys/fs/inotify/max_queued_events

16384           \\监控时间队列 

[root@localhost ~]# vi /proc/sys/fs/inotify/max_user_instances

128            \\最多监控实例树

[root@localhost ~]# vi /proc/sys/fs/inotify/max_user_watches

8192             \\每隔实例最多监控文件数

安装inotify 实现同步复制

[root@localhost ~]# cd /usr/src
[root@localhost src]# tar zxf inotify-tools-3.14.tar.gz 
[root@localhost src]# cd inotify-tools-3.14
[root@localhost inotify-tools-3.14]# ./configure
[root@localhost inotify-tools-3.14]# make
[root@localhost inotify-tools-3.14]# make install

[root@localhost ~]# vim /etc/server.pass //添加一个密码文件 方便脚本使用

添加
pwd123

[root@localhost ~]# chmod 600 /etc/server.pass //限制权限
[root@localhost ~]# vim 1.sh //写个同步脚本

#!/bin/bash 
INOTIFY_CMD="inotifywait -mrq -e modify,create,move,delete /var/www/html"
RSYNC_CMD="rsync -avzH --delete --password-file=/etc/server.pass /var/www/html/ bdqn@192.168.10.102::www" 
$INOTIFY_CMD | while read DIRECTORY EVENT FILE 
do
        $RSYNC_CMD
Done

-m:持续监控
-r:递归整个目录
-q:简化输出信息

打开另一个终端在var/www/html中创建文件,观察上个终端的变化
在到服务器(A)上看看var/www/html中的变化

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值