服务-rsync

sync:同步
async:异步
rsync:远程同步
数据同步方式:
1、rsync
2、共享存储
3、云存储

123、软件三步曲
# rpm -ql rsync
/etc/xinetd.d/rsync  子配置文件
/usr/bin/rsync  二进制命令
4、了解配置文件
5、需求:rsync不作为一个服务使用,只是用rsync命令来同步数据

rsync — a fast, versatile, remote (and local) file-copying tool
-a:归档拷贝   等于 -rlptgoD (no -H,-A,-X)
-r:递归拷贝目录
-l:保留软连接
-p:保留权限信息
-t:保留修改时间
-g:保留属组
-o:保留属主
-D:表示支持b,c,s,p类型文件
-R:保留相对路径
-H:保留硬链接
-A:保留acl策略
-e:指定执行shell命令
-E:保留可执行权限
-X:保留扩展属性 a属性


demo1:本地同步
rsync -av /dir1 /dir2/   ——>   /dir1    同步过去后将保留源目录的目录
rsync -av /dir1/ /dir2/  ——> /dir1/ 同步过去后不保留源目录的目录

rsync -Rav /dir1/ /dir2/  ——> -R 不管有没有“/”都会保留源目录

demo2:远程同步
vm1:/dir1
vm2:/dir2
从本地将文件同步到远程
rsync -av /dir1/ 10.1.1.2:/dir2/
rsync -Rav /dir1 10.1.1.2:/dir2/
从远程将文件同步到本地
rsync -av 10.1.1.1:/dir1 /dir2

# rsync -av /dir1/ stu1@10.1.1.2:/dir2/

# rsync -av /dir1/ -e 'ssh -p10022 -lstu1' 10.1.1.2:/dir2


demo3:只允许10.1.1.2主机在指定的时间内同步数据
思路:
1、rsync作为服务去运行
2、通过配置文件可以完成访问控制

步骤:
1、创建配置文件 /etc/rsyncd.conf
vim /etc/rsyncd.conf
以下是全局选项:
       uid = xx
       gid = xxx
       use chroot = yes
       max connections = 4
       syslog facility = local5
       pid file = /var/run/rsyncd.pid
       motd file = file 指定一个消息文件
[share] 局部标签,可以定义需要共享的文件路径
path = /dir1  真正共享的路径
comment = xxx 描述

2、重启xinetd服务
3、测试验证
vm2:
rsync -av 10.1.1.1::  发现远程主机所共享的共享名
rsync -av 10.1.1.1::share /dir2  同步


允许10.1.1.2主机访问:
# cat rsync 
...
    only_from   = 10.1.1.2
    access_times    = 10:00-18:00

...

demo4:密码同步

# cat /etc/rsyncd.conf 
motd file = /etc/rsync.mes
[share]
path = /dir1
secrets file = /etc/rsync.sec
auth  users = u1,u2

# chmod 600 /etc/rsync.sec


课堂练习:
在上面的基础上增加一些功能:
1、把日志记录到/var/log/rsyncd.log文件 
2、隐藏共享模块
3、同时只能1个客户端连接进行同步该模块
4、只允许10.1.1.2和你的物理机同步该模块
5、只能早上9点到下午6点同步

man 5 rsyncd.conf

思考:如果源端有文件删除,那么目标端怎么办?

参考:
1、关于日志记录有以下4种方法
/etc/xinetd.d/rsync
log_type = file /var/log/rsyncd.log
或者
log_type = syslog local0
vim /etc/rsyslog.conf
local0.*    /var/log/rsyncd.log

/etc/rsyncd.conf
log file = /var/log/rsyncd.log
或者

syslog facility = local0
vim /etc/rsyslog.conf
local0.*    /var/log/rsyncd.log

2、list = false
3、max connections = 1
4、hosts allow = 10.1.1.2 10.1.1.254
5、vim /etc/xinetd.d/rsync


rsync+inotify架构实现实时同步:

vm1:10.1.1.1
share:/dir1
步骤:
1、vim /etc/rsyncd.conf
[share]
path = /dir1

2、vim /etc/xinetd.d/rsync
...
disable = no

3、service xinetd restart

4、安装第三方软件
tar -xf inotify-tools-3.13.tar.gz -C /usr/src/
cd /usr/src/
cd inotify-tools-3.13/
./configure 
make
make install

5、了解该软件带来哪些命令
inotifywait --help
-m:
-q:
-r:
-e:
....

6、根据需求编写监控目录的脚本
需求:当vm1的/dir1目录删除、创建文件、修改文件等操作时需要实时同步到vm2的/dir2目录里
vim /tmp/inotify.sh
#!/bin/bash
/usr/local/bin/inotifywait -mrq -e create,delete,move,modify,attrib /dir1 |while read events
do

rsync -av --delete /dir1 10.1.1.2::dir 
echo "date +'%F %T' 发生了$events" >> /tmp/inotify.log

done



vm2:10.1.1.2
/dir2
1、vim /etc/rsyncd.conf
[dir]
path = /dir2
2、vim /etc/xinetd.d/rsync
diable = no
3、service xinetd restart


测试验证:


课后了解sersync+rsync架构

如果需要双向实时同步怎么办?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值