使用Rsync + Lsync可實現即時檔案同步,本文就介紹如何配置Rsync + Lsync。並測試檔案同步功能。
伺服器資訊:
主:10.86.255.154
從:10.86.255.155
1.從服務設定。
1.1.安裝配置rsync xinet。
# yum -y install rsync xinet
# cp /etc/xinetd.d/rsync /etc/xinetd.d/rsync.bkp0625
# 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
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
1.2.啟動xinetd並設定開機啟動項。
# /etc/init.d/xinetd start
# chkconfig xinetd o
1.3.建立同步檔案的存放目錄。
# mkdir /home/backup
1.4.建立rsyncd.conf設定檔。
# vim /etc/rsyncd.conf
[zabbix.cc-web] →名字任意
path = /home/backup →同步檔案存放路徑
hosts allow = 10.86.255.154 →允許串連的主機(指定主伺服器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
test
test.txt
2.3.在主伺服器,執行以下命令。
測試將主伺服器/var/www/html/下檔案同步到從伺服器/home/backup
# rsync -avz --delete --exclude-from=/etc/rsync_exclude.lst /var/www/html/ 10.86.255.155::zabbix.cc-web
backup]# ls zabbix.cc
3.使用Rsync + Lsync,當檔案或目錄發生變更時,可做到即時同步。
3.1.登入主伺服器,下載安裝Lsync。
# yum install lsyncd -y
3.2.配置lsyncd檔案。
# cp /etc/sysconfig/lsyncd /etc/sysconfig/lsyncd.bkp0625
# 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="/var/www/html/", →指定需同步的源檔案目錄
target="10.86.255.155::zabbix.cc-web", →從伺服器IP:(rsyncd.conf設定的名字)
excludeFrom="/etc/rsync_exclude.lst", →排除檔案清單
}
3.4.啟動lsync。
# /etc/rc.d/init.d/lsyncd start
3.5.在主伺服器,建立新測試檔案。
html]# touch kongqiao
3.6.在從伺服器確認,同步成功。
backup]# ls
kongqiao