Rsync:用于同步文件、目录的工具

inotify:监控源目录的变化,借助rsync将变化的数据同步到目的目录 


环境描述:

系统:rhel 6.3

源服务器:10.1.1.1 

目的服务器:10.1.1.2 

目的:将源服务上的/testdir目录实时同步到目的服务器的/testdir目录 

iptables防火墙,selinux关闭



在目的服务器172.16.1.2上操作:


1、安装rsync服务端软件 

# yum install -y rsync xinetd 

编辑rsync配置文件,设置sync开机自动启动

# vim /etc/xinetd.d/rsync 

service rsync

{

        disable = no //将disabled=yes修改为no

        socket_type     = stream

        wait            = no

        user            = root

        server          = /usr/bin/rsync

        server_args     = --daemon

        log_on_failure  += USERID

}

启动xinetd服务(以xinted管理rsync服务)

/etc/init.d/xinetd start 


2、创建rsyncd.conf配置文件 

# vim  /etc/rsyncd.conf 

log file=/var/log/rsyncd.log //指定日志文件名称,rsync服务启动后自动创建该文件

pidfile=/var/run/rsyncd.pid //pid文件的位置

lock file=/var/run/rsync.lock //支持max connections参数的锁文件

secrets file=/etc/rsync.pass //用户认证文件,保存用户名及密码

motd file=/etc/rsyncd.motd //rsync启动时保存欢迎信息的文件

[testdir] //同步目录名称描述,建议与同步目录同名

path=/testdir //数据目录位置 

comment=testdir

uid=root //设置rsync运行权限为root

gid=root //设置rsync运行权限为root

port=873 //默认端口号

use chroot=no //默认为true,表示不同步软链接文件;改为no表示同步软链接

read only=no //设置rsync服务端文件为读写权限

list=no //不显示rsync服务端资源列表

max connections=200 //最大连接数

timeout=600 //超时时间

auth users=root //执行数据同步的用户名,可设置多个,不同用户名用逗号隔开

hosts allow=172.16.1.1 //允许进行数据同步的客户端IP地址,多个IP使用逗号隔开


3、创建用户认证文件 

# vim /etc/rsync.pass

root:redhat


格式  用户名:密码,一行一个用户信息


4、分别将配置文件及用户认证文件的权限修改为600

# chmod 600 /etc/rsyncd.conf 

# chmod 600 /etc/rsync.pass


5、重新启动rsync服务

# /etc/init.d/xinetd restart 



在源服务器上操作:

1、安装rsync客户端软件 

# yum install -y rsync xinetd

编辑rsync配置文件,设置sync开机自动启动

# vim /etc/xinetd.d/rsync 

service rsync

{

        disable = no //将disabled=yes修改为no

        socket_type     = stream

        wait            = no

        user            = root

        server          = /usr/bin/rsync

        server_args     = --daemon

        log_on_failure  += USERID

}

启动xinetd服务(以xinted管理rsync服务)

/etc/init.d/xinetd start 


2、创建密码认证文件,该文件只保存目的服务器上的认证用户密码

# vim /etc/passwd.txt

redhat

# chmod 600 /etc/passwd.txt 


测试源服务器与目的服务器数据是否同步成功 

分别在源服务器和目的服务器上创建/testdir目录,在源服务器的/testdir中创建任意文件,使用如下同步命令后,查看目的服务器是否会出现创建的文件,如果在目标服务器上可以查看到源服务器创建的文件,表明同步成功  

# rsync -avH --port=873 --progress --delete /testdir/ [email protected]::testdir --password-file=/etc/passwd.txt 


命令说明:

-a:递归传输文件,并保留文件所有属性

-v: 显示详细过程

-H: 保留硬链接

--progress:显示备份过程

--delete:同步时,会删除目的服务器中目录中源服务器中没有的文件 

/testdir/: 源数据目录,最后一个/表示同步时只同步子文件及目录; 如果不加/,表示整个/testdir目录都会同步的目标服务器

