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

1.用rsync+ssh软件做文件定时备份,增量备份,但不是做到实时备份,所以单独使用效果不是很好.
服务器A(ip:2.2.2.135):
[root@CentOS ~]# ifconfig  eth0      -- 查看IP地址
eth0      Link encap:Ethernet  HWaddr 08:00:27:56:AA:AA
inet addr:2.2.2.135  Bcast:2.255.255.255  Mask:255.0.0.0
inet6 addr: fe80::a00:27ff:fe56:aaaa/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:13072 errors:0 dropped:0 overruns:0 frame:0
TX packets:328 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1009592 (985.9 KiB)  TX bytes:31733 (30.9 KiB)
[root@centos ~]# vim /etc/hosts    --添加以下行
2.2.2.145 centos1
2.2.2.135 centos
[root@centos ~]# yum install rsync    --安装rsync软件,记住两台电脑必须全部安装
[root@centos ~]# 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:
12:c6:7b:6a:12:4a:15:c8:8b:0f:90:54:02:fa:08:27 root@centos
The key's randomart image is:
+--[ RSA 2048]----+
|+=.o.            |
|+ +  o          |
|E.... +          |
|+=.. . o        |
|.oo . o S        |
| ... . +        |
|  . . o          |
|    o          |
|                |
+-----------------+
[root@centos ~]# scp ~/.ssh/id_rsa.pub  2.2.2.145:/root/.ssh/authorized_keys  --将公钥放到别一台服务器上
[root@centos ~]# ssh 2.2.2.145    --登陆不用密码了
Last login: Tue Dec 17 09:46:05 2013 from centos
[root@centos1 ~]#

服务器B(ip:2.2.2.145):
[root@centos1 ~]$ ifconfig  eth0    --查看IP地址
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:14005 errors:0 dropped:0 overruns:0 frame:0
TX packets:503 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1081748 (1.0 MiB)  TX bytes:46832 (45.7 KiB)
[root@centos1 ~]$ vim /etc/hosts    --添加以下两行
2.2.2.135  centos
2.2.2.145  centos1
[root@centos1 ~]$ ping 2.2.2.135 -c 1      --是否相通
PING 2.2.2.135 (2.2.2.135) 56(84) bytes of data.
64 bytes from 2.2.2.135: icmp_seq=1 ttl=64 time=0.438 ms
--- 2.2.2.135 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.438/0.438/0.438/0.000 ms
[root@centos ~]# yum install rsync
PING 2.2.2.135 (2.2.2.135) 56(84) bytes of data.
64 bytes from 2.2.2.135: icmp_seq=1 ttl=64 time=0.438 ms
--- 2.2.2.135 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.438/0.438/0.438/0.000 ms
[root@centos ~]# yum install rsync    --安装软件
[root@centos1 ~]# 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:
4b:74:44:ca:38:50:17:25:3a:1e:b0:ac:a4:7d:f3:be root@centos1
The key's randomart image is:
+--[ RSA 2048]----+
|    o.. +++      |
|  . + = +      |
|  . o * + .      |
| + . . = .      |
|. o o . S        |
|  . o . .      |
|      . .        |
|    .          |
|      E.        |
+-----------------+
[root@centos1 ~]# scp ~/.ssh/id_rsa.pub  2.2.2.135:/root/.ssh/authorized_keys
[root@centos1 ~]$ rsync  2.2.2.135:/home/*.txt /home/    --远程复制文件,-r是目录
[root@centos1 ~]$ ll /home/*.txt        --复制成功
-rw-r--r-- 1 root root 36 Dec 16 14:40 /home/1.txt
-rw-r--r-- 1 root root 12 Dec 16 14:34 /home/2.txt
-rw-r--r-- 1 root root  0 Dec 16 15:48 /home/3.txt
[root@centos1 ~]$ crontab  -e
* 24 * * *  /usr/bin/rsync 2.2.2.135:/home/*.txt /home/    --每天24点备份

2.rsync+ssh+inotify做文件实时备份工作(inotify文件系统监控)
服务器A(ip:2.2.2.135): 备份文件

[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
[email protected]'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 ~]#

3)服务器2登陆服务器1不用密码
[root@centos1 unison-2.40.63]# 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:
be:43:3a:24:4c:83:fe:bd:22:e2:ed:c2:89:7e:ab:c9 root@centos1
The key's randomart image is:
+--[ RSA 2048]----+
|                |
|                |
|  .            |
|  . o            |
| . o .  S        |
|  . o ...        |
|o .. + o.        |
|+++.o + ..      |
|oE==...o..      |
+-----------------+
[root@centos1 unison-2.40.63]# scp ~/.ssh/id_rsa.pub 2.2.2.135:/root/.ssh/authorized_keys
[root@centos1 unison-2.40.63]# ssh 2.2.2.135
Last login: Tue Dec 17 13:26:06 2013 from centos1
[root@centos ~]#

4)服务器1,2文件同步
[root@centos1 bin]# unison /home ssh://[email protected]:/home    --文件同步
[root@centos1 bin]# unison /home ssh://[email protected]:/home --path  目录  --只同步指定的目录

Rsync 的详细介绍:请点这里
Rsync 的下载地址:请点这里

推荐阅读:

相关推荐