rsync远程同步和inotify结合配置完成自动同步

前言

一:理论

1.1:rsync简介

●rsync,全称为:Remote Sync(远程同步),是一款开源的快速增量备份工具,可以在不同主机之间镜像同步整个目录树

●还支持本地复制,增量备份、保持连接和权限,或者与其他SSH,rsync主机同步

●采用优化的同步算法,传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用

在这里插入图片描述

1.2:rsync源服务器的关系

在这里插入图片描述

二:实验

2.1:rsync命令

1、启动rsync服务:

rsync --daemon

2、关闭rsync服务:

kill $(cat /var/run/rsyncd.pid)

3、同步本地文件系统数据:

rsync [选项] 原始位置 目标位置
常用选项:
-a:归档模式,递归并保留对象属性,等同于 -rlptgoD
-r 对子目录以递归模式处理,主要是针对目录来说的,如果单独传一个文件不需要加-r,但是传输的是目录必须加-r选项
-l 保留软链接
-p 保持文件权限
-v:显示同步过程的详细(verbose)信息
-z:在传输文件时进行压缩(compress)
-H:保留硬连接文件
-A:保留ACL属性信息
--delete:删除"目标""源"没有的文件
--checksum:根据对象的校验和来决定是否跳过文件
--progress 在同步的过程中可以看到同步的过程状态,比如统计要同步的文件数量、同步的文件传输速度等等
路径的格式可以是本地路径,也可以是使用user@host:path或user@host::path的远程路径,如果主机和path路径之间使用单个冒号隔开,表示使用的是远程shell通信方式,而使用双冒号隔开的则表示的是连接rsync daemon

4、下行同步的两种方式(使用客户端将rsync服务器下的wwwroot共享模块下的内容同步到本地的/opt目录下(共享模块下的真实共享路径需要对other用户具有 ‘r’ 权限))

(1):命令格式用户名@主机地址::共享模块名

例如:[root@rsyncClient ~]# rsync -avz lisi@192.168.233.131::wwwroot /opt

(2):命令格式:rsync://用户名@主机地址/共享模块名

例如:[root@slave opt]# rsync -avz rsync://lisi@192.168.233.131/wwwroot /root

5、rsync通过ssh的方式同步

命令与平常的scp命令类似

例如:将本地/opt/abc.txt上传到目标服务器/opt目录:rsync -avz /opt/abc.txt lisi@192.168.233.131:/opt
例如:将目标服务器/opt/qwe.txt文件下载到本地/opt目录下:rsync -avz lisi@192.168.233.131:/opt/qwe.txt /opt

2.2:实验环境

master:192.168.100.100

slave:192.168.100.8
在这里插入图片描述

实验过程

2.3 配置rsync源服务器

[root@master yum.repos.d]# vi /etc/rsyncd.conf 
 uid = nobody	
 gid = nobody	
 use chroot = yes                           '禁锢在源目录'
 address = 192.168.100.100             '监听地址'
 port 873                                   '监听端口号'
 log file = /var/log/rsyncd.log            '日志文件位置'
 pid file = /var/run/rsyncd.pid           '存放进程ID的文件位置'
 hosts allow = 192.168.100.0/24            '允许访问的客户机地址网段'
[wwwroot]                                       '共享模块名称'
 path = /var/www/html                       '源目录的实际路径'
 comment = Document Root of www.51xit.top
 read only =yes                                 '是否只读'
 dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z          '同步时不在压缩的文件类型'
 auth users = backuper                          '授权账户'
 secrets file = /etc/rsyncd_users.db                '存放账户信息的数据文件'

创建于用户密码文件

[root@master yum.repos.d]# vi /etc/rsyncd_users.db	'创建存放账户信息的数据文件'
backuper:abc123	'采用“用户名:密码”的记录格式,每行一个用户记录独立的账号数据,不依赖于系统账号'

[root@master yum.repos.d]# chmod 600 /etc/rsyncd_users.db '给数据文件设置权限'
[root@master yum.repos.d]# ll -ld /var/www/html/	'查看下源目录的文件权限'
drwxr-xr-x. 2 root root 6 94 2017 /var/www/html/

启动rsync服务

