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

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文件里,实现开机启动.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值