Rsync + inotify for real-time unidirectional Synchronization

Source: Internet
Author: User

Source server 10.13.114.16
Target Server 10.13.114.17
Objective: To synchronize the source server 10.13.114.16/home/admin/www/folder files to the/home/admin/www directory of the target server 10.13.114.17 in real time

Step 1: Install rsync on the source server
Wget http://docs.linuxtone.org/soft/lemp/CentOS-Base.repo-O/etc/yum. repos. d/CentOS-Base.repo
Yum-y install vim-enhanced

Wget http://www.samba.org/ftp/rsync/rsync-3.0.8.tar.gz
Tar-zxvf rsync-3.0.8.tar.gz
Cd rsync-3.0.8
./Configure -- prefix =/usr/local/rsync -- disable-ipv6
Make & make install

Cd/usr/local/rsync/
Mkdir etc
Mkdir var
Vim/usr/local/rsync/etc/rsyncd. conf
Log file =/usr/local/rsync/var/rsyncd. Log
Pid file =/usr/local/rsync/var/rsyncd. Pid
Lock file =/usr/local/rsync/var/rsyncd. Lock
# Secrets file =/usr/local/rsync/etc/rsyncd. Pas
Motd file =/usr/local/rsync/etc/rsyncd. Motd
Hosts allow = 10.13.114.17/32
List = yes
Uid = root
Gid = root
Use chroot = no
Max connections = 30
[Www]
Path =/home/admin/www
Comment = bakup www
Read only = no
Auth users = admin
Vim/usr/local/rsync/etc/rsyncd. motd
 
Welcome
This is a welcome word. You can leave it unspecified.

Create an admin user on both the server and client for synchronization
Useradd admin

Generate a public key at the source end and copy it to the target segment for password-free Login
Ssh-keygen-t rsa
Copy. ssh/id_rsa.pub to the. ssh/authorized_keys file on the server.
Then set the permission
Execute in target segment
# Chmod 600. ssh/authorized_keys

 

Start the source rsync Service

Rsync -- daemon -- config =/usr/local/rsync/etc/rsyncd. conf

View
Ps-ef | grep rsync
Port 873

Step 2: Install inotify on the source server

Wget http://sourceforge.net/projects/inotify-tools/files/inotify-tools/3.13/inotify-tools-3.13.tar.gz/download
Tar-zxvf inotify-tools-3.13.tar.gz
Cd inotify-tools-3.13
./Configure -- prefix =/usr/local/inotify
Make & make install
Cd/usr/local/inotify/bin
#! /Bin/sh
SRC =/home/admin/www
DST = admin@10.13.114.17:/home/admin
 
/Usr/local/inotify/bin/inotifywait-mrq-e modify, delete, create, attrib $ {SRC} | while read status
Do
Su-admin-c "/usr/bin/rsync-ahqzt -- delete $ SRC $ DST"
Echo "'date + '% Y-% m-% d % H: % M: % s' $ {status} has rsynced"> rsync. log
Done

Start monitor. sh
# Chmod + x monitor. sh
#./Monitor. sh &
You can put it in rc. local to enable startup.
Cat "/usr/local/inotify/bin/monitor. sh &">/etc/rc. local
The files created and deleted at/home/admin/www on the source end will be synchronized to the target end.
Note:
/Usr/local/bin/inotifywait-mrq-e modify, delete, create, attrib $ {src}
-M: Keep listening
-R is used to recursively view directories.
-Q indicates the output event.
-E create, move, delete, modify, and attrib refer to the "listening for creating a mobile delete write permission" event.
/Usr/bin/rsync-ahqzt -- delete $ 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.
Complete rsync parameter description:
-V, -- verbose detailed mode output
-Q, -- quiet simplified output mode
-C, -- checksum: enable the verification switch to force file transfer verification
-A, -- archive mode, indicating that the file is transmitted recursively and all file attributes are kept, equal to-rlptgoD.
-R, -- recursive processes subdirectories in recursive Mode
-R, -- relative uses relative path information
-B, -- backup creates a backup, that is, if the object already has the same file name, rename the old file ~ Filename. You can use the -- suffix option to specify different backup file prefixes.
-- Backup-dir: backs up files (for example ~ Filename) is stored in the directory.
-Suffix = SUFFIX defines the backup file prefix.
-U, -- update only performs updates, that is, skipping all files that already exist in DST and whose file time is later than the time to be backed up. (Do not overwrite the updated file)
-L, -- links retains soft links
-L, -- copy-links: process soft links like regular files
-- Copy-unsafe-links: only copies links other than the SRC path directory tree.
-- Safe-links ignores links other than the SRC path directory tree
-H, -- hard-links
-P, -- perms to keep File Permissions
-O, -- owner keeps file owner information
-G, -- group: Keep file group information
-D, -- devices: Keep Device File Information
-T, -- times preserve the file time information
-S, -- sparse performs special processing on sparse files to save DST Space
-N, -- dry-run which files will be transmitted
-W, -- whole-file: Copy files without incremental Detection
-X, -- one-file-system do not span the boundaries of the file system
-B, -- block-size = SIZE indicates the block size used by the algorithm. The default value is 700 bytes.
-E, -- rsh = COMMAND specifies that rsh and ssh are used for data synchronization.
-- Rsync-path = PATH specifies the path of the rsync command on the remote server
-C, -- cvs-exclude automatically ignores files in the same way as CVS to exclude files that do not want to be transmitted
-- Existing only updates the files that already exist in DST, instead of backing up the new files.
-- Delete: delete the files that are not in the SRC file in DST.
-- Delete-excluded: delete Files specified by this option at the receiving end.
-- Delete-after: delete after transmission
-- Ignore-errors is deleted when an IO error occurs in a timely manner.
-- Max-delete = NUM: a maximum of NUM files can be deleted.
-- Partial retains the files that are not completely transferred for any reason, so as to speed up subsequent re-transmission.
-- Force directory deletion, even if not empty
-- Numeric-ids does not match the number user and group ID with the user name and group name.
-- Timeout = time ip timeout, in seconds
-I, -- ignore-times do not skip files with the same time and length
-- Size-only: when determining whether to back up a file, only check the file size, regardless of the file time
-- Modify-window = NUM determines whether the timestamp window of the file is used at the same time. The default value is 0.
-T -- temp-dir = DIR create a temporary file in DIR
-- Compare-dest = DIR: compare the files in DIR to determine whether to back up data.
-P is equivalent to -- partial
-- Progress displays the backup process
-Z, -- compress compresses backup files during transmission
-- Exclude = PATTERN specifies to exclude file modes that do not need to be transmitted
-- Include = PATTERN specifies the file mode to be transmitted without exclusion
-- Exclude-from = FILE: exclude files in the specified mode in the FILE.
-- Include-from = FILE: files with the specified FILE pattern matching are not excluded.
-- Version: prints version information.
-- Address: bind to a specific address
-- Config = FILE: specify other configuration files. The default rsyncd. conf FILE is not used.
-- Port = PORT specify other rsync service ports
-- Blocking-io: block IO for remote shell
-Stats indicates the transmission status of some files.
-- SS actual transmission process during transmission
-- Log-format = formAT specifies the log file format
-- Password-file = FILE get password from FILE
-- Bwlimit = KBPS limits I/O bandwidth, KBytes per second
-H, -- help: displays help information
When synchronizing a directory, add the -- exculde = PATTERN parameter to 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.

Author: ERDP Technical Architecture"

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.