rsync

rsync 远程同步

一:rysnc 基础操作

1   rsync 概述

rsync (Remote Sync,远程同步),支持本地复制,或与其他SSH,rsync主机同步.

官方网站:http://rsync.samba.org/

是一个开源的快速备份工具,可以在不同主机之间镜像同步整个目录树,支持增量备份,并保持链接和权限,且采用优化的同步算法,传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用。

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



2 sync 同步操作

命令用法:

rsync [选项....] 源目录 目标目录


本地同步:

  • rsync [选项...] 本地目录1 本地目录2 #同步整个文件夹
  • rsync [选项.....] 本地目录1/ 本地目录2 #只同步目录下的数据


rsync 操作选项:

选项 释义
-r 递归模式,包含目录及子目录中的所有文件
-l(小写L) 对于符号链接文件仍然复制为符号链接文件
-v 显示同步过程中的详细(verbose)信息
-z 在传输文件时进行压缩(compress)
-a 归档模式,保留文件的权限,属性等信息,等同于组合选项"-rlptgoD"
-p 保留文件的权限标记
-t 保留文件的时间标记
-g 保留文件的属组标记(仅超级用户使用)
-o 保留文件的属主标记(仅超级用户使用)
-H 保留硬链接文件
-A 保留ACL属性信息
-D 保留设备文件及其他特殊文件
-S(--sparce) 处理稀疏文件时使用
--delete 删除目录位置有,而原始位置没有的文件。
--checksum 更具校验和(而不是文件大小,修改时间)来决定是否跳过文件.
--password-flie= 指定密码文件

#当前,/test01目录里有文件a.txt,b.txt。 /test02 和 /test03目录为空
[root@host104 ~]# ls /test01 /test02 /test03
/test01:
a.txt  b.txt

/test02:

/test03:


#同步/test01 目录到 /test02目录下
[root@host104 ~]# rsync -a /test01 /test02

#同步/test01 目录下文件到  /test03目录下
[root@host104 ~]# rsync -a /test01/  /test03

[root@host104 ~]# ls /test01 /test02 /test03
/test01:
a.txt  b.txt

/test02:
test01

/test03:
a.txt  b.txt

image-20210923160749988

image-20210923152848837





二: sync +ssh 同步

2.1 配置rsync 源服务器

基本思路:

  • 建立rsyncd.conf配置文件,独立的账号文件
  • 启用rsync 的--daemon模式

image-20210923172932287


2.1.1  修改配置文件 /etc/rsyncd.conf

[root@host104 ~]# systemctl  stop  firewalld
[root@host104 ~]# setenforce  0
[root@host104 ~]# rpm -q rsync

#查看rsync的默认监听端口
[root@host104 ~]# cat  /etc/services  | grep rsync
rsync           873/tcp                         # rsync
rsync           873/udp                         # rsync


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

 uid = nobody
 gid = nobody
 use chroot = yes                  #禁锢在源目录
 address = 192.168.23.104          #监听地址。
 port 873                          #监听端口(UDP/TCP 873端口。
 log file = /var/run/rsyncd.log    #日志文件位置
 pid file = /var/run/rsyncd.pid    #存放进程pid文件的位置
 hosts allow = 192.168.23.0/24     #允许访问的客户机


 [wwwroot]                                 #模块名称   
 path = /var/www/html                      #源目录的实际路径
 comment = Document Root of web server     #描述信息
 read only = yes                           #设置为只读
 dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2   #同步时不再压缩的文件类型
 auth users = backuper                      #授权用户,多个账户用空格分隔
 secrets file = /etc/rsyncd_users.db        #设置存放账户信息的数据文件

如果采用匿名的方式,只要将其中的“auth users“ 和“secrets file”去掉就可以了 


2.1.2 创建备份账户数据文件

[root@host104 ~]# vim /etc/rsyncd_users.db
#rsync 的认证用户和密码。不用创建同名的系统用户
backuper:abc123

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



2.1.3 配置源目录的权限

要保证所有用户对源目录(/var/www/html) 都有读取权限

[root@host104 ~]# chmod  +r /var/www/html/

[root@host104 ~]# ls -ld /var/www/html/
drwxr-xr-x 2 root root 6 9月  23 16:12 /var/www/html/



2.1.4 启动rsync 服务

#启动rsync 服务,以堵路监听服务的防水(守护进程)运行
[root@host104 ~]# rsync  --daemon
[root@host104 ~]# netstat -natp | grep rsync
tcp        0      0 192.168.23.104:873      0.0.0.0:*               LISTEN      82116/rsync

