rsync_innotify實現文件雙向同步

主機:
192.168.40.105   host5
192.168.40.106   host6html

系統
centos 6.8linux

必備的軟件包
inotify-tools-3.14.tar.gz
http://pan.baidu.com/s/1nvSAs1Fc++

首先實現雙機互信
host5
生成密鑰對
[root@test5 ~]# ssh-keygen -t rsa -P ''
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
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:
46:e0:39:fc:e6:d6:d0:6c:72:f6:3b:fc:0f:1e:34:5b root@test5
The key's randomart image is:
+--[ RSA 2048]----+
|      .          |
|     o o         |
|      = .        |
|       + o       |
|        S *   o E|
|       + B . . + |
|        o ... +  |
|       .    oo o |
|            .oo..|
+-----------------+
把公鑰複製到遠程主機
[root@test5 ~]# ssh-copy-id -i .ssh/id_rsa.pub 192.168.40.106
The authenticity of host '192.168.40.106 (192.168.40.106)' can't be established.
RSA key fingerprint is 52:69:48:c4:70:3c:9c:91:c1:c2:c5:2b:c2:29:30:49.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.40.106' (RSA) to the list of known hosts.
root@192.168.40.106's password:
Now try logging into the machine, with "ssh '192.168.40.106'", and check in:git

  .ssh/authorized_keysgithub

to make sure we haven't added extra keys that you weren't expecting.
測試,看是否不用輸入密碼直接執行遠程命令
[root@test5 ~]# ssh 192.168.40.106 'hostname'
test6centos

host6
一樣執行實現密鑰進行認證
[root@test6 ~]# ssh-keygen -t rsa -P ''
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:
54:b3:69:7d:94:33:6c:73:9a:e8:6f:b4:cf:e6:87:b3 root@test6
The key's randomart image is:
+--[ RSA 2048]----+
|          o  ... |
|         . = .B .|
|        . + .o.B |
|       . .  ..o  |
|        S  .     |
|            . .  |
|             o o |
|              *.o|
|             .E*=|
+-----------------+
[root@test6 ~]# ssh-copy-id -i .ssh/id_rsa.pub 192.168.40.105
The authenticity of host '192.168.40.105 (192.168.40.105)' can't be established.
RSA key fingerprint is c4:72:2a:50:9e:b6:9d:4b:86:0e:4d:e6:c5:e8:50:26.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.40.105' (RSA) to the list of known hosts.
root@192.168.40.105's password:
Now try logging into the machine, with "ssh '192.168.40.105'", and check in:bash

  .ssh/authorized_keys服務器

to make sure we haven't added extra keys that you weren't expecting.dom

[root@test6 ~]# ssh 192.168.40.105 'hostname'
test5ssh

保持兩主機時間一致,經過ntpdate命令同步一臺時間服務器
兩主機都執行
ntpdate NTP_SERVER

host5上
經過腳本自動化安裝
rsync各參數和選項使用介紹
rsync --help
rsync是遠程文件同步工具,有多種工做模式,詳細使用可經過man rsync查看
將rsync以服務的方式啓動
chkconfig rsync on啓動rsync
rsync是受xinetd管理的,須要啓動xinetd,只輸出錯誤信息,正常輸出都重定向到/dev/null中
SRC是源目錄
DES是目標目錄
HOST爲目標主機

安裝腳本爲:
# cat agentd.sh
#!/bin/bash

SRC="/data/static/"
DES="/data/static/"
HOST=192.168.40.106

yum install -y xinetd 1> /dev/null
chkconfig rsync on

cat > /etc/rsyncd.conf << EOF
# Global setting
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log

# Directory to be synced
[tools]
path = /data/static
ignore errors = yes
read only = no
write only = no
hosts allow = 192.168.40.106
hosts deny = *
list = false
uid = root
gid = root
EOF

service xinetd start

#sync directory
rsync -vzrtopg --progress ${SRC} root@${HOST}:${DES} 1> /dev/null

運行腳本
bash agentd.sh

host6上
經過自動化安裝
安裝腳本爲:
# cat agentd.sh
#!/bin/bash

SRC="/data/static/"
DES="/data/static/"
HOST=192.168.40.105

yum install -y xinetd 1> /dev/null
chkconfig rsync on

cat > /etc/rsyncd.conf << EOF
# Global setting
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log

# Directory to be synced
[tools]
path = /data/static
ignore errors = yes
read only = no
write only = no
hosts allow = 192.168.40.105
hosts deny = *
list = false
uid = root
gid = root
EOF

service xinetd start

#sync directory
rsync -vzrtopg --progress ${SRC} root@${HOST}:${DES} 1> /dev/null

運行腳本
bash agentd.sh

