rsync ( Remote sync,远程同步)
是一个开源的快速备份工具,可以在不同主机之间镜像同步整个目录树,支持增量备份,并保持链接和权限,且采用优化的同步算法,传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用。
在远程同步任务中,负责发起rsync同步操作的客户机称为发起端,而负责响应来自客户机的rsync同步操作的服务器称为同步源。在同步过程中,同步源负责提供文件的原始位置,发起端应对该位置具有读取权限。
Rsync是Linux系统下的数据镜像备份工具,使用快速增量备份工具Remote sync 可以远程同步,可以在不同主机之间进行同步,可实现全量备份与增量备份,保持链接和权限,且采用优化的同步算法,传输前执行压缩,因此非常适合用于架构集中式备份或异地备份等应用。同时Rsync支持本地复制,或者与其他SSH、 rsync主机同步。
Rsync官方网站:https://rsync.samba.org/
同步方式
(1)完整备份:每次备份都是从备份源将所有的文件或目录备份到目的地。
(2)差量备份:备份上次完全备份以后有变化的数据(他针对的上次的完全备份,他备份过程中不清除存档属性)。
(3)增量备份:备份上次备份以后有变化的数据(他才不管是那种类型的备份,有变化的数据就备份,他会清除存档属性)
同步过程

选项 含义
-r 递归模式,包含目录及子目录中的所有文件
-l 对于符号链接文件仍然复制为符号链接文件
-v 显示同步过程的详细(verbose)信息
-z 在传输文件时进行压缩( compress )
-a 归档模式,保留文件的权限、属性等信息,等同于组合选项"-r lptgoD"
-p 保留文件的权限标记
-t 保留文件的时间标记
-g 保留文件的属组标记(仅超级用户使用)
-o 保留文件的属主标记(仅超级用户使用)
-H 保留硬连接文件
-A 保留ACL属性信息
-D 保留设备文件及其他特殊文件
-delete 删除目标位置有而原始位置没有的文件
-checksum 根据校验和(而不是文件天小、修改时间)来决定是否跳过文件
实验操作:
主机 备注
192.168.32.129 客户端,发起端
192.168.32.130 服务端,同步源
powershell
客户端:192.168.32.129
[root@localhost ~]# systemctl stop firewalld.service && setenforce 0
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
[root@localhost opt]# rpm -q rsync
若没有rsync,则yum安装
[root@localhost opt]# yum install -y rsync
[root@localhost opt]# rpm -q rsync
rsync-3.1.2-12.el7_9.x86_64
[root@localhost opt]# cd /home/
[root@localhost home]# ls
[root@localhost home]# mkdir -p /home/student && echo "this is school" > /home/student/1.txt
[root@localhost home]# ls /home/student/1.txt
/home/student/1.txt
[root@localhost home]# cat /home/student/1.txt
this is school
同步/home/student/下的文件到/opt/
[root@localhost home]# rsync -avz /home/student/ /opt/
sending incremental file list
./
1.txt
sent 121 bytes received 38 bytes 318.00 bytes/sec
total size is 15 speedup is 0.09
[root@localhost home]# cd /opt/
[root@localhost opt]# ls
1. txt
[root@localhost opt]# mkdir
student
[root@localhost opt]# ls
1. txt student
服务端:192.168.32.130
[root@localhost ~]# systemctl stop firewalld.service && setenforce 0
[root@localhost opt]# yum install -y rsync
[root@localhost opt]# rpm -q rsync
rsync-3.1.2-12.el7_9.x86_64
[root@localhost opt]# rpm -qc rsync
/etc/rsyncd.conf
/etc/sysconfig/rsyncd
### 备份好方便回滚
[root@localhost ~]# cp /etc/rsyncd.conf /etc/rsyncd.conf_bak
### 配置
[root@localhost ~]# vim /etc/rsyncd.conf

powershell
[root@localhost ~]# mkdir -p /var/www/html
[root@localhost ~]# ls -ld /var/www/html
drwxr-xr-x. 2 root root 6 12月 11 10:54 /var/www/html
[root@localhost ~]# chmod +r /var/www/html/
[root@localhost ~]# vim /etc/rsyncd_users.db
##给个权限,只有自己的用户访问,别的用户访问不了
[root@localhost ~]# cat /etc/rsyncd_users.db
zhangsan:123456
## 重新加载rsync
[root@localhost ~]# rsync --daemon
[root@localhost ~]# netstat -antp|grep rsync
tcp 0 0 192.168.32.130:873 0.0.0.0:* LISTEN 59812/rsync
[root@localhost ~]# cd /var/www/html
[root@localhost html]# cp /etc/passwd /etc/shadow ./
## 拷贝文件到当前的目录中
[root@localhost html]# ls
passwd shadow
[root@localhost html]#
## myhtml是/etc/rsyncd.conf 配置文件下指定的参数目录
下载服务端备份源中的数据,使用配置源的第二种方式进行备份
powershell
(rsync -avz zhangsan@192.168.32.130::myhtml /opt/ )
客户端:192.168.32.129
[root@localhost opt]# rsync -avz zhangsan@192.168.32.130::myhtml /opt/
Password:
receiving incremental file list
shadow
sent 46 bytes received 374 bytes 120.00 bytes/sec
total size is 1,361 speedup is 3.24
## 拷贝文件到当前的目录中
[root@localhost opt]# ls
1.txt passwd shadow student
增量数据备份
powershell
服务端:192.168.32.130
## 修改192.168.32.130配置文件下的password ,新增一行,表示增量数据
[root@localhost html]# vim /var/www/html/passwd

