Rsync+inotify备份

Rsync+inotify
Inotify是一个通知接口,用来监控文件系统的各种变化,如果文件存取,删除,移动。可以非常方便地实现文件异动告警,增量备份,并针对目录或文件的变化及时作出响应。rsync+inotify可以实触发式实时同步增量备份

案例: 实现web上传视频文件,写入NFS共享存储,然后将NFS存储内容实时复制至Backup服务器

环境准备

角色 外网IP(NAT) 内网IP(LAN) 安装工具
Rsync-server eth0:10.0.0.41 eth1:172.16.1.41 rsync-server
nfs-server eth0:10.0.0.31 eth1:172.16.1.31 rsync+inotify
web01 eth0:10.0.0.7 eth1:172.16.1.7 nginx+php
Rsync-Server操作步骤如下:

1.安装rsync

[[email protected] ~]# yum install -y rsyncd
2.配置rsync

[[email protected] ~]# vim /etc/rsyncd.conf
uid = rsync
gid = rsync
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.password
log file = /var/log/rsyncd.log
#####################################
[backup]
path = /backup

[data]
path = /data

准备密码文件以及虚拟用户对应的虚拟密码
[[email protected] ~]# cat /etc/rsync.password
rsync_backup:123456
[[email protected] ~]# ll /etc/rsync.password
-rw------- 1 root root 20 Jul 23 11:42 /etc/rsync.password

创建backup data仓库目录
[[email protected] ~]# mkdir /backup
[[email protected] ~]# chown -R rsync.rsync /backup/

[[email protected] ~]# mkdir /data
[[email protected] ~]# chown -R rsync.rsync /data/
3.启动rsync

[[email protected] ~]# systemctl restart rsyncd
[[email protected] ~]# netstat -lntp|grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 1372/rsync
4.nfs客户端验证rsync是否正常使用

[[email protected] ~]# rsync -avz /root/oldboy [email protected]::backup
Password:
sending incremental file list
oldboy

[[email protected] ~]# rsync -avz /root/oldboy [email protected]::data
Password:
sending incremental file list
oldboy
NFS-Server操作步骤如下:

1.安装nfs

[[email protected] ~]# yum install nfs-utils rpcbind -y
2.配置nfs

[[email protected] ~]# cat /etc/exports
/data/ 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)

[[email protected] ~]# id www
uid=666(www) gid=666(www) groups=666(www)

[[email protected] ~]# mkdir /data
[[email protected] ~]# mkdir /data/www
[[email protected] ~]# chown -R www.www /data
[[email protected] ~]# ll -d /data/
drwxr-xr-x 5 www www 98 Jul 26 09:29 /data/
[[email protected] /]# ll -d /data/www/
drwxr-xr-x 2 root root 6 Jul 27 10:40 /data/www/
3.启动nfs

[[email protected] ~]# systemctl restart rpcbind
[[email protected] ~]# systemctl restart nfs-server
4.安装inotify

[[email protected] ~]# yum install inotify-tools rsync -y
5.配置inotify

在linux内核中,默认的inotify机制提供了三个调控参数:
[[email protected] ~]# ls /proc/sys/fs/inotify/
max_queued_events #设置监控服务实例可以监控的事件个数
max_user_instances #设置用户可以开启的服务进程数
max_user_watches #可以监控的最大文件数

参数值修改
cat >> /etc/sysctl.conf <<EOF
fs.inotify.max_queued_events=99999999
fs.inotify.max_user_watches=99999999
fs.inotify.max_user_instances=65535
EOF

使修改的参数生效
[[email protected] ~]# sysctl -p

inotify工具中常用于实时同步的命令是inotifywait(监控文件或目录的变化),如下列出常用参数
-m 表示持续监控
-r 递归形式监视目录
-q 简化输出信息
-e 指定要监视的事件(create,move,delete,modify)
--timefmt 自定义时间格式,常用参数‘%y/%m/%d %H:%M‘
--format 自定义输出格式信息, 常用的格式符如下
%e: 使用了什么事件
%T: 时间
%w:显示被监控文件的文件名;
%f:如果发生某事件的对象是目录,则显示被监控
6.利用inotifywait实现数据实时监控

[[email protected] ~]# inotifywait -mrq --format ‘%e %T %w%f‘ --timefmt ‘%y/%m/%d %H:%M‘ -e close_write,modify,delete,create,move /backup/
7.使用inotifywait配合rsync实现实时同步脚本

