教你用rsync同步

rsync同步简介

一款快速增量备份工具

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

应用场景

“推”
即由客户端推送数据至服务器,比如个人电脑上传数据至云盘。
“拉”
即由客户端拉取服务器上的数据,比如利用个人电脑下载云盘文件。

rsync 包括如下的一些特性

  • 能更新整个目录树和文件系统;
  • 有选择性的保持符号链链、硬链接、文件属性、权限、设备以及时间等;
  • 传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用。
  • 能用rsh、ssh 或直接端口做为传输端口;
  • 支持匿名rsync 同步文件,是理想的镜像工具;

Rsync可以通过rsh或ssh使用,也能以daemon模式去运行,在以daemon方式运行时Rsync server会打开一个873端口,等待客户端去连接。
在远程同步任务中,负责发起rsync同步操作的客户机称为发起端,而负责响应来自客机的rsync同步操作的服务器称为备份源。

配置rsync源服务器

rsync同步源简介

备份操作的远程服务器,也称为备份源

如何配置rsync源

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

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

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

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

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

rsync命令的用法

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

配置rsync远程同步


地址

20.0.0.11

同步目录

/var/www/html

客户端地址

20.0.0.12

rsyns源站配置

确认rsync是否安装

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

修改配置文件

[root@server1 ~]# 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.lcx.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中

创建backuper用户的密码文件

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

###服务端的密码文件要600权限

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

启动服务查看状态

[root@server1 ~]# rsync --daemon #启动rsync守护进程
[root@server1 ~]# netstat -anpt | grep rsync
tcp        0      0 20.0.0.11:873           0.0.0.0:*               LISTEN      8717/rsync  

[root@server1 ~]# yum -y install httpd #安装apache
[root@server1 ~]# cd /var/www/html/ #此时会创建这个目录

[root@server1 html]# vi index.html
<h1>this is web!</h1>

[root@server1 html]# cd ..
[root@server1 www]# chmod 777 html/ #给/var/www/html所有权限

客户端验证

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

方法一

[root@server1 ~]# yum -y install httpd
[root@server1 html]# cd /var/www
[root@server1 www]# chmod 777 html/

[root@server1 www]# rsync -avz backuper@20.0.0.11::wwwroot /var/www/html
Password:  #输入密码,不显示
receiving incremental file list
./
index.html

sent 83 bytes  received 179 bytes  47.64 bytes/sec
total size is 22  speedup is 0.08

方法二

[root@server1 www]# rsync -avz rsync://backuper@20.0.0.11/wwwroot /var/www/html

免密方式同步文件:
要先在客户端本地创建密码文件/etc/server.pass

[root@server1 ~]# vi /etc/server.pass
123456
[root@server1 ~]# rsync -avz --delete --password-file=/etc/server.pass backuper@20.0.0.11::wwwroot /var/www/html

rsync实时同步

为什么要实时同步

  • 定期同步的不足
    执行备份的时间固定,延迟明显,实时性差
    当同步源长期不变化时,密集的定期任务是不必要的
  • 实时同步的优点
    一旦同步源出现变化,立即启动备份
    只要同步源无变化,则不执行备份

关于inotify

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

rsync+inotify实时同步

客户端调整inotify内核参数

[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

源站修改配置文件

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

客户端安装inotify-tools辅助工具

[root@server1 ~]# yum -y install gcc gcc-c++ make #安装编译环境

[root@server1 inotify-tools-3.14]# tar zxvf inotify-tools-3.14.tar.gz 

[root@server1 ~]# cd inotify-tools-3.14/
[root@server1 inotify-tools-3.14]# ./configure 
[root@server1 inotify-tools-3.14]# make && make install
[root@server1 inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html #开启监控
###
-m持续监控、r递归、-q简要型输出,-e操作(更新),modify(修改),create(更新),move(移动),delete(删除),/var/www/html(监控地址)
此时监控台不可操作,可以通过远程登录多开页面进行操作
###

客户端第二页面

[root@server1 html]# cd /var/www/html/
[root@server1 html]# touch abc
[root@server1 html]# rm -rf abc

客户端

[root@server1 inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/
/var/www/html/ CREATE abc #显示刚才操作
/var/www/html/ DELETE abc #显示刚才操作

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

[root@server1 ~]# vi inotify.sh
#!/bin/bash
INOTIFY="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html"
RSYNC="rsync -avz --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
同步源

[root@server1 ~]# ls -lh /var/www
总用量 352K
drwxr-xr-x. 2 root root    6 102 00:52 cgi-bin
drwxrwxrwx. 3 root root   47 1230 23:27 html

#发起端

[root@server1 ~]# ls -lh /var/www/
总用量 0
drwxr-xr-x. 2 root root  6 84 2017 cgi-bin
drwxrwxrwx. 2 root root 57 1230 23:35 html

运行脚本,在客户端/var/www/html目录下创建文件,查看源端/var/www/html目录是否同步到
客户端:

[root@server1 ~]# cd /var/www/html/
[root@server1 html]# echo '<h1>this is web!</h1>' > index.html

客户端监控:

[root@server1 ~]# ./inotify.sh 
sending incremental file list
html/
html/index.html

sent 120 bytes  received 31 bytes  302.00 bytes/sec
total size is 22  speedup is 0.15
sending incremental file list

sent 58 bytes  received 9 bytes  134.00 bytes/sec
total size is 22  speedup is 0.33

源站

[root@server1 ~]# cd /var/www/html/
[root@server1 html]# ls
abc  html  index.html
[root@server1 html]# cd html/
[root@server1 html]# ls
index.html
[root@server1 html]# cat index.html 
<h1>this is web!</h1>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值