博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
学会rsync远程同步和inotify实时同步一篇就够了!!!
阅读量:2026 次
发布时间:2019-04-28

本文共 8057 字,大约阅读时间需要 26 分钟。

文章目录

一:rsync介绍

Remote Sync,远程同步,它是一个开源的快速增量备份工具,可以在不同主机之间镜像同步整个目录树。

支持增量备份、保持连接和权限,且采用优化的同步算法,传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用
支持本地复制,或者与其他SSH、rsync主机同步
rsync 与scp FTP等工具备份的机制的优越性在于rsync 同步备份是先比较在拷贝变化过的数据,这样更节省资源,如有1T的数据只有1K的数据改变,则rsync 基本上备份只要同步1k 的数据而 scp 是个傻瓜式的拷贝,全部拷贝。

1.1:rsync 服务的模式

1、ssh方式进行同步

2、C/S 方式,rsync 有服务器端daemon模块 和rsync 客户端

1.2:rsync服务原理

在远程同步任务中,负责发起rsync同步操作的客户机称为发起端,而负责相应来自客户机的rsync同步操作的服务器称为同步源。

在同步过程中,同步源负责提供文档的原始位置,发起端应对该位置有读取权限。
如下图:
在这里插入图片描述
rsync是一款快速增量备份工具,支持:

  • 本地复制;
  • 与其他SSH同步;
  • 与rsync主机同步。

稍后从这三个方面来演示怎么使用rsync备份工具。

1.3:配置rsync源思路

配置rsync源服务器大致分为三步:

  • 建立rsync配置文件;
  • 为备份账户创建数据文件;
  • 启动rsync服务。

二、搭建rsync服务

准备两台虚拟机,一台作为同步源,一台用于客户机发起同步。

2.1:建立rsync配置文件

语法rsync [选项] 原始位置 目标位置常用选项-a:         归档模式,递归并保留对象属性,等同于-rlptgoD-v:         显示同步过程的详细(verbose)信息-z:         在传输文件时进行压缩(compress)-H:         保留硬连接文件-A:         保留ACL属性信息-p:         保留文件的权限标记-t:         保留文件的时间标记-g:         保留文件的属组标记-o:         保留文件的属主标记-delete:    删除目标位置有而原始位置没有的文件-checksum:  根据对象的校验和来决定是否跳过文件

同步源服务器:

[root@server1 ~]# rpm -q rsync          #检查rsync是否安装rsync-3.0.9-18.el7.x86_64[root@server1 ~]# vi /etc/rsyncd.confuid = nobodygid = nobodyuse chroot = yes                        #禁锢在宿主目录中address = 192.168.158.10                #监听地址port 873                                #端口号pid file = /var/run/rsyncd.pid          #进程文件位置log file = /var/log/rsyncd.log          #日志文件位置hosts allow = 192.168.158.0/24               #允许地址范围dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2         #不再压缩这几种格式的文件[wwwroot]path = /var/www/html                    #同步的目录comment = www.tom.comread only = yes                         #只读auth users = backupersecrets file = /etc/rsyncd_users.db     #用户密码存放位置
  • 创建backuper用户的密码文件
[root@server1 ~]# vi /etc/rsyncd_users.dbbackuper:123456
  • 给密码文件设置执行权限
[root@server1 ~]# chmod 600 /etc/rsyncd_users.db
  • 启动rsync
[root@server1 ~]# rsync --daemon[root@server1 ~]# netstat -anpt |grep rsynctcp        0      0 192.168.158.10:873      0.0.0.0:*               LISTEN      7360/rsync
  • 安装httpd,有一个/var/www/html的共享目录,也可以自己创建
[root@server1 ~]# yum -y install httpd[root@server1 ~]# echo "this is test web" > /var/www/html/index.html

2.2:客户机服务器B测试

[root@client1 ~]# rsync -avz backuper@192.168.158.10::wwwroot /optPassword: receiving incremental file list./index.htmlsent 83 bytes  received 172 bytes  102.00 bytes/sectotal size is 17  speedup is 0.07[root@client1 ~]# cat /opt/index.html this is test web[root@client1 ~]# rm -rf /opt/index.html [root@client1 ~]# cat /opt/index.html cat: /opt/index.html: 没有那个文件或目录[root@client1 ~]# rsync -avz rsync://backuper@192.168.158.10/wwwroot /optPassword: receiving incremental file list./index.htmlsent 83 bytes  received 172 bytes  72.86 bytes/sectotal size is 17  speedup is 0.07[root@client1 ~]# cat /opt/index.html this is test web
  • 免密登录