[[email protected] ~]# vim /server/scripts/inotify_rsync.sh
#!/bin/bash
export RSYNC_PASSWORD=123456
SRC=/data/
[email protected]::backup/

inotifywait -mrq -e modify,delete,create,attrib ${SRC} | while read file
do
rsync -az --delete $SRC $DST
done
[[email protected] ~]# chmod +x /server/scripts/inotify_rsync.sh
web01操作步骤如下:

1.安装nginx+php以及nfs相关工具

[[email protected] ~]# yum install nginx php php-fpm php-pdo nfs-utils rpcbind -y
2.配置nfs-client

[[email protected] ~]# mkdir /data/www
[[email protected] ~]# mount -t nfs 172.16.1.31:/data/www /data/www/
#加入开机自动挂载该nfs设备
[[email protected] ~]# tail -n1 /etc/fstab
172.16.1.31:/data/www /data/www nfs defaults 0 0

[[email protected] ~]# umount /data/www/
[[email protected] ~]# mount -a
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
172.16.1.31:/data/www 50G 1.6G 49G 4% /data/www
3.配置nginx

[[email protected] ~]# vim /etc/nginx/nginx.conf
user www;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
root /data/www;
index index.php index.html index.htm;
autoindex on;
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
include fastcgi_params;
}
}
}
#注意:nginx修改运行用户需要调整对应的临时目录权限
[[email protected] ~]# chown -R www.www /var/lib/nginx/
4.修改php程序运行的属主和属组,统一用户id

[[email protected] ~]# sed -i ‘s#user = apache#user = www#g‘ /etc/php-fpm.d/www.conf
[[email protected] ~]# sed -i ‘s#group = apache#group = www#g‘ /etc/php-fpm.d/www.conf
[[email protected] ~]# egrep "^user|^group" /etc/php-fpm.d/www.conf
user = www
group = www
5.启动nginx+php

[[email protected] ~]# systemctl start nginx php-fpm
[[email protected] ~]# netstat -lntp|egrep "nginx|php"
tcp 0 0 127.0.0.1:9000 0.0.0.0: LISTEN 1661/php-fpm: maste
tcp 0 0 0.0.0.0:80 0.0.0.0:
LISTEN 1670/nginx: master
6.上传对应代码至网站目录,然后进行附件提交测试

backup补充部分:与nfs-server保持高度一致

1.安装nfs
[[email protected] ~]# yum install nfs-utils -y
2.配置nfs
[[email protected] ~]# cat /etc/exports
/data/ 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
3.启动nfs
[[email protected] ~]# systemctl enable nfs-server
[[email protected] ~]# systemctl start nfs-server
架构补充部分:进程至后台

[[email protected] ~]# yum install screen -y
[[email protected] ~]# screen -S Socket_name #建议指定一个名称
####进入screen会话---->
1.强制关闭终端
2.ctrl+a+d 退出scrren,但不会结束当前在screen运行的程序
注意: 使用exit会结束screen整个会话
[[email protected] ~]# screen -r [pid|socket_name] #如果指定名称,如果没有指定名称需要使用pid

1.前端页面上传文件->web服务器
2.web存放的代码和附件目录是挂载至nfs存储上
3.web->nfs->backup
nfs故障
在web上卸载nfs的挂载信息
重新挂载至backup
nfs-server:模拟nfs故障

[[email protected] ~]# systemctl stop nfs-server
1.web01处理操作,卸载当前已down的nfs-server,然后重新挂载至backup服务器,解决。
[[email protected] ~]# umount -lf /data/www && mount -t nfs 172.16.1.41:/data/www /data/www/
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
172.16.1.41:/data/www 50G 2.2G 48G 5% /data/www

注意:此时还是无法正常访问web,因为rsync使用的rsync用户管理目录
[[email protected] ~]# cat /etc/rsyncd.conf #变更如下配置
uid = www
gid = www
[[email protected] ~]# systemctl restart rsyncd
[[email protected] ~]# chown -R www.www /data/
[[email protected] ~]# chown -R www.www /backup/
nfs-server:nfs故障后恢复

#1.修复nfs-server后,手动拉取backup下的所有数据,保持一致。
rsync -avz --delete [email protected]::data /data/

#2.在web上卸载,然后重新挂载至nfs存储
[[email protected] ~]# umount /data/www/ && mount -t nfs 172.16.1.31:/data/www /data/www
sersync实时同步
sersync是国人基于rsync+inotify-tools开发的工具,不仅保留了优点同时还强化了实时监控,文件过滤,简化配置等功能,帮助用户提高运行效率,节省时间和网络资源。

