linux服务器之间的文件同步;rsync+inotifywait;同步多个目录

一、双向同步:unison+inotifynode

二、单向同步:rsync+inotifypython

 

 

python版的pyinotify
linux

本文介绍第二种方法:nginx

一、Inotify 是一个 Linux特性,它监控文件系统操做,好比读取、写入和建立。Inotify 反应灵敏,用法很是简单,而且比 cron 任务的繁忙轮询高效得多。学习如何将 inotify 集成到您的应用程序中,并发现一组可用来进一步自动化系统治理的命令行工具。inotify是一种强大的,细粒度的,异步文件系统时间监控机制,它能够替代crond实现与rsync的触发式文件同步,从而监控文件系统中添加,删除,修改,移动等细粒事件,从LINUX 2.6.13起,就已加入了对inotify的支持,因此咱们只须要安装一个第三方软件inotify-tools便可管理此服务git

二、inotify安装:github

官网:http://inotify.aiken.cz/?section=inotify&page=download&lang=enweb

里面就两个C语言的头文件inotify.h和inotify-syscalls.hshell

linux内核2.6.13之后开始支持这个特性,查看支持状况:[admin@19-56 ~]$ ls -lsart /proc/sys/fs/inotify/安全

上图表示是支持的bash

三、inotify-tools安装:其实就是inotify的命令行工具包,这样你就能够经过命令行使用inotify特性

官网:https://github.com/rvoicilas/inotify-tools

https://github.com/rvoicilas/inotify-tools/wiki

inotify-tools is a C library and a set of command-line programs for Linux providing a simple interface to inotify. These programs can be used to monitor and act upon filesystem events.

inotify-tools安装完成以后会有两个命令,inotifywait 和 inotifywatch。inotifywait用于等待文件或者文件集上的一个特定事件,能够监控任何文件或者目录位置,而且能够递归地监控整个目录树;inotifywatch 用于收集被监控的文件系通通计数据,包括每一个inotify事件发生多少次等信息。

源码安装:./configure --prefix=/usr && make && su -c 'make install'

yum安装:yum install -y inotify-tools

四、inotifywait -h查看inotifywait的帮助信息,经常使用选项(options)

    -r|--recursive     Watch directories recursively. 递归查询目录

    -m|--monitor      Keep listening for events forever.  Without
                         this option, inotifywait will exit after one
                         event is received. 始终监控

    -q|--quiet         Print less (only print events). 打印较少信息,仅打印监控相关信息

经常使用事件(Events):

    modify        file or directory contents were written

    create        file or directory created within watched directory

    delete        file or directory deleted within watched directory

 五、rsync -h查看rsync的帮助信息

 -v, --verbose               increase verbosity

 -a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)

     --delete                delete extraneous files from destination dirs  意思是删除目的目录多余源目录的内容。这样源目录和目的目录的内容才能一致,不然,目的目录的内容不断增长。保持两个目录同步增长和删除

 

六、重点来了,配置文件

host ip status kernel bit file
A
192.168.10.220
master x86_64 64 /tmp
B
192.168.10.221
slave x86_64 64 /tmp

注意,不要把容易变化又不是监控目标的东西放到监控目录里,这样的话,就不会频繁产生同步操做。更加须要注意的是,不要把下文的日志记录放到监控目录里,这样就产生了死循环:开始同步,产生日志,日志文件发生变化,继续同步,再次产生日志,日志文件再次发生变化,再次同步……

1、主服务器A:

创建密码认证文件

[root@nginx rsync-3.0.9]# cd /usr/local/rsync/
[root@nginx rsync]# echo "rsync-pwd" >/usr/local/rsync/rsync.pwd

其中rsync-pwd能够本身设置密码,rsync.passwd名字也能够本身设置
[root@nginx rsync]# chmod 600 rsync.passwd

不管是为了安全,仍是为了不出现如下错误,密码文件都须要给600权限

建立rsync复制脚本 此项功能主要是将master端的目录/tmp里的内容,若是修改了(不管是添加、修改、删除文件)可以经过inotify监控到,并经过rsync实时的同步给client的/tmp里,下面是经过shell脚本实现的。

