歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Rsync+inotify文件實時同步

Rsync+inotify文件實時同步

日期:2017/2/27 16:03:46   编辑:Linux教程
Rsync+inotify文件傳輸同步
目的:把主服務器192.168.2.132上的文件實時同步到備服務器192.168.2.133上。
使用inotify實時監控主服務器/servyou/webroot目錄變化,並通過rsync把改變的部分推送給備服務器。
主備服務器安裝rsync
主服務器(數據源)安裝inotify

1、ssh認證生成密鑰對

# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
c2:81:43:6b:4f:cf:03:60:64:b2:d7:04:b6:65:80:f7 root@servyouhome
一路回車,不做任何修改

2、ssh認證copy公鑰到備份服務器192.168.2.133

# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
輸入yes,一路回車
The authenticity of host '192.168.2.133 (192.168.2.133)' can't be established.
RSA key fingerprint is 67:82:61:72:fa:37:06:3f:8e:03:3c:63:ac:1b:d1:d4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.2.133' (RSA) to the list of known hosts.
[email protected]'s password:
Now try logging into the machine, with "ssh '[email protected]'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.

3、主備服務器安裝rsync

#wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz
#tar -zxvf rsync-3.0.9.tar.gz
#cd rsync-3.0.9
#./configure
#make
#make install

4、主備服務器以守護進程啟動rsync,並設置開機啟動

# rsync --daemon
#chkconfig rsync on

5、主服務器(源數據端)安裝inotify

#wget http://cloud.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
#make
#make install

6、備服務器配置rsync

#vim /etc/rsyncd.conf
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/rsync.log
[webroot]
path = /servyou/webroot/
comment = webroot files
ignore errors
read only = no
write only = no
hosts allow = 192.168.2.0/255.255.255.0
#hosts deny = 0.0.0.0/32
list = false
uid = root
gid = root
#auth users = backup
#secrets file = /etc/server.pass

7、在主服務器配置內容發布節點

#!/bin/bash
SRC=/servyou/webroot/
[email protected]:/servyou/webroot/
HOST=192.168.2.133
/usr/local/bin/inotifywait -mrq --timefmt '%Y-%m-%d %H:%M:%S' --format '%T %w%f%e' -e modify,close_write,delete,create,attrib $SRC | while read files
do
/usr/bin/rsync -azvl --delete --progress --exclude-from=/servyou/backup/exclude.list $SRC $DES
echo "${files} was rsynced" >> /var/log/rsync.log 2>&1
done
將這個腳本命名為rsync.sh,放到/servyou/目錄下,然後指定可執行權限,放到後台運行,如下:
#chmod 755 /servyou/rsync.sh
#nohup /servyou/rsync.sh & > /dev/null 2>&1
#echo “nohup /servyou/rsync.sh & ” >> /etc/rc.local
Copyright © Linux教程網 All Rights Reserved