lsyncd在ubuntu12.04上的配置

Lsyncd采用inotify原理监听某一目录,如果目录内发生增、删、改、则自动同步到多服务器。 可以实现两台服务器之间的文件自动同步,代替rsyncd+crontab的方式

参考文档

http://weekyuan.blog.51cto.com/2389856/1334324

使用 lsyncd 同步本地和远程目录

Linux: Using lsyncd – Live Syncing (Mirror) Daemon

Synchronizing mirror directories in Linux


https://community.rackspace.com/products/f/18/t/158

服务器端IP:192.168.0.19
客户端IP:192.168.0.10

配置ssh-keygen
在19上:
ssh-keygen -t rsa
生成id_rsa.pub文件
复制到10上的/root/.ssh/目录下并重命名为authorized_keys

apt-get -y install liblua5.1-0-dev lua5.1
wget https://lsyncd.googlecode.com/files/lsyncd-2.1.5.tar.gz
tar -zxvf lsyncd-2.1.5.tar.gz
cd lsyncd-2.1.5
./configure
make
make install
mkdir /var/log/lsyncd
mkdir /etc/lsyncd

 

配置文件示例一:使用rsync同步

settings {
 logfile = "/var/log/lsyncd/lsyncd.log",
 statusFile = "/var/log/lsyncd/lsyncd.status"
}

sync {
 default.rsync,
 source = "/usr/local/apache/htdocs",
 target ="192.168.0.10:/usr/local/apache/htdocs",
 rsync ={
  binary ="/usr/bin/rsync",
  archive =true,
  compress =true,
  verbose = true,
  rsh = "/usr/bin/ssh -p 11122 -o StrictHostKeyChecking=no"
 },

 --default.rsyncssh,
 --source = "/usr/local/apache/htdocs",
 --host = "192.168.0.10",
 --targetdir = "/usr/local/apache/htdocs",
 --ssh ={
   --binary ="/usr/bin/ssh"(default),
   --port =11122,
 --}
}

 

配置文件示例二:使用rsyncssh同步

settings {
 logfile = "/var/log/lsyncd/lsyncd.log",
 statusFile = "/var/log/lsyncd/lsyncd.status"
}

sync {
 --default.rsync,
 --source = "/usr/local/apache",
 --target ="192.168.0.10:/usr/local/apache",
 --rsync ={
  --binary ="/usr/bin/rsync",
  --archive =true,
  --compress =true,
  --verbose = true,
  --rsh = "/usr/bin/ssh -p 11122 -o StrictHostKeyChecking=no"
 --},

  default.rsyncssh,
  source = "/usr/local/apache",
  host = "192.168.0.10",
  targetdir = "/usr/local/apache",
  --rsyncOpts = "-az",
  exclude = "logs",
  ssh = {
    binary ="/usr/bin/ssh",
    port =11122,
  }
}

 

配置文件示例三:多目录同步

settings {
 logfile = "/var/log/lsyncd/lsyncd.log",
 statusFile = "/var/log/lsyncd/lsyncd.status"
}

sync {
 default.rsync,
 source = "/usr/local/bin",
 target ="192.168.0.10:/usr/local/bin",
 rsync ={
  binary ="/usr/bin/rsync",
  archive =true,
  compress =true,
  verbose = true,
  rsh = "/usr/bin/ssh -p 11122 -o StrictHostKeyChecking=no"
 },
}

sync {
 default.rsync,
 source = "/usr/local/apache",
 target ="192.168.0.10:/usr/local/apache",
 rsync ={
  binary ="/usr/bin/rsync",
  archive =true,
  compress =true,
  verbose = true,
  rsh = "/usr/bin/ssh -p 11122 -o StrictHostKeyChecking=no"
 },

}

 

服务脚本:

#! /bin/sh
### BEGIN INIT INFO
# Provides:          lsyncd
# Required-Start:    $remote_fs
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: lsyncd daemon init script
# Description:       This script launches the lsyncd daemon.
### END INIT INFO

# Author: Ignace Mouzannar <ignace@enovance.com>

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="synchronization daemon"
NAME=lsyncd
DAEMON=/usr/local/bin/$NAME
CONFIG=/etc/lsyncd/lsyncd.conf.lua
DAEMON_ARGS="${CONFIG}"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Exit if config file does not exist
[ -r "$CONFIG" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON \
        --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --make-pidfile --pidfile $PIDFILE \
        --exec $DAEMON -- \
$DAEMON_ARGS \
|| return 2
}

#
# Function that stops the daemon/service
#
do_stop()
{
start-stop-daemon --stop --quiet --pidfile $PIDFILE --name $NAME
RETVAL="$?"
[ "$RETVAL" = 2 ] && return 2
start-stop-daemon --stop --quiet --oknodo --exec $DAEMON
[ "$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f $PIDFILE
return "$RETVAL"
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
return 0
}

case "$1" in
  start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
  stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
  restart|force-reload)
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
  0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
  *)
  # Failed to stop
log_end_msg 1
;;
esac
;;
  *)
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 3
;;
esac

:

 

遇到的问题

tail -f /var/log/lsyncd/lsyncd.log
遇到:Error: Terminating since out of inotify watches.
Consider increasing /proc/sys/fs/inotify/max_user_watches
需要修改

echo 65535000 >  /proc/sys/fs/inotify/max_user_watches

然后使用以下命令使永远生效:

echo  fs.inotify.max_user_watches=65535000 >>  /etc/sysctl.conf