1.
两台服务器AB
A做客户端,B做备机,A --》 B同步数据
进入A
如果是centos系统可以使用以下命令查看是否安装了rsync
# rpm -qa | grep rsync
如果有安装则可跳过以下源码安装
a.rsync源码安装
# wget http://www.samba.org/ftp/rsync/src/rsync-3.0.7.tar.gz
# tar zxvf rsync-3.0.7.tar.gz
# cd
rsync-3.0.7
# ./configure --prefix=/usr/local/rsync
# make
# make install
b.创建密码认证文件:
# cd /usr/local/rsync/
# echo "111111" >/usr/local/rsync/rsync1.passwd
c.给密码文件赋予600权限:
# chmod 600 rsync1.passwd
d.安装inotify:
# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
# tar zxvf inotify-tools-3.14.tar.gz
# cd inotify-tools-3.14
# ./configure --prefix=/usr/local/inotify
# make
# make install
e、创建监控脚本:
#!/bin/bash
host=172.16.10.134
src=/root/test/
des=web
user=rsync
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T
%w%f%e' -e modify,delete,create,attrib $src | while read files
do
#同步后 --delete 删除文件,同步增量把 --delete删除
/usr/bin/rsync -vzurtopg --delete --progress
--password-file=/usr/local/rsync/rsync1.passwd $src $user@$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
注意:host为第二台服务器IP,src为要监控的路径,web是认证模块名称,最后把监控脚本命名为rsync1.sh放在要监控的路径下。
f、给监控脚本赋予764权限:
# chmod 764 rsync1.sh
!!!进入B服务器
查看是否安装了rsync
# rpm -qa | grep rsync
如果有安装则可跳过以下源码安装
a.rsync源码安装
# wget http://www.samba.org/ftp/rsync/src/rsync-3.0.7.tar.gz
# tar zxvf rsync-3.0.7.tar.gz
# cd
rsync-3.0.7
# ./configure --prefix=/usr/local/rsync
# make
# make install
b.创建密码认证文件:
# cd /usr/local/rsync/
# echo "rsync:111111" >/usr/local/rsync/rsync1.passwd
c.给密码文件赋予600权限:
# chmod 600 rsync1.passwd
d、创建rsync配置文件:
uid = root
gid = root
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[web]
path = /root/test/
comment = web file
ignore errors
read only = no
write only = no
hosts allow = 172.16.10.134
hosts deny = *
list = false
uid = root
gid = root
auth users = rsync
secrets file = /usr/local/rsync/rsync1.passwd
该配置文件是用来接收另一台服务器的文件。其中web是server服务端(134服务器)的认证模块名称,需要与134服务器里的一致。把配置文件命名为rsync1.conf,放到/usr/local/rsync/目录里
h、启动该配置文件
# /usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync1.conf
需要开机启动的话:
# echo "/usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync1.conf" >> /etc/rc.local
二、A是备机,B做为客户端 B --》A
172.16.10.207
进入B服务器(rsync已安装)
安装inotify:
# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
# tar zxvf inotify-tools-3.13.tar.gz
# cd inotify-tools-3.13
# ./configure
--prefix=/usr/local/inotify
# make
# make
install
创建密码
b.创建密码认证文件:
# cd /usr/local/rsync/
# echo "111111" >/usr/local/rsync/rsync2.passwd
c.给密码文件赋予600权限:
# chmod 600 rsync2.passwd
e、创建监控脚本:
#!/bin/bash
host=172.16.10.207
src=/root/test/
des=web2
user=rsync2
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T
%w%f%e' -e modify,delete,create,attrib $src | while read files
do
/usr/bin/rsync -vzrtopg --delete --progress
--password-file=/usr/local/rsync/rsync2.passwd $src $user@$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
注意:host为第二台服务器IP,src为要监控的路径,web是认证模块名称,最后把监控脚本命名为rsync2.sh放在要监控的路径下。
f、给监控脚本赋予764权限:
# chmod 764 rsync2.sh
!!!进入客户端A服务器
.创建密码认证文件:
# cd /usr/local/rsync/
# echo "rsync:111111" >/usr/local/rsync/rsync2.passwd
chmod 600 rsync2.passwd
g、创建rsync配置文件:
uid = root
gid = root
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[web2]
path = /root/test/
comment = web file
ignore errors
read only = no
write only = no
hosts allow = 172.16.10.207
hosts deny = *
list = false
uid = root
gid = root
auth users = rsync2
secrets file = /usr/local/rsync/rsync2.passwd
该配置文件是用来接收另一台服务器的文件。其中web是server服务端(207服务器)的认证模块名称,需要与134服务器里的一致。把配置文件命名为rsync2.conf,放到/usr/local/rsync/目录里
h、启动该配置文件
# /usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync2.conf
需要开机启动的话:
# echo "/usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync2.conf" >> /etc/rc.local
最后启动两台服务器的监控脚本:
# sh /root/test/rsync1.sh &
需要开机启动的话:
# echo "/root/test/rsync1.sh" >> /etc/rc.local
sh /root/test/rsync2.sh &
需要开机启动的话:
# echo "/root/test/rsync2.sh" >> /etc/rc.local
测试
任意在其中一台服务器/root/test/路径下,新增一个文件,你会发现另一台服务器也同步了该文件。
同步之后,两台服务器下都会有rsync1.sh和rsync2.sh监控脚本,注意勿删!
如果需要3台或3台以上的服务器之间进行同步,则需要更改shell脚本,方法类似。
如果出现错误
yum -y install xinetd
service xinetd start
ps -ef|grep rsync
kill进程 然后删除/var/run 目录下的rsyncd.pid,重启进程