I
want to copy (rsync to remote server)
a directory tree whenever file uploaded or deleted in
/var/www/html/upload/ directory under Linux operating systems for backup
purpose and/or load balancing purpose without getting into complex file
sharing setup such as NFS
or GFS iscsi storage. How do I monitor /var/www/html/upload/ and its
subdirectory for new files and executes rsync command to make copy back
to www2.example.com:/var/www/html/upload/?
inotify is an inode-based filesystem notification technology. It
provides possibility to simply monitor various events on files in
filesystems. It is a very much powerful replacement of (obsolete)
dnotify. inotify brings a comfortable way how to manage files used in
your applications.
The incrond (inotify cron daemon) is a daemon which monitors filesystem events (such as add a new file, delete a file and so on) and executes commands or shell scripts. It’s use is generally similar to cron .
Install incron
Type the following command under RHEL / Fedora / CentOS Linux:$ sudo yum install incron
Type the following command under Debian / Ubuntu Linux:$ sudo apt-get install incron
Configuration Files
- /etc/incron.conf - Main incron configuration file
- /etc/incron.d/ - This directory is examined by incrond for system table files. You should put all your config file here as per directory or domain names.
- /etc/incron.allow - This file contains users allowed to use incron.
- /etc/incron.deny - This file contains users denied to use incron.
- /var/spool/incron - This directory is examined by incrond for user table files which is set by users running the incrontab command.
incron Syntax
The syntax is as follows:
<directory> <file change mask> <command or action> options /var/www/html IN_CREATE /root/scripts/backup.sh /sales IN_DELETE /root/scripts/sync.sh /var/named/chroot/var/master IN_CREATE,IN_ATTRIB,IN_MODIFY /sbin/rndc reload
Where,
- <directory> - It is nothing but path which is an absolute filesystem path such as /home/data. Any changes made to this path will result into command or action.
-
<file change mask>
-
Mask is is nothing but various file system events such as deleting a
file. Each event can result into command execution. Use the following
masks:
- IN_ACCESS - File was accessed (read)
- IN_ATTRIB - Metadata changed (permissions, timestamps, extended attributes, etc.)
- IN_CLOSE_WRITE - File opened for writing was closed
- IN_CLOSE_NOWRITE - File not opened for writing was closed
- IN_CREATE - File/directory created in watched directory
- IN_DELETE - File/directory deleted from watched directory
- IN_DELETE_SELF - Watched file/directory was itself deleted
- IN_MODIFY - File was modified
- IN_MOVE_SELF - Watched file/directory was itself moved
- IN_MOVED_FROM - File moved out of watched directory
- IN_MOVED_TO - File moved into watched directory
- IN_OPEN - File was opened
- The IN_ALL_EVENTS symbol is defined as a bit mask of all of the above events.
- <command or action> - Run command or scripts when mask matched on given directory.
- options - It can be any one of the following with command (i.e. you can pass it as arg to your command):
- $$ - dollar sign
- $@ - watched filesystem path (see above)
- $# - event-related file name
- $% - event flags (textually)
- $& - event flags (numerically)
Turn Service On
Type the following command:# service incrond start
# chkconfig incrond on
Examples:
Type the following command to edit your incrontabincrontab -e
Run logger command when file created or deleted from /tmp directory:/tmp IN_ALL_EVENTS logger "/tmp action for $# file"
Save and close the file. Now cd to /tmp and create a file:$ cd /tmp
$ >foo
$ rm foo
To see message, enter:$ sudo tail -f /var/log/messages
Sample outputs:Jul 17 18:39:25 vivek-desktop logger: "/tmp action for foo file"
How Do I Run Rsync Command To Replicate Files For /var/www/html/upload Directory?
Type the following command:# incrontab -e
Append the following command:/var/www/html/upload/ IN_CLOSE_WRITE,IN_CREATE,IN_DELETE
/usr/bin/rsync --exclude '*.tmp' -a /var/www/html/upload/
user@www2.example.com:/var/www/html/upload/
Now, wherever files are uploaded in /var/www/html/upload/ directory,
rsync will be executed to sync files to www2.example.com server. Make
sure ssh keys are set for password
less login.
How Do I Monitor /var/www/html/upload/ and Its Subdirectories Recursively?
You cannot monitor /var/www/html/upload/ directory recursively.
However, you can use the find command to add all sub-directories as
follows:find /var/www/html/upload -type d -print0 | xargs -0 -I{} echo "{}
IN_CLOSE_WRITE,IN_CREATE,IN_DELETE /usr/bin/rsync --exclude '*.tmp' -a
/var/www/html/upload/ user@www2.example.com:/var/www/html/upload/" >
/etc/incron.d/webroot.conf
This will create /etc/incron.d/webroot.conf config as follows:
/var/www/html/upload IN_CLOSE_WRITE,IN_CREATE,IN_DELETE /usr/bin/rsync --exclude '*.tmp' -a /var/www/html/upload/ user@www2.example.com:/var/www/html/upload/ /var/www/html/upload/css IN_CLOSE_WRITE,IN_CREATE,IN_DELETE /usr/bin/rsync --exclude '*.tmp' -a /var/www/html/upload/ user@www2.example.com:/var/www/html/upload/ /var/www/html/upload/1 IN_CLOSE_WRITE,IN_CREATE,IN_DELETE /usr/bin/rsync --exclude '*.tmp' -a /var/www/html/upload/ user@www2.example.com:/var/www/html/upload/ /var/www/html/upload/js IN_CLOSE_WRITE,IN_CREATE,IN_DELETE /usr/bin/rsync --exclude '*.tmp' -a /var/www/html/upload/ user@www2.example.com:/var/www/html/upload/ /var/www/html/upload/3 IN_CLOSE_WRITE,IN_CREATE,IN_DELETE /usr/bin/rsync --exclude '*.tmp' -a /var/www/html/upload/ user@www2.example.com:/var/www/html/upload/ /var/www/html/upload/2010 IN_CLOSE_WRITE,IN_CREATE,IN_DELETE /usr/bin/rsync --exclude '*.tmp' -a /var/www/html/upload/ user@www2.example.com:/var/www/html/upload/ /var/www/html/upload/2010/11 IN_CLOSE_WRITE,IN_CREATE,IN_DELETE /usr/bin/rsync --exclude '*.tmp' -a /var/www/html/upload/ user@www2.example.com:/var/www/html/upload/ /var/www/html/upload/2010/12 IN_CLOSE_WRITE,IN_CREATE,IN_DELETE /usr/bin/rsync --exclude '*.tmp' -a /var/www/html/upload/ user@www2.example.com:/var/www/html/upload/ /var/www/html/upload/2 IN_CLOSE_WRITE,IN_CREATE,IN_DELETE /usr/bin/rsync --exclude '*.tmp' -a /var/www/html/upload/ user@www2.example.com:/var/www/html/upload/ /var/www/html/upload/files IN_CLOSE_WRITE,IN_CREATE,IN_DELETE /usr/bin/rsync --exclude '*.tmp' -a /var/www/html/upload/ user@www2.example.com:/var/www/html/upload/ /var/www/html/upload/images IN_CLOSE_WRITE,IN_CREATE,IN_DELETE /usr/bin/rsync --exclude '*.tmp' -a /var/www/html/upload/ user@www2.example.com:/var/www/html/upload/
How Do I Troubleshoot Problems?
You need to see /var/log/cron log file:# tail -f /var/log/cron
# grep something /var/log/cron
相关推荐
Linux inotify 机制详解 Linux inotify 是一种文件系统变化通知机制,允许文件或者文件夹的增删等事件即刻反映在用户空间。inotify 机制是 Linux 内核中的一种机制,用于通知用户空间程序文件系统的变化。 一、...
python-inotify linux inotify的python绑定许可证:麻省理工学院演示 #! /usr/bin/env python"""backup your file automatically. """import osimport timeimport shutilimport sysimport errno import os . pathtry...
首先,创建一个通知程序: notifier = INotify::Notifier.new然后,告诉它关注您感兴趣的事件的路径: notifier.watch("path/to/foo.txt", :modify) {puts "foo.txt was modified!"}notifier.watch("path/to/bar", ...
【标题】:“Linux unison+inotify 文件实时双向同步” 在Linux系统中,文件同步是一项重要的任务,特别是在多台机器间协同工作或者备份数据时。本文将深入探讨如何利用unison和inotify这两个工具实现文件的实时...
harmonyos底层是linux Doc 用于记录一些优秀的文档 ...inotify: rtc: linux不支持5G: 802-1.x: 下一代工业通信—TSN(时间敏感网络),工业物联网的助推器:QCA TSN相关协议标准 beacon帧字段结构——VH
SMTP邮箱消息钉钉群机器人飞书群机器人自定义使用方法Docker安装docker run --name=inotify -d -p 8000:80 -v inotify_data:/inotify_data --restart=always xpnas/inotify:latest配置Nginx代理server{ location / {...
Inotify是一种强大的、细粒度的、异步的文件系统事件监控机制, linux内核从 2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种事件,利用这个内核接口,第三方软件就可以...
inotify, 面向 Rust 编程语言的惯用inotify包装器 inotify Idiomatic inotify 用于 Rust 程序语言的包装器。extern crate inotify;use std::env;use inotify::{ event_m
NULL 博文链接:https://sharejava.iteye.com/blog/1776333
1. **配置inotify:** 在CentOS 7.8服务器上,设置inotifywait来监控需要备份的文件或目录,一旦有变化,就触发一个脚本。 2. **编写脚本:** 脚本可以调用rsync命令,将变化的文件或目录同步到远程Windows服务器上...
let mut inotify = Inotify :: init () . expect ( "Failed to initialize inotify" ); let current_dir = env :: current_dir () . expect ( "Failed to determine current directory" ); inotify . add_...
Inotify地址:访问 # -*- coding:utf-8 -*- import os import pyinotify from functions import * WATCH_PATH = '' #监控目录 if not WATCH_PATH: wlog('Error',"The WATCH_PATH setting MUST be set.") sys.exit...
首先,我们需要在客户端机器上启动 inotify: ``` [root@localhost ~]# inotifywait -m /aaa ``` 然后,我们可以使用 Rsync 同步文件: ``` [root@mail run]# rsync -r /aaa /ccc ``` 这样,我们就实现了实时远程...
《Inotify工具与Rsync在Linux文件实时同步中的应用》 在Linux系统中,管理和监控文件系统的变更是一项重要的任务,特别是在需要实时同步文件或者备份数据的场景下。Inotify是Linux内核提供的一种文件系统事件监控...
Linux中的inotify是一个强大的文件系统事件监控工具,它允许用户空间的应用程序实时跟踪和响应文件系统的变化。这个特性被内置于Linux内核中,自2.6.13版本开始引入,极大地增强了开发者和系统管理员对文件系统活动...
inotify-rs Rust编程语言的惯用语inotify包装器。 木箱化 inotify-rs Rust编程语言的惯用语inotify包装器。 木箱化 使用std :: env; 使用inotify :: {EventMask,WatchMask,Inotify,}; fn main(){let mut ...