返回顶部

rsync远程同步

rsync远程同步

一、rsync同步简介

1. 关于rsync


rsync(Remote Sync,远程同步)是一款开源的快速增量备份工具,可以在不同主机之间镜像同步整个目录树,支持增量备份,并保持链接和权限,且采用优化的同步算法,传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用。
支持本地复制,或者与其他SSH、rsync主机同步
官方网站:http://rsync.samba.org

2. rsync同步源(备份源)


指备份操作的远程服务器,也称为备份源。
在远程同步任务中,负责发起rsync同步操作的客户机称为发起端,而负责响应来自客户机的rsync同步操作的服务器称为同步源。在同步过程中,同步源负责提供文件的原始位置,发起端应对该位置具有读取权限。

二、配置rsync备份源

1. 关闭防火墙

[root@rsync ~]# systemctl stop firewalld
[root@rsync ~]# systemctl disable firewalld
[root@rsync ~]# setenforce 0
setenforce: SELinux is disabled

2. 查看rsync是否已安装,一般系统已默认安装rsync

[root@rsync ~]# rpm -q rsync
rsync-3.0.9-18.el7.x86_64

3. 建立/etc/rsync.conf配置文件

[root@rsync ~]# vim /etc/rsyncd.conf

##添加以下配置
uid = nobody
gid = nobody
##禁锢在源目录
use chroot = yes
##监听地址
address = 192.168.122.10
##监听端口tcp/udp873,可通过"cat /etc/services | grep rsync"查看
port 873
##日志文件位置
log file = /var/log/rsyncd.log
##存放进程ID的文件位置
pid file = /var/run/rsyncd.pid
##允许访问的客户机地址
hosts allow = 192.168.122.0/24

##共享模块
##共享模块名称
[rsync]
##源目录的实际路径
path = /var/www/html
comment = Document Root of www.test.com
##是否为只读
read only = yes
##同步时不再压缩的文件类型
dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z
##授权账户,多个账号以空格分隔
auth users = backuper
##存放账户信息的数据文件
secrets file = /etc/rsyncd_users.db

如采用匿名的方式,只要将其中的“auth users”和“secrets file”配置去掉即可。

4. 为备份账户创建数据文件

[root@rsync ~]# vim /etc/rsyncd_users.db

backuper:123456		##无须建立同名系统用户

[root@rsync ~]# chmod 600 /etc/rsyncd_users.db 

5. 保证所有用户对源目录/var/www/html都有读取权限

[root@rsync ~]# chmod +r /var/www/html
[root@rsync ~]# ls -ld /var/www/html
drwxr-xr-x. 2 root root 32 8月   1 02:43 /var/www/html

6. 启动rsync服务程序

[root@rsync ~]# rsync --daemon
#启动rsync服务,以独立监听服务的方式(守护进程)运行
[root@rsync ~]# netstat -natp | grep rsync
tcp        0      0 192.168.122.10:873      0.0.0.0:*               LISTEN      3709/rsync          

7. 关闭rsync服务

[root@rsync ~]# kill $(cat /var/run/rsyncd.pid)
[root@rsync ~]# rm -rf /var/run/rsyncd.pid
[root@rsync ~]# netstat -natp | grep rsync

8. 编写测试网页

[root@rsync ~]# echo "this is rsync test!" > /var/www/html/test.html 

三、rsync命令基本用法

1. 基本格式

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

2. 常用选项

常用选项 说明
-r 递归模式,包含目录及子目录中的所有文件。
-l 对于符号链接文件仍然复制为符号链接文件。
-v 显示同步过程的详细(verbose)信息。
-z 在传输文件时进行压缩(compress)。
-a 归档模式,保留文件的权限、属性等信息,等同于组合选项“-rlptgoD”
-p 保留文件的权限标记。
-t 保留文件的时间标记。
-g 保留文件的属组标记(仅超级用户使用)。
-o 保留文件的属主标记(仅超级用户使用)。
-H 保留硬链接文件。
-A 保留ACL属性信息。
-D 保留设备文件及其他特殊文件。
--delete 删除目标位置有而原始位置没有的文件。
--checksum

四、配置发起端

1. 关闭防火墙

[root@backuper ~]# systemctl stop firewalld
[root@backuper ~]# systemctl disable firewalld
[root@backuper ~]# setenforce 0
setenforce: SELinux is disabled

2. 查看rsync是否已安装,一般系统已默认安装rsync

[root@backuper ~]# rpm -q rsync
rsync-3.0.9-18.el7.x86_64

3. 将指定的资源下载到本地/opt目录下进行备份

格式一:

[root@backuper ~]# rsync -avz backuper@192.168.122.10::rsync /var/www/html
Password: 
receiving incremental file list
./
test.html

sent 81 bytes  received 174 bytes  102.00 bytes/sec
total size is 20  speedup is 0.08
[root@backuper ~]# systemctl start httpd
[root@backuper ~]# curl http://192.168.122.11/test.html
this is rsync test!

格式二:

[root@backuper ~]# rm -rf /var/www/html/*
[root@backuper ~]# rsync -avz rsync://backuper@192.168.122.10/rsync /var/www/html
Password: 
receiving incremental file list
./
test.html

sent 81 bytes  received 174 bytes  72.86 bytes/sec
total size is 20  speedup is 0.08
[root@backuper ~]# curl http://192.168.122.11/test.html
this is rsync test!

4. 免交互格式配置

[root@backuper ~]# echo "123456" > /etc/server.pass
[root@backuper ~]# chmod 600 /etc/server.pass 
[root@backuper ~]# rm -rf /var/www/html/*
[root@backuper ~]# rsync -avz --password-file=/etc/server.pass backuper@192.168.122.10::rsync /var/www/html
receiving incremental file list
./
test.html

sent 81 bytes  received 174 bytes  510.00 bytes/sec
total size is 20  speedup is 0.08
[root@backuper ~]# curl http://192.168.122.11/test.html
this is rsync test!

5. 计划性定时同步

[root@backuper ~]# crontab -e

0 1 * * * /usr/bin/rsync/ -az --delete --password-file=/etc/server.pass backuper@192.168.122.10::
rsync /var/www/html

[root@backuper ~]# systemctl restart crond
[root@backuper ~]# systemctl enable crond

五、rsync实时同步

1. 定期同步的不足

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

2. 实时同步的优点

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

3. Linux内核的inotify机制

从版本2.6.13开始提供
可以监控文件系统的变动情况,并作出通知响应
辅助软件:inotify-tools
https://img2020.cnblogs.com/blog/2387990/202109/2387990-20210923001203004-668255826.png

4. 发起端配置rsync+inotify

使用inotiify通知接口,可以用来监控文件系统的各种变化情况,如文件存取、删除、移动、修改等。利用这一机制,可以非常方便地实现文件异动告警、增量备份,并针对目录或文件的变化及时作出响应。
将inotify机制与rsync工具向结合,可以实现触发式备份(实时同步),即只要原始位置的文档发生变化,则立即启动增量备份操作;否则处于静默等待状态。这样,就避免了按固定周期备份时存在的延迟性、周期过密等问题。
因为inotify通知机制由Linux内核提供,因此主要做本机监控,在触发式备份中应用时更适合上行同步。

4.1 修改rsync源服务器配置文件

[root@rsync ~]# vim /etc/rsyncd.conf 

##修改read only为no
read only = no

[root@rsync ~]# kill $(cat /var/run/rsyncd.pid)
[root@rsync ~]# rm -rf /var/run/rsyncd.pid
[root@rsync ~]# rsync --daemon
[root@rsync ~]# netstat -natp | grep rsync
tcp        0      0 192.168.122.10:873      0.0.0.0:*               LISTEN      1694/rsync   
       
[root@rsync ~]# chmod 777 /var/www/html

4.2 调整inotify内核参数

在Linux内核中,默认的inotify机制提供了三个调控参数:
max_queued_events(监控事件队列,默认值为16384)、
max_user_instances(最多监控实例数,默认值为128)、
max_user_watches(每个实例最多监控文件数,默认值为8192)。
当要监控的目录、文件数较多或者变化较频繁时,建议加大这三个参数的值。

[root@backuper ~]# cat /proc/sys/fs/inotify/max_queued_events 
16384
[root@backuper ~]# cat /proc/sys/fs/inotify/max_user_instances 
128
[root@backuper ~]# cat /proc/sys/fs/inotify/max_user_watches 
8192

可通过/etc/sysctl.conf文件进行配置,设定参数应符合服务器性能以及服务需要。

[root@backuper ~]# vim /etc/sysctl.conf 

fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

[root@backuper ~]# sysctl -p
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@backuper ~]# cat /proc/sys/fs/inotify/max_queued_events 
16384
[root@backuper ~]# cat /proc/sys/fs/inotify/max_user_instances 
1024
[root@backuper ~]# cat /proc/sys/fs/inotify/max_user_watches 
1048576

4.3 安装inotify-tools

