rsync+inotify自动同步数据

软件包提取码:0oa0
rsync
什么rsync呢
rsync (remote sync) 远程同步 可以将数据同步到LAN/VAN中能够通讯的其他主机 可以用来代替rcp的命令 能够有守护进程 后台运行
scp 远程同步 scp只能去全量复制 rsync可以增量复制
sync :同步复制 数据库 主从 主写入 从写入 回应给客户端
async :异步复制 数据库 主从 主写入 不论从有没有复制成功 直接回应给客户端
线程: 同步:只能干一件是请求
异步:可以做多件事情
作用:
1.代替了 scp rcp
2.对网站进行备份 (镜像目录树 文件系统)
3.对数据进行备份
特点:
1.支持全量复制也支持增量
2.支持匿名复制 也支持身份验证
3.镜像目录树
4.在传输过程中,可以把文件进行压缩 提高传输效率
5.保持原数据的属性 时间 软链接

在架构当中的应用
备份数据(数据库/web)

应用的案例
1.针对公司的数据比较混乱的状况,把这些数据进行备份
2.防止重大事故导致数据丢失
rsync命令格式:

  • rsync 【选项】 src root@ip:/dest A主机 上传数据
  • rsync 【选项】 root@ip:/src /dest B主机 下载数据

选项:

  • -r 递归复制
  • -l 同步链接文件
  • -o 同步时 不修改文件的属主
  • -g 同步时 不修改文件属组
  • -p 同步时 不修改文件的权限
  • -a 代表以上所有的选项
  • -v 显示详情
  • -z 同步时,在传输过程中对文件进行压缩
  • –delete 同步时,如果目标目录和源目录有不一致的文件 自动删除
  • -L 同步时,如果有链接文件 则同步链接文件的源文件
主机A192.168.1.1
主机B192.168.1.2

ssh免密登录

#主机A
[root@CentOS1 ~]# ssh-keygen
[root@CentOS1 ~]# ssh root@192.168.1.2
[root@CentOS1 ~]# ssh-copy-id root@192.168.1.2

创建目录

#主机A
[root@CentOS1 ~]# mkdir test1
#主机B
[root@CentOS2 ~]# mkdir test2

确保主机A和主机B都要有rsync

[root@CentOS1 ~]# rpm -qa | grep rsync
rsync-3.0.9-17.el7.x86_64
#主机B
[root@CentOS2 ~]# rpm -qa | grep rsync
rsync-3.0.9-17.el7.x86_64

上传数据

创建文件并且上传

#主机A
[root@CentOS1 ~]# cd test1/
[root@CentOS1 test1]# touch aa
[root@CentOS1 test1]# rsync /root/test1/* root@192.168.1.2:/root/test2/
#主机B
[root@CentOS2 ~]# ls test2/
aa

-r:递归

[root@CentOS1 test1]# mkdir -p a/b/c
[root@CentOS1 test1]# rsync -r /root/test1/* root@192.168.1.2:/root/test2/
[root@CentOS2 ~]# ls test2/
a  aa

-l:同步连接文件

#主机A
[root@CentOS1 test1]# ln -s aa ee
[root@CentOS1 test1]# rsync -lv /root/test1/* root@192.168.1.2:/root/test2/
skipping directory a
aa
ee -> aa

sent 83 bytes  received 34 bytes  78.00 bytes/sec
total size is 2  speedup is 0.02
#主机B
[root@CentOS2 ~]# ls test2
a  aa  ee

-o:不修改属主

#同步操作
[root@CentOS1 test1]# useradd test
#主机A
[root@CentOS1 test1]# chown test aa 
[root@CentOS1 test1]# ls -l aa
-rw-r--r--. 1 test root 0 6月  24 09:04 aa
[root@CentOS1 test1]# rsync -o /root/test1/* root@192.168.1.2:/root/test2/
#主机B
[root@CentOS2 ~]# ls -l test2/aa 
-rw-r--r--. 1 test root 0 6月  24 09:13 test2/aa

-g:不更改属组

#主机A
[root@CentOS1 test1]# chown :test aa 
[root@CentOS1 test1]# ls -l aa
-rw-r--r--. 1 test test 0 6月  24 09:04 aa
[root@CentOS1 test1]# rsync -g /root/test1/* root@192.168.1.2:/root/test2/
#主机B
[root@CentOS2 ~]# ls -l test2/aa 
-rw-r--r--. 1 root test 0 6月  24 09:17 test2/aa

-p:不修改权限

#主机A
[root@CentOS1 test1]# chmod 777 aa
[root@CentOS1 test1]# ls -l aa 
-rwxrwxrwx. 1 test test 0 6月  24 09:04 aa
[root@CentOS1 test1]# rsync -p /root/test1/* root@192.168.1.2:/root/test2/
#主机B
[root@CentOS2 ~]# ls -l test2/aa 
-rwxrwxrwx. 1 root root 0 6月  24 09:19 test2/aa