如果要关闭rsync服务:

kill $(cat  /var/run/rsyncd.pid)
rm -rf /var/run/rsyncd.pid





2.2 发起端配置

2.2.1 发起端同步的命令

查看远端共享目录的内容

格式: rsync -avz backuper@192.168.23.104::wwwroot

  • backuper为rsync 同步的用户,密码设置的是abc123

将制定的资源下载到本地的/opt目录下备份

格式一: rsync backuper@192.168.23.104::wwwroot /opt

  • backuper为rsync 同步的用户,密码设置的是abc123
  • ip 后面跟的是 双冒号::后面是 设置的共享模块名

格式二: rsync -avz rsync://backuper@192.168.23.104/wwwroot /opt

  • 使用url 路径。使用的是 **冒号双斜线 :// **格式
  • backuper为rsync 同步的用户,密码设置的是abc123

#查看远程 源服务器共享目录 下有哪些内容
[root@host103 opt]# rsync  backuper@192.168.23.104::wwwroot 
Password: #输入密码
drwxr-xr-x          19 2021/09/23 16:49:54 .
#这就是当前 源服务器共享目录下的内容a.txt
-rw-r--r--           0 2021/09/23 16:49:54 a.txt  


#源服务器的wwwroot 模块下的内容备份到本地的 /opt 目录下
[root@host103 opt]# rsync -avz backuper@192.168.23.104::wwwroot  /opt/
Password:   #输入rsync 账户 backuper 的密码

[root@host103 opt]# ls
#文件被拉取到了本地的/opt 
a.txt 


image-20210923170044520

image-20210923170305736



2.2.2 免交互式配置定期同步

#创建文件,将密码写入文件中
[root@host103 ~]# echo "abc123" > /etc/server.pass

#设置权限,防止密码文件被其他用户篡改
[root@host103 ~]# chmod  600 /etc/server.pass

[root@host103 ~]# crontab  -e
30 1 * * *  /usr/bin/rsync -az  --delete --password-file=/etc/server.pass backuper@192.168.23.104::wwwroot  /opt/
#每天1:30  j进行备份,将源服务器的wwwroot模块下文件备份到本地的 /opt/目录下
#--deleete   删除源服务器没有,而本地有的文件
#--password-file=   指定保存rsync 用户backuper密码的文件,以实现免交互


[root@host103 opt]# systemctl restart crond
[root@host103 opt]# systemctl enable crond

image-20210923171909692





三: rsync + inotify 机制实现实时同步

3.1 为什么使用实时同步

定期同步的不足:

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

实时同步的优点:

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



3.2 关于inotify/

Linux 内核的inotify机制:

  • 从版本2.6.13 开始提供
  • 可以监控文件系统的变动情况,并作出通知响应
  • 辅助软件:inotify-tools

image-20210923172720851


使用inotify通知接口,可以用来监控文件系统的各种变化情况,如文件存取、删除、移动、修改等。利用这一机制,可以非常方便地实现文件异动告警、增量备份,并针对目录或文件的变化及时作出响应。

将inotify机制与rsync工具相结合,可以实现触发式备份(实时同步),即只要原始位置的文档发生变化,则立即启动增量备份操作,否则处于静默等待状态。这样,就避免了按固定周期备份时存在的延迟性、周期过密等问题。

因为 inotify 通知机制由 Linux 内核提供,因此主要做本机监控,在触发式备份中应用时更适合上行同步。



3.3 源服务器 修改rsync 服务配置文件,并修改目录权限

[root@host104 ~]# vim /etc/rsyncd.conf
........
#uid和gid 修改问目录/var/www/html 属组和属主
 uid = root
 gid = root
#关闭只读,上行同步需要可以写。
read only = no
......

#关闭rsync 服务
[root@host104 ~]# kill $(cat /var/run/rsyncd.pid)
[root@host104 ~]# rm -rf /var/run/rsyncd.pid

#启动rsync服务
[root@host104 ~]# rsync --daemon
[root@host104 ~]# netstat -natp | grep rsync
tcp        0      0 192.168.23.104:873      0.0.0.0:*               LISTEN      83003/rsync

#修改目录权限
[root@host104 ~]# chmod 777 /var/www/html/

image-20210923195055037

image-20210923173723184

image-20210923173650731



3.4 发起端调整inotify 内核参数

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

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


[root@host103 ~]# vim /etc/sysctl.conf 
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

[root@host103 ~]# sysctl  -p



3.5 发起端安装 inotify-tools

