Linuxinotify + rsync + ssh Data Synchronization

Source: Internet
Author: User
Article Title: linuxinotify + rsync + ssh data synchronization. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Reference:

Inotify is a file system event monitoring mechanism and is planned to be included in the forthcoming Linux kernel as an effective replacement for dnotify. Dnotify is a file monitoring mechanism supported by earlier kernels. Inotify is a powerful, fine-grained, asynchronous mechanism that meets various file monitoring needs, not limited to security and performance. Next let's learn how to install inotify and how to build an example user space application to respond to file system events.

File System event monitoring is necessary for various programs from File Manager to security tools, but dnotify (standard in earlier kernels) has some limitations, this makes us look forward to a more comprehensive mechanism. With this expectation, we found inotify, a more modern alternative to file system event monitoring.

Why inotify?

There are many reasons for replacing dnotify with inotify. The first reason is that dnotify requires you to open a file descriptor for each directory to be monitored for change. When multiple directories are monitored at the same time, this consumes a lot of resources because it may limit the file descriptor of each process.

In addition, the file descriptor locks the Directory and does not allow unmount. This causes problems in the environment where removable media exists. When inotify is used, if you are monitoring files on the uninstalled file system, the monitoring will be automatically removed and you will receive an unload event.

The second reason why dnotify is not as complex as inotify is that dnotify is a bit complicated. Note that the monitoring granularity of a simple File System Using the dnotify infrastructure is only at the directory level. To use dnotify for more fine-grained monitoring, application programmers must keep a stat structure cache for each monitored directory. The stat structure cache of the user space needs to be used to determine what changes have occurred to the directory when the notification signal is received. When a notification signal is obtained, the stat structure list is generated and compared with the latest status. Obviously, this technology is not ideal.

Another advantage of inotify is that it uses file descriptors as the basic interface so that application developers can use select and poll to monitor devices. This allows effective multi-channel I/O and integration with the mainloop of Glib. On the contrary, the signals used by dnotify often make programmers feel a headache and not very elegant.

Inotify solves these problems by providing a more elegant API that uses the least file descriptor and ensures more fine-grained monitoring. The communication with inotify is provided by the device node. For the above reasons, inotify is your best choice for monitoring files on Linux 2.6.

Linux/l-inotify.html # N10081 "> http://www-128.ibm.com/developerworks/cn/linux/l-inotify.html#N10081

Body:
Sotfware: inotify-tools-3.13.tar.gz
OS: ubuntu server 8.10
Server1: 192.168.6.2
Server2: 192.168.6.3

Server1 directory/home/ludy/rsync
Installation steps:
1. First confirm that your kernel supports inotify. If you do not support Kernel Patching, the kernel is generally supported at 2.6.3 or later. You can confirm to run

Ludy @ server1 :~ $ Ls-l/proc/sys/fs/inotify/
Total usage 0
-Rw-r -- 1 ludy 0 max_queued_events
-Rw-r -- 1 ludy 0 max_user_instances
-Rw-r -- 1 root 0 max_user_watches
If not, install inotify ~

2. Install software inotify-tools-3.13.tar.gz
Ludy @ server1 :~ $ Tar zxvf inotify-tools-3.13.tar.gz

Ludy @ server1 :~ $ Inotify-tools-3.13 cd

Ludy @ server1 :~ $./Configure -- prefix =/usr/local/inotify
Ludy @ server1 :~ $ Make
Ludy @ server1 :~ $ Make install

2. generate an ssh key so that server1 ssh can access server2 without a password ~
Ludy @ server1 :~ $ Ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ludy/. ssh/id_rsa): // press Enter
Enter passphrase (empty for no passphrase): // Enter the password and press Enter
Enter same passphrase again: // Enter it again and press Enter.
Your identification has been saved in/home/ludy/. ssh/id_rsa.
Your public key has been saved in/home/ludy/. ssh/id_rsa.pub.
The key fingerprint is:
F1: 35: 4e: 88: 11: f1: c4: 5a: 7a: c9: 2a: 90: d3: 5e: 0a: 6f ludy @ ludy
The key's randomart image is:
+ -- [RSA 2048] ---- +
| ++. |
| = O. |
| O = o. + |
| =. + O ++. |
| * OSo... |
| E. |
|... |
|
|
+ ----------------- +
3. Then copy the. ssh/id_rsa.pub in your user directory to the root of server2 (I used root for the test. It is better not to use root. It is dangerous ~)

Ludy @ server1 :~ /. Ssh $ scp id_rsa.pub root@192.168.6.3 :~ /. Ssh
After copying the file, go to server2 id_rsa.pub and change the name to authorized_keys.
Root @ server2 :~ /. Ssh $ mv id_rsa.pub authorized_keys
Root @ server2 :~ /. Ssh $ shown root: root authorized_keys // Change User root
Root @ server2 :~ // Ssh $ chmod 600 authorized_keys // change the permission

4. Compile a shell script in server1
Cat inosync. sh

#! /Bin/sh
SRC =/home/ludy/rsync/
DST = root@192.168.6.3:/home/ludy
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 $ SRC $ DST
Done
Let me explain
$ INWT-mrq-e create, move, delete, modify $ SRC | while read d e f; do
Rsync-aHqzt -- delete $ SRC $ DST
-M: Keep listening
-R is used to recursively view directories.
-Q is used to print the event ~
-E create, move, delete, modify
Listen to create, move, delete, and write events

Rsync-aHqzt $ SRC $ DST

-A archive Mode
-H Save the hard connection
-Q: Stop non-error messages
-Z: the compressed file data is being transmitted.
-T maintenance modification time
-Delete: delete unnecessary files.
5. Test

Ludy @ server1 :~ $./Inosync. sh &

Ludy @ server1 :~ $ Cd rsync

Ludy @ server1 :~ /Rsync $ touch asdfa

View on Server 2

Root @ server2:/home/ludy $ ls
Asdfa

Note: you may need to enter the password for the first SSH connection. You do not need to enter the password for the first time ~

This article is from the "linuxer" blog, please be sure to keep this source http://deidara.blog.51cto.com/400447/120008

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.