目录
Linux Rsync 下行同步 和 Inotify 同步部署
Linux Rsync 下行同步 和 Inotify 同步部署
一、Rsync
Rsync 简介
一款快速增量备份工具
Remote Sync,远程同步
支持本地复制,或者与其他SSH、rsync主机同步
官方网站:rsync
Rsync 服务器
rsync是一款开源的、快速的、多功能的、可实现全量及增量的本地或远程数据同步备份的优秀工具。并且可以不进行改变原有数据的属性信息,实现数据的备份迁移特性
rsync软件适用于unix/linux/windows等多种操作系统平台
rsync是一个快速和非常同样的文件复制工具。它能本能复制,远程复制,或者远程守护进程方式复制,它提供了大量的参数来控制其行为的各个方面,并且允许非常灵活的方式来实现文件的传输复制
以其delta-transfer算法闻名
rsync监听端口:873
rsync运行模式:C/S
同步方式
全量备份
原有的数据全部传送
把原来的文件和新的文件一起统一传送
全量复制,效率低
增量备份
在传输数据之前通过一些算法通过你有的数据和我有的数据进行对比,把不一样的数据通过网络传输
增量复制,效率高
A服务器同步B服务器的数据,B服务器就是备份源
反过来,B服务器同步A服务器的数据,那么A服务器就是备份源
Rsync 命令
rsync [选项] 原始位置 目标位置
常用选项 说明
-r 递归模式,包含目录及子目录中的所有文件
-l 对于符号链接文件仍然复制为符号链接文件
-v 显示同步过程的详细信息
-z 在传输文件时进行压缩
-a 归档模式,递归并保留对象属性,等同于-rlptgoD
-p 保留文件的权限标记
-t 保留文件的时间标记
-g 保留文件的属组标记(仅超级用户使用)
-o 保留文件的属主标记(仅超级用户使用)
-H 保留硬链接文件
-A 保留ACL属性信息
-D 保留设备文件及其他特殊文件
- -delete 删除目标位置有而原始位置没有的文件
- -checksum 根据对象的校验和来决定是否跳过文件
配置源的两种方法
格式一: 用户名@主机地址::共享模块名
格式二: rsync://用户名@主机地址/共享模块名
二、本地复制操作(下行复制)
rsync:192.168.118.140
client:192.168.118.150
Master:192.168.118.140
[root@master ~]# systemctl stop firewalld
[root@master ~]# setenforce 0
[root@master ~]# rpm -q rsync
rsync-3.0.9-18.el7.x86_64
[root@master ~]# yum install -y httpd rsync
[root@master ~]# vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes
address = 192.168.118.140
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.118.0/24
[yh]
path = /var/www/html
comment = Document Root of www.yh.com
read only = yes
dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z
auth users = yh
secrets file = /etc/rsyncd_users.db
[root@master html]# vim /etc/rsyncd_users.db
[root@master ~]# chmod 600 /etc/rsyncd_users.db
[root@master ~]# rsync --daemon
[root@master ~]# netstat -natp |grep rsync
tcp 0 0 192.168.118.140:873 0.0.0.0:* LISTEN 30618/rsync
[root@master ~]# cd /var/www/html/
[root@master html]# touch a.html b.html
Client:192.168.118.150
[root@slave1 ~]# systemctl stop firewalld
[root@slave1 ~]# setenforce 0
[root@slave1 ~]# yum install -y rsync
[root@slave1 ~]# cd /opt/
[root@slave1 opt]# mkdir yh
[root@slave1 opt]# chmod 777 yh
[root@slave1 opt]# vim /etc/server.pass
[root@slave1 opt]# chmod 600 /etc/server.pass
[root@slave1 opt]# rsync -az --delete --password-file=/etc/server.pass yh@192.168.118.140::yh /opt/yh
##rsync使用yh用户,密钥文件/etc/server.pass将192.168.118.140的共享模块文件进行压缩,归档同步至当前服务器的/opt/yh 目录下并删除差异内容
[root@slave1 opt]# ls yh/
a.html b.html ##查看yh目录,下行同步成功
三、Inotify
inotify是一种文件系统的变化通知机制。 如文件增加、删除等事件可以立刻让用户态得知
利用这一机制,可以非常方便地实现文件异动告警、增量备份,并针对目录或文件的变化及时作出响应
将inotify机制与rsync相结合,可以实现触发式备份(实时同步),即只要原始位置的文档发生变化,则立即启动增量备份操作;否则处于静默等待状态。这样,就避免了按固定周期备份时存在的延迟性、周期过密等问题
inotifywait常用选项 说明
-e 用来指定要监控哪些事情
-m 表示持续监控
-r 表示递归整个目录
-q 简化输出信息
四、Resync + Inotify 实施同步操作
Master:192.168.118.140
[root@master ~]# vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes
address = 192.168.118.140
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.118.0/24
[yh]
path = /var/www/html
comment = Document Root of www.yh.com
read only = no ##yes修改为no
dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z
auth users = yh
secrets file = /etc/rsyncd_users.db
[root@master ~]# vim /etc/rsyncd.conf
[root@master ~]# kill `cat /var/run/rsyncd.pid`
[root@master ~]# netstat -natp |grep rsync
[root@master ~]# rsync --daemon
[root@master ~]# netstat -natp |grep rsync
tcp 0 0 192.168.118.140:873 0.0.0.0:* LISTEN 31606/rsync
[root@master ~]# chmod 777 /var/www/html/
Client:192.168.118.150
[root@slave1 opt]# cat /proc/sys/fs/inotify/max_queued_events
16384
[root@slave1 opt]# cat /proc/sys/fs/inotify/max_user_instances
128
[root@slave1 opt]# cat /proc/sys/fs/inotify/max_user_watches
8192
[root@slave1 opt]# vim /etc/sysctl.conf ##优化inotify内核参数
fs.inotify.max_queued_events = 32768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
##加大三个参数的值
[root@slave1 opt]# sysctl -p ##加载内容参数配置文件
fs.inotify.max_queued_events = 32768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@slave1 opt]# yum install -y gcc gcc-c++
[root@slave1 opt]# rz -E
rz waiting to receive.
[root@slave1 opt]# tar zxf inotify-tools-3.14.tar.gz
[root@slave1 opt]# cd inotify-tools-3.14/
[root@slave1 inotify-tools-3.14]# ./configure
[root@slave1 inotify-tools-3.14]# make && make install -j4
[root@slave1 opt]# vim /opt/inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e create,delete,move,modify,attrib /opt/yh/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /opt/yh yh@192.168.118.140::yh"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ] ; then
$RSYNC_CMD
fi
done
[root@slave1 opt]# chmod +x inotify.sh
[root@slave1 opt]# ./inotify.sh &
[root@slave1 opt]# cd yh/
[root@slave1 yh]# ls
a.html b.html
[root@slave1 yh]# touch c.html
[root@slave1 yh]# ls
a.html b.html c.html
[root@slave1 yh]# rm -rf a.html
[root@slave1 yh]# ls
b.html c.html
Master:192.168.118.140 验证
[root@master ~]# cd /var/www/html/yh/
b.html c.html
之前 slave 同步时会出现报错,是因为是以匿名用户身份登陆的
[root@master yh]# vim /etc/rsyncd.conf
uid = root ##改为root
gid = root
use chroot = yes
address = 192.168.118.140
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.118.0/24
[yh]
path = /var/www/html
comment = Document Root of www.yh.com
read only = no
dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z
auth users = yh
secrets file = /etc/rsyncd_users.db
[root@master yh]# kill `cat /var/run/rsyncd.pid`
[root@master yh]# rsync --daemon
[root@master yh]# ls
b.html c.html
Client:192.168.118.150
Master:192.168.118.140