Rsync远程同步

一、Rsync服务基本介绍

  • Rsync服务器
    Rsync是一款开源的、快速的、多功能的、可实现全量及增量的本地或远程数据同步备份的优秀工具。并且可以不进行改变原有数据的属性信息,实现数据的备份迁移特性。Rsync软件适用于unix/linux/windows等多种操作系统平台。 Rsync是一个快速和非常通用的文件复制工具。它能本地复制,远程复制,或者远程守护进程方式复制。它提供了大量的参数来控制其行为的各个方面,并且允许非常灵活的方式来实现文件的传输复制。它以其delta-transfer算法闻名。
  • rsync监听端口:873
  • rsync运行模式:C/S

二、备份方式

全量备份

所有数据全部传送
把原来的文件和新的文件一起统一传送
全量复制,效率低
假设客户端上有 file1 file2 file3 文件,服务端上有 file1 文件,现要将客户端上的数据备份至服务端
完全备份方式:
在这里插入图片描述

增量备份

在传输数据之前通过一些算法通过你有的数据和我有的数据进行对比,把不一样的数据通过网络传输
增量复制,效率较高
假设客户端上有 file1 file2 file3 文件,服务端上有 file1 文件,现要将客户端上的数据备份至服务端
增量备份方式:
在这里插入图片描述

三、rsync命令使用方法

语法

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

常用选项

常用选项作用
-a归档模式,递归并保留对象属性,等同于-rlptgoD
-v显示同步过程的详细(verbose)信息
-z在传输文件时进行压缩(compress)
-H保留硬连接文件
-A保留ACL属性信息
-p保留文件的权限标记
-t保留文件的时间标记
-g保留文件的属组标记
-o保留文件的属主标记
-delete删除目标位置有而原始位置没有的文件
-checksum根据对象的校验和来决定是否跳过文件

四、配置rsync远程同步

配置环境

rsync服务器:192.168.10.10
client客户机:192.168.10.20
同步目录:/www

配置过程

1.同步源服务器,查看安装rsync并修改配置文件

[root@server1 ~]# rpm -qa | grep rsync   ## 检查rsync是否安装,系统默认安装了rsync
rsync-3.0.9-18.el7.x86_64
[root@server1 ~]# vi /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes                        #禁锢在宿主目录中
address = 192.168.10.10                #监听地址
port 873                                #端口号
pid file = /var/run/rsyncd.pid          #进程文件位置
log file = /var/log/rsyncd.log          #日志文件位置
hosts allow = 192.168.10.0/24               #允许地址范围
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2         #不再压缩这几种格式的文件
[wwwroot]
path = /var/www                    #同步的目录
comment = www.tom.com
read only = yes                         #只读
auth users = backuper
secrets file = /etc/rsyncd_users.db     #用户密码存放位置

2.创建backuper用户的密码文件

[root@server1 ~]# vi /etc/rsyncd_users.db
backuper:123456

3.给密码文件设置执行权限

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

4.启动rsync

[root@server1 ~]# rsync --daemon
[root@server1 ~]# netstat -anpt |grep rsync
tcp        0      0 192.168.10.10:873      0.0.0.0:*               LISTEN      7360/rsync

5.同步目录下创建三个文件用于验证

[root@server1 ~]# mkdir /var/www
[root@server1 ~]# cd /var/www
[root@server1 www]# touch a b c
[root@server1 www]# ll
总用量 0
-rw-r--r--. 1 root root 0 1月   1 13:19 a
-rw-r--r--. 1 root root 0 1月   1 13:19 b
-rw-r--r--. 1 root root 0 1月   1 13:19 c

6.在客户端验证
方法一

[root@server2 ~]# rsync -avz backuper@192.168.10.10::wwwroot /var/www
Password: 
receiving incremental file list
created directory /www
./
a
b
c

sent 83 bytes  received 172 bytes  102.00 bytes/sec
total size is 17  speedup is 0.07

方法二

[root@client1 ~]# rsync -avz rsync://backuper@192.168.10.10/wwwroot /opt
Password: 
receiving incremental file list
./
a
b
c

sent 83 bytes  received 172 bytes  72.86 bytes/sec
total size is 17  speedup is 0.07

方法三:免密方式同步文件
客户端本地创建密码文件

root@server1 ~]# vi /etc/server.pass
123456
[root@server1 ~]# chmod 600 /etc/server.pass
[root@server1 ~]# mkdir /test
[root@server1 ~]# rsync -az --password-file=/etc/server.pass backuper@192.168.10.10::wwwroot /test
receiving incremental file list
deleting rh/
./
index.html

sent 83 bytes  received 172 bytes  510.00 bytes/sec
total size is 17  speedup is 0.07

五、rsync实时同步配置

实时同步介绍

定期同步存在一些不足之处,比如:

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

实时同步的优点

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