sersync优点

1)支持通过配置文件管理
2)真正的守护进程socket(不需要写脚本)。
3)可以对失败文件定时重传(定时任务功能)。
4)第三方的HTTP接口(例如更新cdn缓存)。
5)默认多线程rsync同步。

sersync项目地址

角色 外网IP(NAT) 内网IP(LAN) 安装工具
nfs-server eth0:10.0.0.31 eth1:172.16.1.31 rsync+inotify+sersync
backup eth0:10.0.0.41 eth1:172.16.1.41 rsync-server
Backup操作步骤如下

1.安装rsync

[[email protected] ~]# yum install rsync -y
2.配置rsync

[[email protected] ~]# cat /etc/rsyncd.conf
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.password
log file = /var/log/rsyncd.log
#####################################
[backup]
path = /backup

[data]
path = /data

建立对应密码文件

[[email protected] ~]# echo "rsync_backup:123456" > /etc/rsync.password
4.创建对应目录并授权

[[email protected] ~]# groupadd -g 666 www
[[email protected] ~]# useradd -u 666 -g www www
[[email protected] ~]# id www
uid=666(www) gid=666(www) groups=666(www)
[[email protected] ~]# mkdir -p /{backup,data}
[[email protected] ~]# chown -R www.www /{backup,data}
5.启动rsync

[[email protected] ~]# systemctl enable rsyncd
[[email protected] ~]# systemctl start rsyncd
[[email protected] ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 1680/rsync
Nfs-Server操作步骤如下

1.安装sersync

sersync需要依赖inotify和rsync,所以需要安装对应软件

[[email protected] ~]# yum install rsync inotify -y

安装sersync

[[email protected] ~]# mkdir /server/tools -p
[[email protected] ~]# cd /server/tools/
[[email protected] tools]# wget https://raw.githubusercontent.com/wsgzao/sersync/master/sersync2.5.4_64bit_binary_stable_final.tar.gz
[[email protected] tools]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
[[email protected] tools]# mv GNU-Linux-x86/ /usr/local/sersync
[[email protected] tools]# cd /usr/local/sersync/
[[email protected] sersync]# cp confxml.xml confxml.bak
[[email protected] sersync]# vim confxml.xml
5 <fileSystem xfs="true"/> <!-- 文件系统 -->
6 <filter start="false"> <!-- 排除不想同步的文件-->
7 <exclude expression="(.).svn"></exclude>
8 <exclude expression="(.
).gz"></exclude>
9 <exclude expression="^info/"></exclude>
10 <exclude expression="^static/
"></exclude>
11 </filter>

12 <inotify> <!-- 监控的事件类型 -->
13 <delete start="true"/>
14 <createFolder start="true"/>
15 <createFile start="true"/>
16 <closeWrite start="true"/>
17 <moveFrom start="true"/>
18 <moveTo start="true"/>
19 <attrib start="false"/>
20 <modify start="false"/>
21 </inotify>

23 <sersync>
24 <localpath watch="/data/www"> <!-- 监控的目录 -->
25 <remote ip="172.16.1.41" name="data"/> <!-- backup的IP以及模块 -->
26 <!--<remote ip="192.168.8.39" name="tongbu"/>-->
27 <!--<remote ip="192.168.8.40" name="tongbu"/>-->
28 </localpath>

29 <rsync> <!-- rsync的选项 -->
30 <commonParams params="-az"/>
31 <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
32 <userDefinedPort start="false" port="874"/><!-- port=874 -->
33 <timeout start="true" time="100"/><!-- timeout=100 -->
34 <ssh start="false"/>
35 </rsync>

        <!-- 每60分钟执行一次同步-->

36 <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--def
ault every 60mins execute once-->
3.启动sersync服务守护进程

查看启动参数

[[email protected] sersync]# ./sersync2 -h
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param



参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
c参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序

启动配置,可以使用多套配置
[[email protected] ~]# /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml

原文地址:http://blog.51cto.com/13855748/2170910

时间: 2024-08-29 20:09:57

Rsync+inotify备份的相关文章

rsync+inotify 备份

配置rsync+ inotify 实现实时同步    同步项目实战之rsync篇    1.多种备份方式的介绍    2.rsync实现目录备份    3.配置企业级无交互备份实战    4.配置rsync企业服务器实现实时同步备份方式:        完整备份 rsync 远程同步: rsync(Remote sync)  ==> 做数据备份rsync 客户端好处:优点:支持增量备份       选择性保存:符号链接,硬链接,文件属性,权限及时间等不变.       传输的执行压缩,适用于异地