host5上
經過自動化安裝
inotify工具能監控目錄下各文件屬性是否變化,當發生改變時,會執行腳本配置的命令,經過配置腳本同步使用rsync命令遠程同步文件。要實現雙向同步,則再另一臺主機上也部署好inotify工具,監控好對應的目錄,便可實現文件的雙向同步,從而實現文件共享。
inotifywait各參數和選項使用介紹
inotifywait --help
安裝腳本爲:
# cat server.sh
#!/bin/bash
yum install -y automake  gcc  gcc-c++  libtool 1> /dev/null

HOST=192.168.40.106
SRC="/data/static/"
DES=tools
wget https://codeload.github.com/rvoicilas/inotify-tools/zip/master
unzip master
cd inotify-tools-master/
./autogen.sh 1> /dev/null
./configure 1> /dev/null
make 1> /dev/null && make install 1> /dev/null

cd ~
cat > rsync_inotify.sh << EOF
#!/bin/bash

/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' -e modify,delete,create,attrib ${SRC} | while read file
do
  /usr/bin/rsync -vzrtopg --delete --progress ${SRC} root@${HOST}::${DES} 1> /dev/null
  echo "\${file} was rsynced" >> /tmp/rsync.log 2>&1
done
EOF

bash rsync_inotify.sh &

運行腳本
bash server.sh

 

[root@test5 src]# ps aux | grep rsync
root      9667  0.0  0.0 106072  1296 pts/0    S    18:21   0:00 bash rsync_inotify.sh
root      9669  0.0  0.0 106072   492 pts/0    S    18:21   0:00 bash rsync_inotify.sh
root      9672  0.0  0.0 103312   876 pts/0    S+   18:22   0:00 grep rsync

 

host6上
經過自動化安裝
安裝腳本爲:
# cat server.sh
#!/bin/bash
yum install -y automake  gcc  gcc-c++  libtool 1> /dev/null

HOST=192.168.40.105
SRC="/data/static/"
DES=tools
wget https://codeload.github.com/rvoicilas/inotify-tools/zip/master
unzip master
cd inotify-tools-master/
./autogen.sh 1> /dev/null
./configure 1> /dev/null
make 1> /dev/null && make install 1> /dev/null

cd ~
cat > rsync_inotify.sh << EOF
#!/bin/bash

/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' -e modify,delete,create,attrib ${SRC} | while read file
do
  /usr/bin/rsync -vzrtopg --delete --progress ${SRC} root@${HOST}::${DES} 1> /dev/null
  echo "\${file} was rsynced" >> /tmp/rsync.log 2>&1
done
EOF

bash rsync_inotify.sh &

運行腳本
bash server.sh

 

[root@test6 src]# ps aux | grep rsync
root     20202  0.0  0.0 106072  1292 pts/0    S    18:21   0:00 bash rsync_inotify.sh
root     20204  0.0  0.0 106072   488 pts/0    S    18:21   0:00 bash rsync_inotify.sh
root     28661  0.0  0.0 103316   836 pts/0    S+   18:22   0:00 grep rsync

 

參考連接:
# http://www.cnblogs.com/linuxzkq/p/rsync-inotify.html
# http://www.2cto.com/os/201211/172046.html
# http://www.2cto.com/os/201211/172046.html 

 

測試:
新增文件
[root@test5 src]# ls /data/static/
[root@test5 src]# ssh 192.168.40.106 'ls /data/static/'
[root@test5 src]# cp -a /etc/init /data/static/
[root@test5 src]# ls /data/static/
init
[root@test5 src]# ssh 192.168.40.106 'ls /data/static/'
init
[root@test5 src]# ls /data/static/init/
ck-log-system-restart.conf  init-system-dbus.conf   quit-plymouth.conf  rcS-sulogin.conf                 serial.conf
ck-log-system-start.conf    kexec-disable.conf      rc.conf             readahead-collector.conf         splash-manager.conf
ck-log-system-stop.conf     plymouth-shutdown.conf  rcS.conf            readahead.conf                   start-ttys.conf
control-alt-delete.conf     prefdm.conf             rcS-emergency.conf  readahead-disable-services.conf  tty.conf
[root@test5 src]# ssh 192.168.40.106 'ls /data/static/init'
ck-log-system-restart.conf
ck-log-system-start.conf
ck-log-system-stop.conf
control-alt-delete.conf
init-system-dbus.conf
kexec-disable.conf
plymouth-shutdown.conf
prefdm.conf
quit-plymouth.conf
rc.conf
rcS.conf
rcS-emergency.conf
rcS-sulogin.conf
readahead-collector.conf
readahead.conf
readahead-disable-services.conf
serial.conf
splash-manager.conf
start-ttys.conf
tty.conf

刪除文件
[root@test5 src]# ls /data/static/
init
[root@test5 src]# rm -rf /data/static/init
[root@test5 src]# ls /data/static/
[root@test5 src]# ssh 192.168.40.106 'ls /data/static/'

更多測試能夠本身實驗

相關文章
相關標籤/搜索