rsync实现服务器之间文件实时同步

1、主服务器git

其中主服务器须要安装rsync与inotify,主服务器做为server,向备份服务器client传输文件github

1.安装rsyncvim

yum -y install rsyncbash


2.创建密码文件服务器

echo '123456' >> /etc/data.passwdide

chmod 600 /etc/data.passwd测试


三、安装inotify
ui

wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz spa

tar zxf inotify-tools-3.14.tar.gzorm

cd inotify-tools-3.14

./configure --prefix=/usr/local/inotify

make && make install


四、建立rsync复脚本

不管是添加、修改、删除文件都可以经过inotify监控到,并经过rsync实时的同步给client的/data里

#!/bin/bash  

ip=10.6.88.229

src=/data/test

des=test

user=root

/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=/etc/data.passwd $src $user@$ip::$des

echo "${files} was rsynced" >>/var/log/rsync.log 2>&1

done


5.启用脚本

echo "/tw/rsync.sh" >> /etc/rc.local 

sh /tw/rsync.sh &


2、备份服务器


1.安装rsync

yum -y install rsync

2.创建rsync配置文件

vim /etc/rsyncd.conf

uid = root

gid = root

port = 873

max connections = 20000

use chroot = yes

timeout = 200

log file = /var/log/rsyncd.log

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsyncd.lock

log format = %t %a %m %f %b

auth users = root

secrets file = /etc/data.passwd


[test]

path = /data/

comment = "test directory file"

list = yes

read only = no

ignore errors = yes

hosts allow = 10.6.88.0/255.255.0.0


3.创建密码文件

echo 'root:123456' >> /etc/data.passwd

chmod 600 /etc/data.passwd


4.启动rsync

/usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync.conf 

echo '/usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync.conf ' >> /etc/rc.local


接下来咱们来作一下测试,在server上/data/test/目录下建立个test文件,看看client是否能收到

注:如需在主服务器上删除备份文件,而备份服务器上保留文件。直接把脚本里--delete参数去掉就能够。

相关文章
相关标签/搜索