rsync文件实时同步
一、rsync简介
rsync
rsync(remote synchronize)是linux下一款开源的数据镜像备份工具。可以使本地的不同分区、不同目录之间快速实现数据同步,也可实现远程主机之间的数据快速同步,达到数据备份的效果。同步的文件可以保持原来文件的权限、时间、软硬链接等附加信息。
rsync特性
①镜像保存整个目录树和文件系统
②保持原来文件的权限、时间、软硬链接等等
③无需特殊权限即可安装
④快速:第一次同步时rsync会复制全部内容,但在下一次只传输修改过的文件。rsync在传输的过程可以实时的压缩和解压缩操作,因此可以使用更少的带宽。
⑤安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的scket连接。
⑥支持匿名传输,以方便进行网站镜像
二、rsync+inotify实现远程同步
1、环境准备
背景:通过rsync+inotify 同步/test71/目录到/test72/ 达到快速备份的效果
①两台主机
主机:192.168.105.71
192.168.105.72
②创建同步的目录
192.168.105.71上:
[root@centos71 ~]# mkdir /test71
192.168.105.72上:
[root@centos72 ~]# mkdir /test72
2、主机免密登录
①配置免密登录
192.168.105.71上:
[root@centos71 ~]# ssh-keygen
[root@centos71 ~]# ssh-copy-id root@192.168.105.72
②验证免密登录
[root@centos71 ~]# ssh root@192.168.105.72
3、rsync服务安装介绍
rsync常用的几个参数(详情请man rsync):
-a, –archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD。
-v, –verbose 详细模式输出。
-z, –compress 对备份的文件在传输时进行压缩处理。
– --delete 删除目标文件夹内多余的文档。
-q, –quiet 精简输出模式。
-r, –recursive 对子目录以递归模式处理。
-R, –relative 使用相对路径信息。
-h, –help 显示帮助信息。
例如:
rsync --delete -avz /test71/ root@192.168.105.72:/test72/
①安装rsync服务
[root@centos71 ~]# yum -y install rsync
②启动服务
[root@centos71 ~]# systemctl restart rsyncd
[root@centos71 ~]# systemctl enable rsyncd
③同步测试
71主机上面:
[root@centos71 ~]# cd /test71/
[root@centos71 test71]# touch haha.txt
[root@centos71 test71]# rsync -avPz --delete --progress /test71/ 192.168.105.72:/test72/
观察72主机目录变化:
[root@centos72 ~]# cd /test72/ ;ls -rlht
总用量 0
-rw-r--r--. 1 root root 0 11月 21 13:45 haha.txt
4、inotify监控程序安装介绍
inotify
Inotify可用于检测单个文件,也可以检测整个目录。当检测的对象是一个目录的时候,目录本身和目录里的内容都会成为检测的对象
–用法参数详解
-e:事件
-d:后台运行
-m:始终保持事件监听状态
-q:打印很少的信息,仅仅打印监控事件的信息 安静状态
-r :递归查询目录
-timefmt:指定时间输出的格式
-excluder:排除文件或者目录的时候不区分大小写
①inotify监控安装
下载inotify-tools-3.13.tar.gz包并上传到192.168.105.71上:
[root@centos71 ~]# yum -y install gcc make
解包
[root@centos71 ~]# tar -xf /inotify-tools-3.13.tar.gz -C /
编译安装
[root@centos71 ~]# cd /inotify-tools-3.13/
[root@centos71 inotify-tools-3.13]# ./configure
[root@centos71 inotify-tools-3.13]# make && make install
②监控测试
输入测试进程后,另起一个终端,cd /test71/ ;touch haha1.txt,效果如下:
[root@centos71 test71]# inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib /test71/
21/11/19 13:52 /test71/haha1.txt
21/11/19 13:52 /test71/haha1.txt
5、编写实时同步脚本并测试
①编写实时同步脚本
[root@centos71 ~]# vi rsync.sh
#!/bin/bash
host=192.168.105.72
src1=/test71/
src2=/test72/
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib $src1 | while read file
do
rsync -avPz --delete --progress $src1 $host:$src2
echo "${file} was rsynced" >>/root/rsync.log
done
② 测试脚本
使用nohup执行脚本并放入后台,防止终端断开脚本中段
[root@centos71 ~]# nohup sh ~/rsync.sh &
在71机子上创建文件
[root@centos71 ~]# cd /test71/ ;touch haha{1..10}.txt
在72机子上查看效果如下
[root@centos72 ~]# ls -rlht /test72/
总用量 0
-rw-r--r--. 1 root root 0 11月 21 13:45 haha.txt
-rw-r--r--. 1 root root 0 11月 23 01:49 haha1.txt
-rw-r--r--. 1 root root 0 11月 23 01:49 haha9.txt
-rw-r--r--. 1 root root 0 11月 23 01:49 haha8.txt
-rw-r--r--. 1 root root 0 11月 23 01:49 haha7.txt
-rw-r--r--. 1 root root 0 11月 23 01:49 haha6.txt
-rw-r--r--. 1 root root 0 11月 23 01:49 haha5.txt
-rw-r--r--. 1 root root 0 11月 23 01:49 haha4.txt
-rw-r--r--. 1 root root 0 11月 23 01:49 haha3.txt
-rw-r--r--. 1 root root 0 11月 23 01:49 haha2.txt
-rw-r--r--. 1 root root 0 11月 23 01:49 haha10.txt