Centos7rsync远程同步


实验材料:
两台Centos7
一台担任rsync服务器:192.168.1.10
一台担任客户机:192.168.1.110

配置rsync 源服务器

1.rsync工具简介:
一个开源的快速备份工具,可以在不同主机间镜像同步整个目录树,支持增量备份,保持链接和权限,采用优化的同步算法,传输前执行压缩,适用于异地备份,镜像服务器等应用 (是Linux系统默认安装的基本组件之一)

2.服务器身份区分:
在这里插入图片描述
发起端: 负责发起rsync同步操作的客户机 (对同步源的文档位置要有读取权限)
同步源: 负责去响应来自客户机rsync同步操作的服务器 (负责提供文档的原始位置)

要求: 将服务器A的/var/www/html 目录,用备份账户ajbn 备份到服务器B上
实现步骤:
(一)、配置同步源
1.安装httpd,准备/var/www/html目录

[root@A-Server ~]# systemctl stop firewalld       (关闭防火墙)
[root@A-Server ~]# setenforce 0			  (临时关闭selinux)
[root@A-Server ~]# mount /dev/cdrom /media/cdrom  (挂光盘)
[root@A-Server ~]# yum -y install httpd           (安装httpd服务)
[root@A-Server ~]# cd /var/www/html/		  (进入网站根目录)
[root@A-Server html]# touch aaa bbb               (创建两个文件)

在这里插入图片描述
2、建立/etc/rsyncd.conf 配置文件

[root@A-Server html]# vim /etc/rsyncd.conf
use chroot = yes      #禁锢在源目录
address = 192.168.1.10    #监听地址
port = 873    #监听端口
log file = /var/log/rsyncd.log   #日志文件
pid file = /var/run/rsyncd.pid   #进程文件
hosts allow = 192.168.1.0/24	 #允许访问的客户机地址

[aaa]      #共享模块名
path = /var/www/html   #需要同步的实际路径
comment = ajbn	       #描述信息,随便写
read only = yes        #只读
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2   #同步时不在压缩的文件类型
auth users = ajbn      #授权同步的账户
secrets file = /etc/rsync_users.db   #账户所在的数据文件

3、创建备份账户数据文件

[root@A-Server html]# vim /etc/rsync_users.db  #创建授权账户文件
[root@A-Server html]# cat /etc/rsync_users.db
ajbn:123.com   (ajbn用户名;123.com密码)
[root@A-Server html]# chmod 600 /etc/rsync_users.db (对用户的数据文件进行权限保护)

4、启动rsync 服务程序

[root@A-Server html]# rsync --daemon    (启用)
[root@A-Server html]# netstat -anpt | grep 873   (查看监听状态)

在这里插入图片描述

使用rsync 工具 (进行同步备份)

本地备份: 类似于cp 命令: 举例: rsync /etc/fstab /opt (将本地文件/etc/fstab 同步到/opt 目录下)
基本语法: rsync 选项 源位置 目标位置

选项 作用
-a 归档模式,保留文件权限、属性等信息 等同于组合选项 “-rlptgoD”
-v 显示同步的详细过程
-H 保留硬链接文件
-A 保留ACL属性信息
-z 传输时进行压缩
–delete 删除目标位置有而原始位置没有的文件
–checksum 根据校验和来决定是否跳过文件

在客户机上备份测试:

[root@B-Clinet ~]# systemctl stop firewalld  (关闭防火墙)
[root@B-Clinet ~]# setenforce 0    (临时关闭selinux)
[root@B-Clinet ~]# rsync -avz ajbn@192.168.1.10::aaa /root  (远程同步)
[root@B-Clinet ~]# ls   (查看)

在这里插入图片描述
结合crond计划任务进行自动备份

vim /etc/server.pass
123.com    (所写的就是ajbn 的密码)
[root@B-Clinet ~]# chmod 600 /etc/server.pass   (对用户的数据文件进行权限保护)
[root@B-Clinet ~]# mkdir /myweb			(创建备份目录)
[root@B-Clinet ~]# crontab -e			 (创建计划任务)
30 22 * * * /usr/bin/rsync -az --delete --password-file=/etc/server.pass ajbn@192.168.1.10::aaa /myweb
[root@B-Clinet ~]# crontab -l     (查看计划任务)
[root@B-Clinet ~]# date -s "2020-09-24 22:29"    (设置时间,方便测试)
[root@B-Clinet ~]# systemctl restart crond	 (重启服务)
[root@B-Clinet ~]# date		(查看当前时间)
[root@B-Clinet ~]# ll /myweb/	(查看验证)

在这里插入图片描述

配置inotify + rsync 实时同步

(一)、定期备份的不足
1、备份时间固定,延迟明显,实时性差
2、如果备份源长时间没有发生变化,而进行定期备份严重浪费资源

(二)、解决方案: 实时同步
1、只要源一发生变化,立即启动同步
2、源无变化,则不进行同步备份

在这里插入图片描述
依托: inotify
inotify: 用来监控文件系统的各种变化情况,如文件的存取、删除、移动、修改等,可以方便的实现文件异动告警、增量备份,并针对目录或文件的变化及时做出响应
inotify + rsync 结合: 实现触发式备份(实时同步)——只要原始位置的文档发生变化,立即启动增量备份,否则处于静默等待状态

