rsync远程同步并结合inotify实时同步

一、关于Frsync

centos7自带rsync工具无需安装。
■一款快速增量备份工具
●Remote Sync,远程同步
●支持本地复制,或者与其他SSH、rsync主机同步
●官方网站: http://rsync.samba.org

二、rsync命令用法

rsync  [选项] 原始位置 目标位置

常用选项
-r:  递归模式,包含目录及子目录中的所有文件。
-1:  对于符号链接文件仍然复制为符号链接文件。
-v:  显示同步过程的详细(verbose) 信息。
-a:  归档模式,保留文件的权限、属性等信息,等同于组合选项“-rlptgoD”
-Z:  在传输文件时进行压缩(compress)-p:  保留文件的权限标记。
-t:  保留文件的时间标记。
-g:  保留文件的属组标记(仅超级用户使用)
-O:  保留文件的属主标记(仅超级用户使用)-H:  保留硬连接文件。
-A:  保留ACL属性信息。
-D:  保留设备文件及其他特殊文件。

三、 远程同步案例

3.1 环境

两台服务器,服务器A作为同步源,开启rsync服务,服务器B作为客户机,备份服务器A的共享目录,即同步源的下行同步。
在这里插入图片描述

3.2 同步源的配置

检查rsync是否安装

[root@rsync ~]# rpm -q rsync
rsync-3.0.9-18.el7.x86_64

配置同步源,修改配置文件

[root@rsync ~]# vi /etc/rsyncd.conf
uid = nobody   #匿名用户
gid = nobody
use chroot = yes                   #禁锢在宿主目录中
address = 192.168.257.160             #监听服务器自己的地址
port 873                          #端口号
pid file = /var/run/rsyncd.pid       #进程文件位置
log file = /var/log/rsyncd.log         #日志文件位置
hosts allow = 192.168.247.0/24               #允许进行同步的主机网段
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2     #不压缩这几种格式的文件

[wwwroot]                            #共享目录的名称,可根据需要命名
path = /var/www/html                   #本地同步给客户端的目录
comment = web dir
read only = yes                        #只读,只允许客户端进行读操作
auth users = backuper                #客户端登录rsync的用户名,可根据需要命名
secrets file = /etc/rsyncd_users.db      #用户密码存放位置

新建密码文件,存放设置用户名和密码

[root@rsync ~]# vi /etc/rsyncd_users.db   #配置文件中指定的密码路径
backuper:123456   #格式必须为 用户名:密码

为了安全,给密码文件限制权限

[root@rsync ~]# chmod 600 /etc/rsyncd_users.db 

启动rsync服务

[root@rsync ~]# rsync --daemon
[root@rsync ~]# netstat -antp |grep rsync
tcp        0      0 192.168.247.160:873           0.0.0.0:*               LISTEN      4751/rsync  

安装httpd服务,生成/var/www/html 目录,并添加文件,用于测试同步(也可以自己创建目录,文件)

[root@rsync ~]# yum -y install httpd
[root@rsync ~]# echo "web" > /var/www/html/index.html
[root@rsync ~]# systemctl start httpd
[root@rsync ~]# curl http://localhost
web

3.3 客户机同步测试

客户机需要有同步目录(用来存放备份rsync服务器的共享文件),这里也利用安装httpd,自动生成/var/www/html。

执行同步的命令格式一:

#rsync 选项 用户名@同步源IP::共享目录名 客户机自己的同步目录
[root@rsync ~]# rsync -avz backuper@192.168.247.160::wwwroot /var/www/html/
Password:  #输入在同步源设置的密码
receiving incremental file list    #可以看到文件的传输过程
./
index.html

sent 81 bytes  received 160 bytes  96.40 bytes/sec
total size is 4  speedup is 0.02
[root@rsync ~]# cat /var/www/html/index.html    #同步成功
web

格式二:

#rsync 选项 rsync://用户名@同步源IP::共享目录名 客户机自己的同步目录
[root@client ~]# rsync -avz rsync://backuper@192.168.247.160/wwwroot /var/www/html/

免密同步方式:
建立密码文件,并修改权限

[root@client ~]# vi /etc/rsync.pass
123456   
[root@client ~]# chmod 600 /etc/rsync.pass 