2-3-2 rsync+inotify备份同步数据

RSYNC = Remote Sync 远程同步 高效,一定要结合shell 官网:https://rsync.samba.org Author: Andrew Tridgell, Wayne Davison, and others Andrew Tridgell是Samba项目的领导者和主要开发人员,同时还在参与开发rsync\Linux Kernel. 与SCP的比较:scp=无法备份大量数据,类似windows的复制 rsync=边复制 ,边统计,边比较 Rsync特性和优点 可以镜像保存

rsync+inotify 实现数据的实时备份

我这个人写一些东西难免要发一番感慨,今天做rsync+inotify实现实时备份,做了好长时间没做出来,这段时间我看了好多博文还有一些视频,但自己做的时候还是没做出来,非常郁闷,就拿起书慢慢的看起来,最终我把思路整理好,又重新试验了一遍终于成功了.是的,你百分之九十的时间在实践,而剩下百分之十的时间才能到达成功,坚持加再看一遍很重要. 我先整理一下大致思路,如有时间,我再整理完整的文档出来. 1.先在两台主机里面安装rsync. 2.在服务节点上配置rsync. 3.在内容发布节点上安装inot

inotify介绍及rsync + inotify 实时同步备份

1.前言 rsync (remote sync)是一款非常好的数据同步工具,能够通过对比同步双方的数据变动,实现增量同步,还可以通过LAN/WAN实现远程多台主机间文件的同步,还能结合crond任务计划来执行自动备份,又可以结合ssh实现远程数据备份的安全,种种特性使他看起来相当优秀.但如果需备份数据十分庞大时,它的不足之处就显现出来了,比如每次执行同步操作时,rsync都会扫描全部数据进而计算出增量部分,而后再同步增量数据,这将会十分耗时,使其变得低效:并且受限于crond计划任务最小时间间隔

通过rsync+inotify实现文件的实时备份同步

原文参考:http://ixdba.blog.51cto.com/2895551/580280/ http://www.bamaol.com/Psy/Showposts-010000108-5048.html 一,rsync安装 目前rsync的最新版本是3.0.9 wget http://rsync.samba.org/ftp/rsync/rsync-3.0.9.tar.gz tar-xvzfrsync-3.0.9.tar.gz;cdrsync-3.0.9; ./configure;make;

rsync+inotify同步备份MYSQL数据

rsync+inotify同步备份MYSQL数据 rsync具有安全性高.备份迅速.支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,但是rsync不能实时的去监测.同步数据.inotify 是一种强大的.细粒度的.异步的文件系统事件监控机制,通过inotify可以监控文件系统中添加.删除,修改.移动等各种细微事件. 实验环境:备份端192.168.1.123(rsync server) 备份源192.168.124(rsync client inotify mysql)

CentOS6.6 rsync+inotify实现数据时时备份

rsync+inotify实现数据时时备份 注意:rsync的daemon模式已提前配置好了,只需要配置inotify即可. 基本环境   系统版本 主机名 IP地址 角色 备份/监控目录 CentOS  6.6 backup 10.0.0.10 rsync服务端 /backup CentOS  6.6 nfs-server 10.0.0.7 rsync客户端 /data inotify安装配置 查看系统是否支持inotify,显示以下三个文件表示支持 [[email protected] to

rsync+inotify实现数据实时同步备份

在实际生产环境当中,我们总会遇见需要把一些重要数据进行备份,且随着应用系统规模的增大,对数据的安全性.可靠性.时效性要求还是比较高的, 因此我自己有在用rsync+inotify来实现数据实时同步备份,下面记录下操作步骤,以防日后自己忘记. 实验背景: 操作系统          IP         机器名        角色 CentOS 7.2       172.16.22.1     nginx01        数据源(服务器端) CentOS 7.2       172.16.22

通过rsync+inotify实现数据实时备份同步

一.环境描述 测试环境 需求:服务器A与服务器B为主备服务模式,需要保持文件一致性,现采用sersync基于rsync+inotify实现数据实时同步 环境描述: 主服务器172.26.7.50 ,从服务器172.26.7.51 实时同步/home/ 及/download 目录到从服务器 二.实施方法 1.从服务器172.26.7.51 rsync服务搭建 1.1下载软件包至从服务器 下载地址:http://rsync.samba.org/ftp/rsync/src 可根据环境需求下载相应的软件