彩运网网址-彩运网网上登录

彩运网网址Linux系统文件同步rsync+ssh+inotify+uniso

    引用:

1.用rsync+ssh软件做文件定时备份,增量备份,但不是做到实时备份,所以单独使用效果不是很好.
服务器A(ip:2.2.2.135):
[[email protected] ~]# 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)
[[email protected] ~]# vim /etc/hosts    --添加以下行
2.2.2.145 centos1
2.2.2.135 centos
[[email protected] ~]# yum install rsync    --安装rsync软件,记住两台电脑必须全部安装
[[email protected] ~]# 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 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|+=.o.            |
|+ +  o          |
|E.... +          |
|+=.. . o        |
|.oo . o S        |
| ... . +        |
|  . . o          |
|    o          |
|                |
+-----------------+
[[email protected] ~]# scp ~/.ssh/id_rsa.pub  2.2.2.145:/root/.ssh/authorized_keys  --将公钥放到别一台服务器上
[[email protected] ~]# ssh 2.2.2.145    --登陆不用密码了
Last login: Tue Dec 17 09:46:05 2013 from centos
[[email protected] ~]#

    Inotify 是文件系统事件监控机制,计划包含在即将发布的 Linux 内核中作为 dnotify 的有效替代。dnotify 是较早内核支持的文件监控机制。Inotify一种强大的、细粒度的、异步的机制,它满足各种各样的文件监控需要,不仅限于安全和性能。下面让我们一起学习如何安装 inotify 和如何构建一个示例用户空间应用程序来响应文件系统事件。

