rsync远程同步+inotify

一、Rsync服务基本介绍

1.关于rsync

  • 一款增量备份工具
  • Remote Sync,远程同步
  • 支持本地复制,或者与其他SSH、rsync主机同步
  • 官方网站: http://rsync.samba.org

二、配置rsync备份源

2.1 配置rsync源服务器

rsync同步源

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

2.2 配置rsync源

基本思路

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

应用示例

  • 用户backuper,允许下行同步
  • 操作目录为/var/www/html (安装HTTP)

配置文件rsyncd.conf

  • 需手动建立,语法类似于Samba配置
  • 认证配置auth users、secrets file,不加则为匿名

rsync账号文件

  • 采用"用户名:密码"的记录格式,每行一个用户记录
  • 独立的账户数据,不依赖于系统账号

启用rsync服务

  • 通过 --daemin独自提供服务

三、rsync命令基本用法

3.1 使用rsync备份工具

rsync格式:

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

常用选项:

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

配置源的两种表示方法格式:
格式1:用户名@主机地址::共享模块名

[root@localhost etc]# rsync backuper@192.168.1.10::wwwroot /opt/

格式2:rsync://用户名@主机地址/共享模块名

[root@localhost etc]#  rsync -avz rsync://backuper@192.168.1.10::wwwroot /opt/

四、rsync备份操作示例

源地址:192.168.1.10

同步目录:/var/www/html

客户端地址:192.168.1.11

4.1 rsync源站配置

1.确定rsync是否安装

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

2.修改配置文件

uid = nobody
gid = nobody
use chroot = yes             #禁锢家目录
address = 192.168.1.10       #提供同步服务的地址     
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.1.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中

在这里插入图片描述
3.创建backuper用户的密码文件

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

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

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

5.启动服务查看状态

[root@server1 ~]# rsync --daemon   #启动rsync守护进程
[root@server1 ~]# netstat -anpt | grep rsync
tcp        0      0 192.168.1.10:873        0.0.0.0:*               LISTEN      8108/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所有权限

4.2 客户端验证

常用选项:

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

方法一:

[root@server1 ~]# yum -y install httpd
[root@server1 ~]# cd /var/www/
[root@server1 www]# chmod 777 html/
[root@server1 www]# rsync -avz backuper@192.168.1.10::wwwroot /var/www/html/
Password: 
receiving incremental file list

sent 61 bytes  received 113 bytes  20.47 bytes/sec
total size is 22  speedup is 0.13

方法二、

[root@server1 www]# rsync -avz rsync://backuper@192.168.1.10/wwwroot /var/www/html/
Password: 
receiving incremental file list

sent 61 bytes  received 113 bytes  49.71 bytes/sec
total size is 22  speedup is 0.13

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

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

五、rsync实时同步

5.1 为什么要实时同步

定期同步的不足

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

实时同步的优点

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

5.2 关于inotify

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

5.3 rsync+inotify实时同步

客户端调整inotify内核参数

[root@server2 ~]# vim /etc/sysctl.conf 
fs.inotify.max_queued_events = 16384  ###监控事件队列大小
fs.inotify.max_user_instances = 1024  ###最多监控实例数
fs.inotify.max_user_watches = 1048576  ###每个实例最多监控文件数

[root@server2 ~]# 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@server2 ~]# yum -y install gcc gcc-c++ make #安装编译环境
[root@server2 ~]# tar zxvf inotify-tools-3.14.tar.gz 
[root@server2 ~]# cd inotify-tools-3.14/
[root@server2 inotify-tools-3.14]# ./configure 
[root@server2 inotify-tools-3.14]# make && make install
[root@server2 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@server2 html]# cd /var/www/html/
[root@server2 html]# touch abc
[root@server2 html]# rm -rf abc

客户端

[root@server2 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@server2 ~]# 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 [email protected]::wwwroot/" #本地路径在前为源与本地同步
$INOTIFY | while read DIRECTORY EVENT FILE   #逐条读取监控记录
do
        if [ $(pgrep rsync | wc -l) -le 0 ];then
            $RSYNC
        fi
done

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

[root@server1 ~]# chmod 777 /var/www/html/ 添加权限
[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/
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@server2 ~]# cd /var/www/html/
[root@server2 html]# echo '<h1>This is web</h1>' > index.html

客户端监控

[root@server2 ~]# ./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>

猜你喜欢

转载自blog.csdn.net/F2001523/article/details/112169205
今日推荐