Linux rsync real-time synchronization

Source: Internet
Author: User
Tags inotify rsync

rsync sync

Differences in synchronization and replication:
Copy: Full copy Source to target
Sync: Incremental copy, transfer only changed data

Synchronous operation:
Remote Sync
Supports local replication, or synchronizes with other Ssh,rsync hosts.
Official website: http://rsync.samba.org/

Command usage
rsync [options] Source directory destination Directory
Common options:
-A: Archive mode, equivalent to-rlptgid
-V: Show synchronization process details
-Z: Compression enabled during transfer
-R: Recursive, including directories/subdirectories and all files
-L: Preserve Symbolic link files
-p,-t: Preserve file permissions, time stamp
-o,-g: Preserve the owner/group tag of a file
-D: Keep device files and other special files
-H: Keep hard-link files
-A: Preserve ACL attribute information for a file
-N: Test the synchronization process without actual modification
--delete: Delete Unnecessary documents in the destination folder (make sure the source and destination are exactly the same)

Local synchronization
rsync [Options] Local directory 1 local directory 2
rsync [Options] Local directory 1/local directory 2

[[email protected] ~]# rsunc-a/boot/xxx (Sync entire folder)
[[email protected] ~]# rsunc-a/boot//xxx/(sync directory data only)


Server-side requirements: The server should open the SSHD service, and provide the authorized user, password, this directory must have the appropriate permissions to the remote directory
RSYNC+SSH Remote Synchronization
downlink sync: rsync [options] [email protected] Remote directory/local Directory
Uplink sync: rsync [options] Local directory/[email protected] Remote directory

Downstream synchronization instances:
[[email protected] ~]# rsync [email protected]:/boot/(Browse remote directory)
[[email protected] ~]# rsync-av [email protected]:/boot//xxx/(Backup remote host/boot/directory to local)
[Email protected] ~]# ls/xxx/(View results)
Upstream synchronization instances:
[[email protected] etc]# rsync-av/etc [email protected]:/opt/(Backup local/etc/directory to remote host)
[[email protected] opt]# ls (remote machine view results)
Rh

————————————————————————————————————————————
Rsync+rsync Remote Synchronization

Synchronizing Resource Profiles/etc/rsyncd.conf
[[email protected] ~]# chkconfig rsync on (open)
[[Email protected] ~]# service xinetd Restart (restart services)

Create a rsyncd.conf shared configuration (if anonymous sharing. Auth users settings can be removed)
[Email protected] ~]# vim/etc/rsyncd.conf
[Hydra] (shared file name)
Path =/USR/SRC (shared files directory)
Comment = Rsunc Share Test
Read Only = yes
Dont compress = *.gz *.bz2 *.tgz *.zip
Auth users = ruser (licensed user)
Secrets file =/etc/rsyncd_users.db (the storage path of the user account password)
[[email protected] ~]# vim/etc/rsyncd_users.db (create user, password store file)
ruser:123456 (User: password)
Root:taren1
Hydra:anonymous
[[email protected] ~]# chmod 600/etc/rsyncd_users.db (change storage user, password file permissions)

View shared resources: rsync [email protected]::

downlink sync: rsync [options] [email protected] Share name/local directory
Uplink sync: rsync [options] Local directory/[email protected] Share name

Downstream synchronization instances:
[Email protected] ~]# rsync-av [email protected]::hydra//myhydra
[[email protected] ~]# ls/myhydra/(view file)
Debug kernels
Upstream synchronization instances:
[Email protected] ~]# rsync-av/opt [email Protected]::hydra
[[email protected] ~]# ls/usr/src/(view file)
Debug kernels

——————————————————————————————————————————

About the real-time nature of synchronization
Depends on two components:
Monitor source document changes (inotify)
Command Line (rsync) statement that invokes a synchronization operation
About the inotify mechanism:
Linux kernel 2.6.13 or later default support
Additional installation control tools are required, such as Inotify-tools

INotify Real-time synchronization (inotifywait Monitoring directory program)
Inotify-tools-3.13.tar.gz
Source Package Compilation Installation:
Unpacking: [[email protected] ~]# tar xf/root/inotify-tools-3.13.tar.gz-c/usr/src/
Configuration: [[email protected] ~]# cd/usr/src/
[Email protected] inotify-tools-3.13]#./configure
Compile: [[email protected] inotify-tools-3.13]# make
Installation: [[email protected] inotify-tools-3.13]# make install
[Email protected] ~]# inotifywait
No files specified to watch!

Inotifywait Monitoring
Basic usage
inotifywait [Options] Destination folder
Common Command Options
-m,--monitor: Enable monitoring
-r,--recursive: recursive, covering all subdirectories
-q,--quiet: Reduce output information
-e,--event: Limiting the types of events to monitor

Instance:
Monitoring/opt Folder
Event Type: Modify,move,create,delete,attrib
[[email protected] ~]# inotifywait-mrq-e Modify,move,create,delete,attrib/opt
[[email protected] ~]# [[email protected] ~]# vim/root/ irsync.sh (write a script, background execution)
#!/bin/bash
dir1= "/opt" (DIR1 is a variable, good late change)
rcmd= "Rsync-az--delete $DIR 1/opt2/" (RCMD variable)
inotifywait-mrq-e create,delete,move,attrib,modify $DIR 1 | While the read x y z
do
$RCMD (this variable is equivalent to performing rsync- AZ--delete/opt/opt2/)
done &

Instance:
Site Real-time mirroring
Implement host svr-"host PC's website real-time mirroring
Both sides of the catalogue are/var/www/html/
Configure Inotfy+rsync synchronization with SVR as the initiator of synchronization
PC as the synchronization target, based on SSH authentication (if you do not want to lose the password, you can give the other public key)
Rsync-az--delete/var/www/html/[Email protected]:/var/www/html/
[[email protected] ~]# vim/root/irsync.sh (To modify the script file in real-time sync)
#!/bin/bash
dir1= "/var/www/html/"
rcmd= "Rsync-az--delete $DIR 1 [email protected]: $DIR 1
"
Inotifywait-mrq-e create,delete,move,attrib,modify $DIR 1 | While read x y Z
Do
$RCMD
Done &

————————————————————————————————————————————

Linux rsync real-time synchronization

Related Article

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.