Use rsync + inotify-tools + ssh to implement triggered remote real-time synchronization

Source: Internet
Author: User
Tags inotify man rsync
Using rsync + inotify-tools + ssh to implement triggered remote real-time file synchronization images are required in many places. Therefore, rsync is a free software that has been widely used, the cwRsyncServer that supports rsync is available on Windows. However, we generally use... using rsync + inotify-tools + ssh to implement triggered remote real-time file synchronization images are required in many places. Therefore, rsync is a free software that has been widely used, including on Windows, there is already a "cwRsyncServer" that supports rsync ". However, we usually implement file synchronization by combining crontab to schedule tasks. the disadvantage of this is that it is inefficient and cannot be synchronized in real time. Now, on the Linux platform, we can use the inotify monitoring file system mechanism in the 2.6 kernel to implement real-time synchronization through inotify-tools. The procedure is as follows: 1. currently, all major Linux releases have software packages rsync and inotify-tools. we recommend that you install the software by using RPM, yum, apt-get, and other methods. RHEL: [root @ server1 ~] # Rpm-ivh rsync-* [root @ server1 ~] # Rpm-ivh inotify-tools-* CentOS: [root @ server1 ~] # Yum install rsync inotify-tools Ubuntu: [root @ server1 ~] # The steps for installing apt-get install rsync inotify-tools using the source code are as follows: [root @ server1 ~] # Wget ftp://ftp.samba.org/pub/rsync/rsync-3.0.8.tar.gz [Root @ server1 ~] # Tar xzvf rsync-3.0.8.tar.gz [root @ server1 ~] # Cd rsync-3.0.8 [root @ server1 ~] #./Configure [root @ server1 ~] # Make [root @ server1 ~] # Make install [root @ server1 ~] # Wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz [Root @ server1 ~] # Tar xzvf inotify-tools-3.14.tar.gz [root @ server1 ~] # Cd inotify-tools-3.14 [root @ server1 ~] #./Configure [root @ server1 ~] # Make [root @ server1 ~] # Make install 2. it is recommended that you use common users to configure the ssh key trust. The reason is that the root operation is inherently dangerous, and the root login without a password is even more dangerous. Create an rsync user on two servers [root @ server1 ~] # Useradd-m rsync [root @ server1 ~] # Passwd rsync [root @ server2 ~] # Useradd-m rsync [root @ server2 ~] # Passwd rsync [root @ server1 ~] # Su-rsync [rsync @ server1 ~] $ Ssh-keygen-t rsa use the default value when prompted to save the location of the private key (key) and public key (public key); when prompted whether the private key password (passphrase) is required, press enter directly, that is, do not use the private key password. Then, a pair of keys, id_rsa (private key file) and id_rsa.pub (public key file) will be generated and stored in the/home/rsync/. ssh/directory. Add the public key to the authorized_keys file of the remote host and upload the file to the remote host (assuming that the IP address of the remote host is 192.168.10.4) [rsync @ server1 ~] $ Scp ~ /. Ssh/id_rsa.pub rsync@192.168.10.4:/home/rsync/use rsync user SSH to log on to the remote host and add the public key to the authorized_keys file [rsync @ server2 ~] $ Mkdir. ssh [rsync @ server2 ~] $ Chmod 700. ssh [rsync @ server2 ~] $ Mv ~ /Id_rsa.pub ~ /. Ssh/authorized_keys restart the SSH service [root @ server1 ~] #/Etc/init. d/sshd restart [root @ server2 ~] #/Etc/init. d/sshd restart 3. create inotify_rsync.sh script [root @ server1 ~] # Vim inotify_rsync.sh #! /Bin/sh SRC =/home/rsync/test DST = rsync@192.168.10.4: /home/rsync/test/bin/su-rsync/usr/local/bin/inotifywait-mrq-e modify, delete, create, attrib $ {SRC} | while read d e f do/usr/bin/rsync-ahqzt -- delete $ SRC $ DST done related annotations are as follows: /usr/local/bin/inotifywait-mrq-e modify, delete, create, attrib $ {src}-m is always listening-r is recursively viewing Directory-q is printing out event-e create, move, delete, modify, attrib refers to the event "listening for creating a mobile delete write permission"/usr/B In/rsync-ahqzt -- delete $ SRC $ DST-a Archive mode-h save hard connection-q stop non-error message-z compressed file data during transmission-t maintenance modification time-delete when you want to exclude unnecessary files from synchronizing a directory, add the -- exculde = PATTERN parameter for rsync. Note that the path is relative. for details, see man rsync. To exclude the processing of event listening in a directory, add the -- exclude or -- excludei parameter for inotifywait. for details, see man inotifywait. The inotifywait command generates three return values: "date, time, and file", which are passed as parameters to read, therefore, the "while read d e f" statement in the script details the return value. Grant the script executable permission [root @ server1 ~] # Chmod + x inotify_rsync.sh run the script [root @ server1 ~] #/Root/inotify_rsync.sh & set the script to start automatically after startup [root @ server1 ~] # Cat "/root/inotify_rsync.sh &">/etc/rc. local 4. first, create a file or directory under the/home/rsync/test Directory of the server1 server, and then view the file in the/home/rsync/test directory of server2, if you see it, it means it is successful. [Rsync @ server1 ~] $ Cd test [rsync @ server1 test] $ touch a.txt note: During the first SSH connection, you may need to enter a password, and then you do not need to enter the password. [Rsync @ server2 ~] $ Cd test [rsync @ server2 test] $ ls a.txt indicates that the.txt file has been synchronized successfully!

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.