您的位置:首页 > 其它

Redhat服务器利用rsync和inotify-tools实现实时增量备份的方法(详解)

2013-08-21 15:08 726 查看
1.首先检查系统是否安装了rsync软件包,一般RedHat服务器默认是安装的

[root@localhost ~]# rpm -qa |grep rsync #检查系统是否安装了rsync软件包

2.在RedHat服务器上一般不用配置rsync,测试ssh联通是否正常即可.

[root@localhost ~]# rsync -aSvH /test/ root@192.9.169.107:/test/

会提示输入107服务器root的密码,输入密码后能正常传送,说明rsync运行正常,客户端不用配置。

3.免SSH密码认证,创建server1到server2的信任连接

在server1上执行

[root@localhost ~]# ssh-keygen -b 1024 -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):   #直接回车
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:
9c:c0:b0:a9:b6:c2:b7:b0:f7:37:89:1e:eb:2c:85:3a root@localhost.localdomain


传送证书到server2上,这一步要先在server2上建立/root/.ssh文件夹。

[root@localhost ~]# cd /root/.ssh/

[root@localhost .ssh]# scp id_rsa.pub root@192.9.169.107:~/.ssh/authorized_keys

测试SSH连接

[root@localhost ~]# ssh 192.9.169.107 #如果不用输入密码就能登录到107,说明成功了。如不成功请检查authorized_keys文件的属性

4.安装inotify-tools

# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz  (下载inotify-tools)
# tar zxvf inotify-tools-3.14.tar.gz
#  cd inotify-tools-3.14
# ./configure --prefix=/usr/local/inotify
# make
# make install


5.挂再脚本

脚本1:建立脚本inotify,内容如下:

#!/bin/sh
SRC=/test2/
DST=root@192.9.169.107:/test/
INWT=/usr/local/inotify/bin/inotifywait
RSYNC=/usr/bin/rsync
$INWT -mrq -e create,move,delete,modify $SRC | while read D E F;do
rsync -aHqzt --delete $SRC $DST
done


脚本2:建立脚本inotify2,内容如下:

#!/bin/bash
########### ssh 传输
srcdir="/test2/"
ip="192.9.169.107"              #多台服务器可以自己添加IP
dstdir="/test2/"

/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y-%H:%M' --format '%T %w%f' -e modify,delete,create,attrib ${srcdir} | while read file
do
for i in ${ip}
do
rsync -aqztH --delete -progress '-e ssh -p 22'  ${srcdir} root@${i}:${dstdir}    # 22为ssh端口号
done
done


建议使用脚本2,脚本1只改变文件的属性,不会同步,脚本2支持向多个ip备份。

挂在方法可以使用[root@localhost ~]# nohup ./inotify2 & 保证在终端断开的情况下能就绪执行,

也可以把nohup ./inotify2 & 添加到rc.load文件里,实现开机启动.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: