centos7.x rsync+inotify实时监控备份

news/2024/7/4 13:09:09

#简介

rsync 官方描述 他是一个快速拷贝工具,可以复制到本地或者远程

Rsync is a fast and extraordinarily versatile file  copying  tool.   It

       can  copy  locally,  to/from  another  host  over  any remote shell, or

       to/from a remote rsync daemon.  It offers a  large  number  of  options

       that  control  every  aspect  of  its behavior and permit very flexible

       specification of the set of files to be copied.  It is famous  for  its

       delta-transfer  algorithm,  which  reduces the amount of data sent over

       the network by sending only the differences between  the  source  files

       and  the  existing  files in the destination.  Rsync is widely used for

       backups and mirroring and as an improved copy command for everyday use.


       Rsync finds files that need to be transferred  using  a  "quick  check"

       algorithm  (by  default) that looks for files that have changed in size

       or  in  last-modified  time.   Any  changes  in  the  other   preserved

       attributes  (as  requested by options) are made on the destination file

       directly when the quick check indicates that the file’s data  does  not

       need to be updated.



inotify 实时监控文件,主要是结合shell

DESCRIPTION

       inotifywait efficiently waits for changes to files using  Linux's  ino‐

       tify(7)  interface.   It  is  suitable for waiting for changes to files

       from shell scripts.  It can either exit once an event occurs,  or  con‐

       tinually execute and output events as they occur.


#安装

源服务器安装 inotify和 rsync客户端

目标服务器安装 rsync服务端

最好做一下hosts解析比如:

a    10.0.0.1

b    10.0.0.2


#目标服务端    把源服务器的/etc/ansible/目录下的所有文件备份到/root/test目录

#rsync 默认端口是873

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
yum  install  rsync  -y
cat  /etc/rsyncd .conf
######
# /etc/rsyncd: configuration file for rsync daemon mode
 
# See rsyncd.conf man page for more options.
 
# configuration example:
 
# uid = nobody
# gid = nobody
# use chroot = yes
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
 
# [ftp]
#        path = /home/ftp
#        comment = ftp export area
log  file  /var/log/rsyncd .log
pid  file  /var/run/rsyncd .pid
lock  file  /var/run/rsync .lock
secrets  file  /etc/rsync .pass
motd  file  /etc/rsyncd .Motd
 
[chunk]
 
path =  /root/test
comment = chunk
uid = root
gid = root
port = 873
use chroot = no
read  only = no
list = no
max connections = 200
timeout = 600
auth  users  rsync
hosts allow = 10.0.0.0 /24
hosts deny = 0.0.0.0 /0
1
2
##创建用户认证文件
echo  "chunk:123456"  /etc/rsync .pass
1
2
3
4
5
6
7
8
9
10
#启动rsyncd程序
service rsyncd start
systemctl  enable  rsyncd
systemctl status rsyncd
● rsyncd.service - fast remote  file  copy program daemon
    Loaded: loaded ( /usr/lib/systemd/system/rsyncd .service; enabled; vendor preset: disabled)
    Active: active (running) since Fri 2017-07-14 16:39:18 HKT; 9min ago
  Main PID: 598 ( rsync )
    CGroup:  /system .slice /rsyncd .service
            └─598  /usr/bin/rsync  --daemon --no-detach

至此目标服务器已经完成


#源服务器端

1
2
3
4
#sersync在centos7上运行无法正常运行,也没有报错,经不起考验。
#inotify监控是经得起考验的,直接yum就可以安装了,当然也可以源码编译安装,不过还是别折腾啦
yum  install  rsync  -y
yum  install  inotify-tools -y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#修改inotify默认参数(inotify默认内核参数值太小)
查看系统默认参数值
 
sysctl -a |  grep  max_queued_events
 
结果是:fs.inotify.max_queued_events = 16384
 
sysctl -a |  grep  max_user_watches
 
结果是:fs.inotify.max_user_watches = 8192
 
sysctl -a |  grep  max_user_instances
 
结果是:fs.inotify.max_user_instances = 128
 
修改参数:
 
sysctl -w fs.inotify.max_queued_events= "99999999"
 
sysctl -w fs.inotify.max_user_watches= "99999999"
 
sysctl -w fs.inotify.max_user_instances= "65535"
 
vi  /etc/sysctl .conf  #添加以下代码
 
fs.inotify.max_queued_events=99999999
 
fs.inotify.max_user_watches=99999999
 
fs.inotify.max_user_instances=65535
 
:wq!  #保存退出

参数说明:

max_queued_events:

inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确


max_user_watches:

要同步的文件包含多少目录,可以用:find /etc/ansible -type d | wc -l 统计,必须保证max_user_watches值大于统计结果(这里/etc/ansible为同步文件目录)