[email protected]::testdir: root同步用户, 目标服务器中的目录只需要写目录名称即可

--password-file=/etc/passwd.txt: 指定密码文件 

在源服务器上安装inotify软件,实时监测源目录的变化实现实时同步

1、查看服务器内核是否支持inotify,2.6.13内核后开始支持

# ll /proc/sys/fs/inotify/

total 0

-rw-r--r-- 1 root root 0 Dec 10 09:26 max_queued_events

-rw-r--r-- 1 root root 0 Dec 10 09:26 max_user_instances

-rw-r--r-- 1 root root 0 Dec 10 09:26 max_user_watches


2、安装inotify

# tar zxf inotify-tools-3.14.tar.gz

# cd inotify-tools-3.14

# ./configure --prefix=/usr/local/inotify 

# make 

# make install 


3、修改环境变量PATH,并为inotify软件的库文件、头文件创建软链接

# vim /etc/profile

export PATH=$PATH:/usr/local/inotify/bin 

# source /etc/profile


导出inotify的库文件

# echo "/usr/local/inotify/lib" > /etc/ld.so.conf.d/inotify.conf 

# ldconfig


导出inotify的头文件

ln -s /usr/local/inotify/include /usr/include/inotify 




4、修改inotify内核参数(默认的inotify参数值太小)

# vim /etc/sysctl.conf 

fs.inotify.max_queued_events=9999999

fs.inotify.max_user_watches=99999999

fs.inotify.max_user_instances=65535


# sysctl -p

net.ipv4.ip_forward = 0

net.ipv4.conf.default.rp_filter = 1

net.ipv4.conf.default.accept_source_route = 0

kernel.sysrq = 0

kernel.core_uses_pid = 1

net.ipv4.tcp_syncookies = 1

kernel.msgmnb = 65536

kernel.msgmax = 65536

kernel.shmmax = 4294967295

kernel.shmall = 268435456

fs.inotify.max_queued_events = 9999999

fs.inotify.max_user_watches = 99999999

fs.inotify.max_user_instances = 65535


参数说明:

max_queued_events:inotify队列最大长度,值太小,导致监控文件不准确

max_user_watches: 要同步的文件包含多少目录; 该值要大于find /testdir -type d | wc -l的值

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


5、创建脚本,根据inotify的监测事件,实时触发rsync同步

# vim rsync.sh 

#!/bin/bash

srcdir=/testdir/

dstdir=testdir

excludedir=/usr/local/inotify/exclude.list

rsyncuser=root

rsyncpassdir=/etc/passwd.txt

dstip=10.1.1.2


while true

do

/usr/local/inotify/bin/inotifywait -rq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib -t 1 $srcdir

rsync -avH --delete $srcdir  --timeout=100 $rsyncuser@$dstip::$dstdir --password-file=$rsyncpassdir >/dev/null 2>&1

done

exit 0


该脚本只适用于目标服务器只有一个的情况,如果存在多个,在脚本中使用for循环同步到多个目标服务器


# chmod a+x /root/rsync.sh 


在源服务器目录中创建文件,手动运行该脚本,查看目标服务器上是否同步了新创建的文件,如果可以查看到,表明实时同步成功

通过设置脚本开机自动运行 或者通过计划任务定时运行该脚本,可自动实现实时 

开机自动启动:

# vim /etc/rc.d/rc.local 

sh /root/rsync.sh & 


计划任务:

# crontab -e 

*/10 * * * * sh /root/rsync.sh & 


脚本中inotifywait命令说明:

-m:始终监听目录状态变化 

-r:递归监听

-q:显示状态变化信息

-format: 指定输出格式;常用的格式符如:%w:表示发生事件的目录  %f:表示发生事件的文件  %e:表示发生的事件  %T:使用由-timefmt定义的时间格式

    -timefmt:指定时间格式,用于-format选项中的%T格式

-e close_write,modify,delete,create,attrib,move:常用的监控事件