rsync远程同步 配置rsync验证数据库 rsync实时数据同步

rsync远程同步

一、安装rsync

删除系统自带源

[root@centos01 ~]# rm -rf /etc/yum.repos.d/CentOS-*

挂载

[root@centos01 ~]# mount /dev/cdrom /mnt/

安装

[root@centos01 ~]# yum -y install rsync

备份主配置文件

[root@centos01 ~]# cp /etc/rsyncd.conf  /etc/rsyncd.conf.bak

编辑主配置文件

[root@centos01 ~]# vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes
pid file = /var/run/rsyncd.pid
address = 192.168.100.10
host allow = 192.168.100.*
port = 873
[bdqn]
path = /bdqn
comment test
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
read only = yes
auth users = bob
secrets file = /etc/rsync.db

启动服务

[root@centos01 ~]# rsync –daemon

查看服务

[root@centos01 ~]# netstat -anput | grep rsync 端口为873

在这里插入图片描述
关闭服务

[root@centos01 ~]# killall rsync

查看服务

[root@centos01 ~]# netstat -anput | grep rsync

在这里插入图片描述
成功关闭

配置rsync验证数据库

[root@centos01 ~]# vim /etc/rsyncd.db

配置内容

bob:pwd@123

增加读写权限

[root@centos01 ~]# chmod 600 /etc/rsyncd.db

创建文件

[root@centos01 ~]# touch /bdqn/1.txt
[root@centos01 ~]# touch /bdqn/2.txt
[root@centos01 ~]# touch /bdqn/3.txt
[root@centos01 ~]# touch /bdqn/4.txt

同步

[root@centos01 ~]# rsync -avz /bdqn/ /benet/

在这里插入图片描述
创建5.txt

[root@centos01 ~]# touch /bdqn/5.txt

再次使用同步,可以看到重复的就不同步了
在这里插入图片描述
在/benet创建6.txt

[root@centos01 ~]# touch /benet/6.txt

普通同步

[root@centos01 ~]# rsync -avz /bdqn/ /benet/

在这里插入图片描述
增加—-delete

[root@centos01 ~]# rsync -avz --delete /bdqn/ /benet/

在这里插入图片描述
远程同步

[root@centos01 ~]# rsync -avz bob@192.168.100.10::bdqn /benet/

一定要有bdqn 和 benet这两个文件夹
在这里插入图片描述
创建7.txt 进行同步

[root@centos01 ~]# touch /bdqn/7.txt

在这里插入图片描述
创建8.txt进行第二种方法同步

[root@centos01 ~]# touch /bdqn/8.txt

在这里插入图片描述
创建9.txt进行无密码交互同步

[root@centos01 ~]# touch /bdqn/9.txt

创建一个密码文本

[root@centos01 ~]# echo "pwd@123" > /1.txt

给密码文本一个去权限

[root@centos01 ~]# chmod 600 /1.txt

同步

[root@centos01 ~]# rsync -avz --password-file=/1.txt bob@192.168.100.10::bdqn /benet/

在这里插入图片描述

rsync实时数据同步

一、安装inotify

卸载光盘

[root@centos01 ~]# umount /mnt/

切换光盘为Linux.iso
在这里插入图片描述
挂载光盘

[root@centos01 ~]# mount /dev/cdrom /mnt/

解压

[root@centos01 inotify-tools-3.14]# tar zxvf /mnt/inotify-tools-3.14.tar.gz -C /usr/src/

进入/usr/src/inotify-tools-3.14/

[root@centos01 inotify-tools-3.14]# cd /usr/src/inotify-tools-3.14/

配置

[root@centos01 inotify-tools-3.14]# ./configure

编译安装

[root@centos01 inotify-tools-3.14]# make && make install

动态监控bdqn目录变化

[root@centos01 ~]# inotifywait -mrq -e move,delete,create,modify,attrib /bdqn/

centos02创建目录/xsh/

[root@centos02 ~]# mkdir /xsh/

centos01中修改Linux系统内核

[root@centos01 bdqn]# vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16387
fs.inotify.max_user_instances = 2048
fs.inotify.max_user_watches = 1048576

设置免交互

[root@centos01 ~]# ssh-keygen -t rsa 
[root@centos01 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.100.20
输入:yes
输入密码:pwd@123

成功免交互

[root@centos01 ~]# ssh root@192.168.100.20

在这里插入图片描述
编写脚本

[root@centos01 ~]# vim rsync.sh
#!/bin/bash
INW="inotifywait -mrq -e move,delete,create,modify,attrib /bdqn/"
RSY="rsync -avz /bdqn root@192.168.100.20:/xsh"
$INW | while read DIRECTORY EVENT FILE;do
$RSY &> /dev/null
done

在这里插入图片描述
添加脚本执行权限

[root@centos01 ~]# chmod +x rsync.sh

运行脚本放后台

[root@centos01 ~]# sh rsync.sh &

测试
首先清空centos01中/bdqn下所有文件
在这里插入图片描述
清空centos02中/xsh下的所有文件
在这里插入图片描述
在centos01的bdqn中创建文件1.txt 内容为“月亮真圆”
在这里插入图片描述
查看centos02的/xsh目录中的/bdqn目录中有无1.txt
在这里插入图片描述

查看1.txt的内容
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值