Linux文件实时同步--inotify + rsync + pyinotify
grep INOTIFY_USER /boot/config-$(uname -r)
输出:CONFIG_INOTIFY_USER=y 表示支持inotify机制
yum install inotify-tools (版本为3.13)
#!/bin/sh # get the current path CURPATH=`pwd` inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' \ -e close_write /tmp/test | while read date time dir file; do FILECHANGE=${dir}${file} # convert absolute path to relative FILECHANGEREL=`echo "$FILECHANGE" | sed 's_'$CURPATH'/__'` rsync --progress --relative -vrae 'ssh -p 22' $FILECHANGEREL usernam@example.com:/backup/root/dir && \ echo "At ${time} on ${date}, file $FILECHANGE was backed up via rsync" done
稍后填充!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
#!/usr/bin/env python
#encoding=utf8
import os
import subprocess
import time
import sys
from pyinotify import WatchManager, Notifier,ProcessEvent,IN_DELETE, IN_CREATE,IN_MODIFY
class rsync_file_cmd():
def __init__( self ,src_file,dst,dst_file):
self .src_file = src_file
self .dst = dst
self .dst_file = dst_file
self .cmd = 'rsync -arz --timeout=60 -e "ssh -p 22" %s %s:%s' % ( self .src_file, self .dst, self .dst_file)
self .del_cmd = 'ssh -p 22 %s "rm -rf %s"' % ( self .dst, self .dst_file)
class EventHandler(ProcessEvent):
"""Handle"""
def process_IN_CREATE( self , event):
if event.name.startswith( '.' ) or event.name.endswith( '~' ) or event.name = = '4913' :
pass
else :
create_sync = rsync_file_cmd( str (event.pathname), 'root@198.72.107.18' , str (event.pathname))
subprocess.call(create_sync.cmd,shell = True )
def process_IN_DELETE( self , event):
if event.name.startswith( '.' ) or event.name.endswith( '~' ) or event.name = = '4913' :
pass
else :
delete_sync = rsync_file_cmd( str (event.pathname), 'root@198.72.107.18' , str (event.pathname))
subprocess.call(delete_sync.del_cmd,shell = True )
def process_IN_MODIFY( self , event):
if event.name.startswith( '.' ) or event.name.endswith( '~' ) or event.name = = '4913' :
pass
else :
modify_sync = rsync_file_cmd( str (event.pathname), 'root@198.72.107.18' , str (event.pathname))
subprocess.call(modify_sync.cmd,shell = True )
def FSMonitor(path = '/root/wpf' ):
wm = WatchManager()
mask = IN_DELETE | IN_MODIFY | IN_CREATE
notifier = Notifier(wm, EventHandler(),read_freq = 10 )
notifier.coalesce_events()
# 设置受监视的事件,这里只监视文件创建事件,(rec=True, auto_add=True)为递归处理
wm.add_watch(path,mask,rec = True , auto_add = True )
notifier.loop()
if __name__ = = '__main__' :
try :
pid = os.fork()
if pid > 0 :
sys.exit( 0 )
except OSError, e:
print >>sys.stderr, 'fork failed: %d (%s)' % (e.errno, e.strerror)
sys.exit( 1 )
os.setsid()
os.umask( 0 )
FSMonitor()
print 'start!'
本文转自 位鹏飞 51CTO博客,原文链接:http://blog.51cto.com/weipengfei/1195019,如需转载请自行联系原作者
|
Linux文件实时同步--inotify + rsync + pyinotify相关推荐
- Linux下实现文件实时同步(rsync命令+rsync作为服务+xinetd托管rsync)
文章目录 一.rsync概念 1. 什么是rsync 2. rsync的特点 3. rsync和scp的区别 二.rsync用法 1. 基本语法 2. 本机同步 1) -R(保留相对路径) 2)- - ...
- Inotify+Rsync实现linux文件实时同步
公司一套系统的同步使用的donotify,不能实现子目录的实时同步,通过查资料,发现inotify可以实现子目录的实时同步,以下为笔记. 一.介绍 Inotify 是文件系统事件监控机制,作为 dno ...
- linux文件同步方法,inotify + rsync实现linux文件实时同步
一.项目需求:服务器A的数据目录内容变化后,改变要实时更新到多台服务器B C 的相关目录. 二.解决方法: 1.多台服务器做为rsync服务器端,服务器A做为rsync客户端,使用rsync命令同步数 ...
- rsync+sersync实现数据文件实时同步
rsync+sersync实现数据文件实时同步 使用rsync+sersync,实现nfs与backup服务器间实时数据同步 一.数据同步工具介绍 rsync是一款开源的.快速的.多功能的.可实现全量 ...
- 【Linux下Inotify + Rsync文件实时同步】
Linux下Inotify + Rsync文件实时同步 一.使用前言* 随着公司业务的发展,单web服务器不能满足用户的访问,需要增加多台web服务器实现均衡高可用性访问.为了统一各web服务器之间的 ...
- inotify用法简介及结合rsync实现主机间的文件实时同步
一.inotify简介 inotify是Linux内核2.6.13 (June 18, 2005)版本新增的一个子系统(API),它提供了一种监控文件系统(基于inode的)事件的机制,可以监控文件系 ...
- MySQL :rsync+sersync文件实时同步
文章目录 1. rsync是什么 rsync+sersync文件实时同步 一:环境 二:备份服务器操作 三:数据源服务器操作 安装rsync客户端软件 安装sersync工具,实时触发rsync进行同 ...
- Linux与Windows之间实现文件实时同步的方案
记linux与Windows之间实现文件实时同步的方案 近日,数据分析部提了个需求说要让一台linux的机器上的文件同步到一台Windows机器的某个目录下,由于本姑娘从来没有碰到过这种问题,于是上网 ...
- linux间文件实时同步(syncthing) ---带历史版本“后悔药”
一.概念简介 syncthing 一款开源免费的数据同步工具,基于P2P的跨平台文件同步工具,通过tcp建立设备连接,再通过TLS进行数据安全传输,支持公网与局域网搭建.支持单双向同步与历史版本控制- ...
最新文章
- QT的QDial类的使用
- oracle 数据库中拆分,oracle数据库字符串拆分
- idea报错 电脑死机蓝屏
- 老板不相信我的方案结论!怎么让数据分析驱动决策?
- ENVI下Landsat8辐射定标与大气校正
- php hprose扩展1.5.5,基于Hprose for PHP 1.5.6、Yii2.0.7的RPC的基本实现流程
- mount: unknown filesystem type
- 2007年“网络十大炒女”排行榜
- Windows下pig-0.17启动时遇到的问题及解决
- 预产期计算器在线计算生男生女计算机,预产期计算器生男生女在线查询-预产期计算器及天数计算时间软件2017版-腾牛安卓网...
- 2021年西式面点师(中级)新版试题及西式面点师(中级)模拟试题
- 求职-平安产险科技一二面
- 我要大学答案-大学生都在用的小程序 课后习题答案|实验报告|考研资料|期末真题~
- JAVA实现zip压缩/解压缩
- UV-LED紫外消毒灯可以有效灭杀气溶胶中附着的新冠病毒
- windows bitlocker 控制台加密、解密脚本
- 三大受欢迎的免费开源CMS建站系统
- webservice小解
- 电脑桌面两个计算机图标怎么删除,电脑声音图标怎么有两个,怎么除掉一个
- 【课后习题】高等数学第七版上第三章 微分中值定理与导数的应用 第二节 洛必达法则
热门文章
- Linux下安装Foxit Reader
- 转移指令的原理03 - 零基础入门学习汇编语言45
- linux查看网卡物理编号_Linux下多网卡时,如何快速辨别网卡ID与物理网卡的对应关系(即ethtool命令)...
- ORACAL去除表中null转化为0
- coc跑团san数值规则_为什么B站上有些coc跑团7版规则san值四五十,但是掉的时候只是掉1D3,是不是应该是20上限?...
- sql怎么实现取当前数据以及累计7天数据_年薪60万+大佬吐血整理字节跳动大数据面试真题...
- Swift3.0语言教程查找字符集和子字符串
- python基础教程博客_python基础教程(一)
- python 生成器_Python生成器的用法
- 自定义的代码块怎么移到别的电脑上