rsync--实现远程同步(理论和具体部署步骤)

一、关于rsync

  • Remote Sync,远程同步
  • 支持本地复制,或者与其他SSH、rsync主机同步
  • 官方网站:http://rsync.samba.org
  • rsync 主要是让各个服务器中数据同步
  • 只读可以下载,不可以上传 下载决定于读
  • 所有的内容只需要去找源点 备份点也叫发起端
  • kill -9 强制结束 pid文件还在
  • kill 结束 删除pid文件

二、 rsync命令的用法

- rsync  [选项] 原始位置  目标位置     ## 把原始位置同步到目标位置

 - a: 归档模式,递归保留对象属性,等同于 -rlptgoD   如属组属主
 
-v: 显示同步过程的详细(verbose) 信息

-z: 在传输文件时进行压缩(compress)

-H: 保留硬连接位置

-A:保留ACL属性信息

--delete : 删除目标位置有而原始位置没有的文件   #保持两边文件一致 (如果两边的数据不一致,那么用户访问获取的内容就是不一致了)        这条命令要小心

--checksum: 根据对象的校验和来决定是否跳过文件

三、rsync实时同步

3.1 定期同步的不足

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

3.2 实时同步的优点

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

四、inotify

  • Linux内核的inotify机制
  • 从版本2.6.13开始提供
  • 可以监控文件系统的变动情况,并做出通知响应
  • 辅助软件:inotify-tools

4.1 调整inotify内核参数

  • max_queue_events:监控事件队列大小
  • max_user_instances:最多监控实例数
  • max_user_watches:每个实例最多监控文件数
[root@localhost ~]# vi /etc/sysctl.conf 
……
fs.inotify.max_queued_events = 16384  ##监控数应大于监控目标的总文件数

fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

[root@localhost ~]# sysctl -p

4.2 安装inotify-tools辅助工具

  • inotifywait:用于持续监控,实时输出结果
  • inotifywatch:用于短期监控,任务完成后再出结果
  • inotify 作用是监控 然后做出通知响应
  • 监控的事件 —— modify 修改 create 创建 move 移动 delete 删除
    参数说明

inotifywait

语法:

inotifywait [-hcmrq] [-e ] [-t ] [--format ] [--timefmt ] [ ... ]

参数:
-h,–help
输出帮助信息
@
排除不需要监视的文件,可以是相对路径,也可以是绝对路径。
–fromfile
从文件读取需要监视的文件或排除的文件,一个文件一行,排除的文件以@开头。
-m, –monitor
接收到一个事情而不退出,无限期地执行。默认的行为是接收到一个事情后立即退出。
-d, –daemon
跟–monitor一样,除了是在后台运行,需要指定–outfile把事情输出到一个文件。也意味着使用了–syslog。
-o, –outfile
输出事情到一个文件而不是标准输出。
-s, –syslog
输出错误信息到系统日志
-r, –recursive
监视一个目录下的所有子目录。
-q, –quiet
指定一次,不会输出详细信息,指定二次,除了致命错误,不会输出任何信息。
–exclude
正则匹配需要排除的文件,大小写敏感。
–excludei
正则匹配需要排除的文件,忽略大小写。
-t , –timeout
设置超时时间,如果为0,则无限期地执行下去。
-e , –event
指定监视的事件。
-c, –csv
输出csv格式。
–timefmt
指定时间格式,用于–format选项中的%T格式。
–format
指定输出格式。
%w 表示发生事件的目录
%f 表示发生事件的文件
%e 表示发生的事件
%Xe 事件以“X”分隔
%T 使用由–timefmt定义的时间格式

inotifywatch

语法:

