rsync远程同步原理加实验

关于rsync

●一款快速增量备份工具

●Remote Sync,远程同步

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

●官方网站:http://rsync.samba.org
在这里插入图片描述

配置rsync源服务器

●rsync同步源

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

●基本思路:

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

●应用实例

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

●配置文件rsync.conf

  • 需手工建立,语法类似于Samba配置
  • 认证配置auth users secrets file,不加则为匿名

●rsync账号文件

  • 采用“用户名:密码”的记录格式,每行一个用户记录
  • 独立的账号数据,不依赖于系统账号
  • 不依赖于系统账号是安全的,因为用独立的账号,和系统没有关系

●启用rsync服务

  • 通过–daemon独自提供服务,daemon手动同步

使用rsync备份工具

●rsync命令的用法

Rsync[选项] 原始位置 目标位置

●常用选项

  • -a:归档模式,递归并保留对象属性,等同于 -rlptgoD
  • -v:显示同步过程的详细信息
  • -z:在传输文件时进行压缩
  • -H:保留硬链接文件
  • -A:保留ACL属性信息
  • –delete:删除目标位置有而原始位置没有的文件
  • –checksum:根据对象的校验和来决定是否跳过文件

rsync远程同步实验

实验环境

在这里插入图片描述

推荐步骤

1.配置源站点虚拟机修改rsync配置文件

[root@source ~]# hostnamectl set-hostname source
[root@source ~]# su
[root@source ~]# vim /etc/rsyncd.conf
uid = nobody     ##指定uid为nobody
gid = nobody     ##指定gid
use chroot = yes    ##禁锢在源目录
port 873         ##监听端口号
#max connections = 4
log file = /var/log/rsyncd.log    ##日志文件存放位置
pid file = /var/run/rsyncd.pid    ##pid文件存放位置
hosts allow = 192.168.148.0/24   ##允许访问的客户机地址网段
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
[wwwroot]              ##共享模块名称
path = /var/www/html            ##源目录的实际路径
comment = www.test.com 
read only = yes             ##是否只读
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2   ##同步时不在压缩的文件类型
auth users = backuper       ##授权用户为backuper
secrets file = /etc/rsyncd_users.db    ##存放账户信息的数据文件

2.创建用于密码的文件

[root@source ~]# vim /etc/rsyncd_users.db    ##创建文件
backuper:123456     ##用户名称:密码
[root@source ~]# chmod 666 /etc/rsyncd_users.db    ##给数据文件设置权限
[root@source ~]# yum -y install httpd     ##安装httpd服务,我们需要用到站点目录
[root@source ~]# cd /var/www/html/
[root@source html]# echo "this is test" > test.txt     ##写入两个文件信息
[root@source html]# echo "this is benet" > 123.html
[root@source html]# ls
123.html  index.html
[root@source html]# rsync --daemon    ##启动服务

3.在目标站点同步信息

[root@localhost ~]# hostnamectl set-hostname mubiao    ##修改主机名方便区分
[root@localhost ~]# su             ##切换主机名
[root@localhost ~]# setenforce 0     ##关闭防护系统
[root@localhost ~]# iptables -F      ##清空防火墙规则
[root@bendi ~]# rpm -q rsync     ##服务不需要安装,系统自带的
rsync-3.1.2-4.el7.x86_64

格式一:rsync -zva 用户名@对方地址::模块名称 拷贝到哪个目录下

[root@mubiao ~]# rsync -zva backuper@192.168.148.135::wwwroot /opt 

在这里插入图片描述
格式二:rsync -zva rsync://用户名@对方地址/模块名称 拷贝到哪个目录下

[root@mubiao opt]# rm -rf *.html    ##先将文件全部删除
[root@mubiao opt]# rsync -zva rsync://backuper@192.168.148.135/wwwroot /opt

格式三:面交互的方式去同步

[root@mubiao opt]# vim /etc/server.pass    ##创建一个文件存放对方用户密码
123456
[root@mubiao opt]# chmod 600 /etc/server.pass    ##给密码文件设置权限为600
[root@mubiao opt]# ls
rh      ##/opt目录下有rh文件
[root@mubiao opt]# rsync -zva --delete --password-file=/etc/server.pass backuper@192.168.148.135::wwwroot /opt

在这里插入图片描述
因为–delete会删除目标位置有而原始位置没有的文件,所有rh文件被删除了

rsync实时同步

●定期同步不足

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

●实时同步的优点

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

关于inotify

●Linux内核的inotify机制

  • 从版本2.6.13开始提供
  • 可以监控文件系统的变动情况,并做出通知响应
  • 辅助软件:inotify-tools
    在这里插入图片描述

rsync+inotify实验

实验环境

在这里插入图片描述

推荐步骤

1.接着上面的环境继续,服务端改为148.136并进行配置

[root@mubiao opt]# yum -y install httpd   ##安装apache服务
[root@mubiao opt]# cd /var/www/html/
[root@mubiao html]# ls
[root@mubiao html]#      ##站点目录是空的
[root@mubiao 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@mubiao html]# sysctl -p   ##配置让其生效

在这里插入图片描述

[root@mubiao opt]# tar zxvf inotify-tools-3.14.tar.gz    ##解压工具
[root@mubiao opt]# cd inotify-tools-3.14/  
[root@mubiao inotify-tools-3.14]# yum -y install gcc gcc-c++  ##安装编译安装包
[root@mubiao inotify-tools-3.14]# ./configure 
[root@mubiao inotify-tools-3.14]# make&&make install
[root@mubiao inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete  /var/www/html    ##配置监控/var/www/html目录
这时需要再打开一个192.168.148.136的终端
[root@mubiao ~]# cd /var/www/html/
[root@mubiao html]# ls
[root@mubiao html]# touch 123.txt     ##在新终端的/var/www/html目录下创建文件

在这里插入图片描述

[root@mubiao html]# vim /opt/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/ backuper@192.168.148.135::wwwroot/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
    if [ $(pgrep rsync | wc -l) -le 0 ] ; then
         $RSYNC_CMD
    fi
done
~      
[root@mubiao html]# chmod +x /opt/inotify.sh    ##给脚本增加执行权限
[root@mubiao html]# chmod 777 /var/www/html/
[root@mubiao opt]# ./inotify.sh   ##执行监控脚本(需要将原来的监控停止)

2.客户端192.168.148.135配置

[root@source opt]# cd /var/www/html/
[root@source html]# ls
123.html  index.html
[root@source html]# rm -rf *.html  ##将148.135虚拟机中的apache站点内容删除
[root@source html]# vim /etc/rsyncd.conf    ##修改rsync配置文件
[wwwroot]
path = /var/www/html
comment = www.test.com
read only = no      ##将只读改为no
[root@source html]# netstat -ntap | grep rsync
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      10464/rsync         
tcp6       0      0 :::873                  :::*                    LISTEN      10464/rsync         
[root@source html]# kill 10464     ##将进程号删除
[root@source html]# rsync --daemon    重新启动服务
[root@source html]# chmod 777 /var/www/html/    ##站点目录增加权限
回到服务端的另一个终端,在站点中写入内容

在这里插入图片描述
3.服务端写入内容
在这里插入图片描述
在这里插入图片描述
4.客户端站点也有信息,同步成功在这里插入图片描述
5.这时将服务端的信息删除
在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值