案例:
服务器A: 192.168.1.10 发起端 ,安装inotify
服务器B: 192.168.1.110 同步源
要求: 只要服务器A的数据一发生变化,立即同步到服务器B上

inotify源码包:
https://pan.baidu.com/s/150e64cB7dBIi0EaZqvqtFA
提取码:j4jf
步骤:
(一)、调整inotify 内核参数 (监控事件队列);最多监控实例数;每个实例最多监控文件数
192.168.1.10:
把三个参数数值加大: 监控的目录、文件数量较多或变化频繁

(查询默认的监控事件队列)
[root@A-Server ~]# cat /proc/sys/fs/inotify/max_queued_events
(查询默认的最多实例数)
[root@A-Server ~]# cat /proc/sys/fs/inotify/max_user_instances 
(查询每个实例最多监控的文件数)
[root@A-Server ~]# cat /proc/sys/fs/inotify/max_user_watches 

调优:

[root@A-Server ~]# vim /etc/sysctl.conf
添加:
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@A-Server ~]# sysctl -p  (让其立即生效)

在这里插入图片描述
(二)、安装inotify-tools 软件;以便提供inotifywait、inotifywatch 辅助工具程序
192.168.1.10:

[root@A-Server ~]# tar zxf inotify-tools-3.14.tar.gz 
[root@A-Server ~]# cd inotify-tools-3.14/
[root@A-Server inotify-tools-3.14]# ./configure && make && make install

测试:监控 /mnt 目录的变化

[root@A-Server ~]# inotifywait -mrq -e modify,create,move,delete  /mnt
监控选项 作用
-m 表示持续监控
-r 表示递归整个目录
-q 简化输出消息
-e 指定监控哪写事件
modify修改
create 创建
move 移动
delete 删除
attrib 属性变化

在这里插入图片描述

在打开一个192.168.1.10的终端
[root@A-Server ~]# cd /mnt/  (进入/mnt目录)
[root@A-Server mnt]# ls      (查看是否有数据)
[root@A-Server mnt]# touch a.txt b.txt   (创建两个文件)

在这里插入图片描述

回来原来的监控终端进行查看

在这里插入图片描述
(三)、编写触发同步脚本
192.168.1.10:

[root@A-Server ~]# vim /opt/tb.sh
#!/bin/bash
AAA="inotifywait -mrq -e modify,create,attrib,move,delete /mnt"
BBB="rsync -azH --delete --password-file=/etc/server.pass /mnt [email protected]::aaa"
$AAA | while read DIRECTORY EVERT FILE 
do
$BBB
done

在这里插入图片描述
创建密码文件:

[root@A-Server ~]# vim /etc/server.pass
123.com   (同步源的密码)
[root@A-Server ~]# chmod 600 /etc/server.pass  (对用户的数据文件进行权限保护)

在这里插入图片描述
(四)、配置同步源
192.168.1.110:
1、建立/etc/rsyncd.conf 配置文件

[root@B-Client ~]# vim /etc/rsyncd.conf
use chroot = yes
address = 192.168.1.110
port = 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.1.0/24
fake super = yes

[aaa]
path = /mnt
comment = ajbn
read only = no
dont comperss = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
auth users = ajbn
secrets file = /etc/rsync_users.db
[root@B-Client ~]# chmod 777 /mnt/

在这里插入图片描述
2、创建备份账户数据文件

[root@B-Client ~]# vim /etc/rsync_users.db
[root@B-Client ~]# cat /etc/rsync_users.db
ajbn:123.com     (ajbn 用户名; 123.com 密码)
[root@B-Client ~]# chmod 600 /etc/rsync_users.db  (对用户的数据文件进行权限保护)
[root@B-Client ~]# rsync --daemon   (启用)
[root@B-Client ~]# netstat -anpt | grep 873  (查看rsync服务状态)

3、启动rsync 服务程序

[root@B-Client ~]# rsync --daemon     (启用)
[root@B-Client ~]# netstat -anpt | grep 873  (查看端口号)

(五)、在发起端触发脚本
192.168.1.10:

把脚本添加到自运行中执行脚本:
[root@A-Server ~]# echo '/opt/tb.sh' >> /etc/rc.local  
后台运行该脚本:
[root@A-Server ~]# .  /opt/tb.sh &

在这里插入图片描述
在这里插入图片描述
(六)、验证
192.168.1.10 机器上 cd /mnt (创建文件)

[root@A-Server ~]# cd /mnt/  (进入/mnt目录)
[root@A-Server mnt]# ls	     (查看)
a.txt  b.txt                 (这是原有文件)
[root@A-Server mnt]# touch ajbn    (创建一个文件)
[root@A-Server mnt]# touch ajbm    (再创建一个文件)
[root@A-Server mnt]# ls	           (进行查看)
ajbm  ajbn  a.txt  b.txt

在这里插入图片描述
192.168.1.110 机器上 cd /mnt (发现里面数据和192.168.1.10 是同步的)

[root@B-Client ~]# cd /mnt/  (进入/mnt目录)
[root@B-Client mnt]# ls	     (查看)
mnt			     (发现有个子目录)
[root@B-Client mnt]# cd mnt/ (进入子/mnt目录)
[root@B-Client mnt]# ls	     (再次进行查看)
ajbm  ajbn  a.txt  b.txt     (数据已经同步)

猜你喜欢

转载自blog.csdn.net/weixin_46902396/article/details/108758775