inotify-tools用法
chkconfig配置开机启动

rsync+inotify-tools+ssh触发式同步

妙音 posted @ 2014年12月02日 14:53 in linux with tags rsync;inotify;同步 , 3926 阅读

一、配置环境

  • 安装rsync
apt-get install rsync
  • 安装inotify-tools
apt-get install inotify-tools
  • 启动ssh服务
service sshd start
  • 配置SSH公钥登陆
* 生成秘钥
ssh-keygen
* 将公钥存到远程主机
ssh-copy-id username@ip

二、脚本示例

示例dir_sync.sh
#!/bin/sh
SRC=/home/wyq/workspace/src/
DST=wyq@localhost:/home/wyq/workspace/dest

#增量同步(同步)
dir_sync(){
    echo "开始同步"
    #rsync -avz --delete $SRC $DST  #完全一致同步
    rsync -avz $SRC $DST
    echo "完成同步!"
}

#主函数
main(){
    # 启动时先同步一次
    dir_sync
    echo "进入阻塞状态,等待触发事件..."
    #循环
    inotifywait -mrq -e modify,delete,create ${SRC} | while read D E F 
        do 
             echo "目录变化" $D, $E, $F
             # 执行同步
             dir_sync
             echo "进入阻塞状态,等待触发事件..."
        done
}

#入口
echo "启动同步"
main
  • 命令rsync -avz $SRC $DST
-a 归档模式,递归传输所有文件,并保持文件属性
-v 详细模式
-z 压缩传输
  • 命令inotifywait -mrq -e modify,delete,create ${SRC}
-m 无期限地执行
-r  监控一个目录下的所有子目录
-q  精简输出
-e  监视的事件
 
Avatar_small
依云 说:
2014年12月07日 21:56

https://github.com/ehamberg/fswatcher/ 这个更好用的说~

Avatar_small
妙音 说:
2014年12月13日 20:03

@依云: 用Haskell实现的,我看看

Avatar_small
seo service london 说:
2024年1月13日 22:06

This article is an appealing wealth of informative data that is interesting and well-written


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter