Rsync+lsync实现触发式实时同步

2023-05-12,,

使用rsync+lsync实现触发实时同步

服务器信息

centos6.5

主:192.168.5.4 搭建lsync

从:192.168.5.3 搭建rsync

1.1 从服务器设置

# yum -y install rsync xinetd
# cp /etc/xinetd.d/rsync /etc/xinetd.d/rsync.blk
# vim /etc/xinetd.d/rsync
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
# allows crc checksumming etc.
service rsync
{
disable = no        →修改‘yes’为‘no’
flags = IPv6 →修改‘IPv6’为‘IPv4’
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
} 1.2.启动xinetd并设置开机启动项。

# service xinetd start

# chkconfig xinetd on

1.3.创建同步文件的存放目录。

# mkdir /home/backup

1.4.创建rsyncd.conf配置文件。

# vim /etc/rsyncd.conf

[backup]                      →名字任意

path = /home/backup       →同步文件存放路径

hosts allow = 192.168.5.4          →允许连接的主机(指定主服务器IP)

hosts deny = *

list = true

uid = root

gid = root

read only = false

2.主服务器设定。

2.1.安装rsync。

# yum -y install rsync

2.2.创建同步对象中,不需要同步的文件一览表。

# vim /etc/rsync_exclude.lst

xx

xx.txt

2.3.在主服务器,执行以下命令。

测试将主服务器/data/下文件同步到从服务器/home/backup

# rsync -avz --delete --exclude-from=/etc/rsync_exclude.lst /data 192.168.5.3::backup

3.使用Rsync + Lsync,当文件或目录发生变更时,可做到实时同步。

3.1.登录主服务器,下载安装Lsync。

# rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

# yum install lsyncd -y

3.2.配置lsyncd文件。

# cp /etc/sysconfig/lsyncd /etc/sysconfig/lsyncd.blk

# vim /etc/sysconfig/lsyncd

# Keep the space there for systemd. Don't nest variables without

# changing the systemd service file to use: /usr/bin/sh -c 'eval XXXX'

#

LSYNCD_OPTIONS="-pidfile /var/run/lsyncd.pid /etc/lsyncd.conf"  →去掉’‘#’

#LSYNCD_OPTIONS=" "

3.3.配置lsyncd.conf,添加以下内容:

# vim /etc/lsyncd.conf

settings{

statusFile = "/tmp/lsyncd.stat",

statusInterval = 1,

}

sync{

default.rsync,

source="/data",                        →指定需同步的源文件目录

target="192.168.5.3::backup",                   →从服务器IP:(rsyncd.conf设定的名字)

excludeFrom="/etc/rsync_exclude.lst",    →排除文件列表

}

3.4.启动lsync。

# /etc/rc.d/init.d/lsyncd start

# chkconfig lsyncd on

3.5.在主服务器,创建新测试文件。

data]# touch xx

3.6.在从服务器确认,同步成功。

# ls /home/backup

Rsync+lsync实现触发式实时同步的相关教程结束。

《Rsync+lsync实现触发式实时同步.doc》

下载本文的Word格式文档,以方便收藏与打印。