#!/bin/bash 
host=192.168.10.221 #slave地址
src=/tmp/    #被监控目录
des=web 
user=webuser 
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \ 
| while read files 
do
/usr/bin/rsync -va --delete --progress --password-file=/usr/local/rsync/rsync.passwd $src $user@$host::$des 
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1 
done

read的用法

若是把rsync.log的放到tmp(备份的目录)就会发送一直复制的问题,因此建议各位吧rsync的日志放到其余的目录下(非备份目录)。
其中host是slave的ip,src是master端要实时监控的目录,des是认证的模块名,须要与slave一致,user是创建密码文件里的认证用户。
把这个脚本命名为rsync.sh,放到监控的目录里,好比个人就放到/tmp下面,并给予764权限

[root@nginx tmp]# chmod 764 rsync.sh

而后运行这个脚本

[root@nginx tmp]# sh /tmp/rsync.sh &

请记住,只有在备份服务器client端的rsync安装并启动rsync以后,在启动rsync.sh脚本,不然有时候会满屏出现:

rsync: failed to connect to 192.168.10.221: Connection refused (111) 
rsync error: error in socket IO (code 10) at clientserver.c(107) [sender=2.6.8] 

咱们还能够把rsync.sh脚本加入到开机启动项里

[root@nginx tmp]# echo "/tmp/rsync.sh" >> /etc/rc.local

2、备份服务器(slave)

创建用户与密码认证文件

[root@nginx-backup rsync-3.0.9]# echo "webuser:rsync-pwd" > /usr/local/rsync/rsync.passwd

请记住,在master端创建的密码文件,只有密码,没有用户名;而在备份服务端slave里创建的密码文件,用户名与密码都有。

[root@nginx-backup rsync]# chmod 600 rsync.passwd 须要给密码文件600权限

创建rsync配置文件

uid = root 
gid = root 
use chroot = no 
max connections = 10 
strict modes = yes
pid file = /var/run/rsyncd.pid 
lock file = /var/run/rsync.lock 
log file = /var/log/rsyncd.log 
[web] 
path = /tmp/   #同步的目的目录
comment = web file
ignore errors 
read only = no 
write only = no 
hosts allow = 192.168.10.220 
hosts deny = * 
list = false
uid = root 
gid = root 
auth users = webuser 
secrets file = /usr/local/rsync/rsync.pwd

其中web是master服务端里的认证模块名称,须要与主服务器里的一致,以上的配置个人本身服务器里的配置,以供参考。

把配置文件命名为rsync.conf,放到/usr/local/rsync/目录里

启动rsync
[root@nginx-backup rsync]# /usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync.conf

咱们能够把rsync脚本加入到开机启动项里

[root@nginx-backup rsync]# echo "/usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync.conf" >> /etc/rc.local

如今rsync与inotify在master端安装完成,rsync在备份服务器slave端也安装完成

 

参考:http://www.showerlee.com/archives/678

http://www.jb51.net/article/57011.htm

 

20181117更新:

注意点:

一、rsync+inotifywait的同步模式是阻塞模式,必定放到后台执行,不要影响其余任务执行

二、rc.local启动的任务帐号都是root帐号,因此注意对文件的读写和执行权限

三、启动日志:/var/log/boot.log,能够经过这个日志查看rc.local里的开机启动任务启动状况,是否有异常

四、只能够同步目录和目录下的文件,可是不能够直接同步文件

目的:把服务器A的两个目录的全部变化同步到服务器B

先上服务器配置A

rc.local配置:

#!/bin/sh -x
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
exec 1>/tmp/rc.local.log 2>&1  # send stdout and stderr from rc.local to a log file rc.local的日志默认是在/var/log/boot.log里的,这里把它的日志写到/tmp/rc.local.log中
set -x  # tell sh to display commands before execution
touch /var/lock/subsys/local          # 做用是确保rc.local不会被执行两次
/bin/sh /usr/local/rsync/rsyncJobs.sh &    # 这里必定要加 & 符号,由于这个同步任务是个阻塞的任务,让它在后台执行,这样能够继续执行其余开机启动项
/bin/sh /usr/local/rsync/rsyncNodes.sh &   # 上面加了后台执行符号 &,这个任务才会执行