免密同步格式:

#delete 指的是源端没有的数据,而客户机端有的数据,需要把客户机端的数据删除
rsync -avz --delete --password-file=/etc/rsync.pass backuper@192.168.247.160::wwwroot /var/www/html/

四、实时同步

4.1 为什么有要实时同步

■定期同步的不足
●执行备份的时间固定,延迟明显、实时性差
●当同步源长期不变化时,密集的定期任务是不必要的
实时同步的优点
●一旦同步源出现变化,立即启动备份
●只要同步源无变化,则不执行备份

4.1 关于inotify

■Linux内核的inotify机制
●从版本2.6.13开始提供
●可以监控文件系统的变动情况,并做出通知响应
辅助软件: inotify-tools

4.3 实时同步案例

在这里插入图片描述

源端
因为要对同步源进行写入的操作,配置文件修改只读模式为no,并且共享目录权限要改为777

read only = no
[root@maneger ~]# chmod -R 777 /var/www/html/

重启rsync服务

[root@maneger ~]# pkill -9 rsync
[root@maneger ~]# netstat -anpt | grep rsync
[root@maneger ~]# rsync --daemon
[root@maneger ~]# failed to create pid file /var/run/rsyncd.pid: File exists
^C
[root@maneger ~]# rm -rf /var/run/rsyncd.pid   #必须要删除原来的pid文件,才能再启动
[root@maneger ~]# rsync --daemon
[root@maneger ~]# netstat -anpt | grep rsync
tcp        0      0 192.168.247.160:873     0.0.0.0:*               LISTEN      4227/rsync          

客户端
更改inotify的内核参数

vim /etc/sysctl. conf
fs. inotify. max_ queued_ events = 16384 //监控事件的最大队列数
fs. inotify. max_ _user_ instances = 1024 //监控的最大实例数
fs. inotify. max_ _user_ watches = 1048576 //监控的每实例的最大文件数
sysctl -p

安装inotify

[root@client html]# tar zxf /root/inotify-tools-3.14.tar.gz -C /root
[root@client html]# cd 
[root@client ~]# cd inotify-tools-3.14/
[root@client inotify-tools-3.14]# ./configure 
[root@client inotify-tools-3.14]# make && make install

inotifywait:用于持续监控,实时输出结果
inotifywatch:用于短期监控,任务完成后再出结果

测试实时监控命令

# -e 监控后面的几个对目录/var/www/html的操作
[root@client inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html 
-m,  持续进行监控
-r,  递归监控所有子对象
-q,  简化输出信息
-e,  指定要监控哪些事件类型
#会持续监控,另起一个连接进行添加删除操作
[root@client html]# touch aa
[root@client html]# rm -fr 1
[root@client ~]# inotifywait -mrq -e modify,create,move,delete /var/www/html
/var/www/html/ CREATE aa
/var/www/html/ DELETE 1   #监控过程记录
...

修改同步目录权限,允许源端的同步,读写

[root@client ~]# chmod -R 777 /var/www/html/

编写脚本,进行实时同步文件给源端

[root@client ~]# vi inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,move,delete /var/www/html/
RSYNC_CMD="rsync -avzH --password-file=/etc/rsync.pass /var/www/html/ backup@192.168.247.160::wwwroot/"
#向源端同步自己的/var/www/html目录里的文件,到共享目录wwwroot里
$INOTIFY_CMD | while read DIRECTORY EVENT FILE   #当检测到目录有变化,则进行同步
do
$RSYNC_CMD
done
[root@client ~]# chmod +x inotify.sh 
[root@client ~]# ./inotify.sh   #开始监控,停留此状态

另起一个连接验证,对目录进行操作

[root@client html]# touch 1 
[root@client html]# touch 2
[root@client html]# touch 3
[root@client html]# touch test

在源端查看是否同步成功

[root@maneger html]# ll
总用量 4
-rw-r--r--. 1 nobody nobody 0 1112 11:58 1
-rw-r--r--. 1 nobody nobody 0 1112 11:58 2
-rw-r--r--. 1 nobody nobody 0 1112 12:11 3
-rw-r--r--. 1 nobody nobody 0 1112 12:19 test
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值