文章目录
rsync服务介绍:
rsync是远程增量文件备份工具
简介
rsync英文称为remote synchronizetion,
从软件的名称就可以看出来,
rsync具有可使本地和远程两台主机之间的数据快速复制同步镜像、远程备份的功能
rsync官方地址:rsync
rsync监听端口:873
rsync运行模式:C/S client/server
rsync简称叫做远程同步,可以实现不同主机之间的数据同步,还支持全量和增量
rsync特性
支持拷贝特殊文件,如连接文件、设备等。
可以有排除指定文件或目录同步的功能,相当于打包命令tar的排除功能。
可以做到保持原文件或目录的权限、时间、软硬链接、属主、组等所有属性均不改变 –p。
可以实现增量同步,既只同步发生变化的数据,因此数据传输效率很高(tar-N)。
可以使用rcp、rsh、ssh等方式来配合传输文件(rsync本身不对数据加密)。
可以通过socket(进程方式)传输文件和数据(服务端和客户端)。
支持匿名的活认证(无需系统用户)的进程模式传输,可以实现方便安全的进行数据备份和镜像。
Rsync应用场景
cron + rsync : 跟定时任务进行绑定。
rsync + innotify : 以守护进程的方式同步文件
全量备份:将数据完整的复制一份保留下了
增量备份:备份上一此备份后新增的数据
rsync的传输方式
push推:客户端将数据从本地推送至服务端
pull拉:客户端将数据从服务端拉取到本地
Rsync的传输模式
- 本地传输模式
- 远程传输模式
- 以守护进程的方式
配置rsync源服务器
建立rsyncd.conf配置文件,独立的账号文件
启动rsync的-daemon模式
-
应用示例
用户backuper,允许下行同步
操作的目录为/var/www/html -
配置文件rsyncd.conf
需要手动建立,语法类似于samba配置
认证配置auth users,secrets file 不加则为匿名 -
rsync账号文件
采用“用户名:密码”的记录格式,每行一个用户记录
独立的账号数据,不依赖于系统账号 -
启用rsync服务
通过–daemon肚子提供服务
执行kill $(cat /var/run/rsyncd.pid)关闭rsync服务
rsync命令参数
rsync 选项 原始位置 目标位置
-a 归档模式,递归并保留对象属性,等于-rlptgoD
-v 显示同步过程的详细信息
-z 在传输时进行压缩
-H 保留硬链接文件
-A 保留ACL属性信息
--delete 删除目标位置有,原始位置没有的文件
--checksum 根据对象的校验和来决定是否跳过文件
配置源的两种表示方式
格式1: 用户名@主机地址::共享模块名
格式2: rsync://用户名@主机地址/共享模块名
配置rsync下行同步
环境
master 192.168.3.11 安装rsync
slave 192.168.3.12 安装rsync / inotify-toos-3.14.tar.gz
192.168.3.11master
将master服务器数据备份到slave服务器
关闭防火墙
yum -y install httpd rsync #安装rsync
[root@slave opt]# rz -e
[root@slave opt]# ls
inotify-tools-3.14.tar.gz rh
vim /etc/rsyncd.conf #进入配置
uid = root #匿名用户
gid = root #组
use chroot = yes #开启禁锢家目录
address = 192.168.3.11 #配置监听端口
port 873 #端口号
log file = /var/log/rsyncd.log #日志文件路径
pid file = /var/run/rsyncd.pid #pid文件路径
hosts allow = 192.168.3.0/24 #允许地址段访问
[wwwroot] #共享模块名
path = /var/www/html #共享文件路径
comment = Document Root of www.wxb.com #定义名称
read only = yes #只读权限
dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z # 同步时不再压缩的文件类型,这些本为压缩文件
auth users = backuper # 授权用户,多个账号以空格隔开
secrets file = /etc/rsyncd_users.db #存放账号信息的数据文件,一行一个
:wq
vim /etc/rsyncd_users.db
kiki:123123
chmod 600 /etc/rsyncd_users.db #官方授权600,不然报错
rsync --daemon #开启服务
netstat -natp | grep rsync #检测端口服务是否启动
cd /var/www/html #切换到共享目录下
touch aaa.html bbb.html #创建两个文本
ls
[root@master ~]# vim /etc/rsyncd.conf
backuper:123123
:wq
[root@master ~]# 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 0.0.0.0:873 0.0.0.0:* LISTEN 109225/rsync
tcp6 0 0 :::873 :::* LISTEN 109225/rsync
[root@master ~]# cd /var/www/html/
[root@master html]# touch aaa.html bbb.html
[root@master html]# ll -ld /var/www/html
drwxr-xr-x. 2 root root 38 11月 16 14:28 /var/www/html
[root@master html]# ls
aaa.html bbb.html
slave 192.168.3.12
systemctl stop firewalld.service
setenforce 0
[root@slave ~]# yum -y install rsync #安装rsync
[root@slave ~]# cd /opt/
[root@slave opt]# mkdir hhh
[root@localhost opt]# chmod 777 hhh
[root@localhost opt]# vim /etc/server.pass
123123
:wq
[root@localhost opt]# chmod 600 /etc/server.pass
[root@localhost opt]# rsync -az --delete --password-file=/etc/server.pass [email protected]::wwwroot /opt/hhh
[root@localhost opt]# ls hhh
aaa.html bbb.html
发起端配置 rsync + inotify
使用 inotiify 通知接口,可以用来监控文件系统的各种变化情况,如文件存取、删除、移动、修改等。利用这一机制,可以非常方便地实现文件异动告警、增量备份,并针对目录或文件的变化及时作出响应。
将 inotify 机制与 rsync 工具相结合,可以实现触发式备份(实时同步),即只要原始位置的文档发生变化,则立即启动增量备份操作;否则处于静默等待状态。这样,就避免了按固定周期备份时存在的延迟性、周期过密等问题。
因为 inotify 通知机制由 Linux 内核提供,因此主要做本机监控,在触发式备份中应用时更适合上行同步。
配置 Rsync+Inotify 实时同步
master 192.168.3.11
安装rsync
[root@localhost etc]# vim /etc/rsyncd.conf
read only = no
:wq
[root@localhost etc]# kill `cat /var/run/rsyncd.pid`
[root@localhost etc]# netstat -natp |grep rsync
[root@localhost etc]# rsync --daemon
[root@localhost etc]# netstat -natp |grep rsync
tcp 0 0 192.168.3.11:873 0.0.0.0:* LISTEN 104556/rsync
[root@localhost etc]# chmod 777 /var/www/html/
[root@localhost opt]# cat /proc/sys/fs/inotify/max_queued_events
[root@localhost opt]# cat /proc/sys/fs/inotify/max_user_instances
[root@localhost opt]# cat /proc/sys/fs/inotify/max_user_watches
[root@localhost opt]# vim /etc/sysctl.conf
fs.inotify.max_queued_events = 32768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
:wq
[root@localhost opt]# sysctl -p
fs.inotify.max_queued_events = 32768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
从服务器192.168.3.12
安装rsync 和辅助工具inotify
yum -y install gcc gcc-c++ #安装编译环境,centos7默认安装
#放入安装包
tar zxvf inotify-tools-3.14.tar.gz -C /opt
cd /opt/inotify-tools-3.14/ #切到压缩好的目录下
./configure #安装软件,使用系统默认路径/usr/local
make && make install #编译安装
在另一个终端编写触发式同步脚本(backuper)
[root@localhost opt]# vim /opt/inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e create,delete,move,modify,attrib /opt/hhh/"
RSYNC_CMD="rsync -az --delete --password-file=/etc/server.pass /opt/hhh/ [email protected]::wwwroot"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ] ; then
$RSYNC_CMD
fi
done
:wq
#上述脚本用来检测本机 /var/www/html 目录的变动情况,一旦有更新触发 rsync 同步操作,上传备份至服务器 192.168.10.30 的 rsync 共享目录下。
#触发式上行同步的验证过程如下:
在本机运行 /opt/inotify_rsync.sh 脚本程序
[root@localhost opt]# chmod +x inotify.sh
[root@localhost opt]# ./inotify.sh & #后台执行脚本
[1] 102208
[root@localhost opt]# cd hhh/
[root@localhost hhh]# ls
aaa.html bbb.html
[root@localhost hhh]# touch ccc.html #新建一个
[root@localhost hhh]# rm -rf aaa.html #删除一个文本
[root@localhost hhh]# ls
bbb.html ccc.html
- 一定要看清目录名,下面有错/haha应该为hhh,写错了
Master(192.168.3.11)验证
同步成功