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

前端之家收集整理的这篇文章主要介绍了centos7.x rsync+inotify实时监控备份前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

#简介

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解析比如:

a10.0.0.1

b 10.0.0.2


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

#rsync 默认端口是873

yuminstallrsync-y
cat/etc/rsyncd.conf
######
#/etc/rsyncd:configurationfileforrsyncdaemonmode

#Seersyncd.confmanpageformoreoptions.

#configurationexample:

#uid=nobody
#gid=nobody
#usechroot=yes
#maxconnections=4
#pidfile=/var/run/rsyncd.pid
#exclude=lost+found/
#transferlogging=yes
#timeout=900
#ignorenonreadable=yes
#dontcompress=*.gz*.tgz*.zip*.z*.Z*.rpm*.deb*.bz2

#[ftp]
#path=/home/ftp
#comment=ftpexportarea
logfile=/var/log/rsyncd.log
pidfile=/var/run/rsyncd.pid
lockfile=/var/run/rsync.lock
secretsfile=/etc/rsync.pass
motdfile=/etc/rsyncd.Motd

[chunk]

path=/root/test
comment=chunk
uid=root
gid=root
port=873
usechroot=no
readonly=no
list=no
maxconnections=200
timeout=600
authusers=rsync
hostsallow=10.0.0.0/24
hostsdeny=0.0.0.0/0
##创建用户认证文件
echo"chunk:123456">/etc/rsync.pass
#启动rsyncd程序
servicersyncdstart
systemctlenablersyncd
systemctlstatusrsyncd
●rsyncd.service-fastremotefilecopyprogramdaemon
Loaded:loaded(/usr/lib/systemd/system/rsyncd.service;enabled;vendorpreset:disabled)
Active:active(running)sinceFri2017-07-1416:39:18HKT;9minago
MainPID:598(rsync)
CGroup:/system.slice/rsyncd.service
└─598/usr/bin/rsync--daemon--no-detach

至此目标服务器已经完成


#源服务器端

#sersync在centos7上运行无法正常运行,也没有报错,经不起考验。
#inotify监控是经得起考验的,直接yum就可以安装了,当然也可以源码编译安装,不过还是别折腾啦
yuminstallrsync-y
yuminstallinotify-tools-y
#修改inotify默认参数(inotify默认内核参数值太小)
查看系统默认参数值

sysctl-a|grepmax_queued_events

结果是:fs.inotify.max_queued_events=16384

sysctl-a|grepmax_user_watches

结果是:fs.inotify.max_user_watches=8192

sysctl-a|grepmax_user_instances

结果是:fs.inotify.max_user_instances=128

修改参数:

sysctl-wfs.inotify.max_queued_events="99999999"

sysctl-wfs.inotify.max_user_watches="99999999"

sysctl-wfs.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实例最大值


#添加/etc/passwd.txt文件
echo"123456">/etc/passwd.txt
#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"
foripin$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'-eclose_write,modify,delete,create,attrib,move$srcdir|whilereadfile
do
foripin$dstip
do
rsync-avH--port=873--progress--delete--exclude-from=$excludedir$srcdir$rsyncuser@$ip::$dstdir--password-file=$rsyncpassdir
echo"${file}wasrsynced">>/tmp/rsync.log2>&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 #脚本运行日志记录


#添加权限
chmod+xrsync.sh

#设置脚本开机自动执行
vi/etc/rc.d/rc.local
sh/usr/local/inotify/rsync.sh&

#本地执行一次
sh/usr/local/inotify/rsync.sh&
测试命令:
#把源服务器文件传到目标服务器
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 显示帮助信息

============================================

inotify参数

-m是保持一直监听

-r是递归查看目录

-q是打印出事件

-ecreate,move,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=TIMEIP超时时间,单位为秒

-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带宽,KBytespersecond

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

============================================


参考:

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

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

原文链接:https://www.f2er.com/centos/376765.html

猜你在找的CentOS相关文章