Rsync + inotify synchronize notes in real time, rsyncinotify

Source: Internet
Author: User
Tags inotify

Rsync + inotify synchronize notes in real time, rsyncinotify

Copy files from the remote rsync server to the local machine. This mode is enabled when the SRC path information contains the ":" separator.

Copy files from the local machine to the remote rsync server. This mode is enabled when the DST path information contains the ":" separator.

The configuration file contains the following content:

12345678910111213141516171819202122 uid = rootgid = rootport = 873max connections = 200timeout = 600pid file /var/run/rsyncd.pidlog file /var/log/rsync.loglockfile = /var/run/rsyncd.lockmotd file /etc/rsyncd/rsyncd.mothlog format = %t %a %m %f %bsyslog facility = local3host allow = 10.34.38.130host deny = *[test]path = /home/rsyncignore errors = yeslist = noignore errorsread only = noauth users = roothost allow = 10.34.38.130secrets file /etc/rsyncd/rsyncd.secrets


Start rsync:

Rsync -- daemon starts the rsync service as a daemon.

-- Address: bind a specified IP address to provide services

-- Config = xxx specifies the configuration file path

-- Port = 897 specifies the port, instead of the default port


Rsync parameters:

-A operates in archive mode, copies directories, and symbols to connect-rlptgoD

-R is recursion

-L indicates a link file, which means copying a link file.

-P: retain original File Permissions

-T preserve the original time of the file

-G: original user group for retaining files

-O maintain the original owner of the file

-D is equivalent to a block device file.

-Z compression during transmission

-P transmission progress

-V transmission progress and other messages, and-P relationship

-E create an encrypted connection using ssh Parameters

-U is only updated to prevent new local files from being overwritten. Pay attention to the clock of both machines at the same time.

-- Progress indicates that the detailed progress is displayed.

-- Delete: if the server deletes this file, the client also deletes the file to ensure true consistency.

-- Password-file =/password/path/file specifies the password file, which can be used in the script without the need to enter the verification password interactively, note that the permission attribute of this password file must be set to only the owner-readable.

-- Exclude is used when a file is not included during upload.


Set the password File Permission to 600, that is, chmod 600/etc/xxx/password.

Packaging:

Tar-zcvf html _ $ (date must have f0000.tar.gz./html/

Push:

Rsync-avzp html_2015-01-03.tar.gz root@10.34.38.128: test -- password-file =/etc/rsync. password

Find/xxx-type f-name "*. gz"-mtime + 7 | xargs rm-f

Rsync-avzP -- delete a/B/copy the content under a to B and delete the original file content in B.

Rsync-avzP -- delete B/root@10.34.38.128: test -- password-file =/etc/rsync. password: Synchronize the content in local a to the rsync server, and synchronize the content in a. If there are other content in the server, delete it.


Inotify Tool

Inotify is a powerful fine-grained and asynchronous file system event monitoring mechanism. Linux kernel has been added to Inotify since 2.6.13. Inotify can be used to monitor the addition and deletion of file systems, with this kernel interface, third-party software can monitor various file changes in the file system. inotify-tools is such a third-party software.
In the previous chapter, we have mentioned that rsync can implement triggered file synchronization, but trigger through the crontab daemon process. The synchronized data is different from the actual data, inotify can monitor various changes in the file system. When there are any changes to the file, rsync synchronization is triggered, which just solves the problem of real-time data synchronization.

Inotifywait parameters:

Inotifywait is a monitoring wait event that can be used with shell scripts. The following describes some common parameters:

-M, that is, -- monitor, indicates that the event listening state is always maintained.

-R, that is, -- recursive, indicates the recursive query directory.

-Q, that is, -- quiet, indicates that the monitoring event is printed.

-E, that is, -- event. You can use this parameter to specify the event to be monitored. Common events include modify, delete, create, and attrib.

-- Timefmt: output format of the specified time

-- Format: Specifies the detailed information of the variable file.

These two parameters are generally used together. by specifying the output format, the output is similar:

15/04/10/web/www/xxxx. shDELETE, ISDIR was rsynced
15/04/10/web/www/index.html MODIFY was rsynced

The script is as follows:

1234567891011 #!/bin/bashsrc=/root/rsync/des1=testhost=10.34.38.128user=root/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,delete,create,attrib $src|while read filesdo/usr/bin/rsync -avzP --delete $src $user@$host::$des1 --password-file=/etc/rsync/rsync.passwordecho "${files} was rsynced" >>/var/log/rsync2.log 2>&1done###########################################################

The role of this script is to monitor the changes in the file directory through inotify, and then trigger rsync for synchronization operations. Because this process is an active trigger operation completed through the system kernel, it is much more efficient than scanning methods that traverse the entire directory.

Sometimes this problem occurs: it takes some time to write a large file to the inotify-monitored directory (here/root/rsync, at this time, inotify will continuously output the updated information of the file, which will continuously trigger rsync to execute synchronization operations and occupy a large amount of system resources. In this case, the best practice is to wait until the file is written before triggering rsync synchronization. In this case, you can modify the inotify monitoring event, namely, "-e close_write, delete, create, attrib ".

Add the script/etc/rsync/inotify. sh to the system self-starting file.

Chmod + x inotify. sh

Echo "/etc/rsync/inotify. sh &">/etc/rc. local

Now, you can test whether the synchronization is successful !!

-------------------------------------------------------------

Other brilliant articles

Android KSOAP2 call. net webservicejQuery tutorial (8)-DOM tree operation using reverse Insertion Method android learning notes (34) using AlertDialog to create a simple dialog box android learning notes (33) Gallery view (Gallery) android navidgation drawer in the navigation drawer how to change the List of selected items...

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.