Introduction: This is a detailed page of rsync + inotify. It introduces PHP, related knowledge, skills, experience, and some PHP source code.
Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 335097 'rolling = 'no'>
Http://code.google.com/p/sersync/
Rsync is easy to configure. You can use scripts to execute client commands to achieve automatic synchronization, but it cannot achieve real-time synchronization. Next we will introduce two real-time synchronization functions.
Lsync is an open-source software tool developed by Google's open-source organization. It can monitor rsync in real time and synchronize data in a timely manner by making any changes to the data. The difference is that the rsync client synchronizes the data on the server, while the lsync synchronizes the data from the local directory to the directory on the rsync server.
Inotify is a file system event monitoring mechanism, which is an effective alternative to 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.
Summary: In general, rsync, rsync + lsync are easy to configure and deploy, but rsync cannot implement synchronization, rsync + lsync is a problem in excluding non-synchronous files (no solution is found). To start rsync + inotify, you need to write scripts to listen to and synchronize data.
============================= Rsync
MASTER: 192.168.93.129
From: 192.168.93.small
Http://rsync.samba.org/ftp/rsync/
--------- Master:
Wget http://rsync.samba.org/ftp/rsync/rsync-3.0.8.tar.gz
./Configure -- prefix =/usr/local/rsync
Create the configuration file rsycnd. conf in the/etc/directory.
PID file =/var/run/rsyncd. PID
Port = 873
Uid = root
Gid = root
Use chroot = Yes
Read Only = Yes
Hosts allow = 192.168.93.small/255.255.255.0
Hosts deny = *
Max connections = 5
Motd file =/etc/rsyncd. motd
Log File =/var/log/rsyncd. Log
Log format = % T % A % m % F % B
Syslog facility = local3
Timeout = 300
[TMP]
Path =/tmp
List = Yes
Ignore errors
Auth users = rsyncuser
Secrets file =/etc/rsyncd. Secrets
Comment = pics WWW
Create a file in the/etc/directory, rsyncd. motd: record the text message sent to the customer when the client connects to the server. Eg: welcome to use the rsync server
Create File rsyncd. Secrets in the/etc/directory: record the User Password required for client connection. Format: User name: password.
Eg: rsyncuser: maos1213
Chmod 600/etc/rsyncd. Secrets
Start rsync
/Usr/local/rsync/bin/rsync -- daemon -- Config =/etc/rsyncd. conf
Rsync startup
Rsycn has many startup methods. Here we will introduce the following:
●. Daemon mode:
/Usr/local/rsync/bin/rsync -- daemon
●. Inetd method:
Add the following to/etc/services:
Rsync 873/tcp
Then add the following to/etc/inetd. conf:
Rsync stream tcp Nowait root/usr/local/bin/rsync rsyncd -- daemon
●. Add RC. Local (FreeBSD)
Edit/etc/rc. d/rc. Local and add:
/Usr/local/bin/rsync -- daemon
Verify that startup is successful
PS-Aux | grep rsync
Root 59120 0.0 0.2 1460 972 ?? SS pm. 00/usr/local/rsync/bin/rsync �� daemon
Netstat-an | grep 873
Tcp4 0 0 *. 873 *. * listen
---------------- From:
Wget http://rsync.samba.org/ftp/rsync/rsync-3.0.8.tar.gz
./Configure -- prefix =/usr/local/rsync
# Vim/etc/rsync. Secrets
Maos1213
/Usr/local/rsync/bin/rsync: // syncuser@192.168.93.129/------- OK
/Usr/local/rsync/bin/rsync-vzrtopg -- delete -- Progress rsyncuser@192.168.93.129: TMP/tmp2 ------- OK
Enter Password: maos1213
--------------------- Not required
CD/usr/local/rsync/bin
Download files
./Rsync-vzrtopg -- Progress -- delete rsyncuser@67.217.170.80: TMP/tmp2
Upload files
./Rsync-vzrtopg -- Progress -- delete/tmp/rsync livedoorcn@10.5.3.98: Test
---------------
======================================= Inotify
Http://inotify-tools.sourceforge.net/
Wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
Principle: inotify is used to monitor file changes. If the file changes, Rsync is started. You can also use this software to monitor some fixed directories, if the monitored directory is changed, execute the relevant specified program, such as sending mail and alerting.
Disadvantage: This method is only applicable to the rsync push method, rather than the rsync acquisition method.
Install On the master server
Note the Linux kernel version when installing inotify-tools. Only version 2.6.13 or later can be used.
./Configure
Make & make install
Check whether inotify is supported. The kernel has been formally integrated into kernel since Kernel 2.6.13, and rhel5 has supported it.
Check whether the/proc/sys/fs/inotify/directory exists to determine whether the kernel supports inotify.
[Root @ rhel5 rsync] # ll/proc/sys/fs/inotify
Total 0
-RW-r -- 1 Root 0 Oct 9 09: 36 max_queued_events
-RW-r -- 1 Root 0 Oct 9 09: 36 max_user_instances
-RW-r -- 1 Root 0 Oct 9 09: 36 max_user_watches
Installation Complete
Http://www.bitscn.com/plus/view.php? Aid = 158600 Linux inotify + rsync + SSH Data Synchronization from time to time
Generate an SSH key so that server1 SSH can access server2 without a password ~
Ssh-keygen-T RSA
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): 123123 // 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.93.133 :~ /. 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 $ chown root: Root authorized_keys // Change User Root
Root @ server2 :~ // SSH $ chmod 600 authorized_keys // change the permission
Test SCP test root@192.168.93.129:/tmp OK
??????? Now you can log on with a key in server1
[Root @ localhost. Ssh] # SSH root@192.168.93.133
The authenticity of host' 192. 168.93.129 (192.168.93.129) 'can't be established.
RSA key fingerprint is 9d: 9f: 5f: A2: 43: 61: 08: AF: A4: A8: B1: B4: da: D2: 54: 19.
Are you sure you want to continue connecting (Yes/No )? Yes
Warning: Permanently added '192. 168.93.129 '(RSA) to the list of known hosts.
Root@192.168.93.129's password:
OpenSSH tells you that it does not know this host, but you do not have to worry about this problem, because it is the first time you log on to this host. Type "yes ". This will add the "recognition mark" of this host to "~ /. Ssh/know_hosts "file. This prompt is no longer displayed when you access this host for the second time.
Correct:
SSH root@192.168.93.129
The authenticity of host' 192. 168.93.129 (192.168.93.129) 'can't be established.
RSA key fingerprint is 9d: 9f: 5f: A2: 43: 61: 08: AF: A4: A8: B1: B4: da: D2: 54: 19.
Are you sure you want to continue connecting (Yes/No )? Yes
Warning: Permanently added '192. 168.93.129 '(RSA) to the list of known hosts.
Enter passphrase for key'/root/. Ssh/id_rsa ':
Enter passphrase for key'/root/. Ssh/id_rsa ':
Last login: Fri Jun 10 18:51:12 2011 from 192.168.93.small
4. Compile a shell script in server1
Vim/usr/local/rsync/inosync. Sh
#! /Bin/sh
Src =/tmp/
DST = root@192.168.93.133:/tmp
Inwt =/usr/local/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 ~
SSH 192.168.93.129
Enter passphrase for key'/root/. Ssh/id_rsa ': ### enter the private key password instead of the server password ###
More articles on "rsync + inotify"
Love J2EE follow Java Michael Jackson video station JSON online tools
Http://biancheng.dnbcw.info/php/335097.html pageno: 10.