powershell
客户端:192.168.32.129
## 此时服务端的数据已经变动了,更新了里面修改过后的文件,密码为123456上面服务端中的创建的密码
[root@localhost ~]# rsync -avz zhangsan@192.168.32.130::myhtml /opt/student/
Password:
receiving incremental file list
./
passwd
shadow
sent 65 bytes received 768 bytes 238.00 bytes/sec
total size is 1,395 speedup is 1.67
[root@localhost opt]# vim /opt/student/passwd
## 可以看到文件已经和服务端修改过的一样的了

配置免交互密码
powershell
客户端:192.168.32.129
[root@localhost opt]# echo "123456" > /etc/server.pass
[root@localhost opt]# chmod 600 /etc/server.pass
服务端:192.168.32.130
## 由于本地没有此文件需要先自行创建一个文件数据,里面的数据可以随便输入
[root@localhost html]# vim /etc/yum.repos.d/local.repo
## 把创建好的数据拷贝进html目录中
[root@localhost html]# cp /etc/yum.repos.d/local.repo /var/www/html/
## 备份源中已有新增的数据

powershell
客户端:192.168.32.129
[root@localhost opt]# rsync -avz --password-file=/etc/server.pass zhangsan@192.168.32.130::myhtml /opt/student/
## 此时免交互密码设置成功,可直接下载文件

可以看到客户端已经有同步新增数据文件了
使用inotify工具进行实时同步(上传数据)
发起端(客户端)配置rsync+inotify
使用inotify通知接口,可以用来监控文什系统的各种变化情况,如文件存取、删除、移动、修改等。利用这一机制,可以非常方便地实现文件异动告警、增量备份,并针对目录或文件的变化及时作出响应。
将inotify机制与rsync工具相结合,可以实现触发式备份(实时同步),即只要原始位置的文档发生变化,则立即启动增量备份操作;否则处于静默等待状态。这样,就避免了按固定周期备份时存在的延迟性、周期过密等问题。
因为 inotify 通知机制出 Linux 内核提供,因此主要做木机监控,在触发式备份中应用时更适合上行同步。
powershell
服务端:192.168.32.130
[root@localhost html]# vim /etc/rsyncd.conf

powershell
[root@localhost html]# cat /var/run/rsyncd.pid
59812
## 杀掉进程
[root@localhost html]# kill $(cat /var/run/rsyncd.pid)
[root@localhost html]# netstat -antp|grep rsync
[root@localhost html]# rsync --daemon
[root@localhost html]# netstat -antp|grep rsync
tcp 0 0 192.168.32.130:873 0.0.0.0:* LISTEN 77936/rsync
[root@localhost html]# chmod 777 /var/www/html/
[root@localhost html]# ls /var/www/html/
local.repo passwd shadow
客户端:192.168.32.129
## inotify 的默认内核参数
[root@localhost opt]# cat /proc/sys/fs/inotify/max_queued_events
16384
## 每个用户所能创建的Inotify实例的上限
[root@localhost opt]# cat /proc/sys/fs/inotify/max_user_instances
128
## 每个inotify实例相关联的watches的上限,即每个inotify实例可监控的最大目录、文件数量
[root@localhost opt]# cat /proc/sys/fs/inotify/max_user_watches
8192
[root@localhost opt]# vim /etc/sysctl.conf
[root@localhost opt]# sysctl -p
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
把 inotify-tools-3.14.tar.gz 放入进 opt 目录中
下载地址:https://sourceforge.net/projects/inotify-tools/files/inotify-tools-3.14.tar.gz/download

powershell
## 安装依赖包
[root@localhost opt]# yum install -y gcc gcc-c++ make
[root@localhost opt]# tar -zxvf inotify-tools-3.13.tar.gz
[root@localhost opt]# cd inotify-tools-3.13

