标签:
命运是大海,当你能够畅游时,你就要纵情游向你的所爱,因为你不知道狂流什么会到来,卷走一切希望与梦想。
作者:燁未央_Estelle
声明:测试学习,不足之处,欢迎指正。
①备份服务器root@Backup运行rsync进程作为rsync的服务端。NFS作为rsync的客户端。
②在备份服务器安装并正常启动rsync进程服务。并设置修改配置文件。实现远程拉取、推送备份。
③在客户端NFS服务器,作为客户端,配置rsync的相关密码文件。实现无密码远程备份推送
④在监控端安装inotfiy监控软件,实现备份目录的实时监控。
⑤实现客户端目录文件的实时同步。
4.2.1 远程增量推送拉取的实现
##注意rsync的服务端是在Backup服务器进行配置部署,本章内容主要实现,Backup服务器和NFS服务器共享目录的实时同步。当NFS因故障宕机的时候,Backup服务器能后进行接管继续提供服务。
###Bcakup服务器配置操作
##默认/etc/rsyncd.conf配置文件是不存在的,我们手动进行创建配置
###编辑配置文件内容
#rsync_config____________start
#created by TS-19 at 2014-12-17
##rsyncd.conf start##
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 192.168.109.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
#exclude=a,b
[backup]
path = /data/backup/
[data]
path = /data/html/
#服务端Backup配置(root权限):
[tslove@Backup ~]$ su - root
密码
[root@Backup ~]# /etc/rsyncd.conf#====》首先我们看到没有这个文件,路径如此,默认是不存在的
-bash: /etc/rsyncd.conf: 没有那个文件或目录
[root@Backup ~]# vi /etc/rsyncd.conf #====》直接编辑创建rsync的配置文件
#rsync_config____________start
#created by TS 2014-09-13
##rsyncd.conf start##
uid = rsync#====》赋予相应的用户角色 uid设置用户所属群组证明编号(权限赋予)默认是nobody,nfs是65534
use chroot = no #====》安全性考虑目录,比如出现bug之后的文件处理等,内网传输的话,可以不予以考虑。一般定义为空,减少攻击
max connections = 200 #====》最大数量值为200,连接次服务器。也就是最大服务器连接的并发为200
timeout = 300 #====》连接超时设定,超过此时间设定,就会断开链接
pid file = /var/run/rsyncd.pid #====》进程编号文件
lock file = /var/run/rsync.lock #====》服务进程启动、停止锁文件
log file = /var/log/rsyncd.log #====》日志文件
[backup] #====》模块,远程服务器备份路径设定时候需要引用此模块名称。
path = /data/backup/ #====》上传下载,文件处理目录,rsync服务端的一个路径,客户端存放或者从客户端下载的路径。可以理解为共享目录
ignore errors #====》忽略错误,传输过程中如果遇到错误就自动忽略。
read only = false #====》只读为假,就是表示意思为可读可写。相当于nfs后面的权限参数
list = false #====》不允许列表定义
hosts allow = 192.168.109.0/24#====》只允许的链接主机或网段
hosts deny = 0.0.0.0/32 #====》拒绝链接的主机或网段
auth users = rsync_backup #====》rsync 支持虚拟用户rsync_backup。注意不是系统用户
secrets file = /etc/rsync.password#====》用户对应的密码文件,设定后就避免在内网同步时候,需要进行的密码验证
#rsync_config____________end
##如果需要更进一步的了解信息,可以使用 man rsyncd.conf进行信息查看。
[root@Backup ~]# rsync --daemon #====》表示以守护进程的方式启动rsync服务★
[root@Backup ~]# netstat -lntup|grep "873" #====》查看运行端口,rsync的端口为873★tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 2511/rsync
tcp 0 0 :::873 :::* LISTEN 2511/rsync
[root@Backup ~]# ps -ef|grep "rsync" #====》查看进程信息,如果873端口未被监听,服务进程没有启动,我们可以查看日志文件/var/log/rsyncd.log,排错。
root 2511 1 0 16:47 ? 00:00:00 rsync --daemon
[root@Backup ~]# mkdir /data/html/ -p #====》创建我们配置文件模块指定的远程同步的共享目录 /tslove★
[root@Backup ~]# useradd rsync -s /sbin/nologin -M #====》创建虚拟用户rsync.rsync★ 这个地方,要特别注意。
[root@Backup ~]# chown -R rsync.rsync /data/html #====》对我进行远程操作的目录/tslove进行授权给rsync.rsync用户★前面是用户,后面是用户组。
[root@Backup ~]# chown -R rsync.rsync /data/backup
[root@Backup ~]# echo "rsync_backup:123456" >/etc/rsync.password#====》添加密码文件内容★
[root@Backup ~]# cat /etc/rsync.password #====》查看密码文件,冒号前面为虚拟用户名,后面为密码。★
rsync_backup:123456
[root@Backup ~]# chmod 600 /etc/rsync.password#====》改变密码文件权限属性。保证密码文件的安全★
[root@Backup ~]# ll /etc/rsync.password
-rw-------. 1 root root 20 9月 13 17:19 /etc/rsync.password
##至此,服务器端Backup配置完成!注意真正配置完成的前提设置中,防火墙需要关掉,SElinux需要关掉。
[root@Backup ~]# /etc/init.d/iptables stop #====》关闭防火墙
[root@Backup ~]# service iptables status #====》查看防火墙运行状态
iptables:未运行防火墙
###NFS服务器配置操作
#客户端NFS进行配置:
[root@NFS~]# rpm -qa rsync
rsync-3.0.6-9.el6_4.1.x86_64 #====》查看客户端的rsync软件包安装情况
[root@NFS~]# echo "123456" >/etc/rsync.password #====》创建添加密码文件内容(密码)。(注意此时密码文件内容只需要有密码就可以,另外密码文件路径可以更改)★
[root@NFS~]# cat /etc/rsync.password #====》查看确认密码文件
123456
[root@NFS~]# chmod 600 /etc/rsync.password #====》密码文件同样需要更改文件权限★
[root@NFS~]# ll /etc/rsync.password #====》查看确认密码文件权限
-rw-------. 1 root root 7 Sep 13 17:45 /etc/rsync.password
##至此,NFS客户端端配置完成!注意真正配置完成的前提设置中,防火墙需要关掉,SElinux需要关掉。
[root@NFS~]# /etc/init.d/iptables stop#====》关闭防火墙
[root@NFS~]# service iptables status #====》查看防火墙运行状态
iptables: Firewall is not running.
##到此,我们的rsync远程备份的 daemon模式的配置,已经完成!
rsync -avz rsync_backup@IP地址::指定模块 本地存放目录 --password-file=/etc/rsync.password
(单人模式的第一种操作,推送和拉取需要注意调换位置)
rsync -avz rsync://rsync_backup@IP/指定模块 本地存放目录 --password-file=/etc/rsync.password
(单人模式的第二种操作,推送和拉取需要注意调换位置)
################ 远程拉取 ###################
###在服务端Backup服务器创建测试文件进行测试
[root@Backup html]# cd ../backup/
[root@Backup backup]# ll
total 0
[root@Backup backup]# touch {1..5}.txt
[root@Backup backup]# ll
total 0
-rw-r--r-- 1 root root 0 Dec 20 23:31 1.txt
-rw-r--r-- 1 root root 0 Dec 20 23:31 2.txt
-rw-r--r-- 1 root root 0 Dec 20 23:31 3.txt
-rw-r--r-- 1 root root 0 Dec 20 23:31 4.txt
-rw-r--r-- 1 root root 0 Dec 20 23:31 5.txt
###客户端NFS服务器进行远程拉取(注意此时需要密码验证)
[root@NFS backup]# ll
total 0
[root@NFS backup]# rsync -avz rsync_backup@192.168.109.164::backup .
Password:
receiving incremental file list
./
1.txt
2.txt
3.txt
4.txt
5.txt
sent 162 bytes received 348 bytes 68.00 bytes/sec
total size is 0 speedup is 0.00
###免密码机型远程拉取
[root@NFS backup]# rsync -avz rsync_backup@192.168.109.164::backup . --password-file=/etc/rsync.password
receiving incremental file list
sent 64 bytes received 165 bytes 458.00 bytes/sec
total size is 0 speedup is 0.00
################ 远程推送 ###################
##客户端创建测试文件进行远程推送
[root@NFS backup]# rm -f *
[root@NFS backup]# ll
total 0
[root@NFS backup]# touch {a..f}.log
[root@NFS backup]# ll
total 0
-rw-r--r-- 1 root root 0 Dec 20 23:38 a.log
-rw-r--r-- 1 root root 0 Dec 20 23:38 b.log
-rw-r--r-- 1 root root 0 Dec 20 23:38 c.log
-rw-r--r-- 1 root root 0 Dec 20 23:38 d.log
-rw-r--r-- 1 root root 0 Dec 20 23:38 e.log
-rw-r--r-- 1 root root 0 Dec 20 23:38 f.log
[root@NFS backup]# rsync -avz /data/backup/ rsync_backup@192.168.109.164::backup --password-file=/etc/rsync.password
sending incremental file list
./
a.log
b.log
c.log
d.log
e.log
f.log
sent 313 bytes received 125 bytes 876.00 bytes/sec
total size is 0 speedup is 0.00
###服务端Backup服务器查看验证
[root@Backup backup]# ll
total 0
-rw-r--r-- 1 root root 0 Dec 20 23:31 1.txt
-rw-r--r-- 1 root root 0 Dec 20 23:31 2.txt
-rw-r--r-- 1 root root 0 Dec 20 23:31 3.txt
-rw-r--r-- 1 root root 0 Dec 20 23:31 4.txt
-rw-r--r-- 1 root root 0 Dec 20 23:31 5.txt
[root@Backup backup]# ll
total 0
-rw-r--r-- 1 root root 0 Dec 20 23:31 1.txt
-rw-r--r-- 1 root root 0 Dec 20 23:31 2.txt
-rw-r--r-- 1 root root 0 Dec 20 23:31 3.txt
-rw-r--r-- 1 root root 0 Dec 20 23:31 4.txt
-rw-r--r-- 1 root root 0 Dec 20 23:31 5.txt
-rw-r--r-- 1 rsync rsync 0 Dec 20 23:38 a.log
-rw-r--r-- 1 rsync rsync 0 Dec 20 23:38 b.log
-rw-r--r-- 1 rsync rsync 0 Dec 20 23:38 c.log
-rw-r--r-- 1 rsync rsync 0 Dec 20 23:38 d.log
-rw-r--r-- 1 rsync rsync 0 Dec 20 23:38 e.log
-rw-r--r-- 1 rsync rsync 0 Dec 20 23:38 f.log
###注意推送的时候。推送文件的路径,是否包含目录。以及推送拉去的是指定的模块(文件路径)
[root@NFS backup]# rsync -avz /data/backup rsync_backup@192.168.109.164::backup --password-file=/etc/rsync.password
sending incremental file list
backup/
backup/a.log
backup/b.log
backup/c.log
backup/d.log
backup/e.log
backup/f.log
sent 333 bytes received 126 bytes 918.00 bytes/sec
total size is 0 speedup is 0.00
[root@Backup backup]# ll
total 4
-rw-r--r-- 1 root root 0 Dec 20 23:31 1.txt
-rw-r--r-- 1 root root 0 Dec 20 23:31 2.txt
-rw-r--r-- 1 root root 0 Dec 20 23:31 3.txt
-rw-r--r-- 1 root root 0 Dec 20 23:31 4.txt
-rw-r--r-- 1 root root 0 Dec 20 23:31 5.txt
-rw-r--r-- 1 rsync rsync 0 Dec 20 23:38 a.log
drwxr-xr-x 2 rsync rsync 4096 Dec 20 23:40 backup
-rw-r--r-- 1 rsync rsync 0 Dec 20 23:38 b.log
-rw-r--r-- 1 rsync rsync 0 Dec 20 23:38 c.log
-rw-r--r-- 1 rsync rsync 0 Dec 20 23:38 d.log
-rw-r--r-- 1 rsync rsync 0 Dec 20 23:38 e.log
-rw-r--r-- 1 rsync rsync 0 Dec 20 23:38 f.log
#①--exclude=PATTERN exclude files matching PATTERN (排除文件比较少的情况下使用)
#②--exclude-from=FILE read exclude patterns from FILE (排除的内容放到文件中去)
#③排除的方法:第一个就是在客户端进行排除,通过命令实现。第二个方法就是在服务端通过配置参数来进行排除。
#④排除多个文件参数的使用--exclude={a,b,c}进行排除操作。
###注意事项
rsync服务端配置
#①创建编辑vi /etc/rsyncd.conf 一定要带d。(用户rsync、目录、虚拟用户、及密码文件、模块)
#②配置文件完成后,创建共享目录存在。(创建rsync用户,并且授权访问共享目录)
#③创建密码文件,复制配置文件的路径,然后添加文件内容 (虚拟用户名:密码)
#④注意密码文件权限
#⑤rsync --daemon 启动服务然后放入/etc/rc.local。
#⑥报错的话,查看tail /var/log/rsyncd.log日志文件。(配置文件指定日志文件)
rsync客户端配置 (客户端可以有多个)
#⑦密码文件和服务端没有任何关系
#⑧--passwd-file=/etc/rsync.passwd 内容只是密码这个密码文件的路径可以是任意指定的,我们只是尽量避免交互式的操作。
#⑨密码文件的权限要设置600权限.
#⑩同步:一个是推。一个是拉,命令上每个都分两种。
#排错相关:
#①首先确保防火墙 和selinux关掉。后期学习防火墙设置之后,此两项也可以正确开启。
#②排错要看日志文件/var/log/rsyncd.log
#③整个部署流程考虑。看是否修改过某个流程文件。
#④部署的流程一定要熟练。学会看服务端日志,一个是日志输出,一个是命令行的输出。
#⑤排错的能力练习,学会模拟,重视日常错误。
#⑥注意问题:无共享目录,共享目录无授权,防火墙开启、密码文件的正确。以及密码文件的权限放大。
#⑦是否双向备份要看是否双向写。
#注意事项:
#①每一个过程都是通过rsync实现。备份一般都是定时的。但是有时候也需要时时的备份。避免宕机造成数据丢失。
#②第一个方案rsync+serrsync第二个方案:rsync+inotify,来时时监控数据变化,进行数据同步。rsync一般用于存储的数据实时备份。
#③每次检查备份的最末端的数据,这样前面的额数据都是正常的。生产场景有时候需要两台服务器之间,必须要求数据一致,比如两台负载均衡下的WEB服务器、服务器接管同步。
#④--delete 无差异同步,操作起来简单,但是用起来影响非常大。在没有做事情之前,要先想想后果。运维工作的原则就是,在能满足需求的情况下,要使用自己能掌控的软件。
#⑤rsync支持拷贝特殊文件,如链接文件、设备文件等可以有排除指定目录或文件同步功能,相当与tar的排除功能。
#⑥推送目录的格式,/data表示连目录一起推送过去,/data/则只是推送目录下的文件至远程服务端。
#⑦注意密码文件、共享目录权限。
#⑧我们需要把rsync --daemon放入到/etc/rc.local中★
#⑨推送到企业场景:主要就是备份,如果使用-- delete,那么客户端方面的数据丢失,同步到NFS服务器之后,也会造成NFS服务器的数据丢失。本地有啥,远端就有啥(远端目录数据可能丢失)。★
#⑩拉取到企业场景:就是i就是应用在代码发布等场景。如果使用--delete参数,那么远端有啥,本地就有啥。可能造成(本地目录文件的额丢失)。★1.rsyncd.conf配置文件
在[moudle]之前的参数都是全局参数,也可以在全局参数下定义部分模块参数,这时该参数的值就是所有模块的默认值。
port:指定后台程序使用的端口号,默认是873
log file:指定rsync的日志文件,而不把日志发送给syslog
pid file:指定rsync的pid文件,通常指定为/var/run/rsyncd.pid
motd file:用来指定一个消息文件,当客户连接服务器时,将该文件内容显示给客户,默认是没有该文件的
2.模块参数
主要定义服务器哪个目录需要被同步。其格式必须为[moudle]形式,这个名字是在rsync客户端看到的名字,而服务器真正同步的数据是通过path 来指定的,我们可以通过根据自己的需要,来指定多个模块,模块中可以定义以下参数:
comment:给模块指定一个描述,该描述连同模块名在客户连接得到模块列表时显示给客户,默认是没有描述定义
path:指定该模块的供备份的目录树路径,该参数是必须指定的
use chroot:如果“use chroot”指定为true,那么rsync在传输文件以前首先chroot到path参数所指定的目录下。这样做的原因是实现额外的安全防护,但是缺点是需要root权限,并且不能备份指向外部的符号连接所指向的目录文件,默认情况下chroot的值为true
uid:该选项指定当该模块传输文件时守护进程应该具有的uid,配合gid选项使用可以确定哪些可以访问怎么样的文件权限,默认值是“nobody”
gid:该选项指定当该模块传输文件时守护进程应该具有的gid,默认值是“nobody”
max connections:指定该模块的最大并发连接数量以保护服务器,超过限制的连接请求被告知随后再试。默认值是0,也就是没有限制。
list:该选项设定当客户请求可以使用的模块列表时,该模块是否应该被列出。如果该选项设置为false,可以创建隐藏的模块,默认值为true。
read only:该选项设定是否允许客户上载文件,如果为true那么所有的上载请求都会失败,如果为false并且服务器目录读写权限允许那么上载是允许的,默认值为true。
exclude:用来指定多个空格隔开的多个文件或目录(相对路径),并将其添加到exclude列表中,这等同于在客户端的命令中使用-exclude 来指定模式,一个模块只能指定一个exclude选项,但是需要注意的一点是该选项有一定的安全性问题,客户很有可能绕过exclude列表,如果希望保持特定的文件不能被访问,那就最好结合uid/gid一起使用。
exclude from:指定一个包含exclude模式的定义的文件名,服务器从该文件中读取exclude列表定义
include :用来指定不排除符合要求的文件或目录,这等同于在客户端命令中使用-include来指定模式,结合include和exclude可以定义复杂的exclude/include规则
include from:指定一个包含include模式的定义的文件名,服务器从该文件中读取include列表定义。
auth users:该选项指定由空格或逗号分隔的用户列表,只有这些用户才允许连接该模块,这里的用户和系统用户没有任何关系,如果“auth users”被设置,那么客户端发出对该模块的连接请求以后会被rsync请求challenged进行验证身份,这里使用的 challenge/response认证协议,用户的名和密码以明文方式存放在“secret file”选项指定的文件中,默认情况下无需密码就可以连接模块,也就是匿名模式
secrets file:该选项指定一个包含定义用户名:密码对的文件,只有在“auth users”被定义的时候,该文件才有作用,文件每行包含一个username:passwd对。一般来说密码最好不要超过8个字符。没有默认的 secures file名,需要限式指定一个(例如:/etc/rsyncd.passwd)。注意:该文件的权限一定要是600,否则客户端将不能连接服务器。
stick modes:该选项指定是否监测密码文件的权限,如果该选项值为true,那么密码文件只能被rsync服务器运行身份的用户访问,其他任何用户不能访问该文件,默认值为true
hosts allow: 该选项指定哪些IP的客户允许连接该模块。客户模式定义可以是以下形式:
单个IP地址,例如:192.167.0.1
整个网段,例如:192.168.0.0/24,也可以是192.168.0.0/255.255.255.0
多个IP或网段需要用空格隔开,“*”则表示所有,默认是允许所有主机连接。
hosts deny:指定不允许连接rsync服务器的机器,可以使用hosts allow的定义方式来进行定义。默认是没有hosts deny定义。
ignore errors:指定rsyncd在判断是否运行传输时的删除操作时忽略server上的IO错误,一般来说rsync在出现IO错误时将将跳过–delete操作,以防止因为暂时的资源不足或其它IO错误导致的严重问题。
ignore nonreadable:指定rsync服务器完全忽略那些用户没有访问权限的文件,这对于在需要备份的目录中有些文件所不应该被备份者得到的情况下是有意义的
lock file 指定支持max connections参数的锁文件,默认值是/var/run/rsyncd.lock。
transfer logging 使rsync服务器使用ftp格式的文件来记录下载和上载操作在自己单独的日志中。
timeout 通过该选项可以覆盖客户指定的IP超时时间。通过该选项可以确保rsync服务器不会永远等待一个崩溃的客户端。超时单位为秒钟,0表示没有超时定义,这也是默认值。对于匿名rsync服务器来说,一个理想的数字是600。
refuse options 通过该选项可以定义一些不允许客户对该模块使用的命令参数列表。这里必须使用命令全名,而不能是简称。但发生拒绝某个命令的情况时服务器将报告错误信息然后退出。如果要防止使用压缩,应该是:” dont compress = *” 。
dont compress 用来指定那些不进行压缩处理再传输的文件,默认值是*.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz
3.Rsync命令
在对rsync服务器配置结束以后,下一步就需要在客户端发出rsync命令来实现将服务器端的文件备份到客户端来。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]
1)拷贝本地文件:当SRC和DEST路径信息都不包括单个冒好“:”分隔符时就启动这种工作模式,如:rsync -a /data /backup
2)使用一个远程的shell程序(如rsh,ssh)来实现将本地机器的内容拷贝到远程机器。当DEST路径包括单个冒号“:”分隔符,就启动这种工作模式,如:rsync -avz *.c foo:src
3)使用一个远程的shell程序(如rsh,ssh)来实现将远程机器的内容拷贝到本地机器。当SRC路径包含单个冒号“:” 分隔符,就器送这种工作模式,如:rsync -avz foo:src/bar /backup
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 显示帮助信息
#注意实现的前提是,rsync --daemon进城服务正常启动,可以进行手动的推送拉取(命令实现)。
①innotify软件需要安装在被监控目录变化的服务端,比如我们集群架构需要实现的监控NFS服务端的目录文件的变化,然后把变化的文件实时同步,推送可Backup服务器进行备份,只有这样我们才能保证,在NFS服务器宕机的时候,Backup服务能够顺利接管NFS服务器,进而顺利的继续提供服务。
②Inotify 是一个 Linux特性,它监控文件系统操作,比如读取、写入和创建。Inotify 反应灵敏,用法非常简单,并且比 cron 任务的繁忙轮询高效得多。从 Linux 2.6.13 内核开始,Linux 就推出了 inotify,允许监控程序打开一个独立文件描述符,并针对事件集监控一个或者多个文件,例如打开、关闭、移动/重命名、删除、创建或者改变属性。
③ inotify需要服务端有一个rsync --daemon进程,客户端有一个inotify进程,两边均有一个实时同步的目录。本集群架构,实现,当NFS服务器有文件变化的时候,Inotify监控到文件目录的变化之后,将变化的文件目录,增量推送到Backup服务器。(触发机制:文件系统有变化的时候,就会触发这个机制,触发同步)。
④Inotify的实现有几款软件,最常见的就是它自身,inotify。另一款常见的软件是sersync(国内开发),还有一种lsyncd(不太常见)。相对比来说inotify的性能更高。同步效率更好。
⑤实时同步的监控推送中,按照目录来进行的推送比目标为文件的效率要高。
##配置实时同步环境,内核版本检查以及rsync的正长daemon进程工作。
[root@Backup ~]# rsync --daemon
[root@Backup ~]# ps -ef|grep rsync
root 1020 1 0 09:18 ? 00:00:00 rsync --daemon
root 1022 996 0 09:18 pts/0 00:00:00 grep rsync
[root@Backup ~]# uname -rm
2.6.32-431.el6.x86_64 x86_64
#需要注意的是inotify软件是安装部署在被监控的,所以我们在NFS服务器上进行安装配置
[root@NFS ~]# ls -l /proc/sys/fs/inotify/ ##虚拟的文件系统,如有三个文件,表示支持inotify。
total 0
-rw-r--r-- 1 root root 0 Dec 21 09:24 max_queued_events ###监控事件的最大值
-rw-r--r-- 1 root root 0 Dec 21 09:24 max_user_instances
-rw-r--r-- 1 root root 0 Dec 21 09:24 max_user_watches
[root@NFS ~]# cat /proc/sys/fs/inotify/max_queued_events
16384 ###监控的队列的数值太小,可能影响实时同步的性能。生产场景需要优化。
##进行解压安装
[root@NFS ~]# cd /home/tslove/toos/
[root@NFS toos]# ll
total 352
-rw-r--r-- 1 root root 358772 Dec 13 2012 inotify-tools-3.14.tar.gz
[root@NFS toos]# tar xf inotify-tools-3.14.tar.gz
[root@NFS toos]# cd inotify-tools-3.14
[root@NFS inotify-tools-3.14]# ./configure --prefix=/application/inotify-tools-3.14
[root@NFS inotify-tools-3.14]# echo $?
0
[root@NFS inotify-tools-3.14]# make && make install
[root@NFS inotify-tools-3.14]# echo $?
0
[root@NFS inotify-tools-3.14]# ln -s /application/inotify-tools-3.14/ /application/inotify
[root@NFS inotify-tools-3.14]# ll /application/inotify/
total 16
drwxr-xr-x 2 root root 4096 Dec 21 09:37 bin ##inotify的执行命令
drwxr-xr-x 3 root root 4096 Dec 21 09:37 include ##inotify程序用的头文件
drwxr-xr-x 2 root root 4096 Dec 21 09:37 lib ##inotify动态链接库文件
drwxr-xr-x 4 root root 4096 Dec 21 09:37 share ##帮助文档之类
[root@NFS inotify]# LANG=en
[root@NFS inotify]# tree
.
|-- bin
| |-- inotifywait
| `-- inotifywatch
|-- include
| `-- inotifytools
| |-- inotify-nosys.h
| |-- inotify.h
| `-- inotifytools.h
|-- lib
| |-- libinotifytools.a
| |-- libinotifytools.la
| |-- libinotifytools.so -> libinotifytools.so.0.4.1
| |-- libinotifytools.so.0 -> libinotifytools.so.0.4.1
| `-- libinotifytools.so.0.4.1
`-- share
|-- doc
| `-- inotify-tools
| |-- doxygen.css
| |-- doxygen.png
| |-- files.html
| |-- globals.html
| |-- globals_func.html
| |-- index.html
| |-- inotifytools_8c_source.html
| |-- inotifytools_8h.html
| |-- inotifytools_8h_source.html
| |-- pages.html
| |-- tab_b.gif
| |-- tab_l.gif
| |-- tab_r.gif
| |-- tabs.css
| `-- todo.html
`-- man
`-- man1
|-- inotifywait.1
`-- inotifywatch.1
9 directories, 27 files
###命令的使用:
inotifywait
[root@NFS inotify]# ./bin/inotifywait --help
-r|--recursive Watch directories recursively. ##递归查询目录
-q|--quiet Print less (only print events). ##打印监控事件信息
-m|--monitor Keep listening for events forever. Without ##始终保持事件监听状态
this option, inotifywait will exit after one
event is received.
--excludei <pattern> ##排除监听文件或目录,不区分大小写
Like --exclude but case insensitive.
-qq Print nothing (not even events).
--format <fmt> Print using a specified printf-like format ##执行输出指定格式的字符串
string; read the man page for more details.
--timefmt <fmt> strftime-compatible format string for use with ##执行输出的时间格式
%T in --format string.
-c|--csv Print events in CSV format.
-t|--timeout <seconds> ##监听超时时间
When listening for a single event, time out after
waiting for an event for <seconds> seconds.
If <seconds> is 0, inotifywait will never time out.
-e|--event <event1> [ -e|--event <event2> ... ] ##通过此参数,指定监控的事件
Listen for specific event(s). If omitted, all events are
listened for.
Events:
access file or directory contents were read ##监控文件或目录的读取访问
modify file or directory contents were written ##监控文件或目录备修改
attrib file or directory attributes changed ##监控文件或目录属性的改变
close_write file or directory closed, after being opened in ##文件或目录关闭写模式
writeable mode
close_nowrite file or directory closed, after being opened in
read-only mode
close file or directory closed, regardless of read/write mode
open file or directory opened ##文件或目录被打开
moved_to file or directory moved to watched directory ##文件或目录被从监听目录移动到另一个目录
moved_from file or directory moved from watched directory ##文件或目录被移动到当前监听目录
move file or directory moved to or from watched directory ##文件或目录被移动到另一个目录,或从别的目录移动到当前目录
create file or directory created within watched directory ##文件或目录被创建
delete file or directory deleted within watched directory ##文件或目录被删除
delete_self file or directory was deleted
unmount file system containing file or directory unmounted ##文件系统被卸载
Exit status: ###执行监听返回状态信息。0成功,1失败,2超时。
0 - An event you asked to watch for was received.
1 - An event you did not ask to watch for was received
(usually delete_self or unmount), or some error occurred.
2 - The --timeout option was given and no events occurred
in the specified interval of time
##测试监控事件
命令:/application/inotify/bin/inotifywait -mrq --timefmt ‘%d/%m/%y H%:M%‘ -e 事件 监控目录 (监控多个事件,事件之间使用逗号隔开)
##注意close_write事件和creste使用区别。前者侧重于写入事件的监听。
[root@NFS inotify]# /application/inotify/bin/inotifywait -mrq --timefmt ‘%d/%m/%y H%:M%‘ -e create /data/backup
#克隆标签在监听目录进行创建工作
[root@NFS ~]# cd /data/backup/
[root@NFS backup]# mkdir test
[root@NFS backup]# touch {1..5}.log
##处于监听状态的信息显示
[root@NFS inotify]# /application/inotify-tools-3.14/bin/inotifywait -mrq --timefmt ‘%d/%m/%y %H:%M‘ --format ‘%T %w%f‘ -e create /data/backup
21/12/14 10:18 /data/backup/test
21/12/14 10:18 /data/backup/1.log
21/12/14 10:18 /data/backup/2.log
21/12/14 10:18 /data/backup/3.log
21/12/14 10:18 /data/backup/4.log
21/12/14 10:18 /data/backup/5.log
##提示,按照上述格式的监听记录,在使用rsync推送到的时候,变量的截取使用不是很方便,所以,我们一般就是去掉时间格式,直接监听记录事件,在实时同步的时候,直接作为一个变量来引用。
##简化监听信息的输出:/application/inotify/bin/inotifywait -mrq --format ‘%w%f‘ -e create,delete /data/backup
##调整配置参数优化
[root@NFS inotify]# cd /proc/sys/fs/inotify/
[root@NFS inotify]# cat *
16384
128
8192
[root@NFS inotify]# ll
total 0
-rw-r--r-- 1 root root 0 Dec 21 09:24 max_queued_events
-rw-r--r-- 1 root root 0 Dec 21 09:24 max_user_instances
-rw-r--r-- 1 root root 0 Dec 21 09:24 max_user_watches
[root@NFS inotify]# echo "500000" > /proc/sys/fs/inotify/max_user_watches
[root@NFS inotify]# echo "320000" > /proc/sys/fs/inotify/max_queued_events
##inotify的生产环境测是支持并发200-300每秒,支持文件的大小10-500K。
##具体案例详细脚本操作,参看脚本解析分类。4.2.4 Inotify的生产应用脚本参考
①局限:并发不能大于200左右,(10-100K)。
②文件数量特别大的时候,推送的效率不高。所以一般适用于小文件的实时同步。
##第一个脚本:
[root@NFS scripts]# cat intoify.sh
#!/bin/sh
srcdir=/data/html/
dstdir=data
rsyncuser=rsync_backup
rsyncpassdir=/etc/rsync.password
dstip="192.168.109.164"
for ip in $dstip
do
rsync -avz --stats --progress --delete $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdir
done
/usr/local/inotify-tools-3.14/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 -avz --stats --progress --delete $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdir
echo " ${file} was rsynced" >> /tmp/rsync.log 2>&1
done
done
##第二个脚本
[root@NFS scripts]# cat intoify1.sh
#!/bin/sh
cmd="/application/inotify/bin/inotifywait"
$cmd -mrq --format ‘%w%f‘ -e create,close_write,delete /backup|while read line
do
[ ! -e "$line" ] && continue
rsync -az --delete $line rsync_backup@192.168.109.164::data --password-file=/etc/rsync.password
done
##第三个脚本:
##监控参考脚本:
#!/bin/bash
#para
host01=10.0.0.191
src=/data/www/www/
dst=www #############模块
user=rsync_backup
rsync_passfile=/etc/rsync.password
inotify_home=/application/inotify-tools-3.14/
#judge
if [ ! -e "$src" ] || [ ! -e "${rsync_passfile}" ] || [ ! -e "${inotify_home}/bin/inotifywait" ] || [ ! -e "/usr/bin/rsync" ];
then
echo "Check File and Folder"
exit 9
fi
${inotify_home}/bin/inotifywait -mrq --timefmt ‘%d/%m/%y %H:%M‘ --format ‘%T %w%f‘ -e close_write,delete,create,attrib $src | while read line
do
#rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1
cd $src && rsync -aruz -R --delete ./ --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
done
exit 0
##工作环境脚本程序放到后台执行。4.3 增删改差记录(待补充)
《本节结束》
注:本人水平有限,如有需改进之处,望不吝指导建议,谢谢!
①②③④⑤⑥⑦⑧⑨⑩
参考文章:
linux运维老男孩培训老师博客
http://oldboy.blog.51cto.com/all/2561410
本文出自 “燁未央_Estelle” 博客,请务必保留此出处http://tslove.blog.51cto.com/9115838/1592735
Linux服务器集群架构部署搭建(三)NFS文件系统、SSH、批量分发管理、实时同步(2)
标签:
原文地址:http://tslove.blog.51cto.com/9115838/1592735