[root@client1 ~]# cat /etc/server.pass 123456[root@client1 ~]# rsync -avz --delete --password-file=/etc/server.pass backuper@192.168.158.10::wwwroot /optreceiving incremental file listdeleting rh/./index.htmlsent 83 bytes  received 172 bytes  510.00 bytes/sectotal size is 17  speedup is 0.07

三、rsync实时同步配置

3.1:实时同步介绍

定期同步存在一些不足之处,比如:

  • 执行备份的时间固定,延迟明显、实时性差
  • 当同步源长期不变化时,密集的定期任务是不必要的

实时同步的优点

  • 一旦同步源出现变化,立即启动备份
  • 只要同步源无变化,则不执行备份

inotify介绍

  • Linux内核从2.6.13开始,引入了inotify机制。
  • 它是一种文件系统的变化通知机制,可以监控文件,也 可以监控目录。当监控目录时,它可以同时监控目录及目录中的各子目录及文件的。
  • 可以协助rsync,监控到数据的改变,触发 rsync 进行数据的同步。
    在这里插入图片描述

3.2:配置rsync源服务器A

  • 将只读模式关闭
[root@server1 ~]# vi /etc/rsyncd.conf read only = no
  • 客户端更改内核参数,指定队列大小,最多监控实例数,每个实例最多监控文件数
[root@server1 ~]# vim /etc/sysctl.conffs.inotify.max_queued_events = 16384     #监控事件的最大队列数fs.inotify.max_user_instances = 1024       #监控的最大实例数fs.inotify.max_user_watches = 1048576     #监控的每实例的最大文件数加载[root@server1 ~]# sysctl -pfs.inotify.max_queued_events = 16384fs.inotify.max_user_instances = 1024fs.inotify.max_user_watches = 1048576

重新启动rsync服务,注意:kill pid号和kill -9 pid号的区别:后者不会删除pid文件,导致服务起不来

[root@server1 ~]# cd /var/run[root@server1 run]# cat rsyncd.pid9514[root@server1 run]# kill 9514            #删除pid文件[root@server1 run]# ll | grep rsync.pid[root@server1 run]# rsync --daemon[root@server1 run]# ll | grep rsyncd.pid-rw-r--r--.  1 root           root              6 11月 12 13:19 rsyncd.pid[root@server1 run]# cat rsyncd.pid9514[root@server1 run]# kill -9 9154        #不删除pid文件,导致启动不了[root@server1 run]# ll | grep rsyncd.pid-rw-r--r--.  1 root           root              6 11月 12 13:19 rsyncd.pid[root@server1 run]# rsync --daemon[root@server1 run]# failed to create pid file /var/run/rsyncd.pid: File exists[root@server1 run]# rm -rf rsyncd.pid    #删除pid文件再启动[root@server1 run]# rsync --daemon[root@server1 run]# netstat -anpt | grep rsynctcp        0      0 192.168.158.10:873      0.0.0.0:*               LISTEN      9666/rsync

4.4:客户机服务器B配置

  • 解压缩inotify并安装

inotifywait:用于持续监控,实时输出结果

inotifywatch:用于短期监控,任务完成后再出结果

[root@client1 ~]# tar zxf inotify-tools-3.14.tar.gz [root@client1 ~]# cd inotify-tools-3.14/[root@client1 inotify-tools-3.14]# ./configure [root@client1 inotify-tools-3.14]# make && make install
  • 测试inotifywait监控命令是否正常使用
[root@client1 inotify-tools-3.14]# mkdir -p /var/www/html [root@client1 inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html   inotifywait -mrq -e modify,create,move,delete /var/www/html
  • 执行完上面最后一条命令后处于监控状态,不能做输入操作,在开启一个新的会话
[root@client1 ~]# cd /var/www/html/[root@client1 html]# touch a.txt[root@client1 html]# vi 1.txt[root@client1 html]# rm -rf 1.txt [root@client1 html]# mv a.txt /opt/[root@client1 html]#
  • 监控端查看
