rsync+inotify 雙向同步

rsync+inotify 雙向同步
(注意:雙向同步前雙方同步的文件一定要一致,否則同步時會出現文件丟失的現象)
環境部署
主機名 主機IP地址 系統版本 系統內核版本
inotify-master 192.168.1.65 CentOS release 6.5 (Final) 2.6.32-642.15.1.el6.x86_64
inotify-master 192.168.1.67 CentOS release 6.5(Final) 2.6.32-642.15.1.el6.x86_64

1、默認系統已經安裝rsync,可以查看是否已經安裝
[root@test2 home]# rpm -aq |grep rsync
rsync-3.0.6-12.el6.x86_64

2、服務器端需要安裝inotify,下載地址

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

[root@test2 home]# tar -xvf inotify-tools-3.14.tar.gz
[root@test2 home]# cd inotify-tools-3.14
[root@test2 inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-3.14
[root@test2 inotify-tools-3.14]# make && make install

3、服務器需要增加rsyncd.conf文件
vi /etc/rsyncd.conf
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 100
pid file = /var/log/rsyncd.pid
lock file = /var/log/rsyncd.lock
log file = /var/log/rsyncd.log
[backup1]
path = /data/
igonre errors
read only = false
list = false
hosts allow = 192.168.1.0/24
hosts denoy = 0.0.0.0/32
auth users=rsync_backup
secrets file = /etc/rsyncd.password

4、在root@test2服務器上增加rsyncd.password
vi rsyncd.password
rsync_backup:abcdefg
其中rsync_backup爲rsyncd.conf下的auth users
密碼可以自己定義
在home目錄下添加rsyncpassword
Vi /home/rsyncpassword
abcdefg
chmod 600 /home/rsyncpasswordbr/>5、在root@test2修改rsyncd.password文件的權限
chmod 600 /etc/rsyncd.password

6、在root@test2增加rsync 用戶
[root@test2 ~]# useradd rsync -s /sbin/nologin -M
[root@test2~]# cat /etc/passwd
rsync:x:501:501::/home/rsync:/sbin/nologin
7、客戶端需要滿足以下條件
需要在/proc/sys/fs/inotify/ 有以下文件
[root@test2 inotify-3.14]# ll /proc/sys/fs/inotify/
總用量 0
-rw-r--r-- 1 root root 0 4月 18 10:21 max_queued_events
-rw-r--r-- 1 root root 0 4月 18 10:21 max_user_instances
-rw-r--r-- 1 root root 0 4月 18 10:21 max_user_watches
8、增加用戶
[root@test2 ~]# useradd rsync -s /sbin/nologin -M
[root@test2 ~]#cat /etc/passwd
rsync:x:500:500::/home/rsync:/sbin/nologin
9、創建rsyncd.password
vi rsyncd.password
abcdefg
只需要創建密碼即可,且密碼與服務端的一致
10、創建文件目錄並賦予權限
[root@test2~]# mkdir /data/
[ root@test2 ~]# chown -R rsync.rsync /data/
drwxr-xr-x 2 rsync rsync 4096 4月 18 11:06 data

11、服務器開啓rsync服務並查看服務是否啓動,使用的端口。
[root@test2 ~]# rsync --daemon
[root@test2 ~]# ps -ef |grep rsync
root 19045 1 0 10:45 ? 00:00:00 rsync --daemon
root 19168 25971 0 10:46 pts/1 00:00:00 grep rsync
[root@test2 ~]# netstat -anplt |grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0: LISTEN 19045/rsync
tcp 0 0 :::873 :::
LISTEN 19045/rsync br/>12、在root@test4上測試連接
[root@test2 ~]# rsync -avz aa [email protected]::backup1 --password-file=/home/rsyncpassword
rsync: failed to connect to 192.168.1.65: No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6]
出現No route to host (113),一般都爲防火牆開啓導致
(1)首先用ping 測試與192.168.1.65的聯通性
root@test2 ~]# ping 192.168.1.65
PING 192.168.1.67 (192.168.1.65) 56(84) bytes of data.
64 bytes from 192.168.1.65: icmp_seq=1 ttl=64 time=0.744 ms
64 bytes from 192.168.1.65: icmp_seq=2 ttl=64 time=0.206 ms
(2)telnet ,測試端口是否可以通
[root@test2 ~]# telnet 192.168.1.65 873
Trying 192.168.1.67...
telnet: connect to address 192.168.1.65: No route to host
說明873端口不通
(3)在防火牆增加允許873端口,vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT
重啓防火牆service iptables restart
[root@test4 ~]# service iptables restart
iptables:將鏈設置爲政策 ACCEPT:filter [確定]
iptables:清除防火牆規則: [確定]
iptables:正在卸載模塊: [確定]
iptables:應用防火牆規則: [確定]
(4)telnet ,測試端口是否可以通
[root@test4 ~]# telnet 192.168.1.65 873
Trying 192.168.1.65...
Connected to 192.168.1.65.
Escape character is '^]'.
@RSYNCD: 30.0
^]
telnet> quit
Connection closed.
(5)rsync -avz aa.sh [email protected]::backup1 --password-file=/home/rsyncpassword

