inotify-tool和rsync

简介

安装rsync

rsync

[root@c1 ~]# rpm -qa rsync
rsync-3.0.9-17.el7.x86_64

配置slave及启动

useradd rsync -s /sbin/nologin -M
[root@c1 ~]# grep rsync /etc/passwd
rsync:x:1000:1000::/home/rsync:/sbin/nologin
[root@c1 ~]# mkdir /backup
[root@c1 ~]# chown rsync.rsync /backup
[root@c1 ~]#  ll -d /backup/
drwxr-xr-x 2 rsync rsync 4096 Apr  1 10:01 /backup/

[root@c1 ~]# cat /etc/rsyncd.conf
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[backup]
        path = /backup/
        ignore errors
        read only = false
        list = false
        hosts allow = 192.168.143.0/24
        hosts deny = 0.0.0.0/32
        auth users = rsync_backup
        secrets file = /etc/rsync.password

[root@c1 ~]# rsync --daemon

master配置及测试

[root@inotify-master ~]# echo "leesir" >/etc/rsync.password
[root@inotify-master ~]# cat /etc/rsync.password
leesir   #注意:这里只要写密码即可,切记。
[root@inotify-master ~]# chmod 600 /etc/rsync.password

随便写一个测试文件test.txt,再通过下面的命令向slave(192.168.143.208)同步文件

rsync -avz test.txt rsync_backup@192.168.143.208::backup --password-file=/etc/rsync.password

安装inotify-tools

inotify-tools-3.14

安装依赖包

yum -y install automake
yum -y install libtool.x86_64

进入解压后的目录inotify-tools-3.14,安装

./autogen.sh
./configure --prefix=/usr/local/inotify-3.14
make && make install

测试

创建用于同步的目录

mkdir /backup

编写test.sh,其中192.168.143.208是rsync --daemon的服务器

#!/bin/bash
host01=192.168.143.208  #inotify-slave的ip地址
src=/backup/        #本地监控的目录
dst=backup         #inotify-slave的rsync服务的模块名
user=rsync_backup      #inotify-slave的rsync服务的虚拟用户
rsync_passfile=/etc/rsync.password   #本地调用rsync服务的密码文件
inotify_home=/usr/local/inotify-3.14    #inotify的安装目录
#judge
if [ ! -e "$src" ] \
|| [ ! -e "${rsync_passfile}" ] \
|| [ ! -e "${inotify_home}/bin/inotifywait" ] \
|| [ ! -e "/usr/bin/rsync" ];
then
echo "Check File and Folder"
exit 9
fi
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \
| while read file
do
#  rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1
cd $src && rsync -aruz -R --delete ./  --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
done
exit 0

执行test.sh &,之后在/backup目录下做文件创建、修改等动作,slave上都会被同步

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值