-a:代表以上所有

#主机A
[root@CentOS1 test1]# chmod 755 aa 
[root@CentOS1 test1]# chown test:test aa 
[root@CentOS1 test1]# ln -s aa qq
[root@CentOS1 test1]# mkdir -p 1/2/3
[root@CentOS1 test1]# rsync -a /root/test1/* root@192.168.1.2:/root/test2/
#主机B
[root@CentOS2 ~]# ls -l test2/
总用量 0
drwxr-xr-x. 3 root root 15 6月  24 09:21 1
drwxr-xr-x. 3 root root 15 6月  24 09:06 a
-rwxr-xr-x. 1 test test  0 6月  24 09:04 aa
lrwxrwxrwx. 1 root root  2 6月  24 09:09 ee -> aa
lrwxrwxrwx. 1 root root  2 6月  24 09:20 qq -> aa

-z 在传输过程中进行压缩 提高传输效率

[root@CentOS1 test1]# rsync -z /root/test1/* root@192.168.1.2:/root/test2/

-L:同步时如有链接文件会变为源文件

#主机A
[root@CentOS1 test1]# ln -s a e
[root@CentOS1 test1]# ls
1  a  e
[root@CentOS1 test1]# rsync -aL /root/test1/* root@192.168.1.2:/root/test2/
#主机B
[root@CentOS2 ~]# ls test2/
1  a  aa  e  ee  qq

–delete:删除不一样的目录或文件

#主机B
[root@CentOS2 ~]# touch test2/lkx
[root@CentOS2 ~]# ls test2/
1  a  aa  e  ee  lkx  qq
#主机A
[root@CentOS1 test1]# ls
1  a  e
[root@CentOS1 test1]# rsync -a --delete /root/test1/ root@192.168.1.2:/root/test2/	#注意test1后没有*
#主机B
[root@CentOS2 ~]# ls test2/
1  a  e

inotify:监控目录 文件系统 删除 创建 修改(内容、属性)等操作
安装inotify

[root@CentOS1 ~]# tar zxf inotify-tools-3.14.tar.gz 
[root@CentOS1 ~]# cd inotify-tools-3.14/
[root@CentOS1 inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify && make && make install
[root@CentOS1 inotify-tools-3.14]# ln -s /usr/local/inotify/bin/* /usr/local/bin/

inotify的使用
选项:

  • -m 一直处于监控中
  • -r 递归监控
  • -q 将监控的目录和监控的信息显示在终端上
  • -format 显示内容的格式
  •  %w 产生监控的路径
     %f 监控为目录 输出文件名
     %e 事件名
     %T 输出当前时间的时间
    
  • –timefmt 时间的格式
  •  %Y 年
     %m 月
     %d 日
     %H 小时
     %M 分钟
    
  • e 指定监控的事件
  •  open 打开文件
     move 移动
     create 创建
     modify 修改内容
     close_write 修改文件内容
     close_mowrite 查看只读文件
    

实现rsync+inotify自动同步

[root@CentOS1 ~]# vim /etc/rsyncd.conf
#修改为以下内容
prot = 873	#端口
address = 192.168.1.1	#IP
uid = root	#用户
gid = root	#组
use chroot = no	#不使用root切换模式
max connections = 0	#没有连接数
pid file = /var/run/rsyncd.pid	#pid文件
exclude = lost+found/	#同步数据是,排除lost+found这个目录
transfer logging = yes	#打开日志
log file = /var/lib/rsync.log	#日志文件存放位置
timeout = 900	#超时时间
ignore nonreadable = yes	#忽略没有权限的用户
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2	#遇到这类文件不进行压缩

[test]	#同步模块名
        path = /test1	#监控的目录
        comment = test1	#备注
        read only = no	#不是只读
[root@CentOS1 ~]# systemctl restart rsyncd
[root@CentOS1 ~]# netstat -anput | grep 873
tcp        0      0 192.168.1.1:873         0.0.0.0:*               LISTEN      58446/rsync         

编写脚本文件

#!/bin/bash
/usr/local/bin/inotifywait -mrq --format %w%f -e create,delete,close_write /test1 | while read file
do
if [ -f $file ];
then
        rsync -a --delete $file root@192.168.1.2:/test2
else
        rsync -a --delete /test1/ root@192.168.1.2:/test2
fi
done
[root@CentOS1 ~]# chmod +x rsync.sh 
[root@CentOS1 ~]# ./rsync.sh &

验证

#主机A
[root@CentOS1 ~]# cd /test1/
[root@CentOS1 test1]# touch haha
#主机B
[root@CentOS2 ~]# cd /test2/
[root@CentOS2 test2]# ls
haha
#主机A
[root@CentOS1 test1]# rm haha
rm:是否删除普通空文件 "haha"?y
#主机B
[root@CentOS2 test2]# ls
#主机A
[root@CentOS1 test1]# touch qqq
#主机B
[root@CentOS2 test2]# cat qqq
qqq
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值