用 inotify 机制还需要安装inotify-tools,以便提供 inotifywait、inotifywatch 辅助工具程序,用来监控、汇总改动情况。

inotifywait∶可监控modify(修改)、create(创建)、move(移动)、delete(删除)、attrib(属性更改)等各种事件,一有变动立即输出结果。

inotifywatch∶可用来收集文件系统变动情况,并在运行结束后输出汇总的变化情况。

[root@host103 ~]# cd /opt/
[root@host103 opt]# tar xf inotify-tools-3.14.tar.gz  -C /opt/
[root@host103 opt]# cd /opt/inotify-tools-3.14/
[root@host103 inotify-tools-3.14]# ./configure 
[root@host103 inotify-tools-3.14]# make && make install

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


inotifywait -mrq -e modify,create,move,delete /var/www/html

选项 释义
-e 用来指定要监控哪些事件
-m 表示持续监控
-r 表示递归监控整个目录
-q 简化输出信息
[root@host103 opt]#mkdir -p  /var/www/html
#一个终端监控 目录/var/www/html
[root@host103 html]# inotifywait -mrq -e modify,create,move,delete /var/www/html

#新开一个终端,在/var/www/html 目录下进行增删改操作
[root@host103 opt]# cd /var/www/html/

[root@host103 html]# touch a.txt
[root@host103 html]# touch b.txt
[root@host103 html]# mv b.txt /opt/
[root@host103 html]# echo "123" >> a.txt 
[root@host103 html]# rm -rf a.txt

image-20210923192047397

image-20210923192200073



3.6 客户端编写触发式同步脚本

[root@host103 opt]# 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.23.104::wwwroot/"

$INOTIFY_CMD | while read DIRECTORY EVENT FILE
#while判断是否接收到监控记录



[root@host103 opt]# chmod  +x /opt/inotify.sh
[root@host103 opt]# chmod  777 /var/www/html/
[root@host103 opt]# chmod  +x /etc/rc.d/rc.local
[root@host103 opt]# echo '/opt/inotify.sh' >> /etc/rc.d/rc.local 

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

触发式上行同步的验证过程如下∶

(1)在本机运行/opt/inotify.sh脚本程序。

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

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

--------------------- 客户端------------------
#运行脚本
[root@host103 opt]# ./inotify.sh [root@host103 html]# touch abc.txt

#在/var/www/html下创建文件
[root@host103 html]# touch abc.txt
[root@host103 html]# touch 1.txt
[root@host103 html]# 


------------------- 源服务器 的/var/www/html 目录-----------------
#在客户端运行脚本前,有a,txt文件
[root@host104 html]# ls
a.txt

#客户端运行脚本后,同步了客户端的/var/www/html目录
[root@host104 html]# ls
1.txt  abc.txt

image-20210923195301566

image-20210923194248070

image-20210923194307100



四: 利用rsync 快速删除大量文件

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

#先建立一个空的文件夹
[root@host103 opt]# mkdir /test

#创建大量的文件
[root@host103 opt]# cd /test/
[root@host103 test]# touch a{1..10000}

#准备一个空的目录
[root@host103 test]# rm -rf /var/www/html/*

#利用rsync删除文件.
#注意,空目录做为源目录,放在前面,且要加 / ,表示同步该目录下的文件
[root@host103 test]# rsync --delete-before -a -H -v --progress --stats  /var/www/html/ /test

#test目录下的文件被删除
[root@host103 test]# ls



五:报错

5.1 报错

报错(1)

当使用rsync + inotify 的时候,虽然能够使用,但是客户端监控时,一直报错

“@ERROR: invalid uid roo
rsync error: error starting client-server protocol (code 5) at main.c(1516) [sender=3.0.9]
@ERROR: invalid uid roo
rsync error: error starting client-server protocol (code 5) at main.c(1516) [sender=3.0.9]”

image-20210923201756778


报错(2)

"rsync: failed to set times on "/." (in wwwroot): Operation not permitted (1)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]
rsync: failed to set permissions on "/." (in wwwroot): Operation not permitted (1)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]"

image-20210923201907409


5.2 原因

配置文件里 uid和gid 配置的和 共享的模块里的目录的属主和属组不一致

image-20210924075458542

image-20210924075547732



5.3 解决

将配置文件/etc/rsyncd.conf 里的uid 和gid 分别修改为 共享目录的属主和属组.

或者将共享目录的属主和属组修改为 配置文件里定义的uid 和 gid 项的用户

然后重启rsync 服务

posted @ 2021-09-24 08:09  知己一语  阅读(392)  评论(0编辑  收藏  举报