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): 备份文件

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

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