Lsync+Rsync實時同步
Author:Alvin-zeng
目錄
一、描述
1、需求
服務器上很多時候需要多個文件同步其實可以用lsync代替計劃任務來完成這份工作
有兩臺機器192.168.1.22和192.168.1.23 23配置rsync服務可以接受22上推送過來的數據進行數據同步
二、服務器22上面的配置
1、編譯安裝軟件
# tar -xvf lsyncd-1.39.tar.gz
# cd lsyncd-1.39
# ./configure
# make && make install
#=======================
# tar -xvf rsync-3.0.7.tar.gz
# cd rsync-3.0.7
# ./configure --prefix=/usr/local/rsync
# make && make install
# cp /usr/local/rsync/bin/rsync /usr/bin/
2、將同步路徑寫入配置文件
# vi /etc/lsyncd.conf
/test 192.168.1.23::test 這個TEST要和下面23上配置裏面的[test]對應
3、編寫啓動腳本
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 | 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
4、啓動添加開機啓動
# vi /etc/rc.d/rc.local
/root/lsyncd.sh start
三、 23服務器上的配置
1、編譯安裝軟件
# tar -xvf rsync-3.0.7.tar.gz
# cd rsync-3.0.7
# ./configure --prefix=/usr/local/rsync
# make && make install
# cp /usr/local/rsync/bin/rsync /usr/bin/
2、編輯配置文件
# 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
[test]
path = /test
comment = test
ignore errors
read only = no
write only = no
list = no
hosts allow = 192.168.1.22
3、運行服務
# /usr/bin/rsync –daemon
4、增加開機啓動
# vi /etc/rc.d/rc.local
/usr/bin/rsync --daemon
5、運行22服務器上的服務
# ./root/lsyncd.sh start