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

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

2018年08月07日 11:21:45 沙克顿巴斯 阅读数:93 标签: rsync inotifyhtml

概述c++

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

原理vim

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

配置rsync源服务器服务器

1.检查rsync是否安装tcp

 
  1. [root@localhost ~]# rpm -q rsync工具

  2. rsync-3.0.9-18.el7.x86_64优化

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

 
  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。因为帐号信息采起明文存放,所以应调整文件权限,避免帐号信息泄露。

 
  1. [root@localhost ~]#vim /etc/rsyncd_users.db

  2. backuper:abc123

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

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

 
  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地址形式。

 
  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目录下。

 
  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. 客户端:

  9. [root@localhost opt]# rsync -avz backuper@192.168.126.138::wwwroot/ ./ //下载到当前目录//

  10. Password:

  11. receiving incremental file list

  12. ./

  13. 111.txt

  14. 222.txt

  15.  
  16. sent 102 bytes received 221 bytes 23.93 bytes/sec

  17. total size is 8 speedup is 0.02

  18. [root@localhost html]# ls

  19. 111.txt 222.txt

  20.  

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

 
  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.  
  11. sent 102 bytes received 221 bytes 23.93 bytes/sec

  12. total size is 8 speedup is 0.02 #上传成功

  13.  
  14. 源服务器上查看:

  15. [root@localhost ~]# cd /var/www/html/

  16. [root@localhost html]# ls

  17. b1 b2 b3 b4 b5 rh

  18.  
 
  1. 注意:

  2. 上传前须要把源服务器rsync的配置文件rsyncd.conf中的uid、gid修改成root

配置rsync+inotify实施同步

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

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

1.调整inotify内核参数

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

 
  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辅助工具程序,用来监控和汇总改动状况。

 
  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.编写触发式同步脚本

 
  1. root@localhost opt]# vim inotify.sh

  2.  
  3. #!/bin/bash

  4. INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"

  5. RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ backuper@192.168.126.138::wwwroot/"

  6. $INOTIFY_CMD | while read DIRECTORY EVENT FILE

  7. do

  8. if [ $(pgrep rsync | wc -l) -le 0 ] ; then

  9. $RSYNC_CMD

  10. fi

  11. done

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

4.验证

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

[root@localhost html]# inotifywait -mrq -e modify,create,move,delete /var/www/html/ //静默等待状态//

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

 
  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)查看源服务器的更新触发状态

 
  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目录下的变化状况

 
  1. [root@localhost html]# ls

  2. 111.txt 222.txt

  3. [root@localhost html]# ls

  4. 222.txt

实验成功

相关文章
相关标签/搜索