参数:
-h,–help
输出帮助信息
@
排除不需要监视的文件,可以是相对路径,也可以是绝对路径。
–fromfile
从文件读取需要监视的文件或排除的文件,一个文件一行,排除的文件以@开头。
-m, –monitor
接收到一个事情而不退出,无限期地执行。默认的行为是接收到一个事情后立即退出。
-d, –daemon
跟–monitor一样,除了是在后台运行,需要指定–outfile把事情输出到一个文件。也意味着使用了–syslog。
-o, –outfile
输出事情到一个文件而不是标准输出。
-s, –syslog
输出错误信息到系统日志
-r, –recursive
监视一个目录下的所有子目录。
-q, –quiet
指定一次,不会输出详细信息,指定二次,除了致命错误,不会输出任何信息。
–exclude
正则匹配需要排除的文件,大小写敏感。
–excludei
正则匹配需要排除的文件,忽略大小写。
-t , –timeout
设置超时时间,如果为0,则无限期地执行下去。
-e , –event
指定监视的事件。
-c, –csv
输出csv格式。
–timefmt
指定时间格式,用于–format选项中的%T格式。
–format
指定输出格式。
%w 表示发生事件的目录
%f 表示发生事件的文件
%e 表示发生的事件
%Xe 事件以“X”分隔
%T 使用由–timefmt定义的时间格式

inotifywatch

语法:

inotifywatch [-hvzrqf] [-e ] [-t ] [-a ] [-d ] [ ... ]

参数:

-h, –help
输出帮助信息
-v, –verbose
输出详细信息
@
排除不需要监视的文件,可以是相对路径,也可以是绝对路径。
–fromfile
从文件读取需要监视的文件或排除的文件,一个文件一行,排除的文件以@开头。
-z, –zero
输出表格的行和列,即使元素为空
–exclude
正则匹配需要排除的文件,大小写敏感。
–excludei
正则匹配需要排除的文件,忽略大小写。
-r, –recursive
监视一个目录下的所有子目录。
-t , –timeout
设置超时时间
-e , –event
只监听指定的事件。
-a , –ascending
以指定事件升序排列。
-d , –descending
以指定事件降序排列。

可监听事件

  • access 文件读取
  • modify 文件更改。
  • attrib 文件属性更改,如权限,时间戳等。
  • close_write 以可写模式打开的文件被关闭,不代表此文件一定已经写入数据。
  • close_nowrite 以只读模式打开的文件被关闭。
  • close 文件被关闭,不管它是如何打开的。
  • open 文件打开。
  • moved_to 一个文件或目录移动到监听的目录,即使是在同一目录内移动,此事件也触发。
  • moved_from 一个文件或目录移出监听的目录,即使是在同一目录内移动,此事件也触发。
  • move 包括moved_to和 moved_from
  • move_self 文件或目录被移除,之后不再监听此文件或目录。
  • create 文件或目录创建
  • delete 文件或目录删除
  • delete_self 文件或目录移除,之后不再监听此文件或目录
  • unmount 文件系统取消挂载,之后不再监听此文件系统。

五、 实验 rsync 远程同步

5.1 实验环境

  • 源服务器192.168.75.200
  • 客户端: 192.168.75.144

5.2 实验目的

  • 源服务器和客户端能够远程同步

5.3 实验步骤

  • 源服务器配置
vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes //禁锢家目录

address = 192.168.75.200
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.75.0/24

[wwwroot]
path = /var/www/html
comment = www.kgc.com
read only = yes  //只读和下载,但是不能上传

dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2  //不压缩文件会造成,占用带宽造成网络拥塞,高并发
auth users = ttyy
secrets file = /etc/rsyncd_users.db    //密码文件



[root@rsync ~]# vim /etc/rsyncd_users.db    //写用户和密码

ttyy:123
~             

 yum -y install httpd

[root@rsync ~]# vim /var/www/html/index.html 

<h1>this is  yuanfuwuq webwebweb !!!!!!!!!!</h1>

rsync --daemon  //开启

chmod 600 /etc/rsyncd_users.db
  • 发起端:192.168.75.144
格式一:

 rsync -avz ttyy@192.168.75.200::wwwroot /opt
密码:123

格式二:

 rsync -avz ttyy@192.168.75.200::wwwroot /opt
密码:123





######## 免交互########

vim /etc/server.pass
abc123
[root@localhost opt]# chmod 600 /etc/server.pass 
rsync -az --delete --password-file=/etc/server.pass ttyy@192.168.75.200::wwwroot /opt/

六、实验: inotify+rsync 监控并远程同步

192.168.75.144作为源端服务器

inotify监听本地信息,然后进行同步到发起端

  • 源端服务器配置
———————————————rsync+inotify———————————————————
注意: 源端设置: 
[root@localhost html]# vim /etc/rsyncd.conf 
read only = no 

192.168.75.144作为源端服务器
inotify监听本地信息,然后进行同步到发起端

源端服务器:1
[root@localhost ~]#vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

[root@localhost ~]# sysctl -p  //加载配置

[root@localhost ~]# yum -y install gcc gcc-c++ 
准备安装包  inotify-tools-3.14.tar.gz
[root@localhost ~]# tar zxvf inotify-tools-3.14.tar.gz 
[root@localhost ~]# cd inotify-tools-3.14/
[root@localhost inotify-tools-3.14]# ./configure 
[root@localhost inotify-tools-3.14]# make && make install

[root@localhost ~]# vim inotify.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/ ttyy@192.168.75.200::wwwroot "  ###源服务器端的ip地址
$INOTIFY_CMD | while read DIRECTORY EVENT FILE  ##读取输出的监控记录
do
        if [ $(pgrep rsync | wc -l) -le 0 ]  ##如果rsync未在执行,则立即启动
	then
               		 $RSYNC_CMD
fi
done
~            
#################################脚本二###########################################

 vim inotify22.sh

#!/bin/bash
dir="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ ttyy@192.168.75.200::wwwroot"
dir2="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"
 $dir2 | while read file
do
  $dir
done

yum -y install httpd
chmod 777 inotify22.sh

[root@localhost ~]# chmod 777 /var/www/html/  ##两端服务器都要设置
[root@localhost ~]# ./inotify22.sh   //监听状态,另起窗口进行验证操作,是否同步信息
  • 验证发起端
[root@localhost html]# touch tt4.txt
[root@localhost html]# 

监听状态

[root@localhost ~]# ./inotify22.sh 
rsync: failed to set times on "/." (in wwwroot): Operation not permitted (1)
rsync: chgrp "/.index.html.lAX8Ak" (in wwwroot) failed: Operation not permitted (1)
rsync: chgrp "/.tt1.txt.Rmsur1" (in wwwroot) failed: Operation not permitted (1)
rsync: chgrp "/.tt2.txt.4VdQhI" (in wwwroot) failed: Operation not permitted (1)
rsync: chgrp "/.tt3.txt.uqdc8o" (in wwwroot) failed: Operation not permitted (1)
rsync: chgrp "/.tt4.txt.7MpyY5" (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 "/.index.html.w1DjbA" (in wwwroot) failed: Operation not permitted (1)
rsync: chgrp "/.tt1.txt.eUbplh" (in wwwroot) failed: Operation not permitted (1)
rsync: chgrp "/.tt2.txt.WjVuvY" (in wwwroot) failed: Operation not permitted (1)
rsync: chgrp "/.tt3.txt.7NTAFF" (in wwwroot) failed: Operation not permitted (1)
rsync: chgrp "/.tt4.txt.2e0GPm" (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]


查看发起端 /var/www/html  已经同步了tt4.txt
[root@rsync html]# ll
总用量 4
-rw-------. 1 nobody nobody 32 9月  12 22:36 index.html
-rw-------. 1 nobody nobody  0 9月  12 22:36 tt1.txt
-rw-------. 1 nobody nobody  0 9月  12 22:36 tt2.txt
-rw-------. 1 nobody nobody  0 9月  12 22:36 tt3.txt
-rw-------. 1 nobody nobody  0 9月  12 22:36 tt4.txt
[root@rsync html]# 

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值