max_user_instances:

每个用户创建inotify实例最大值


1
2
#添加/etc/passwd.txt文件
echo  "123456"  /etc/passwd .txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#rsync.sh
#/etc/ansible/  ansible后面添加"/"是有区别的,不添加的话,会直接备份ansible目录,多一个"/"表示目录下的所有文件
 
#!/bin/sh
srcdir= /etc/ansible/
dstdir=chunk
excludedir= /script/inotify/exclude .list
rsyncuser= rsync
rsyncpassdir= /etc/passwd .txt
dstip= "10.0.0.41"
for  ip  in  $dstip
   do
     rsync  -avH --port=873 --progress --delete  --exclude-from=$excludedir  $srcdir $rsyncuser@$ip::$dstdir --password- file =$rsyncpassdir
   done
/usr/bin/inotifywait  -mrq --timefmt  '%d/%m/%y %H:%M'  -- format  '%T %w%f%e'  -e close_write,modify,delete,create,attrib,move $srcdir |   while  read  file
   do
     for  ip  in  $dstip
       do
         rsync  -avH --port=873 --progress --delete  --exclude-from=$excludedir  $srcdir $rsyncuser@$ip::$dstdir --password- file =$rsyncpassdir
         echo  "${file} was rsynced"  >>  /tmp/rsync .log 2>&1
       done
   done

脚本参数说明:

srcdir=/etc/ansible/ #源服务器同步目录

dstdir=chunk  #目标服务器rsync同步目录模块名称

excludedir=/usr/local/inotify/exclude.list   #不需要同步的目录,如果有多个,每一行写一个目录,使用相对于同步模块的路径;

#例如:不需要同步/etc/ansible/ 目录下的a目录和b目录下面的b1目录,exclude.list文件可以这样写

a/

b/b1/


rsyncuser=rsync_user  #目标服务器rsync同步用户名

rsyncpassdir=/etc/passwd.txt  #目标服务器rsync同步用户的密码在源服务器的存放路径

dstip="10.0.0.41 192.168.21.127 192.168.21.128"  #目标服务器ip,多个ip用空格分开

/tmp/rsync.log  #脚本运行日志记录 


1
2
3
4
5
6
7
8
9
#添加权限
chmod  +x  rsync .sh
 
#设置脚本开机自动执行
vi  /etc/rc .d /rc . local
sh  /usr/local/inotify/rsync .sh & 
 
#本地执行一次
sh  /usr/local/inotify/rsync .sh &
1
2
3
测试命令:
#把源服务器文件传到目标服务器
rsync  -avH --port=873 --progress --delete  /etc/ansible/  rsync @10.0.0.41::chunk --password- file = /etc/passwd .txt

到此源服务端已经完成,修改添加源服务器文件,可以看到目标服务器也会同步。


rsync命令:

Rsync的命令格式可以为以下六种: 

  rsync [OPTION]... SRC DEST 

  rsync [OPTION]... SRC [USER@]HOST:DEST 

  rsync [OPTION]... [USER@]HOST:SRC DEST 

  rsync [OPTION]... [USER@]HOST::SRC DEST 

  rsync [OPTION]... SRC [USER@]HOST::DEST 

  rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST] 

  对应于以上六种命令格式,rsync有六种不同的工作模式: 

  1)拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式。如:rsync -a /data /backup 

  2)使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包含单个冒号":"分隔符时启动该模式。如:rsync -avz *.c foo:src 

  3)使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径包含单个冒号":"分隔符时启动该模式。如:rsync -avz foo:src/bar /data 

  4)从远程rsync服务器中拷贝文件到本地机。当SRC路径信息包含"::"分隔符时启动该模式。如:rsync -av root@172.16.78.192::www /databack 

  5)从本地机器拷贝文件到远程rsync服务器中。当DST路径信息包含"::"分隔符时启动该模式。如:rsync -av /databack root@172.16.78.192::www 

  6)列远程机的文件列表。这类似于rsync传输,不过只要在命令中省略掉本地机信息即可。如:rsync -v rsync://172.16.78.192/www 


rsync参数的具体解释如下: 

-v, --verbose 详细模式输出 

-q, --quiet 精简输出模式 

-c, --checksum 打开校验开关,强制对文件传输进行校验 

-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD 

-r, --recursive 对子目录以递归模式处理 

-R, --relative 使用相对路径信息 

-b, --backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用--suffix选项来指定不同的备份文件前缀。 

--backup-dir 将备份文件(如~filename)存放在在目录下。 

-suffix=SUFFIX 定义备份文件前缀 

-u, --update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件) 

-l, --links 保留软链结 

-L, --copy-links 想对待常规文件一样处理软链结 

--copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链结 