powershell
[root@localhost inotify-tools-3.13]# ./configure && make && make install
使用命令进行监控(inotifywait -mrq -e modify,create,move,delete )
选项 含义
-e 用来指定要监控哪些事件
-m 表示持续监控
-r 表示递归整个目录
-q 简化输出信息
powershell
客户端:192.168.32.129
## 表示持续监控/opt/student/目录下的事件
[root@localhost opt]# inotifywait -mrq -e modify,create,move,delete /opt/student/
## 另开一个客户端192.168.32.129窗口,模拟删除操作
[root@localhost ~]# cd /opt/student/
[root@localhost student]# ls
local.repo passwd shadow
[root@localhost student]# rm -rf passwd
## 查看实时监控窗口时间输出
## 日志实时记录打印删除事件

使用Shell脚本进行实时监控
powershell
## 监控端在什么地方,脚本也要放在相同地方
[root@localhost opt]# vim inotify.sh
## --password-file=/etc/server.pass 指定密码文件
## zhangsan@192.168.32.130 指定用户,在/etc/rsyncd.conf配置文件
## /opt/student/ 客户端要同步给远程服务端的目录
## myhtml 远程服务端要同步的目录
[root@localhost opt]# cat inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,move,delete /opt/student/"
RSYNC_CMD="sudo rsync -arvH --delete --password-file=/etc/server.pass /opt/student/ zhangsan@192.168.32.130::myhtml"
# 使用while、read持续获取监控结果,根据结果可以作进一步判断是否读取到输出的监控记录
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
# while判断是否接收到监控记录
do
if [ $(pgrep rsync | wc -l) -le 0 ] ; then
#如果rsync未在执行,则立即启动
sudo $RSYNC_CMD
fi
done

powershell
[root@localhost opt]# chmod +x /opt/inotify.sh
[root@localhost opt]# chmod 777 /opt/student/
[root@localhost opt]# chmod +x /etc/rc.d/rc.local
[root@localhost opt]# echo '/opt/inotify.sh' >> /etc/rc.d/rc.local
[root@localhost opt]# cat /etc/rc.d/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
/opt/inotify.sh
## 启动脚本
[root@localhost opt]# sh /opt/inotify.sh

powershell
## 测试
## 复制文件到监控/opt/student/目录下
[root@localhost student]# cp /etc/yum.repos.d/* /opt/student/
## 可以看到脚本实时记录输出日志
## 查看客户端192.168.32.129/opt/student/

powershell
## 查看监控脚本inotify.sh输出日志

powershell
## 查看远程同步目录服务端192.168.32.130备份同步目录是否自动同步

powershell
## 可以看到已经触发inotify.sh脚本自动同步了
生产环境同步实验(fastdfs)
环境
客户端:172.16.153.201(数据源目录)
服务端:172.16.153.202(远程同步目录)
客户端:172.16.153.201(数据源目录)
powershell
[root@201-app storage]# systemctl stop firewalld.service && setenforce 0
[root@201-app storage]# rpm -q rsync
rsync-3.0.9-18.el7.x86_64
若没有rsync,则yum安装
[root@localhost opt]# yum install -y rsync
[root@201-app storage]# rpm -q rsync
rsync-3.0.9-18.el7.x86_64
## 以上都有则正常
服务端:172.16.153.202(远程同步目录)
powershell
[root@202-app data]# systemctl stop firewalld.service && setenforce 0
[root@202-app data]# rpm -q rsync
rsync-3.0.9-18.el7.x86_64
若没有rsync,则yum安装
[root@localhost opt]# yum install -y rsync
[root@202-app data]# rpm -q rsync
rsync-3.0.9-18.el7.x86_64
## 以上都有则正常
[root@202-app data]# rpm -qc rsync
/etc/rsyncd.conf
/etc/sysconfig/rsyncd
### 备份好方便回滚
[root@202-app data]# cp /etc/rsyncd.conf /etc/rsyncd.conf_bak
### 配置
[root@202-app data]# vim /etc/rsyncd.conf

powershell
[root@202-app data]# mkdir -p /var/www/html
[root@202-app data]# ls -ld /var/www/html
drwxr-xr-x. 2 root root 6 12月 12 11:00 /var/www/html
[root@202-app data]# chmod +r /var/www/html/
[root@202-app data]# vim /etc/rsyncd_users.db
##给个权限,只有自己的用户访问,别的用户访问不了
[root@localhost ~]# cat /etc/rsyncd_users.db
zhangsan:123456
## 给/etc/rsyncd_users.db 密码配置文件授权
[root@202-app html]# chmod 600 /etc/rsyncd_users.db

