Configure Rsync + inotify for real-time bidirectional synchronization of File server data

Source: Internet
Author: User
Tags inotify compressed file types rsync

Rsync Overview

Rsync is an open source fast backup tool that can mirror and synchronize the entire directory tree across different hosts, support incremental backups, maintain links and permissions, and use optimized synchronization algorithms to compress money in transit, making it ideal for applications such as offsite backups, mirror servers, and more.
 
The official website of Rsync is http://rsync.samba.org/, which is maintained by Wayne Davison. As one of the most common file backup tools, Rsync is often one of the basic components of the default installation of Linux and UNIX systems.
 

    • Advantages of Rsync

      Rsync, compared with the traditional CP and tar backup methods, has the advantages of high security, fast backup, support of incremental backup, and so on, rsync can solve the requirement of low-real-time data backup, such as regular backup file server data to remote server, regular data mirroring for local disk, etc.

    • The disadvantage of Rsync

      With the expansion of application system scale, the security and reliability of data also put forward better requirements, rsync in the high-end business system also gradually exposed a lot of shortcomings, first, rsync synchronization data, need to scan all files after the comparison, for differential transmission. If the number of files reaches millions or even tens of thousands of levels, scanning all the files will be very time consuming. And what is changing is often a small part of it, which is a very inefficient way. Second, rsync cannot monitor and synchronize data in real time, although it can trigger synchronization through the way of the Linux daemon, but there must be a time difference between the two triggering actions, which results in inconsistent service and client data and the inability to fully recover data in the event of an application failure. Based on the above reasons, the rsync+inotify combination appeared!

INotify Overview

INotify is a powerful, fine-grained, asynchronous file system event monitoring mechanism, the Linux kernel from 2.6.13, joined the INotify support, through INotify can monitor the file system to add, delete, modify, move and other subtle events, using this kernel interface, Third-party software can monitor the file system under the various changes, and inotify-tools is such a third-party software.
 
 
Rsync can achieve triggered file synchronization, but through the crontab daemon mode trigger, synchronous data and actual data will be different, and inotify can monitor the file system changes, when the file has any changes, trigger rsync synchronization, This just solves the real-time problem of synchronizing data.

Deployment environment
Host Operating System IP Address the directory used for synchronization Major Packages
Server1 CentOS 7.4 x86_64 192.168.125.117 /test rsync-3.0.9-18.el7.x86_64, inotify-tools-v3.14-8.le7.x86_64
Server2 CentOS 7.4 x86_64 192.168.125.118 /test rsync-3.0.9-18.el7.x86_64, inotify-tools-v3.14-8.le7.x86_64
Start deployment

Here we deploy the service to push the host Server1 data to the host Server2, and the bidirectional synchronization on this basis deploys the service on the host Server2 to push the data to the host Server1. At this point, we can synchronize the two servers as long as any one of the changes data will be synchronized to another one.

Data synchronized from Server1 to Server2
  • Two machines configured rsync Service (config file to be modified to the other host)
  • Installing the Rsync Package

    tar zxvf rsync-3.1.3.tar.gz -C /optcd /opt/rsync_3.1.3./configuremake && make install
  • Modifying the rsync configuration file

      # vim/etc/rsyncd.conf ...                  Omit part of the information ... uid = Nobodygid = Nobodyuse Chroot = yes//imprisoned in Source directory address = 192.168.125.117                    Listening address port 873//Listening ports log file =/var/log/rsyncd.log Log file path pid file =/var/run/rsyncd.pid//process ID file path hosts allow = 192.168.125.0/24//                           Client address allowed access [wwwroot]//Shared module name path =/web1/wwwroot   The actual address of the source directory comment = www.wzn.cn Read only = no//is read-only dont compress = *.gz *.tgz *.zip *.z *. Z *.rpm *.deb *.bz2//sync no longer compressed file types Auth users = web1user//Authorized Account Secrets File =/etc/rsyncd_u SERS.DB//Data file that holds account information  
      • Create a data file, source directory for the backup account
        # vim /etc/rsyncd_users.dbbackuper:abc123 # chmod 600 /etc/rsyncd_users.db# mkdir -p /web1/wwwroot# chmod 777 /web1/wwwroot# chown -R nobody:nobody /web1/wwwroot
  • Start rsync and join the system self-boot file

    # rsync --daemon    #运行参数为--daemon# ps -ef | grep rsync# echo "/usr/local/bin/rsync --daemon" >> /etc/rc.local
  • Basic usage of the rsync command

    • -a archive mode, which means that files are transferred recursively, and all file attributes are maintained, equal to-rlptgod.
    • -Z compresses the backed-up files as they are transmitted.
    • -H preserves hard-link knots.
    • -D Keep device file information.
    • -A preserves ACL attribute information
    • --delete Delete files that have the target location but not the original location.
    • --checksum turn on the check switch to force a checksum on the file transfer.
  • Install Inotify-tools (requires GCC, gcc-c++ package support)

    # tar zxvf inotify-tools-3.14.tar.gz -C /opt# cd /opt/inotify-tools-3.14# ./configure# make && make install
  • Basic usage of the inotify command

    • -m means continuous monitoring
    • -R means recursive entire directory
    • -Q simplifies output information
    • inotifywait can be monitored: Modify modify, create creation, move move, delete delete
  • Writing shell scripts to configure content publishing nodes

    # vim /web/inotifyrsync.sh #!/bin/bashhost1=192.168.125.117src=/web/wwwroot/dst1=web1user1=web1user/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 -vzrtopg --delete --progress --password-file= /etc/rsyncd_users.db $src [email protected]$host1::$dst1 > /dev/null 2>&1    echo "${files} was rsynced." >> /tmp/rsync.log 2>&1done
  • Specify executable permissions for the script, put it in the background, and add the system self-boot file
chmod 755 /web/inotifyrsync.sh/web/inotifyrsync.sh &echo "/web/inotifyrsync.sh &" >> /etc/rc.local
    • Adjusting INotify Kernel parameters

      # vim /etc/sysctl.conffs.inotify.max_queued_events = 16384    监控事件队列(16384)fs.inotify.max_user_instances = 1024    最多监控实例数(1024)fs.inotify.max_user_watches = 1048576   每个实例最多监控文件数(1048576)
    • At this time we Server1 on the server to change the data will be synchronized on the Server2, at this time we use the same method to make Server2 data timely synchronization to the Server1 server, you can realize the two-machine synchronization.

Configure Rsync + inotify for real-time bidirectional synchronization of File server data

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.