Chinaunix首页 | 论坛 | 博客
  • 博客访问: 370109
  • 博文数量: 61
  • 博客积分: 2451
  • 博客等级: 上尉
  • 技术积分: 650
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-06 21:24
文章分类

全部博文(61)

文章存档

2012年(1)

2011年(44)

2010年(16)

分类: LINUX

2011-01-06 18:37:02

                 rsync+inotify 实现触发同步

注:rsync是一个很好的备份同步数据的工具,但是它不能够自动当目录中的文件发生变化时就进行同步。所以inotify的作用就是触发rsync实现同步备份操作。

第一部分:基本安装与配置

1.下载最新版本的rsync源码,因为最新版本修补了原来的bug,而且增加了一些新特性。

2.源码进行安装:

tar -xvzf rsync-3.0.6.tar.gz

cd rsync-3.0.6

./configure --prefix=/opt/rsync (这里指定安装的目录为/opt/rsync

make

make install

3.Standalone的方式启动rsync服务,默认端口为873,当配置文件不存在时会有如下提示:

  

所以需要在/etc/下建立配置文件rsyncd.conf

可以看出rsync服务已经起来。

4.通过客户端去访问

  

发现permission denied,可以看下配置文件中的uidgid 如果都为nobody的话就会出现这种情况。我将其修改为root之后就成功了uidgid在后面会讲。

5.由于是通过源码安装的,所以man文件都不在系统man文件所在的目录。所以当你man rsync时你会发现系统提示错误说找不到满文件。安装完rsync

后你可以看到在目录/opt/rsync/share/下的man目录。

我们可以通过如下命令来读取man文件:

第二部分:深入配置文件

1.rsync 的配置文件,其实分成两块,一块是全局配置参数,另一块是模块配置参数。对于全局配置参数,对所有模块都起作用。

个人了解的全局参数有:

motd file

This parameter allows you to specify a “message of the  day”  to

display  to  clients on each connect.

这个参数指定“message of the day” 当客户端每次连接时显示

    

在配置文件指定:motd file = /opt/rsync/motdfile

pid file

    This parameter tells the rsync daemon to write its process ID to

    that file.  If the file already exists, the  rsync  daemon  will

abort rather than overwrite the file.

在配置文件中指定pid file = /var/run/rsyncd.pid 就行了 不需要人工去创建,当该文件存在时,如果你启动rsync ,将会退出并提示说pid文件已经存在。当你killall /opt/rsync/bin/rsync 时该文件就会被删除。

port

指定监听端口默认为873

address

    指定侦听的端口。默认情况下是在所有端口上侦听873端口。

2.接下来是模块参数

   comment 对目录的说明

   例如:我在配置文件的[dep1]中添加comment = this is a important directory you should operate carefully

   要使其显示前提是list = yes

  

   客户端命令:rsync 192.168.0.6::用于列出所有允许列出的模块。

   list 有两个值yes/no yes表示该模块可以被客户端查看,如果不想则设no

   path 指定用于备份的目录

   use chroot 有两个值yes/no,等于yes的话将目录锁定在该目录下,如果在传输文件时遇到了符号链接指向其他目录的文件时,此时将会发生错误。

所以如果你想将符号链接所指向的文件备份起来的话,除了在客户端加上-L参数外(此参数是将符号链接所指向的文件拷贝并以符号链接文件名命名并不是源文件名)还要使use chroot = yes

uid 个人理解为操作服务器端文件时所要替换的用户(理解的似乎不正确)

gid 同上

host deny 设定被拒绝访问的ip地址。

host allow 设定允许访问的ip地址。(host allow 的优先级大于host deny 并且如果匹配了host allow 的话那么就不会去匹配host deny,有点像访问控制列表)

auth users 指定允许连接的用户。

secret file 指定用于验证用户的密码文件格式是usernamepassword,一行一对。

注意:当你创建了secret file时一定要将文件的权限改成只能让该用户访问,否则验证会失败的。chmod 600 passwordfile

如果没有改的话会出现如下错误:

你也可以在客户端命令行上加上—password-file = file 来指定密码文件,以避免手工输入密码。也可以通过设定环境变量来达到目的export RSYNC_PASSWORD=password

第三部分:rsync 客户端和 rsync+ssh

1.在客户端一般常用到的命令行参数:

-v 输出更多的传输信息,-vvv为调试

-z 压缩传输。减少传传输时的数据流量。增加备份速度。

-a 相当于-rlptgoD 是由很多参数组合

-r 递归备份子目录

-l 遇到符号链接创建符号链接,但不创建符号链接所指向的文件。

-p 保持相关属性

-o 保持文件属主

-g 保持文件属组

-u 跳过接收方比发送发更新的文件

--port= 用来指定非873端口。

--delete 同步到服务器,即服务器上没有的文件在接收端要与以删除。或者你可以通过-b --backup-dir=/backup --suffix= 将删除的文件备份起来。

注:只有里面的文件本身内容发生了变化,它才会把这个文件备份到指定目录里面。如果文件没有变化就不备份,所以也就不会产生指定的目录了。

--progress 详细输出处理过程

--password-file 指定认证用户的密码文件。权限为600

昨天上cu学到选项—exclude-from的用法:

就是将要剔除的文件pattern写在一个文件中,然后用上面选项调用

其他选项可以参考:man rsync

在客户端上创建公钥私钥

ssh-keygen -t rsa

创建公钥私钥

将公钥上传到服务器指定用户并改成authorized_keys

采用ssh-copy-id -i /root/.ssh/id_rsa.pub user@remotehost -p (22端口)

创建本地到远端的ssh tunnel

   ssh -N -L 873(本地的端口):localhost(本地ip):873(远端的端口) user@remotehost

-N选项不执行远端命令。

我想是rsync通过ssh来备份数据命令:

rsync -avzL --delete --progress -e “ssh -l mailuser01” backup@192.168.0.6 ::dep1 /root/storetest/

但是执行失败

所以我通过ssh tunnel 来进行备份同样可以达到加密的效果

rsync -avzL --delete  --progress backup@192.168.0.6 ::dep1 /root/storetest

这次成功实现数据加密传输了 。

今天上cu版主回复说:如果要用ssh来实现备份不用再配置rsync的服务,直接用ssh登入执行备份

rsync -avzL --progress -e ssh (也可以用—rsh=ssh :/(具体的路径且user用户要有权限) /destination

但备份的速度明显变慢了,估计是加密的缘故。

下面是怎么只将test1.txt text2.txt test3.txt 备份到客户端的并将服务端上的上面几个文件删除的。

/usr/local/bin/rsync -avzL --include-from=file --exclude="*" --remove-source-files --progress 192.168.0.6::dep1 /root/storetest/

--include-from 是只包含的文件 --exclude是剔除的文件。相当于host allow  host deny

--remove-source-files 将服务端成功拷贝到客户端的文件删除。

rsync 通过选项—partial-dir支持断点续传功能.

在客户端下载备份服务端时:

/usr/local/bin/rsync -avzL --progress --partial-dir=./partial rsync://192.168.0.6/dep1 /root/storetest/

在传输过程中如果连接中断会在目标目录下创建partial目录,里面是客户端已经接收的部分文件。

客户端上传服务器:

/usr/local/bin/rsync-avzL--delete—progress--partial-dir=./partial /root/storetest/ rsync://192.168.0.6/public/test/

还可以用这种方法来指定-f ‘+ pattern  表示包括的文件,把加号改成-表示除去的文件。

--delete-excluded       also delete excluded files on the receiving side

在接收方删除存在于接收方但不存在于发送方的文件,同时也删除被excluded掉的文件(即使这些文件也存在于发送方)

 

第四部分:rsync+inotify

   先判断一下系统是否有目录:

The value in this file is used when an application calls inotify_init(2) to set an upper limit on the number of events that can be queued to the corresponding inotify instance. Events in excess of this limit are dropped, but an IN_Q_OVERFLOW event is always generated.

 

max_queued_envents 是用来定义系统触发inotify event 数量的上限。

/proc/sys/fs/inotify/max_user_instances

  This specifies an upper limit on the number of inotify instances that can be created per real user ID.

用来定义每个用户inotify instances的上限

/proc/sys/fs/inotify/max_user_watches

  This specifies a limit on the number of watches that can be associated with each inotify instance

又该目录表示系统支持inotify

能够被inotifywaitevents   能够被-e选项指定的

1 .access

当被监视的文件或者目录中的文件被读的时候触发事件

2. modify

当被监视的文件或者目录中的文件被写(被修改)时触发的事件

3. attrib

当被监视的文件或者目录中的文件的属性(包括:时间戳,文件的属性,一些扩展属性)被修改时触发的事件

4. close_write

关闭以非写模式打开的文件或者目录中的文件所触发的事件

5. close_nowrite

关闭以只读模式打开的文件或者是目录中的文件所触发的事件

6. close

文件被关闭,不管以何种方式打开的文件。

7. open

   文件被打开

7. move_to

文件或目录被移动到该被监视的目录中。即使是从该目录移动到该目录也会触发该事件

8move_from

文件或目录被移出到被监视的目录中。即使是从该目录移动到该目录也会触发该事件

9. move

  包括上面两种事件

10move_self

              A watched file or directory was moved.  After  this  event,  the file or directory is no longer being watched.

  (还是英文比较明确)

11create

A file or directory was created within a watched directory

12delete

A file or directory within a watched directory was deleted.

13delete_self

    A  watched  file or directory was deleted.  After this event the file or directory is no longer being watched.   Note  that  this event can occur even if it is not explicitly being listened for.

14unmount

              The filesystem on which a watched file or directory resides  was unmounted.   After this event the file or directory is no longer

    being watched.  Note that this event can occur even if it is not explicitly being listened to.

几个重要的选项:

-m : 使inotifywait 能够连续监视文件或目录,而不是一个事件发生后就退出

-r : 使递归监视,如果目录很大,就需要花一段时间

-o : 将监视到的事件输出到指定文件,而不是到标准终端。

-e :指定要被触发的事件。多个之间可以用,隔开

--exclude=pattern: 剔除pattern指定的文件。

--excludei=pattern:同上。

--timefmt

  Set a time format string as accepted by strftime(3) for use with the ‘%T’ conversion in the --format option.

%T 将在后面的—format中使用

参数:

    %a     The abbreviated weekday name according to the current locale.

 

    %A     The full weekday name according to the current locale.

 

    %b     The abbreviated month name according to the current locale.

 

    %B     The full month name according to the current locale.

 

    %d     The day of the month as a decimal number (range 01 to 31).

           

    %T     The time in 24-hour notation (%H:%M:%S). (SU)

    %H     The hour as a decimal number using a 24-hour clock (range 00  to 23).

    %m     The month as a decimal number (range 01 to 12).

 

    %M     The minute as a decimal number (range 00 to 59).

 

    %n     A newline character. (SU)

    %u     The  day of the week as a decimal, range 1 to 7, Monday being  1.

              See also %w. (SU)

    %Y     The year as a decimal number including the century.

 

以上是我man strftime 得到的几个个人认为比较常用的我记录在此

 

 

--format

    Output in a user-specified  format,  using  printf-like  syntax. The  event  strings output are limited to around 4000 characters

and will be truncated to this length.  The following conversions are supported:

     %w     This will be replaced with the name of the Watched file on which  an event occurred.

            当事件发生时,用文件名代替

 

     %f    When an event occurs within a directory, this will  be  replaced  with the name of the File which caused the event to occur.  Oth-erwise, this will be replaced with an empty string.

 

     %e    Replaced with the Event(s) which occurred, comma-separated.

           用触发的事件代替

     %Xe   Replaced  with  the  Event(s)  which  occurred,  separated by   whichever character is in the place of ‘X’.

 

     %T     Replaced  with  the  current Time in the format specified by the  --timefmt option, which should be a format string  suitable  for passing to strftime(3).

 

 

使用例子:

[root@Server1 ~]#inotifywait  -m -e access,modify,move --exclude=".*\.swp" --timefmt='%Y-%m/%d-%H:%M:%S' --format='%T %e %f' /root/test/

Setting up watches.

Watches established.

2010-12/03-23:38:10 MOVED_FROM test2.txt

2010-12/03-23:38:41 MOVED_TO test2.txt

上面的%H:%M:%S 可以用%T代替

上面英语表述的很清楚。

阅读(7880) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~