inotify介绍

  • Linux内核从2.6.13开始,引入了inotify机制。
  • 它是一种文件系统的变化通知机制,可以监控文件,也 可以监控目录。当监控目录时,它可以同时监控目录及目录中的各子目录及文件的。
  • 可以协助rsync,监控到数据的改变,触发 rsync 进行数据的同步。

配置rsync实时同步

配置rsync源服务器

1.将只读模式关闭

[root@server1 ~]# vi /etc/rsyncd.conf 
read only = no

2.客户端更改内核参数,指定队列大小,最多监控实例数,每个实例最多监控文件数

[root@server1 ~]# vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16384     #监控事件的最大队列数
fs.inotify.max_user_instances = 1024       #监控的最大实例数
fs.inotify.max_user_watches = 1048576     #监控的每实例的最大文件数
加载
[root@server1 ~]# sysctl -p
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

3.重新启动rsync服务,注意:kill pid号和kill -9 pid号的区别:后者不会删除pid文件,导致服务起不来

[root@server1 ~]# cd /var/run
[root@server1 run]# cat rsyncd.pid
9514
[root@server1 run]# kill 9514            #删除pid文件
[root@server1 run]# ll | grep rsync.pid
[root@server1 run]# rsync --daemon
[root@server1 run]# ll | grep rsyncd.pid
-rw-r--r--.  1 root           root              6 11月 12 13:19 rsyncd.pid

[root@server1 run]# cat rsyncd.pid
9514
[root@server1 run]# kill -9 9154        #不删除pid文件,导致启动不了
[root@server1 run]# ll | grep rsyncd.pid
-rw-r--r--.  1 root           root              6 11月 12 13:19 rsyncd.pid
[root@server1 run]# rsync --daemon
[root@server1 run]# failed to create pid file /var/run/rsyncd.pid: File exists

[root@server1 run]# rm -rf rsyncd.pid    #删除pid文件再启动
[root@server1 run]# rsync --daemon
[root@server1 run]# netstat -anpt | grep rsync
tcp        0      0 192.168.158.10:873      0.0.0.0:*               LISTEN      9666/rsync

配置客户机服务器

1.解压缩inotify并安装
inotifywait:用于持续监控,实时输出结果

inotifywatch:用于短期监控,任务完成后再出结果

[root@client1 ~]# tar zxf inotify-tools-3.14.tar.gz 
[root@client1 ~]# cd inotify-tools-3.14/
[root@client1 inotify-tools-3.14]# ./configure 
[root@client1 inotify-tools-3.14]# make && make install

2.测试inotifywait监控命令是否正常使用

[root@client1 inotify-tools-3.14]# mkdir -p /var/www 
[root@client1 inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www

3.执行完上面最后一条命令后处于监控状态,不能做输入操作,在开启一个新的会话

[root@client1 ~]# cd /var/www/
[root@client1 www]#rm -rf a 
[root@client1 ~]# cd inotify-tools-3.14/
[root@client1 inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /www
/www/ DELETE a

4.客户端上编写脚本,将inotify监控和rsync远程同步结合起来

[root@client1 inotify-tools-3.14]# vi /opt/inotify.sh
#!/bin/bash
INOTIFY="inotifywait -mrq -e modify,create,attrib,move,delete /var/www"
RSYNC="rsync -azH --delete --password-file=/etc/server.pass /var/www backuper@192。168.10.10::wwwroot/"
$INOTIFY | while read DIRECTORY EVENT FILE   #逐条读取监控记录
do
        if [ $(pgrep rsync | wc -l) -le 0 ];then
            $RSYNC
        fi
done

5.客户端和服务器的同步目录权限都设置777

[root@server1 ~]# chmod 777 /var/www/
[root@client1 ~]# chmod 777 /var/www/

6.运行脚本

[root@client1 inotify-tools-3.14]# chmod +x /opt/inotify.sh
[root@client1 inotify-tools-3.14]# cd /opt/
[root@client1 opt]# ./inotify.sh

6.在客户端/var/www/目录下创建文件,查看/var/www/目录是否同步到源端
客户机

[root@client1 www]# touch d e   #客户机
[root@client1 www]# ll           
总用量 0
-rw-r--r--. 1 root root 0 1月   1 13:19 b
-rw-r--r--. 1 root root 0 1月   1 13:19 c
-rw-r--r--. 1 root root 0 1月   1 16:48 d
-rw-r--r--. 1 root root 0 1月   1 16:48 e

服务器
查看源端/var/www/目录,新建的和原来就存在的文件都成功同步,且属主属组均为nobody用户

[root@server1 www]# cd www
[root@server1 www]# ll                 ##同步源到服务器
[root@server1 www]# ll
总用量 0
-rw-r--r--. 1 nobody nobody 0 1月   1 13:19 b
-rw-r--r--. 1 nobody nobody 0 1月   1 13:19 c
-rw-r--r--. 1 nobody nobody 0 1月   1 16:48 d
-rw-r--r--. 1 nobody nobody 0 1月   1 16:48 e
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值