Chinaunix首页 | 论坛 | 博客
  • 博客访问: 252804
  • 博文数量: 188
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: -30
  • 用 户 组: 普通用户
  • 注册时间: 2017-03-29 11:17
文章分类
文章存档

2013年(8)

2012年(5)

2011年(13)

2010年(26)

2009年(63)

2008年(20)

2007年(32)

2006年(21)

分类: 系统运维

2009-11-19 17:34:57

本示例所需要的软件是:lsyncd与rsync
一般rsync软件是通过crond这支后台进行(计划任务)来实现自动同步数据,如今已有更好的开源软件来代替使用crond了,那就是lsyncd( Live Syncing (Mirror) Daemon)它的工作原理:监视本地(rsync client)的目录,当源数据有文件或目录更新时,更新本地文件或目录到远端机器(rsync server),保持实时文件同步,但是它更新数据时需要远端rsync server运行rsync demon

配置环境如下
(1)需要配置rsyncd.conf文件的一端,称为rsync server
(2)不需要配置rsyncd.conf文件的一端,称为rsync client
一、客户端上的配置:
1、安装rsync
#wget wget
#tar zvxf rsync-3.0.6.tar.gz
#cd rsync-3.0.6
#./configure --prefix=/usr/local/rsync
#make && make install

2、安装lsyncd
#wget
#tar zxvf lsyncd-2.0.7.tar.gz
#cd lsyncd-2.0.7
#./configure
#make && make install
3、新建同步目录
在服务器和客户端新建同步目录
#mkdir  /usr/share/test/
#chown nobody:nobody  /usr/share/test/
 
二、服务器的配置:
1.安装rsync,同上。
2.配置
#vi /etc/rsyncd.conf
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
[test]
path = /usr/share/test/            #保持数据的目录
hosts allow = 192.168.0.19         #允许访问的主机IP
uid = nobody
gid = nobody
read only = false                  #是否只读

        
 
 
三、在服务器端启动rsync
#/usr/local/rsync/bin/rsync --daemon
使rsync随机器启动
echo "/usr/local/rsync/bin/rsync --daemon" >> /etc/rc.local
 
四、客户端配置lsyncd
1.建立配置文件lsyncd.conf
#vi /etc/lsyncd.conf
/usr/share/test/ 192.168.30.206::test
说明:/usr/share/test/     #要实时同步的目录
      192.168.30.206       #要实时同步到的远端机器
      test                 #对应远端机器的rsync的tag。
如果要同步多台就在这个文件中每同步一台加一行
 
2.建立lsyncd日志文件
#vi /etc/logrotate.d/lsyncd
/var/log/lsyncd {
    missingok
    notifempty
    sharedscripts
    postrotate
    /etc/rc.d/init.d/lsyncd restart 2>&1 > /dev/null || true
    endscript
}
         说明:它的日志保存的文件为/var/log/lsyncd,
 
3.建立lsyncd的启动文件
#vi /etc/init.d/lsyncd
如下:
#!/bin/bash
#chkconfig: - 80 20
#lsyncd  this is sync service
#
# description: lsyncd  is sync service
/etc/rc.d/init.d/functions
lsyncd="/usr/local/bin/lsyncd"
lockfile="/var/lock/subsys/lsyncd"
pidfile="/var/run/lsyncd.pid"
function_start_lsyncd()
{
    printf "Starting Lsyncd...\n"
    while read i
    do
      source=`echo $i | awk '{print $1}'`
      target=`echo $i | awk '{print $2}'`
      $lsyncd -rsync $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 755 /etc/init.d/lsyncd
启动lsyncd
#/etc/init.d/lsyncd start
确认lsyncd是否启动
#/etc/rc.d/init.d/lsyncd status
lsyncd (pid 22863) is running...
把lsyncd加入到随机器启动当中
#chkconfig lsyncd on
 
测试:
在服务器端的/usr/share/test/下建立一个文件。客户端的/usr/share/test/发现新建的文件已经同步过去了,
但是如没有同步成功请检查日志文件/var/log/lsyncd,确定错误。
在测试过程中把服务器端得lsyncd杀掉之后,在服务器端得/usr/share/test/上新建和删除文件,
然后重启lsyncd的服务器,在客户端查看的时候已经发现对应的文件已经删除或者是同步过去了。
 
 
阅读(2310) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-12-08 21:46:24

你好 为什么我配置了 都 没报错 但是就是同步不了 ,,, Starting up watching /home/test/ found new directory: 1 in /home/test/ -- added on tosy --- Entering normal operation with [2] monitored direc event CREATE:2 triggered. rsyncing /home/test/ --> 192.168.128.128/ rsyncing /home/test/2/ --> 192.168.128.128/2/