实验操作:rsync远程同步与rsync+inotify实时同步实操-------------------------含泪学

前言

不同服务器需要用到同一个文件,如何处理?
当文件信息在不断更新,且文件不断增大,又如何处理?

如今的微信公众号,小程序等都有开通图片上传的功能,当微信公众号和程序后台部署在不同服务器时,我们需要将微信服务器的图片同步到程序后台服务器上,此时就可以使用Rsync工具。

Rsync(remote synchronize)是一个远程数据同步工具,可快速同步多台主机间的文件。一般应用于不同服务器之间文件的同步。

Rsync使用所谓的“Rsync算法”来使本地和远
程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,相对其他文件同步方法来说比较快。

在这里插入图片描述

一:rsync概述

1.1:是一款快速增量备份的工具

Remote Sync,远程同步

支持本地复制,或者与其他SSH,rsync主机同步

官方网站:http://rsync.samba.org

1.2:配置rsync 同步源服务器

1.21:rsync同步源

  • 指备份操作的远程服务器,也被称为备份源
    在这里插入图片描述

1.3:配置rsync源基本思路

备注:在提供服务的同步源服务器上操作

  • 建立rsyncd.conf配置文件
  • 建立独立的账号文件
  • 启用rsync——daemon模式

1.31: 应用示例

  • 用户gsybk,允许下行同步
  • 操作的目录为/var/www/html

二:配置rsync配置文件

在CentOS 7系统之前/etc/rsyncd.conf文件默认不存在,CentOS 7开始已经有这样的文件,并且其中放置了模板参考信息(根据实际情况编写需要的内容,注意格式)。

  • 服务端IP:20.0.0.41
  • 客户端ip:20.0.0.42
[root@localhost ~]# vim /etc/rsyncd.conf 

uid = nobody                      '//启用匿名用户'
gid = nobody
port 873                          '//监听端口'
use chroot = yes                  '//禁固在目录'
log file = /var/log/rsyncd.log    '//日志文件存放位置'
pid file = /var/run/rsyncd.pid    '//存放进程ID的文件位置'
hosts allow = 20.0.0.0/24         '//允许访问的客户机地址'

'//共享模块'
[wwwroot]                         '//共享模块名称'
path = /var/www/html              '//源目录的实际路径'
comment = www.shuai.com           '//描述(可以省略)'
read only = yes                   '//开启只读'
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
auth users = backuper             '//同步时不在进行也锁的文件类型'
secrets file = /etc/rsyncd_users.db '//存放账户信息的数据文件'

基于安全性考虑,对于rsync的同步源最好仅允许以只读方式做同步。另外,同步可以采用匿名的方式,只要将其中的“auth users”和“secrets file”配置项去除即可!

2.1:为备份账号创建数据文件

  • 根据rsync的配置文件内容,创建账号数据文件。每行一个用户,用户和密码之间用冒号进行分割
[root@localhost ~]# vim /etc/rsyncd_users.db

//编写   采用用户名:密码格式
backuper:123123

  • 由于账号信息采用明文存放,因此需要调整那文件权限,避免账号信息泄露
[root@localhost ~]# chmod 600 /etc/rsyncd_users.db 

  • 启动rsync服务
[root@localhost ~]# rsync --daemon

//查看状态
[root@localhost ~]# netstat -ntap | grep rsync
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      87776/rsync         
tcp6       0      0 :::873                  :::*                    LISTEN      87776/rsync         

  • 测试、安装httpd服务
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0

//安装httpd
[root@localhost html]# yum -y install httpd

//设置主页信息
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# vim index.html 
//编写
<h1>Hello<h1>

//重启服务
[root@localhost html]# systemctl restart httpd

  • 如果需要重启rsync服务,需要:
[root@localhost ~]# kill  $(cat /var/run/rsyncd.pid)
//停止服务
[root@localhost ~]# rsync --daemon
//启动服务
[root@localhost ~]# kill -9 $(cat /var/run/rsyncd.pid)

