前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >在CentOS7上配置rsync源服务器+inotify实时同步

在CentOS7上配置rsync源服务器+inotify实时同步

作者头像
拓荒者
发布2019-03-11 10:57:18
8480
发布2019-03-11 10:57:18
举报
文章被收录于专栏:运维经验分享运维经验分享

概述

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

原理

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

配置rsync源服务器

1.检查rsync是否安装

代码语言:javascript
复制
  1. [root@localhost ~]# rpm -q rsync
  2. rsync-3.0.9-18.el7.x86_64

2.修改rsync默认配置文件,位于/etc/rsyncd.conf。

代码语言:javascript
复制
  1. 插入以下内容
  2. uid = nobody
  3. gid = nobody
  4. use chroot = yes //禁锢在源目录//
  5. address = 192.168.126.138 //监听地址//
  6. port 873 //监听端口//
  7. log file = /var/log/rsyncd.log //日志文件位置//
  8. pid file = /var/run/rsyncd.pid //存放进程ID的文件位置//
  9. hosts allow = 192.168.126.0/24 //允许访问的客户机地址//
  10. # max connections = 4
  11. # pid file = /var/run/rsyncd.pid
  12. # exclude = lost+found/
  13. # transfer logging = yes
  14. # timeout = 900
  15. # ignore nonreadable = yes
  16. # dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
  17. [wwwroot] //共享模块名称//
  18. path = /var/www/html //源目录的实际路径//
  19. read only = no //是否为只读//
  20. dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 //同步时不再压缩的文件类型//
  21. auth users = backuper //授权账户//
  22. secrets file = /etc/rsyncd_users.db //存放账户信息的数据文件//

3.为备份账户创建数据文件

根据上一步的设置,创建账号数据文件,添加一行用户记录,以冒号分隔,用户名称为backup,密码为abc123。由于账号信息采取明文存放,因此应调整文件权限,避免账号信息泄露。

代码语言:javascript
复制
  1. [root@localhost ~]#vim /etc/rsyncd_users.db
  2. backuper:abc123
  3. [root@localhost ~]#chmod 600 /etc/rsyncd_users.db

4.开启rsync服务,运行参数为 --daemon。

代码语言:javascript
复制
  1. [root@localhost opt]# systemctl stop firewalld.service
  2. [root@localhost opt]# setenforce 0
  3. [root@localhost opt]# rsync --daemon
  4. [root@localhost opt]# netstat -ntap | grep rsync
  5. tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 2934/rsync

使用rsync备份工具

配置源的方法:

在执行运程同步任务时,rsync命令需要指定同步源服务器中的资源位置。rsync同步源的资源表示方式为“用户名@主机地址::共享模块名”或者“rsync://用户名@主机地址/共享模块名”,前者为两个冒号分隔形式,后者为URL地址形式。

代码语言:javascript
复制
  1. rsync -avz backuper@192.168.175.129::wwwroot /opt/
  2. rsync -avz rsync://backuper@192.168.175.129/wwwroot /opt/

1.执行以下操作将源服务器中的wwwroot共享模块,下载到客户机的本地/var/www/html目录下。

代码语言:javascript
复制
  1. 源服务器:
  2. [root@localhost ~]# cd /var/www/html/
  3. [root@localhost html]# echo "123" > 111.txt
  4. [root@localhost html]# echo "456" > 222.txt
  5. [root@localhost html]# ls
  6. 111.txt 222.txt
  7. 客户端:
  8. [root@localhost opt]# rsync -avz backuper@192.168.126.138::wwwroot/ ./ //下载到当前目录//
  9. Password:
  10. receiving incremental file list
  11. ./
  12. 111.txt
  13. 222.txt
  14. sent 102 bytes received 221 bytes 23.93 bytes/sec
  15. total size is 8 speedup is 0.02
  16. [root@localhost html]# ls
  17. 111.txt 222.txt

2.在客户端上传文件到源服务器