--safe-links 忽略指向SRC路径目录树以外的链结 

-H, --hard-links 保留硬链结 

-p, --perms 保持文件权限 

-o, --owner 保持文件属主信息 

-g, --group 保持文件属组信息 

-D, --devices 保持设备文件信息 

-t, --times 保持文件时间信息 

-S, --sparse 对稀疏文件进行特殊处理以节省DST的空间 

-n, --dry-run现实哪些文件将被传输 

-W, --whole-file 拷贝文件,不进行增量检测 

-x, --one-file-system 不要跨越文件系统边界 

-B, --block-size=SIZE 检验算法使用的块尺寸,默认是700字节 

-e, --rsh=COMMAND 指定使用rsh、ssh方式进行数据同步 

--rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息 

-C, --cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件 

--existing 仅仅更新那些已经存在于DST的文件,而不备份那些新创建的文件 

--delete 删除那些DST中SRC没有的文件 

--delete-excluded 同样删除接收端那些被该选项指定排除的文件 

--delete-after 传输结束以后再删除 

--ignore-errors 及时出现IO错误也进行删除 

--max-delete=NUM 最多删除NUM个文件 

--partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输 

--force 强制删除目录,即使不为空 

--numeric-ids 不将数字的用户和组ID匹配为用户名和组名 

--timeout=TIME IP超时时间,单位为秒 

-I, --ignore-times 不跳过那些有同样的时间和长度的文件 

--size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间 

--modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0 

-T --temp-dir=DIR 在DIR中创建临时文件 

--compare-dest=DIR 同样比较DIR中的文件来决定是否需要备份 

-P 等同于 --partial 

--progress 显示备份过程 

-z, --compress 对备份的文件在传输时进行压缩处理 

--exclude=PATTERN 指定排除不需要传输的文件模式 

--include=PATTERN 指定不排除而需要传输的文件模式 

--exclude-from=FILE 排除FILE中指定模式的文件 

--include-from=FILE 不排除FILE指定模式匹配的文件 

--version 打印版本信息 

--address 绑定到特定的地址 

--config=FILE 指定其他的配置文件,不使用默认的rsyncd.conf文件 

--port=PORT 指定其他的rsync服务端口 

--blocking-io 对远程shell使用阻塞IO 

-stats 给出某些文件的传输状态 

--progress 在传输时现实传输过程 

--log-format=formAT 指定日志文件格式 

--password-file=FILE 从FILE中得到密码 

--bwlimit=KBPS 限制I/O带宽,KBytes per second 

-h, --help 显示帮助信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
============================================
 
inotify参数
 
-m 是保持一直监听
 
-r 是递归查看目录
 
-q 是打印出事件
 
-e create,move,delete,modify,attrib 是指 “监听 创建 移动 删除 写入 权限” 事件
 
rsync 参数
 
============================================
 
- v , --verbose 详细模式输出
 
-q, --quiet 精简输出模式
 
-c, --checksum 打开校验开关,强制对文件传输进行校验
 
-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
 
-r, --recursive 对子目录以递归模式处理
 
-R, --relative 使用相对路径信息
 
-b, --backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用--suffix选项来指定不同的备份文件前缀。
 
--backup- dir  将备份文件(如~filename)存放在在目录下。
 
-suffix=SUFFIX 定义备份文件前缀
 
-u, --update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件)
 
-l, --links 保留软链结
 
-L, --copy-links 想对待常规文件一样处理软链结
 
--copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链结
 
--safe-links 忽略指向SRC路径目录树以外的链结
 
-H, --hard-links 保留硬链结
 
-p, --perms 保持文件权限
 
-o, --owner 保持文件属主信息
 
-g, --group 保持文件属组信息
 
-D, --devices 保持设备文件信息
 
-t, -- times  保持文件时间信息
 
-S, --sparse 对稀疏文件进行特殊处理以节省DST的空间
 
-n, --dry-run现实哪些文件将被传输
 
-W, --whole- file  拷贝文件,不进行增量检测
 
-x, --one- file -system 不要跨越文件系统边界
 
-B, --block-size=SIZE 检验算法使用的块尺寸,默认是700字节
 
-e, --rsh=COMMAND 指定使用rsh、 ssh 方式进行数据同步
 
-- rsync -path=PATH 指定远程服务器上的 rsync 命令所在路径信息
 
-C, --cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件
 
--existing 仅仅更新那些已经存在于DST的文件,而不备份那些新创建的文件
 
--delete 删除那些DST中SRC没有的文件
 
--delete-excluded 同样删除接收端那些被该选项指定排除的文件
 
--delete-after 传输结束以后再删除
 
--ignore-errors 及时出现IO错误也进行删除
 
--max-delete=NUM 最多删除NUM个文件
 
--partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输
 
--force 强制删除目录,即使不为空
 
--numeric-ids 不将数字的用户和组ID匹配为用户名和组名
 
--timeout=TIME IP超时时间,单位为秒
 
-I, --ignore- times  不跳过那些有同样的时间和长度的文件
 
--size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间
 
--modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0
 
-T --temp- dir =DIR 在DIR中创建临时文件
 
--compare-dest=DIR 同样比较DIR中的文件来决定是否需要备份
 
-P 等同于 --partial
 
--progress 显示备份过程
 
-z, --compress 对备份的文件在传输时进行压缩处理
 
--exclude=PATTERN 指定排除不需要传输的文件模式
 
--include=PATTERN 指定不排除而需要传输的文件模式
 
--exclude-from=FILE 排除FILE中指定模式的文件
 
--include-from=FILE 不排除FILE指定模式匹配的文件
 
--version 打印版本信息
 
--address 绑定到特定的地址
 
--config=FILE 指定其他的配置文件,不使用默认的rsyncd.conf文件
 
--port=PORT 指定其他的 rsync 服务端口
 
--blocking-io 对远程shell使用阻塞IO
 
-stats 给出某些文件的传输状态
 
--progress 在传输时现实传输过程
 
--log- format =formAT 指定日志文件格式
 
--password- file =FILE 从FILE中得到密码
 
--bwlimit=KBPS 限制I /O 带宽,KBytes per second
 
-h, --help 显示帮助信息
 
============================================


参考:

http://www.osyunwei.com/archives/7435.html

http://www.jb51.net/LINUXjishu/66859.html



本文转自 yanconggod 51CTO博客,原文链接:http://blog.51cto.com/yanconggod/1947665


http://www.niftyadmin.cn/n/3461239.html

相关文章

wireshark使用教程-初识wireshark

一.wireshark介绍Wireshark(前称Ethereal)是一个网络封包分析软件。网络封包分析软件的功能是撷取网络封包,并尽可能显示出最为详细的网络封包资料。Wireshark使用WinPCAP作为接口,直接与网卡进行数据报文交换。Wireshark使用目的…

wireshark使用教程-常用抓包规则

使用Wireshark时最常见的问题,是当您使用默认设置时,会得到大量冗余信息,以至于很难找到自己需要的部分。这就是为什么过滤器会如此重要。它们可以帮助我们在庞杂的结果中迅速找到我们需要的信息。过滤器的区别 捕捉过滤器(Captur…

cloudstack4云平台centos6.4安装配置教程二--配置cloudstack

上一篇文章中我们讲到控制节点的安装,这里我们说下控制节点完成后cloudstack的配置,也就是开始配置我们第一个简单云平台。 配置cloudstack 浏览器输入管理节点web地址:http://10.0.0.150:8080/client/ 选择简体中文登录,当然英…

批量删除文件

/da/we/html/a 下面有80万个html 需要删除任意的2w个cd /da/we/html/a;ls -U|head -n 20000|xargs rm -f 1秒钟搞定默认ls是会排序的 这样执行会卡住了。ls -U本文转自 liang3391 51CTO博客,原文链接:http://blog.51cto.com/liang3391/760509

Portable OpenSSH GSSAPI远程代码执行漏洞(CVE-2006-5051)漏洞解决方案

最近给公司内网的服务器做了一次全面的扫描,出现几个高危的漏洞,网上查了一下,普遍存在啊。所谓不扫不知道,一扫吓一跳啊。 漏洞的名称为Portable OpenSSH GSSAPI远程代码执行漏洞(CVE-2006-5051)及OpenSSH J-PAKE授权问题漏洞(CV…

CSS之浮动那些事

1.清除浮动 下面是两种常用的方式&#xff0c;而这两招也够用了(不用千招会&#xff0c;只需一招精)。 1.结尾处加空div标签 clear:both <style type"text/css"> .left{float:left;} .right{float:right;}/*清除浮动代码*/ .clearfloat{clear:both} </style…

网络编程练习这些就ok

1&#xff0c;什么是C/S架构&#xff1f; C指的是client&#xff08;客户端软件&#xff09;&#xff0c;S指的是Server&#xff08;服务端软件&#xff09;一个C/S架构就是&#xff0c;实现服务端软件与客户端软件基于网络通信。互联网中处处是C/S架构如12306网站是服务端&…

Zabbix监控-mingodb数据库状态

最近公司数据库新增加了几台mongodb数据库&#xff0c;为了能实时了解运行的状态&#xff0c;需要增加zabbix对mongodb的运行状态监控。 mongodb默认安装情况下可以运行echo "db.serverStatus()" | mongo 来获取mongodb的运行状态&#xff0c;但是公司数据库安装的时…