//
[root@localhost ~]# rsync --daemon
failed to create pid file /var/run/rsyncd.pid: File exists

或者直接使用“netstat -anpt | grep rsync”命令查出进程号,使用“kill 进程号”一样。
使用第一种方法停止rsync服务必须删除存放rsync服务进程的文件:

[root@localhost ~]# rm -rf /var/run/rsyncd.pid:

2.2:使用rsync备份工具

  • 配置好rsync同步源服务器之后,客户端就可以使用rsync工具来执行远程同步了。
rsync命令的选项:
-r:递归模式,包含目录及子目录中所有文件
-l:对于符号链接文件仍然复制为符号链接文件
-p:保留文件的权限标记
-t:保留文件的时间标记
-g:保留文件的属组标记(仅超级用户使用)
-o:保留文件的属主标记(仅超级用户使用)
-D:保留设备文件及其他特殊文件
-a:归档模式,递归并保留对象属性,等同于 -rlptgoD
-v:显示同步过程的详细(verbose)信息
-z:在传输文件时进行压缩(compress)
-H:保留硬连接文件
-A:保留ACL属性信息
--delete:删除目标位置有而原始位置没有的文件
--checksum:根据对象的校验和来决定是否跳过文件

2.3:配置源的两种表示方法

  • 1.格式:用户名@主机地址::共享模块名

  • 新开一台服务器进行rsync远程操作

[root@localhost ~]# hostnamectl set-hostname shuai
[root@localhost ~]# su
[root@shuai ~]# rpm -q rsync
rsync-3.1.2-4.el7.x86_64

//方法一
[root@shuai ~]# rsync -zva backuper@20.0.0.41::wwwroot /opt
Password: 

//查看opt查看已经同步成功
[root@shuai ~]# cd /opt
[root@shuai opt]# ls
index.html  rh
[root@shuai opt]# cat index.html 
<h1>hello</h1>


[root@localhost ~]# ll /var/www/html/
总用量 4
-rw-r--r--. 1 root root 15 9月  10 17:10 index.html

  • 2.格式:rsync://用户名@主机地址、共享模块名
方法二:
//我们先本地的opt下面的文件删去
[root@shuai opt]# rm -rf *.html
[root@shuai opt]# rsync -zva rsync://backuper@20.0.0.41/wwwroot /opt
Password: 
receiving incremental file list
./
index.html

sent 46 bytes  received 124 bytes  68.00 bytes/sec
total size is 15  speedup is 0.09
[root@shuai opt]# cat /var/www/html/index.html 
<h1>Hello<h1>
[root@shuai ~]# ll /opt
总用量 4
-rw-r--r--. 1 root root 15 9月  10 17:10 index.html
drwxr-xr-x. 2 root root  6 1031 2018 rh

这两种命令效果是一样!
上传只需将目录调换顺序即可(确保对上传的目录具有写入权限)!
在同步源端输入以下命令,方可执行写入权限:

2.4:rsync的免交互处理

  • 设置好一个文件,里面输入对应的密码
  • 在输入命令时添加–password-file=密码文件路径
[root@shuai ~]# vim /etc/server.pass
123123

[root@shuai ~]# chmod 600 /etc/server.pass 
[root@shuai ~]# rsync -zva --delete --password-file=/etc/server.pass backuper@20.0.0.41::wwwroot /opt
receiving incremental file list
deleting rh/

sent 20 bytes  received 63 bytes  55.33 bytes/sec
total size is 15  speedup is 0.18
[root@shuai ~]# ls /opt
index.html
[root@shuai opt]# ll
总用量 4
-rw-r--r--. 1 root root 15 9月  10 17::10 index.html

2.5:设置周期性计划任务

  • 每天晚上十点半对服务器网站更新一次
[root@shuai opt]# mkdir /webbak
[root@shuai opt]#  crontab -e
30 22 * * * /usr/bin/rsync -avz --delete --password-file=/mima gsybk@192.168.247.160::wwwroot /webbak
[root@shuai opt]#systemctl restart crond
[root@shuai opt]#systemctl enable crond