[root@client1 inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html   /var/www/html/ CREATE a.txt/var/www/html/ CREATE .1.txt.swp/var/www/html/ CREATE .1.txt.swx/var/www/html/ DELETE .1.txt.swx/var/www/html/ DELETE .1.txt.swp/var/www/html/ CREATE .1.txt.swp/var/www/html/ MODIFY .1.txt.swp/var/www/html/ MODIFY .1.txt.swp/var/www/html/ CREATE 1.txt/var/www/html/ MODIFY 1.txt/var/www/html/ MODIFY .1.txt.swp/var/www/html/ DELETE .1.txt.swp/var/www/html/ DELETE 1.txt/var/www/html/ MOVED_FROM a.txt
  • 客户端上编写脚本,将inotify监控和rsync远程同步结合起来
[root@client1 inotify-tools-3.14]# vi /opt/inotify.sh#!/bin/bashINOTIFY="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html"RSYNC="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/  backuper@192。168.158.10::wwwroot/"$INOTIFY | while read DIRECTORY EVENT FILE   #逐条读取监控记录do        if [ $(pgrep rsync | wc -l) -le 0 ];then            $RSYNC        fidone[root@client1 inotify-tools-3.14]# chmod +x /opt/inotify.sh[root@client1 inotify-tools-3.14]# cd /opt/注:两边的同步目录权限都设置777[root@server1 run]# chmod 777 /var/www/html/[root@client1 html]# chmod 777 /var/www/html/运行脚本[root@client1 opt]# ./inotify.sh
  • 在客户端/var/www/html目录下创建文件,查看源端/var/www/html目录是否同步到
[root@client1 html]# touch a.txt b.txt   #客户机查看源端/var/www/html目录,新建的和原来就存在的文件都成功同步,且属主属组均为nobody用户[root@server1 html]# ll                  #同步源总用量 0-rw-r--r--. 1 nobody nobody 0 11月 12 19:30 a.txt-rw-r--r--. 1 nobody nobody 0 11月 12 19:30 b.txt

同步时的注意项

使用如下命令进行上行同步(上传)时,本地目录最后要加上/,否则会将目录同步到对方目标文件夹下,而不仅仅是同步本地目录下的文件

  • 将上面的脚本源端目录/var/www/html/改成/var/www/html
[root@client opt]# vi /opt/inotify.sh#!/bin/bashINOTIFY="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html"RSYNC="rsync -azH --delete --password-file=/etc/server.pass /var/www/html  backuper@20.0.0.10::wwwroot"$INOTIFY | while read DIRECTORY EVENT FILE   #逐条读取监控记录do        if [ $(pgrep rsync | wc -l) -le 0 ];then            $RSYNC        fidone运行脚本[root@client opt]# ./inotify.sh
  • 在客户端/var/www/html目录下创建文件,查看源端/var/www/html目录会发现直接同步的时目录
客户机写入数据[root@client html]# touch e.txtrsync上查看[root@client1 html]# touch f.txt[root@client1 html]# ll总用量 0-rw-r--r--. 1 root root 0 11月 12 19:30 a.txt-rw-r--r--. 1 root root 0 11月 12 19:30 b.txt-rw-r--r--. 1 root root 0 11月 12 19:35 dd.txt-rw-r--r--. 1 root root 0 11月 12 19:35 f.txt#整个目录同步到目标主机上[root@server html]# cd html/[root@server1 html]# ll总用量 0-rw-r--r--. 1 nobody nobody  0 11月 12 19:30 a.txt-rw-r--r--. 1 nobody nobody  0 11月 12 19:30 b.txt-rw-r--r--. 1 nobody nobody  0 11月 12 19:35 dd.txtdrwxrwxrwx. 2 nobody nobody 59 11月 12 19:35 html    #整个目录同步到目标主机上

转载地址:http://yfdaf.baihongyu.com/

你可能感兴趣的文章
JVM技术总结之四——JVM内存结构
查看>>
Lucene基本知识入门
查看>>
Windows系统下通过PSCP传输文件至BeagleBone Black
查看>>
OpenCV像素点邻域遍历效率比较,以及访问像素点的几种方法
查看>>
背景提取算法——帧间差分法、背景差分法、ViBe算法、ViBe+算法
查看>>
“王大锤の非诚勿扰” —— Spring IoC / DI 思想详述
查看>>
服务假死问题解决过程实记(三)——缓存问题优化
查看>>
Individual Homework -----questions about the text book by 张静
查看>>
[初心者适用]如何为代码编写基本的文档
查看>>
DailyScrum beta 第三天!
查看>>
骚博记, 又名: building another twitter
查看>>
Daily scrum beta 第五天!
查看>>
为什么牛逼?——"Stonie is a KungFu monk"游戏精品功能介绍与详细规范,以及其中的挑战...
查看>>
影响未来的应用ifttt,互联网自主神经系统的又一个有力证据
查看>>
IT管理人才必备的十大能力
查看>>
迎接五大趋势 拥抱两个世界
查看>>
[置顶] 电信系统方案 >> 电信Boss系统
查看>>
英特尔诺基亚联手研发Symbian系统的智能手机
查看>>
怎样成为优秀的软件模型设计者?
查看>>
解决spring和struts配合问题
查看>>