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參數去掉就能夠。