三:rsync实时同步

概述

  • rsync是一个开源的快速备份工具,可以再不同主机之间镜像同步整个目录树,支持增量备份,保持链接和权限,且采用优化的同步算法,再传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用。:

3.1:定期同步的不足

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

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

3.2:实时同步的特特点

  • 一旦同步源出现变化,立即开启备份

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

3.4:Linux内核的inotify机制

从版本2.6.13开始提供

可以监控文件系统的变动情况,并做出通知相应

辅助软件:inotify-tools

原理

再远程同步任务中,负责发起rsync同步操作的客户机称为发起端,而负责响应来自客户机的rsync同步操作的服务器称为同步源。再同步过程中,同步源负责提供文档的原始位置,而发起端对该位置具有读取权限,如图所示:
在这里插入图片描述

3.5:调整inotify内核参数

max_queue_events:监控事件队列大小

max_user_instances:最多监控实例数

max_user_watches:每个事例最多监控文件数

实验环境

源服务器【shuai】:20.0.0.42

客户端20.0.0.41

把shuai服务器当作源服务器

'//安装http作为数据环境   第一台跟shuai服务器都安装了httpd 两个进行同步更新'
[root@shuai html]# yum -y install httpd     

'//删除原有的主页文件'
[root@localhost html]# rm -rf index.html 


[root@localhost html]# vim /etc/sysctl.conf
# For more information, see sysctl.conf(5) and sysctl.d(5).   //添加//
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

[root@localhost html]# sysctl -p    '//启动'
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

  • 2.安装inotifi-tools

使用inotify机制还需要安装inotifi-tools,以便提供inotifywait和inotifywatch辅助工具程序,用来监控和汇总改动情况。

'//解压inotifi-tools包'
[root@shuai html]# tar zxvf inotify-tools-3.14.tar.gz -C /opt
[root@shuai html]# cd /opt
[root@shuai opt]# cd inotify-tools-3.14/
[root@shuai inotify-tools-3.14]# yum install gcc gcc-c++ make -y
'//执行脚本'
[root@shuai inotify-tools-3.14]# ./configure 

'//编译及安装'
[root@shuai inotify-tools-3.14]# make && make install

'//执行监控 -m:持续进行监控  -r:递归监控所有子对象  -q:简述输出信息 -e:指定要监控的事件类型'
[root@shuai inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/

'//此时是一个持续性的状态'
另外在开启一个新的终端设置 监控的路径是/var/www/html 

'//我们在这个目录创建文件'
[root@shuai ~]# cd /var/www/html/
[root@shuai html]# touch index.txt

'//可以看出都是可以监控到的'
[root@shuai inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/
#直接会显示
/var/www/html/ CREATE index.txt

  • 编写触发式同步脚本
[root@shuai opt]# vim inotfiy.sh


#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ backuper@20.0.0.41::wwwroot/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
    if [ $(pgrep rsync | wc -l) -le 0 ] ; then
        $RSYNC_CMD
    fi
done


'//增加执行权限'
[root@shuai opt]# chmod +x inotfiy.sh

上述脚本用来检测本机/var/www/html目录的变动情况,一旦有更新触发rsync同步操作,上传备份至服务器20.0.0.41的/var/www/html目录下。

  • 设置服务器一的只读模式关闭
read only = no   //要是开了只有读的配置 不能写

  • 增加权限
[root@shuai html]# chmod 777 /var/www/html/

'//服务器一也要增加读写的权限'
[root@localhost html]# chmod 777 /var/www/html/

  • 执行脚本启动监控
[root@shuai opt]# ./inotfiy.sh 
'//等待监控状态'


'//打开源服务器shuai的另一个窗口在/var/www/html目录下创建新的文件'
[root@shuai html]# touch 1.txt
[root@shuai html]# touch 2.txt
[root@shuai html]# touch 3.txt

  • 查看服务器中的/var/www/html目录下的变化情况
[root@localhost html]# ls /var/www/html/
1.txt  2.txt  3.txt  index.txt

已经同步成功,实验结束

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值