[root@test4 home]# rsync -avz aa.sh [email protected]::backup1 --password-file=/home/rsyncpassword
sending incremental file list

sent 26 bytes received 8 bytes 68.00 bytes/sec
total size is 912 speedup is 26.82
13、創建inotify.sh
source /etc/profile
host01=192.168.1.67 #inotify-slave的ip地址
src=/data/ #本地監控的目錄
dst=backup
user=rsync_backup #inotify-slave的rsync服務的虛擬用戶
rsync_passfile=/home/rsyncpassword #本地調用rsync服務的密碼文件
inotify_home=/usr/local/inotify-3.14 #inotify的安裝目錄
#judge
if [ ! -e "$src" ] \
|| [ ! -e "${rsync_passfile}" ] \
|| [ ! -e "${inotify_home}/bin/inotifywait" ] \
|| [ ! -e "/usr/bin/rsync" ];
then
echo "Check File and Folder"
exit 9
fi
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \
| while read file
do

rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1

cd $src && rsync -aruz -R --delete ./ --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
done
exit 0

14、默認系統已經安裝rsync,可以查看是否已經安裝
[root@test4 home]# rpm -aq |grep rsync
rsync-3.0.6-12.el6.x86_64

15、服務器端需要安裝inotify,下載地址

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

[root@test4 home]# tar -xvf inotify-tools-3.14.tar.gz
[root@test4 home]# cd inotify-tools-3.14
[root@test4 inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-3.14
[root@test4 inotify-tools-3.14]# make && make install

16、服務器需要增加rsyncd.conf文件
vi /etc/rsyncd.conf
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 100
pid file = /var/log/rsyncd.pid
lock file = /var/log/rsyncd.lock
log file = /var/log/rsyncd.log
[backup]
path = /data/
igonre errors
read only = false
list = false
hosts allow = 192.168.1.0/24
hosts denoy = 0.0.0.0/32
auth users=rsync_backup
secrets file = /etc/rsyncd.password

17、在root@test4服務器上增加rsyncd.password
vi rsyncd.password
rsync_backup:abcdefg
其中rsync_backup爲rsyncd.conf下的auth users
密碼可以自己定義
在home目錄下添加rsyncpassword
Vi /home/rsyncpassword
abcdefg
chmod 600 /home/rsyncpasswordbr/>18、在root@test4修改rsyncd.password文件的權限
chmod 600 /etc/rsyncd.password

19、在root@test4增加rsync 用戶
[root@test4 ~]# useradd rsync -s /sbin/nologin -M
[root@test4~]# cat /etc/passwd
rsync:x:501:501::/home/rsync:/sbin/nologin
20、客戶端需要滿足以下條件
需要在/proc/sys/fs/inotify/ 有以下文件
[root@test4 inotify-3.14]# ll /proc/sys/fs/inotify/
總用量 0
-rw-r--r-- 1 root root 0 4月 18 10:21 max_queued_events
-rw-r--r-- 1 root root 0 4月 18 10:21 max_user_instances
-rw-r--r-- 1 root root 0 4月 18 10:21 max_user_watches
21、增加用戶
[root@test4 ~]# useradd rsync -s /sbin/nologin -M
[root@test4 ~]#cat /etc/passwd
rsync:x:500:500::/home/rsync:/sbin/nologin
22、創建rsyncd.password
vi rsyncd.password
abcdefg
只需要創建密碼即可,且密碼與服務端的一致
23、創建文件目錄並賦予權限
[root@test4~]# mkdir /data/
[ root@test4 ~]# chown -R rsync.rsync /data/
drwxr-xr-x 2 rsync rsync 4096 4月 18 11:06 data

11、服務器開啓rsync服務並查看服務是否啓動,使用的端口。
[root@test4 ~]# rsync --daemon
[root@test4~]# ps -ef |grep rsync
root 19045 1 0 10:45 ? 00:00:00 rsync --daemon
root 19168 25971 0 10:46 pts/1 00:00:00 grep rsync
[root@test2 ~]# netstat -anplt |grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0: LISTEN 19045/rsync
tcp 0 0 :::873 :::
LISTEN 19045/rsync br/>24、在root@test2上測試連接
[root@test2 ~]# rsync -avz aa [email protected]::backup1 --password-file=/home/rsyncpassword
rsync: failed to connect to 192.168.1.67: No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6]
出現No route to host (113),一般都爲防火牆開啓導致
(1)首先用ping 測試與192.168.1.67的聯通性
root@test2 ~]# ping 192.168.1.67
PING 192.168.1.67 (192.168.1.67) 56(84) bytes of data.
64 bytes from 192.168.1.67: icmp_seq=1 ttl=64 time=0.744 ms
64 bytes from 192.168.1.67: icmp_seq=2 ttl=64 time=0.206 ms
(2)telnet ,測試端口是否可以通
[root@test2 ~]# telnet 192.168.1.67 873
Trying 192.168.1.67...
telnet: connect to address 192.168.1.67: No route to host
說明873端口不通
(3)在防火牆增加允許873端口,vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT
重啓防火牆service iptables restart
[root@test4 ~]# service iptables restart
iptables:將鏈設置爲政策 ACCEPT:filter [確定]
iptables:清除防火牆規則: [確定]
iptables:正在卸載模塊: [確定]
iptables:應用防火牆規則: [確定]
(4)telnet ,測試端口是否可以通
[root@test4 ~]# telnet 192.168.1.67 873
Trying 192.168.1.67...
Connected to 192.168.1.67.
Escape character is '^]'.
@RSYNCD: 30.0
^]
telnet> quit
Connection closed.
(5)rsync -avz aa.sh [email protected]::backup --password-file=/home/rsyncpassword

[root@test4 home]# rsync -avz aa.sh [email protected]::backup --password-file=/home/rsyncpassword
sending incremental file list

sent 26 bytes received 8 bytes 68.00 bytes/sec
total size is 912 speedup is 26.82
25、創建inotify.sh
source /etc/profile
host01=192.168.1.65 #inotify-slave的ip地址
src=/data/ #本地監控的目錄
dst=backup1
user=rsync_backup #inotify-slave的rsync服務的虛擬用戶
rsync_passfile=/home/rsyncpassword #本地調用rsync服務的密碼文件
inotify_home=/usr/local/inotify-3.14 #inotify的安裝目錄
#judge
if [ ! -e "$src" ] \
|| [ ! -e "${rsync_passfile}" ] \
|| [ ! -e "${inotify_home}/bin/inotifywait" ] \
|| [ ! -e "/usr/bin/rsync" ];
then
echo "Check File and Folder"
exit 9
fi
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \
| while read file
do

rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1

cd $src && rsync -aruz -R --delete ./ --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
done
exit 0

26、分別在兩臺服務器上啓動inotify.sh
/home/inotify.sh &

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章