目录一的rsyncJobs.sh配置

#!/bin/bash
host=服务器B的IP
srcJobs=/var/lib/jenkins/jobs/  # 被监控的目录以及递归的目录和文件,可是不能够直接写某个具体的文件
desJobs=jobs  # 目的
user=webuser  # 下面配置的做用是监控目录/var/lib/jenkins/jobs/的任何变化,而后同步到目的地
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $srcJobs \
| while read files
do
sleep 5
rsync -va --delete --progress --password-file=/usr/local/rsync/rsync.pwd $srcJobs $user@$host::$desJobs
echo "${files} was rsynced" >> /tmp/rsyncJobs.log 2>&1  # 把同步文件的简要信息放在这里,大部分相信信息存在/tmp/rc.local.log里
done

目录二的rsyncNodes.sh配置:

#!/bin/bash
host=服务器B的IP
srcNodes=/var/lib/jenkins/nodes/
desNodes=nodes
user=webuser
# inotifwait的做用是监控文件的变化,而后通知rsync进行同步工做 inotifywait
-mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $srcNodes \ | while read files do sleep 5 rsync -va --delete --progress --password-file=/usr/local/rsync/rsync.pwd $srcNodes $user@$host::$desNodes # --delete删除目标文件比源文件多的文件 echo "${files} was rsynced" >> /tmp/rsyncNodes.log 2>&1 done

密码的rsync.pwd配置:

rsync-pwd  # 只有密码

 

服务器B配置:

rc.local

#!/bin/sh -x
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
# 放在rc.local屡次执行
touch /var/lock/subsys/local
# 开启rsync服务进程,等待服务器A发起的同步任务 rsync
--daemon --config=/usr/local/rsync/rsync.conf

rsync.conf配置

uid=admin  # 决定被同步的文件到服务器A上的用户
gid=admin  # 决定被同步的文件到服务器A上的用户组
use chroot=no
max connections=10
strict modes=yes
pid file=/usr/local/rsync/rsyncd.pid  # 有的时候启动不了,是由于kill -9后,pid文件还存在。自动生成
lock file=/usr/local/rsync/rsyncd.lock  # 有的时候新增长的目录或者修改的配置不生效,是由于这个配置文件没有更新,能够删除下,它会自动生成
log file=/usr/local/rsync/rsyncd.log  # 服务器B的rsync服务日志;自动生成
[jobs]  # 目录一的配置
path=/var/lib/jenkins/jobs/ # 服务器A的目录一的位置,能够是任意目录,不须要和服务器A的目录保持一致
comment=web file
ignore errors
read only=no
write only=no
hosts allow= 服务器A的IP  # 容许链接的IP
hosts deny=*
list=false
uid=admin
gid=admin
auth users=webuser
secrets file=/usr/local/rsync/rsync.pwd
[nodes]  # 目录二的配置
path=/var/lib/jenkins/nodes/  # 目录二在服务器B上的位置
comment=web file
ignore errors
read only=no
write only=no
hosts allow=服务器A的IP
hosts deny=*
list=false
uid=admin
gid=admin
auth users=webuser
secrets file=/usr/local/rsync/rsync.pwd

密码的rsync.pwd的配置

webuser:rsync-pwd  # 用户名:密码

 

启动日志:/var/log/boot.log


rsync -azv --delete --exclude .svn --exclude "compile" --exclude "session_cache" --exclude "web"
/cygdrive/c/www/test/trunk/ username@IP:/home/test/
这里要说的是,同步的时候排除多个文件/文件夹的作法是:
--exclude "文件夹名字(1.惟一的时候,能够直接用文件/文件夹名 2.用绝对路径)"
如下示例,排除3个文件夹:
--exclude .svn --exclude "compile" --exclude "session_cache"
rsync经常使用参数:

--delete 删除目标文件比源文件多余的文件

--exclude 排除文件(文件不会被同步)

下面使用nload -u M观察到的同步效果图。使用左右方向键能够切换观察的接口

相关文章
相关标签/搜索