powershell
[root@202-app data]# kill $(cat /var/run/rsyncd.pid)
[root@202-app data]# rsync --daemon
[root@202-app data]# netstat -antp|grep rsync
tcp 0 0 172.16.153.202:873 0.0.0.0:* LISTEN 9566/rsync
## 服务端rsync服务正常启动完成
客户端配置免交互密码(172.16.153.201)
powershell
[root@201-app storage]# echo "123456" > /etc/server.pass
[root@201-app storage]# chmod 600 /etc/server.pass
## 客户端先测试下免密同步是否成功

powershell
## 客户端:172.16.153.201
## 启动客户端rsync 服务
[root@201-app data]# rsync --daemon
[root@201-app data]# netstat -antp|grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 17240/rsync
tcp6 0 0 :::873 :::* LISTEN 17240/rsync
## 测试
[root@201-app data]# rsync -avz --password-file=/etc/server.pass zhangsan@172.16.153.202::myhtml /data/
使用inotify工具进行实时同步(上传数据)
发起端(客户端)配置rsync+inotify
使用inotify通知接口,可以用来监控文什系统的各种变化情况,如文件存取、删除、移动、修改等。利用这一机制,可以非常方便地实现文件异动告警、增量备份,并针对目录或文件的变化及时作出响应。
将inotify机制与rsync工具相结合,可以实现触发式备份(实时同步),即只要原始位置的文档发生变化,则立即启动增量备份操作;否则处于静默等待状态。这样,就避免了按固定周期备份时存在的延迟性、周期过密等问题。
因为 inotify 通知机制出 Linux 内核提供,因此主要做木机监控,在触发式备份中应用时更适合上行同步。
powershell
服务端:172.16.153.202
[root@202-app html]# vim /etc/rsyncd.conf
## 修改此处为no

powershell
[root@202-app html]# kill $(cat /var/run/rsyncd.pid)
[root@202-app html]# netstat -antp|grep rsync
[root@202-app html]# rsync --daemon
[root@202-app html]# netstat -antp|grep rsync
tcp 0 0 172.16.153.202:873 0.0.0.0:* LISTEN 22135/rsync
[root@202-app html]# chmod 777 /var/www/html/
[root@202-app html]# ls /var/www/html/
111 a.txt test
powershell
客户端:172.16.153.201
## inotify 的默认内核参数
[root@201-app data]# cat /proc/sys/fs/inotify/max_queued_events
16384
## 每个用户所能创建的Inotify实例的上限
[root@201-app data]# cat /proc/sys/fs/inotify/max_user_instances
1024
## 每个inotify实例相关联的watches的上限,即每个inotify实例可监控的最大目录、文件数量
[root@201-app data]# cat /proc/sys/fs/inotify/max_user_watches
1048576
[root@201-app data]# vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

powershell
[root@201-app data]# sysctl -p

客户端安装inotify-tools-3.14.tar.gz
powershell
[root@201-app data]# yum install -y gcc gcc-c++ make
[root@201-app data]# tar -zxvf inotify-tools-3.13.tar.gz
[root@201-app data]# cd inotify-tools-3.13
[root@201-app inotify-tools-3.13]# ./configure && make && make install

使用命令进行监控(inotifywait -mrq -e modify,create,move,delete )选项 含义
-e 用来指定要监控哪些事件
-m 表示持续监控
-r 表示递归整个目录
-q 简化输出信息
powershell
客户端:172.16.153.201
## 表示持续监控/data/目录下的事件
[root@201-app inotify-tools-3.13]# inotifywait -mrq -e modify,create,move,delete /data/
## 有日志输出就可以了

客户端使用Shell脚本进行实时监控
powershell
客户端:172.16.153.201
## 监控端在什么地方,脚本也要放在相同地方
[root@201-app data]# vim inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,move,delete /data/"
RSYNC_CMD="sudo rsync -arvH --password-file=/etc/server.pass /data/ zhangsan@172.16.153.202::myhtml"
# 使用while、read持续获取监控结果,根据结果可以作进一步判断是否读取到输出的监控记录
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
# while判断是否接收到监控记录
do
sudo $RSYNC_CMD
done
[root@201-app data]# chmod +x /data/inotify.sh
[root@201-app data]# chmod -R 777 /data/
[root@201-app data]# chmod +x /etc/rc.d/rc.local
[root@201-app data]# echo '/data/inotify.sh' >> /etc/rc.d/rc.local

powershell
客户端:172.16.153.201
服务端:172.16.153.202

powershell
实验成功
## 附件同步脚本:
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,move,delete /home/fastdfs/storage/"
RSYNC_CMD="sudo rsync -arvH --delete --password-file=/etc/server.pass /home/fastdfs/storage/ zhangsan@172.16.153.202::myhtml"
# 使用while、read持续获取监控结果,根据结果可以作进一步判断是否读取到输出的监控记录
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
# while判断是否接收到监控记录
do
sudo $RSYNC_CMD
done