用inotify机制还需要安装inotify-tools,以便提供inotifywait、inotifywatch辅助工具程序,用来监控、汇总改动情况。
inotifywait:可监控modify(修改)、create(创建)、move(移动)、delete(删除)、attrib(属性更改)等各种事件,一旦变动立即输出结果。
inotifywatch:可用来收集文件系统变动情况,并在运行结果后输出汇总的变化情况。

[root@backuper ~]# cd /opt
[root@backuper opt]# rz -E
#传入inotify-tools安装包
rz waiting to receive.
[root@backuper opt]# tar zxvf inotify-tools-3.14.tar.gz -C /opt
[root@backuper opt]# cd inotify-tools-3.14/
[root@backuper inotify-tools-3.14]# ./configure
[root@backuper inotify-tools-3.14]# make -j 2 && make install

可以先执行“inotifywait”命令,然后另外再开启一个新终端向/var/www/html目录下添加文件、移动文件,在原来的终端上跟踪屏幕输出结果
新终端操作:

[root@backuper ~]# cd /var/www/html
[root@backuper html]# touch test.php
[root@backuper html]# mv test.php test.txt
[root@backuper html]# echo 'test' > test.txt
[root@backuper html]# rm -rf test.txt 

原终端监控:

[root@backuper inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html
/var/www/html/ CREATE test.php
/var/www/html/ MOVED_FROM test.php
/var/www/html/ MOVED_TO test.txt
/var/www/html/ MODIFY test.txt
/var/www/html/ DELETE test.txt
inotifywait常用选项 说明
-e 用来指定要监控哪些事件
-m 表示持续监控
-r 表示递归整个目录
-q 简化输出信息

4.4 在另一个终端编写触发式同步脚本

[root@backuper 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.122.10::rsync/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
##while判断是否接收到监控记录
do
    if [ $(pgrep rsync | wc -l) -le 0 ] ; then
        $RSYNC_CMD
    fi
done

[root@backuper opt]# chmod +x inotify.sh 
[root@backuper opt]# chmod 777 /var/www/html/
[root@backuper opt]# chmod +x /etc/rc.d/rc.local 
[root@backuper opt]# echo '/opt/inotify.sh' >> /etc/rc.d/rc.local
##加入开机自动执行

上述脚本用来检测本机/var/www/html目录的变动情况,一旦有更新触发rsync同步操作,上传备份至服务器192.168.122.11的rsync共享目录下。

4.4 验证

触发式上行同步的验证过程如下:
(1)在本机运行/opt/inotify_rsync.sh脚本程序

[root@backuper opt]# ./inotify.sh &
[1] 5786

(2)切换到本机的/var/www/html目录,执行增加、删除、修改文件等操作

[root@backuper opt]# cd /var/www/html
[root@backuper html]# ls
test.html  test.txt
[root@backuper html]# rm -rf *
[root@backuper html]# touch test.html
[root@backuper html]# echo 'this is inotify_rsync test!' > test.html
[root@backuper html]# 

(3)查看远端服务器中的rsync目录下的变化情况

[root@rsync ~]# cd /var/www/html
[root@rsync html]# ls
test.html
[root@rsync html]# cat test.html 
this is inotify_rsync test!

六、使用rsync实现快速删除大量文件

假如要在linux下删除大量文件,比如100万、1000万,像/usr/local/nginx/proxy_temp的nginx缓存等,那么“rm -rf *”可能就不好使了,因为要等待很长一段时间。在这种情况下,我们可以利用rsync的--delete来巧妙处理。rsync实际用的是替换原理。

1. 模拟垃圾文件

[root@rsync html]# touch {1..9999}.txt

2. 建立空文件夹

[root@rsync html]# mkdir /opt/test

3. 使用rsync进行替换删除

[root@rsync html]# rsync --delete-before -a -H -v --progress --stats /opt/test/ /var/www/html/
······
Number of files: 1
Number of files transferred: 0
Total file size: 0 bytes
Total transferred file size: 0 bytes
Literal data: 0 bytes
Matched data: 0 bytes
File list size: 19
File list generation time: 0.001 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 29
Total bytes received: 15

sent 29 bytes  received 15 bytes  29.33 bytes/sec
total size is 0  speedup is 0.00
[root@rsync html]# ls
[root@rsync html]# 

--delete-before:接受者在传输前进行删除操作
-a:归档模式,表示以递归方式传输文件,并保持所有文件属性
-H:保持硬链接文件
-v:详细输出模式
--progress:在传输室显示传输过程
--stats:给出某些文件的传输状态

posted @ 2021-05-05 17:31  丨君丶陌  阅读(186)  评论(0编辑  收藏  举报