lsyncd+rsync实时同步配置

发布时间:2019-08-01编辑:脚本学堂
lsyncd+rsync实时同步配置一例

lsyncd:
1. 采用开源软lsyncd采用inotify原理监听某一目录,如果目录内发生增、删、改、利用rsync协议自动同步到多服务器
2. inotify,从kernel 2.6.13开始正式并入内核,RHEL5支持。
3. 可以本地多点目录的监控并实现到远程目录的同步。
4. 144,145配置rsync服务,可以接收136 rsync推送过来的数据。
5. 官方介绍 http://code.google.com/p/lsyncd

配置环境:
本地服务器 :192.168.162.136
rsync服务器:192.168.162.144::soft
           :192.168.162.145::www
 
lsyncd的安装
1. 136安装lsyncd
 

复制代码 代码如下:
# wget http://lsyncd.googlecode.com/files/lsyncd-1.26.tar.gz
# tar zxvf lsyncd-1.26.tar.gz
# cd lsyncd-1.26
# ./configure
# make && make install

2. 本地136需要安装rsync客户端
centos 默认状态下rsync已经安装上了。没有安装可以装rpm包,也可以安装源码包进行,只需要客户端工具。
 

复制代码 代码如下:
#  wget http://www.samba.org/ftp/rsync/src/rsync-3.0.6.tar.gz
# tar zvxf rsync-3.0.6.tar.gz
# cd rsync-3.0.6
# ./configure --prefix=/usr/local/rsync
# make && make install
# cp /usr/local/rsync/bin/rsync /usr/bin/

3. 本地136 lsyncd启动默认日志文件,可以查看lsyncd启动与工作状态
 

复制代码 代码如下:
/var/log/lsyncd

4. 本地手动同步目录并监视磁盘目录情况,运行后作为进程一直在后台运行
 

复制代码 代码如下:
# /usr/local/bin/lsyncd /data/soft/ 192.168.162.144::soft
# /usr/local/bin/lsyncd /data/www/  192.168.162.145::www

5. 或通过配置文件的方式来启动
 

复制代码 代码如下:
# vi /etc/lsyncd.conf
/data/soft/ 192.168.162.144::soft
/data/www/  192.168.162.145::www

/============/
启动脚本如下
/============/
 

复制代码 代码如下:

vi /root/lsyncd.sh
#!/bin/bash
#lsyncd startup script 

lsyncd="/usr/local/bin/lsyncd"

function_start_lsyncd()
{
    printf "Starting Lsyncd...n"
    while read i
    do
      source=`echo $i | linuxjishu/13830.html target=_blank class=infotextkey>awk '{print $1}'`
      target=`echo $i | awk '{print $2}'`
      $lsyncd $source $target
    done < /etc/lsyncd.conf
}

function_stop_lsyncd()
{
    printf "Stoping Lsyncd...n"
    killall $lsyncd
}

function_restart_lsyncd()
{
    printf "Restarting Lsyncd...n"
    function_stop_lsyncd
    function_start_lsyncd
}

if [ "$1" = "start" ]; then
    function_start_lsyncd
elif [ "$1" = "stop" ]; then
    function_stop_lsyncd
elif [ "$1" = "restart" ]; then
    function_restart_lsyncd
else
    printf "Usage: lsyncd.sh {start|stop|restart}n"
fi

chmod u+x /root/lsyncd.sh

6. 日志文件如下:
出现问题要注意查看lsyncd日志文件的状态,远端rsyncd服务没有启动,lsyncd启动也不会成功。
# cat /var/log/lsyncd
Wed Jan 27 02:21:15 2010: command line options: syncing /data/soft/ -> 192.168.162.144::soft

Wed Jan 27 02:21:15 2010: Starting up
Wed Jan 27 02:21:15 2010: watching /data/soft/
Wed Jan 27 02:21:15 2010: found new directory: b in /data/soft/ -- added on tosync stack.
Wed Jan 27 02:21:15 2010: found new directory: c in /data/soft/ -- added on tosync stack.
Wed Jan 27 02:21:15 2010: found new directory: a in /data/soft/ -- added on tosync stack.
Wed Jan 27 02:21:15 2010: --- Entering normal operation with [4] monitored directories ---

7. 144,145 rsync配置文件示例
 

复制代码 代码如下:

==================
144 rsyncd配置文件
==================
# vi /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log

[soft]
path = /var/soft/ 
comment = soft  
ignore errors
read only = no
write only = no
list = no 
hosts allow = 192.168.162.136

运行rsyncd服务
# /usr/bin/rsync --daemon

==================
145 rsyncd配置文件
==================
# vi /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log

[www]
path = /var/www/ 
comment = soft  
ignore errors
read only = no
write only = no
list = no 
hosts allow = 192.168.162.136

运行rsyncd服务
 

复制代码 代码如下:
# /usr/bin/rsync --daemon

维护注意事项
1.可以用ps -aux查看启动情况 2.客户端要比rsyncd服务端后启动,否则自动启动不了,这时需要手动启动。
如果不好用,killall lsyncd;/root/lsyncd.sh start
保证两个服务都启动起来:rsync;lsync
netstat -anp | grep rsync

重启服务:
1、杀死进程  
2、删除相关文件。

报错消息
Reported by oer...@gmail.com, Oct 15, 2010

What steps will reproduce the problem?
1. large homedir ( million files )
2. lsyncd --exclude-from=/home/oernii/.rsync_exclude --no-daemon --verbose /home/oernii/ remote::/BACKUP/

What is the expected output? What do you see instead?
sync should run without errors

What version of the product are you using? On what operating system?
1.39

Please provide any additional information below.
After running I am getting a LOT of this:

watching /home/oernii/.kde
ERROR: Cannot add watch /home/oernii/.kde (28:No space left on device)

Comment 1 by d...@dans.im, Oct 15, 2010

"No space left on device" probably means you need to increase /proc/sys/fs/inotify/max_user_watches. Eg.: `echo 65536 >/proc/sys/fs/inotify/max_user_watches'.

解决方法
在任何目录下键入:echo 65536 >/proc/sys/fs/inotify/max_user_watches