[root@master yum.repos.d]# rsync --daemon	'//启动rsync服务,如果要停止这个服务请使用kill $(cat /var/run/rsyncd.pid)'
[root@master yum.repos.d]# netstat -ntap |grep rsync
tcp        0      0 192.168.100.100:873     0.0.0.0:*               LISTEN      4371/rsync    

在 /var/www/html目录中,新建文件

[root@localhost run]# cd /var/www/html        '创建两个html文件'
[root@localhost html]# ls
index.html  web.html

2.4 客户端配置,本地同步与下行同步

下行同步的两种方式,密码是abc123

[root@promote html]# rsync -avz backuper@192.168.100.100::wwwroot /opt/
Password: 
receiving incremental file list

sent 20 bytes  received 89 bytes  31.14 bytes/sec
total size is 43  speedup is 0.39
[root@promote html]# ls /opt
index.html      rh       web.html


[root@promote opt]# rsync -avz rsync://backuper@192.168.100.100/wwwroot /opt/
Password: 
receiving incremental file list
./
index.html
web.html

sent 65 bytes  received 215 bytes  112.00 bytes/sec
total size is 43  speedup is 0.15
[root@promote opt]# ls
index.html      rh       web.html

2.5 rsync源的免密交互处理

rsync源的免密交互处理

[root@promote opt]# vim /etc/server.pass           '创建密码文件'
abc123
[root@promote opt]# chmod 600 /etc/server.pass     '给密码文件权限'
[root@promote opt]# rsync -az --delete --password-file=/etc/server.pass lisi@192.168.100.100::wwwroot /opt/myweb	'指定刚刚创建的密码文件,发现已经不需要输入密码了'
[root@promote opt]# ls                       '原文件的rh没了,本命令会将原文件中的文件全部删除,所以慎用'
index.html      web.html

三、rsync + inotify 监控工具 实现自动同步

3.1 rsync实时同步

定期同步的不足

●执行备份的时间固定,延迟明显、实时性差

●当同步源长期不变化时,密集的定期任务是不必要的

实时同步的优点

●一旦同步源出现变化,立即启动备份

●只要同步源无变化,则不执行备份

3.2 inotify介绍

●Linux内核从2.6.13开始,引入了inotify机制。

●它是一种文件系统的变化通知机制

●可以监控文件,也可以监控目录。

●当监控目录时,它可以同时监控目录及目录中的各子目录及文件的。

●可以协助rsync,监控到数据的改变,触发 rsync 进行数据的同步。

3.3. 配置步骤

节点端设置 inotify-tools

[root@localhost opt]# tar zxvf inotify-tools-3.14.tar.gz
[root@localhost opt]# cd inotify-tools-3.14/
[root@localhost inotify-tools-3.14]# ./configure
[root@localhost inotify-tools-3.14]# make && make install
[root@localhost inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/

设置内核文件(当要监控的目录、文件数量较多或者变化较频繁时,建议加大这三个参数的值。可以直接修改/etc/sysctl.conf的配置文件,将管理队列、实例数、监控数进行设置。)

[root@localhost opt]# vim /etc/sysctl.conf
fs.inotify.max_queued_events = 32768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@localhost opt]# sysctl -p

打开一个新的端口,在同步的文件夹中创建新的文件

[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
test.html
[root@localhost html]# touch a.txt

创建脚本文件,实现自动化监控同步

[root@localhost ~]# vim /opt/inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e create,delete,move,modify,attrib /var/www/html"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ backuper@192.168.100.100::wwwroot"

$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
    if [ $(pgrep rsync | wc -l) -le 0 ] ; then
        $RSYNC_CMD
    fi
done

此时脚本执行中,在同步数据

[root@localhost opt]# ./inotify.sh 
rsync: failed to set times on "/." (in wwwroot): Operation not permitted (1)
rsync: chgrp "/.aaa.txt.oCfaQJ" (in wwwroot) failed: Operation not permitted (1)
rsync: chgrp "/.bbb.txt.P6b5Ri" (in wwwroot) failed: Operation not permitted (1)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]
rsync: failed to set times on "/." (in wwwroot): Operation not permitted (1)
rsync: chgrp "/bbb.txt" (in wwwroot) failed: Operation not permitted (1)
rsync: chgrp "/.aaa.txt.KiW24X" (in wwwroot) failed: Operation not permitted (1)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]

此时源端自动同步成功

[root@promote html]# ls
a.txt  test.html
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值