详细讲解rsync远程同步

一.rsync 定义

1是一款快速增量备份工具
2 能更新整个目录树和文件系统; 有选择性的保持符号链链、硬链接、文件属性、权限、设备以及时间等;传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用。能用rsh、ssh 或直接端口做为传输端口;
3 支持匿名rsync 同步文件,是理想的镜像工具
4 Rsync可以通过rsh或ssh使用,也能以daemon模式去运行,在以daemon方式运行时Rsync server会打开一个873端口,等待客户端去连接。在远程同步任务中,负责发起rsync同步操作的客户机称为发起端,而负责响应来自客机的rsync同步操作的服务器称为备份源。

二、如何配置rsync源

1、基本思路
 修改rsyncd.conf配置文件、独立的密码文件
 启用rsync的--daemon模式

2、应用示例
 用户backuper,允许下行同步
 操作的目录为/var/www/html

3、配置文件rsyncd.conf
 语法类似于Samba配置
 认证配置auth users、secrets file,不加则为匿名(nobody)

4、rsync账号文件
采用“用户名:密码”的记录格式,每行一个用户记录
独立的账号数据,不依赖于系统账号(安全)

5、如何启动和关闭rsync
启动rsync:rsync --daemon
关闭rsync:kill $(cat /var/run/rsyncd.pid)

6.rsync命令的用法
rsync [选项] 原始位置 目标位置
常用选项
  -a:归档模式,递归并保留对象属性,等同于-rlptgoD
  -v:显示同步过程的详细信息
  -z:在传输文件时进行压缩
  -H:保留硬连接文件
  -A:保留ACL属性信息
  --delete:删除目标位置有而原始位置没有的文件
  --checksum:根据对象的校验和来决定是否跳过文件

三、配置rsync远程同步

3.1 实验环境拓扑图

在这里插入图片描述

3.2、具体步骤(rsync系统自带)

源站配置

1.修改配置文件
[root@Rsync ~]# vi /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes           #禁锢家目录
address = 20.0.0.11     #提供同步服务的地址
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 20.0.0.0/24   #允许同步的网段

[wwwroot]
path = /var/www/html   #同步的目录
comment = www.test.com
read only = yes      #只读模式开启
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2   #这些结尾的文件同步时不再压缩
auth users = backuper     
secrets file = /etc/rsyncd_users.db   #用户密码存放在secrets file中
2. 创建backuper用户的密码文件,服务端的密码文件要600权限
[root@Rsync ~]# vi /etc/rsyncd_users.db
backuper:abc123
[root@Rsync ~]# chmod 600 /etc/rsyncd_users.db
3. 启动服务并查看状态
[root@Rsync ~]# rsync --daemon
[root@Rsync ~]# netstat -anpt | grep rsync
tcp        0      0 192.168.73.10:873       0.0.0.0:*               LISTEN      49133/rsync
4. 发起端验证
方法一
[root@client ~]# rsync -avz backuper@20.0.0.11::wwwroot /var/www/html
Password:
receiving incremental file list
./
index1.html

sent 83 bytes  received 153 bytes  67.43 bytes/sec
total size is 0  speedup is 0.00

方法二
[root@client html]# rsync -avz rsync://backuper@20.0.0.11/wwwroot /opt
Password:
receiving incremental file list
./
index1.html
index2.html

sent 102 bytes  received 209 bytes  88.86 bytes/sec
total size is 0  speedup is 0.00


[root@client html]# cd /opt
[root@client opt]# ll
总用量 0
-rw-r--r--. 1 root root 0 11月 11 19:31 index1.html
-rw-r--r--. 1 root root 0 11月 11 19:33 index2.html
drwxr-xr-x. 2 root root 6 3月  26 2015 rh

免密方式同步文件

[root@client opt]# vi /etc/server.pass
abc123
[root@client opt]# mkdir /backup
[root@client opt]# rsync -az --password-file=/etc/server.pass backuper@20.0.0.11::wwwroot /backup
[root@client opt]# cd /backup
[root@client backup]# ll
总用量 0
-rw-r--r--. 1 root root 0 11月 11 19:31 index1.html
-rw-r--r--. 1 root root 0 11月 11 19:33 index2.html

rsync+inotify实时同步

在这里插入图片描述

1、调整inotify内核参数
[root@client ~]# vi /etc/sysctl.conf
fs.inotify.max_queued_events = 16384  ###监控事件队列大小
fs.inotify.max_user_instances = 1024  ###最多监控实例数
fs.inotify.max_user_watches = 1048576  ###每个实例最多监控文件数

[root@client ~]# sysctl -p
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

2、源站修改配置文件
[root@Rsync ~]# vi /etc/rsyncd.conf
read only = no


3、安装inotify-tools辅助工具
[root@client ~]# tar zxf inotify-tools-3.14.tar.gz -C /opt
[root@client ~]# cd /opt/inotify-tools-3.14/
[root@client inotify-tools-3.14]# ./configure
[root@client inotify-tools-3.14]# make && make install


4、开启监控
[root@client inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html

inotifywait:用于持续监控,实时输出结果
inotifywatch:用于短期监控,任务完成后再出结果

  -m:持续进行监控
  -r:递归监控所有子对象
  -q:简化输出信息
  -e:指定要监控哪些事件类型
  modify:修改;create:创建;move:移动;delete:删除

5、测试监控是否正常使用
再开一个客户端终端会话

[root@client opt]# cd /var/www/html
[root@client html]# rm -rf index1.html
[root@client inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html
/var/www/html/ DELETE index1.html

6、客户端上编写脚本,将inotify监控和rsync远程同步结合起来
[root@client ~]# vi inotify.sh
#!/bin/bash
INOTIFY="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html"
RSYNC="rsync -azH --delete --password-file=/etc/server.pass /var/www/html  backuper@20.0.0.11::wwwroot/"
$INOTIFY | while read DIRECTORY EVENT FILE   #逐条读取监控记录
do
        if [ $(pgrep rsync | wc -l) -le 0 ];then
            $RSYNC
        fi
done

###同步两边目录权限都为777

7、运行脚本,在客户端/var/www/html目录下创建文件,查看源端/var/www/html目录是否同步到
客户端:
[root@client ~]# cd /var/www/html
[root@client html]# touch test.txt

服务器端:
[root@Rsync ~]# cd /var/www/html
[root@Rsync html]# ll
总用量 0
drwxrwxrwx. 2 nobody nobody 59 11月 12 12:26 html
-rw-r--r--. 1 root   root    0 11月 12 11:31 index1.html
-rw-r--r--. 1 root   root    0 11月 12 11:33 index2.html
[root@Rsync html]# cd html
[root@Rsync html]# ll
总用量 4
-rw-r--r--. 1 nobody nobody  0 11月 12 11:33 index2.html
-rw-r--r--. 1 nobody nobody 14 11月 12 11:27 index.html
-rw-r--r--. 1 nobody nobody  0 11月 12 12:26 test.txt




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值