代码语言:javascript
复制
  1. [root@localhost opt]#mkdir b1 b2 b3 b4
  2. [root@localhost opt]# ls
  3. b1 b2 b3 b4 b5 rh
  4. [root@localhost opt]# rsync -avz backuper@192.168.126.138::wwwroot/ ./ #上传
  5. Password:
  6. receiving incremental file list
  7. ./
  8. 111.txt
  9. 222.txt
  10. sent 102 bytes received 221 bytes 23.93 bytes/sec
  11. total size is 8 speedup is 0.02 #上传成功
  12. 源服务器上查看:
  13. [root@localhost ~]# cd /var/www/html/
  14. [root@localhost html]# ls
  15. b1 b2 b3 b4 b5 rh
代码语言:javascript
复制
  1. 注意:
  2. 上传前需要把源服务器rsync的配置文件rsyncd.conf中的uid、gid修改为root

配置rsync+inotify实施同步

将rsync工具与inotify机制相结合,可以实现触发式备份(实时同步)——只要原始位置的文档发生变化,就立即启动增量备份操作,如图所示,否则处于静默等待状态。这样,就避免了按固定周期备份时存在的延迟性、周期过密等问题。

正因为inotify通知机制由Linux内核提供,因此要做本机监控,在触发式备份中应用时更适合上行同步。下面一次介绍其配置过程。

1.调整inotify内核参数

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

代码语言:javascript
复制
  1. [root@localhost html]# vim /etc/sysctl.conf
  2. # For more information, see sysctl.conf(5) and sysctl.d(5). //添加//
  3. fs.inotify.max_queued_events = 16384
  4. fs.inotify.max_user_instances = 1024
  5. fs.inotify.max_user_watches = 1048576
  6. [root@localhost html]# sysctl -p //启动//
  7. fs.inotify.max_queued_events = 16384
  8. fs.inotify.max_user_instances = 1024
  9. fs.inotify.max_user_watches = 1048576

2.安装inotifi-tools

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

代码语言:javascript
复制
  1. [root@localhost rs]# tar zxvf inotify-tools-3.14.tar.gz -C /opt/ #解包
  2. [root@localhost opt]# cd inotify-tools-3.14/
  3. [root@localhost inotify-tools-3.14]# yum install gcc gcc-c++ make -y #安装编译软件
  4. [root@localhost inotify-tools-3.14]# ./configure
  5. [root@localhost inotify-tools-3.14]#make && make install

3.编写触发式同步脚本

代码语言:javascript
复制
  1. root@localhost opt]# vim inotify.sh
  2. #!/bin/bash
  3. INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"
  4. RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ backuper@192.168.126.138::wwwroot/"
  5. $INOTIFY_CMD | while read DIRECTORY EVENT FILE
  6. do
  7. if [ $(pgrep rsync | wc -l) -le 0 ] ; then
  8. $RSYNC_CMD
  9. fi
  10. done

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

4.验证

1).在源服务器运行inotifywait -mrq -e modify,create,move,delete /var/www/html/

代码语言:javascript
复制
[root@localhost html]# inotifywait -mrq -e modify,create,move,delete /var/www/html/ //静默等待状态//

2)打开源服务器的另一个窗口在/var/www/html目录下创建新的文件

代码语言:javascript
复制
  1. [root@localhost ~]# cd /var/www/html/
  2. [root@localhost html]# ls
  3. [root@localhost html]# echo "this is 111" > 111.txt //写入文件//
  4. [root@localhost html]# echo "this is 222" > 222.txt
  5. [root@localhost html]# rm -rf 111.txt //删除文件//

3)查看源服务器的更新触发状态

代码语言:javascript
复制
  1. [root@localhost html]# inotifywait -mrq -e modify,create,move,delete /var/www/html/
  2. /var/www/html/ CREATE 111.txt
  3. /var/www/html/ MODIFY 111.txt
  4. /var/www/html/ CREATE 222.txt
  5. /var/www/html/ MODIFY 222.txt
  6. /var/www/html/ DELETE 111.txt
  7. //监控端已有反馈//

4)查看服务器中的/var/www/html目录下的变化情况

代码语言:javascript
复制
  1. [root@localhost html]# ls
  2. 111.txt 222.txt
  3. [root@localhost html]# ls
  4. 222.txt

实验成功

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018/10/17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档