Linux系统文件同步rsync+ssh+inotify+unison实现(2)

[root@centos1 samba]$ tar xvf inotify-tools-3.14.tar.gz
[root@centos1 samba]$ cd inotify-tools-3.14
[root@centos1 inotify-tools-3.14]$ ./configure  --prefix=/usr/local/inotify && make && make install    --安装软件
[root@centos inotify-tools-3.14]# cd /usr/local/inotify/
[root@centos inotify]# ll
total 16
drwxr-xr-x. 2 root root 4096 Dec 16 16:08 bin
drwxr-xr-x. 3 root root 4096 Dec 16 16:08 include
drwxr-xr-x. 2 root root 4096 Dec 16 16:08 lib
drwxr-xr-x. 4 root root 4096 Dec 16 16:08 share
[root@centos inotify]# cd bin/
[root@centos bin]# ll
total 72
-rwxr-xr-x. 1 root root 36447 Dec 16 16:08 inotifywait
-rwxr-xr-x. 1 root root 34961 Dec 16 16:08 inotifywatch
[root@centos bin]# vim start.sh
#!/bin/sh
SRC=/home/
DST=/home/
IP=2.2.2.145
/usr/local/inotify/bin/inotifywait -mrq -e modify,delete,create,attrib ${SRC} | while read status
do
/usr/bin/rsync  -auvg  --delete  $DST  $IP:$SRC  &&    --在后台执行
if (echo "$?" == "0");then
echo `date + %D`:back ok >> /home/1.log  --在date前面有反单引号
else
echo `date +%D`:error >> /home/1.log
done
[root@centos1 bin]# chmod o+x start.sh
[root@centos1 bin]# ./start.sh  &
[2] 2915
[root@centos1 bin]# ll /home/1
total 8
-rw-r--r-- 1 root root 36 Dec 16 14:40 1.txt
-rw-r--r-- 1 root root 12 Dec 16 14:34 2.txt
-rw-r--r-- 1 root root  0 Dec 17 10:58 3.txt
[root@centos1 bin]# mkdir /home/1/{1,2,3}
[root@centos1 bin]# ll /home/1
total 20
drwxr-xr-x 2 root root 4096 Dec 17 11:31 1
-rw-r--r-- 1 root root  36 Dec 16 14:40 1.txt
drwxr-xr-x 2 root root 4096 Dec 17 11:31 2
-rw-r--r-- 1 root root  12 Dec 16 14:34 2.txt
drwxr-xr-x 2 root root 4096 Dec 17 11:31 3
-rw-r--r-- 1 root root    0 Dec 17 10:58 3.txt
[root@centos1 bin]# cat /home/1.log    --日志有记录,成功了
:back ok
:back ok
:back ok
:back ok
[root@centos1 bin]#    --去2.2.2.145看看文件有没有同步

服务器B(ip:2.2.2.145):用于读文件
[root@centos1 1]# ifconfig  eth0
eth0      Link encap:Ethernet  HWaddr 08:00:27:56:AA:AA
inet addr:2.2.2.145  Bcast:2.2.2.255  Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe56:aaaa/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:38855 errors:0 dropped:0 overruns:0 frame:0
TX packets:12226 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:3390394 (3.2 MiB)  TX bytes:1557639 (1.4 MiB)
[root@centos1 1]# ll /home/1    --服务器B可以看到了
total 20
drwxr-xr-x 2 root root 4096 Dec 17 11:31 1
-rw-r--r-- 1 root root  36 Dec 16 14:40 1.txt
drwxr-xr-x 2 root root 4096 Dec 17 11:31 2
-rw-r--r-- 1 root root  12 Dec 16 14:34 2.txt
drwxr-xr-x 2 root root 4096 Dec 17 11:31 3
-rw-r--r-- 1 root root    0 Dec 17 10:58 3.txt

3.unison+ssh文件双向同步,无法实现实时双向同步
1)安装unison软件
[root@centos1 samba]# tar xvf ocaml-3.12.1.tar.gz
[root@centos1 samba]# cd ocaml-3.12.1
[root@centos1 ocaml-4.01.0]# ./configure  && make && make install
[root@centos1 ocaml-4.01.0]# cd ..
[root@centos1 samba]# tar xvf unison-2.40.63.tar.gz
[root@centos1 samba]# cd unison-2.40.63
[root@centos1 unison-2.40.63]# make UISTYLE=text
[root@centos1 unison-2.40.63]# mkdir /root/bin
[root@centos1 unison-2.40.63]# make install
[root@centos1 unison-2.40.63]# cp /root/bin/unison /usr/bin

2)ssh登陆不用密(在服务器1登陆服2不用密码)
[root@centos home]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
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:
e3:70:96:80:20:8b:e8:a9:6f:7a:00:64:5f:8b:86:90 root@centos
The key's randomart image is:
+--[ RSA 2048]----+
|...              |
|E= . o          |
|B o + o          |
|o..+ . . .      |
|.o.  . S        |
|o      = .      |
|..      .        |
| .o              |
|.+.              |
+-----------------+
[root@centos home]# scp ~/.ssh/id_rsa.pub  2.2.2.145:/root/.ssh/authorized_keys
root@2.2.2.145's password:
id_rsa.pub                                                                              100%  393    0.4KB/s  00:00
[root@centos home]# ssh 2.2.2.145  --成功,不用输入密码
Last login: Tue Dec 17 13:25:15 2013 from centos
[root@centos1 ~]#

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/bdb84a9ce2e18c50c760efea066b8b9b.html