服务器B(ip:2.2.2.145):
[[email protected] ~]$ 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)
[[email protected] ~]$ vim /etc/hosts    --添加以下两行
2.2.2.135  centos
2.2.2.145  centos1
[[email protected] ~]$ 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
[[email protected] ~]# 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
[[email protected] ~]# yum install rsync    --安装软件
[[email protected] ~]# 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 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|    o.. +++      |
|  . + = +      |
|  . o * + .      |
| + . . = .      |
|. o o . S        |
|  . o . .      |
|      . .        |
|    .          |
|      E.        |
+-----------------+
[[email protected] ~]# scp ~/.ssh/id_rsa.pub  2.2.2.135:/root/.ssh/authorized_keys
[[email protected] ~]$ rsync  2.2.2.135:/home/*.txt /home/    --远程复制文件,-r是目录
[[email protected] ~]$ 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
[[email protected] ~]$ crontab  -e
* 24 * * *  /usr/bin/rsync 2.2.2.135:/home/*.txt /home/    --每天24点备份

    文件系统事件监控对于从文件管理器到安全工具的各种程序都是必要的,但是 dnotify(早期内核中的标准)存在一些局限性,这使我们期待出现一种更加完善的机制。抱着这种期待,我们发现了 inotify,一种更加现代化的文件系统事件监控替代品。

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

    为什么使用 inotify?

[[email protected] samba]$ tar xvf inotify-tools-3.14.tar.gz
[[email protected] samba]$ cd inotify-tools-3.14
[[email protected] inotify-tools-3.14]$ ./configure  --prefix=/usr/local/inotify && make && make install    --安装软件
[[email protected] inotify-tools-3.14]# cd /usr/local/inotify/
[[email protected] 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
[[email protected] inotify]# cd bin/
[[email protected] 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
[[email protected] 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
[[email protected] bin]# chmod o+x start.sh
[[email protected] bin]# ./start.sh  &
[2] 2915
[[email protected] 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
[[email protected] bin]# mkdir /home/1/{1,2,3}
[[email protected] 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
[[email protected] bin]# cat /home/1.log    --日志有记录,成功了
:back ok
:back ok
:back ok
:back ok
[[email protected] bin]#    --去2.2.2.145看看文件有没有同步

    使用 inotify 取代 dnotify 的原因有很多。第一个原因是,dnotify 需要您为每个打算监控是否发生改变的目录打开一个文件描述符。当同时监控多个目录时,这会消耗大量的资源,因为有可能达到每个进程的文件描述符限制。

服务器B(ip:2.2.2.145):用于读文件
[[email protected] 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)
[[email protected] 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

    除此之外,文件描述符会锁定目录,不允许卸载(unmount)支持的设备,这在存在可移动介质的环境中会引发问题。在使用 inotify 时,如果正在监控被卸载的文件系统上的文件,那么监控会被自动移除并且您会接收到一个卸载事件。

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

    dnotify 不如 inotify 的第二个原因是 dnotify 有点复杂。注意,使用 dnotify 基础设施的简单文件系统监控粒度只停留于目录级别。为了使用 dnotify 进行更细粒度的监控,应用程序编程人员必须为每个受监控的目录保留一个 stat 结构的缓存。该用户空间的 stat 结构缓存需要用来明确确定当接收到通知信号时目录发生了什么变化。当获得通知信号时,生成 stat 结构列表并与最新的状态相比较。显而易见,这种技术是不理想的。

2)ssh登陆不用密(在服务器1登陆服2不用密码)
[[email protected] 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 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|...              |
|E= . o          |
|B o + o          |
|o..+ . . .      |
|.o.  . S        |
|o      = .      |
|..      .        |
| .o              |
|.+.              |
+-----------------+
[[email protected] 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
[[email protected] home]# ssh 2.2.2.145  --成功,不用输入密码
Last login: Tue Dec 17 13:25:15 2013 from centos
[[email protected] ~]#

    inotify 的另一个优点是它使用文件描述符作为基本接口,使应用程序开发者使用 select 和 poll 来监控设备。这允许有效的多路 I/O 和与 Glib 的 mainloop 的集成。相反,dnotify 所使用的信号常常使程序员头疼并且感觉不太优雅。

3)服务器2登陆服务器1不用密码
[[email protected] 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 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|                |
|                |
|  .            |
|  . o            |
| . o .  S        |
|  . o ...        |
|o .. + o.        |
|+++.o + ..      |
|oE==...o..      |
+-----------------+
[[email protected] unison-2.40.63]# scp ~/.ssh/id_rsa.pub 2.2.2.135:/root/.ssh/authorized_keys
[[email protected] unison-2.40.63]# ssh 2.2.2.135
Last login: Tue Dec 17 13:26:06 2013 from centos1
[[email protected] ~]#

    inotify 通过提供一个更优雅的 API 解决了这些问题,该 API 使用最少的文件描述符,并确保更细粒度的监控。与 inotify 的通信是通过设备节点提供的。基于以上原因,对于监控 Linux 2.6 平台上的文件,inotify 是您最明智的选择。

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

    linux/l-inotify.html#N10081">http://www-128.ibm.com/developerworks/cn/linux/l-inotify.html#N10081

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

    正文:
    sotfware: inotify-tools-3.13.tar.gz
    OS:       Ubuntu server 8.10
    server1:  192.168.6.2
    server2:  192.168.6.3

推荐阅读:

    需求 server1 目录 /home/ludy/rsync 时时同步 server2 /home/ludy
    安装步骤:
    1.首先确认你的内核支持inotify,如果不支持对内核打补丁,一般情况内核在 2.6.3以上的都支持的。你可以确认下运行

利用inotifywait监控主机文件和目录

    ludy@server1:~$ls -l /proc/sys/fs/inotify/
    总用量 0
    -rw-r--r-- 1 ludy ludy 0 2008-12-16 14:40 max_queued_events
    -rw-r--r-- 1 ludy ludy 0 2008-12-16 14:40 max_user_instances
    -rw-r--r-- 1 root root 0 2008-12-16 09:07 max_user_watches
    如果没有的话,呵呵安装inotify~

利用inotify+rsync实现Linux文件批量更新

    2.安装软件 inotify-tools-3.13.tar.gz
    ludy@server1:~$ tar zxvf inotify-tools-3.13.tar.gz

inotify-tools+rsync实时同步文件安装和配置

    ludy@server1:~$ cd inotify-tools-3.13

rsync同步完整配置

    ludy@server1:~$ ./configure --prefix=/usr/local/inotify
    ludy@server1:~$ make
    ludy@server1:~$ make install

http://www.bkjia.com/Linux/766875.htmlwww.bkjia.comtruehttp://www.bkjia.com/Linux/766875.htmlTechArticle1.用rsync+ssh软件做文件定时备份,增量备份,但不是做到实时备份,所以单独使用效果不是很好. 服务器A(ip:2.2.2.135): [[email protected] ~]# ifconfig e...

本文由彩运网网址发布于网络安全,转载请注明出处:彩运网网址Linux系统文件同步rsync+ssh+inotify+uniso

相关阅读