Rsync
I. Basic Knowledge
1. Rsync: A transmission tool, similar to SCP
To use SCP, you must know the password and account of the remote host.
Upload files locally to remote
SCP [Option] source file [[[email protected] host1:] destions/File
The file is downloaded to the local device remotely.
SCP [Option] [[email protected] host1:] destions/File Source/File
650) This. width = 650; "style =" border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; "Title =" wps_clip_image-25848 "border =" 0 "alt =" wps_clip_image-25848 "src =" http://img1.51cto.com/attachment/201408/24/8400375_1408872747wfFN.png "width =" 706 "Height =" 320 "/>
Rsync ?. Explanation of rsync in a fast, versatile, remote (and local) file-copying tool man Manual
Tool for fast, universal, remote, and local host CP File
# Yum-y install xinetd # chkconfig rsync onrpm-QL rsync/etc/xinetd. d/rsync Service Startup Script/etc/rsyncd. conf service configuration/etc/rsyncd. passwd # service xinetd start service start
Listening on port 873/tcp
Rsync features:
1. images can be used to save the entire directory tree or file system;
2. High data transmission efficiency;
3. Secure data transmission can be achieved through SSH;
4. Anonymous transmission is supported;
650) This. width = 650; "style =" border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; "Title =" wps_clip_image-418 "border =" 0 "alt =" wps_clip_image-418 "src =" http://img1.51cto.com/attachment/201408/24/8400375_1408872752T0FS.png "width =" 725 "Height =" 299 "/>
Rsync algorithm
Rsync command working mode:
The first mode is the shell mode, also known as the local mode. The second mode is the remote shell mode. You can use the SSH protocol to host the remote transmission process. The third mode is the list mode, only list the content in the source.-NV Mode 4: service mode. Rsync works as a daemon and can receive data synchronization requests from the client. Rsync Command Option:-N: synchronization test, do not execute the real synchronization process;-V: -- verbose detailed output mode-Q: -- Quiet silent mode-C: -- checksum, enable the verification function-R: -- Recursive recursive copy note: In the rsync command, if the source path is a directory and there is/at the end of the copy path, the contents in the directory will be copied instead of the directory;
If there is no/at the end of the path, all files in the directory itself and the directory will be synchronized; whether there is/at the end of the target path does not matter;
650) This. width = 650; "style =" border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; "Title =" wps_clip_image-5661 "border =" 0 "alt =" wps_clip_image-5661 "src =" http://img1.51cto.com/attachment/201408/24/8400375_1408872760rRvw.png "width =" 738 "Height =" 188 "/>
The above event transfer directory
The following is all the contents in the transfer directory.
-A: -- Archive, retain the original attributes of the object;-P: -- perms: retain the object permission;-T: -- times: the timestamp of the object;-l: reserved Symbolic Link-G: -- group reserved group-O: (-- owner) reserved owner-D: same as -- devices -- specials keep the device file-e -- RSH = command SSH: Use SSH for transmission;-Z: -- compress for compression and transmission; -- Progress: display progress bar -- stats: show how to perform compression and transmission
650) This. width = 650; "style =" border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; "Title =" wps_clip_image-19697 "border =" 0 "alt =" wps_clip_image-19697 "src =" http://img1.51cto.com/attachment/201408/24/8400375_1408872775CIRI.png "width =" 713 "Height =" 369 "/>
Rsync service mode:
1. Set the rsync server # Yum-y install xinetd # chkconfig rsync on2 and provide the configuration file/etc/rsyncd for rsync. conf 2. The configuration file is divided into two sections: global configuration section: 1 shared configuration section: multiple [partition _name] configuration examples: global?, configuration segment # global settingsuid = Nobody anonymous user gid = Nobody anonymous group use chroot = no allow users to output their home directory Max connections = 10 Max number of requests strict modes = Yes attribute PID file =/var/run/rsyncd. PID file log file =/var/log/rsyncd. log File Sharing configuration section # directory to be synced [tools] Share Name Path =/data real shared directory ignore errors = Yes ignore error read only = no allow read operation write only = no. Do not allow write operation hosts allow = 172.16.0.0/16 allow that segment to transmit hosts deny = * In addition to the above deny all list = false view directory file uid = root Root identity shared gid = root group identity sharing 3. enable service access via rsync daemon: the file is generally downloaded remotely. (pull) download pull: rsync [option...] [[email protected] HOST: SRC... [DEST] Share Method pull rsync [option...] rsync: // [[email protected] host [: Port]/src... [DEST] upload a file locally. Push: rsync [option...] SRC... [[email protected] HOST: DEST shared upload rsync [option...] SRC... rsync: // [[email protected] host [: Port]/DEST on the client, which uses periodic task scheduling; Data inotify can be synchronized: on the server side, it can be used to define all files under the specified directory of monitoring. Once the metadata of a file changes, the client will be notified to pull the file; rsync + inotify: sersync
4. Enable user authentication on the server
Add the following configuration to the sharing definition:
[Tools] Path =/dataignore errors = yesread only = nowrite only = nohosts allow = 172.16.0.0/16 hosts deny = * List = falseuid = rootgid = root auth users = username list authenticated user list; separate users with commas (,). Secrets file =/etc/rsyncd. passwd password storage location, remember that the user password cannot exceed 8 characters
Note: The username list is a comma-separated list of usernames in rsyncd. passwd;
(2) create a password file/etc/rsyncd. passwd
Username: Password File Format
This file cannot be accessible to other users and the password cannot exceed 8 characters;
3. Experiment to complete rsync sharing and Authentication-based functions
Vmware1 client 172.16.1.143
Vmware2 server 172.16.1.140
When configuring the IP address, you 'd better ping each other to ensure normal communication. In addition, you 'd better disable the firewall or configure a firewall policy. Otherwise, the experiment results will be affected.
Server
Yum-y install xinetd
Servcie xinetd restart
Service configuration/etc/xinetd. d/rsync
650) This. width = 650; "style =" border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; "Title =" wps_clip_image-28825 "border =" 0 "alt =" wps_clip_image-28825 "src =" http://img1.51cto.com/attachment/201408/24/8400375_1408872787W0dJ.png "width =" 717 "Height =" 352 "/>
Vim/etc/rsyncd. conf
650) This. width = 650; "style =" border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; "Title =" wps_clip_image-32362 "border =" 0 "alt =" wps_clip_image-32362 "src =" http://img1.51cto.com/attachment/201408/24/8400375_1408873348lPJF.png "width =" 732 "Height =" 451 "/>
Password Authentication-based user password configuration
Note the permissions of the password file.
Chmod 600/etc/rsyncd. passwd
650) This. width = 650; "style =" border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; "Title =" wps_clip_image-14092 "border =" 0 "alt =" wps_clip_image-14092 "src =" http://img1.51cto.com/attachment/201408/24/8400375_1408873351g900.png "width =" 320 "Height =" 117 "/>
Lab results
650) This. width = 650; "style =" border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; "Title =" wps_clip_image-16787 "border =" 0 "alt =" wps_clip_image-16787 "src =" http://img1.51cto.com/attachment/201408/24/8400375_1408873356mcff.png "width =" 639 "Height =" 89 "/>
650) This. width = 650; "style =" border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; "Title =" wps_clip_image-13483 "border =" 0 "alt =" wps_clip_image-13483 "src =" http://img1.51cto.com/attachment/201408/24/8400375_1408873370ZONM.png "width =" 641 "Height =" 309 "/>
Iv. Experiment
Inotify Introduction
Inotify is a powerful fine-grained and asynchronous file system event monitoring mechanism. The Linux kernel has been added to inotify since 2.6.13,
Inotify can monitor the addition, deletion, modification, movement, and other minor events in the file system. Using this kernel interface, third-party software
Inotify-tools is a third-party software.
Inotifywait is a monitoring wait event that can be used with shell scripts. The following describes some common parameters:
Inotifywait, used to monitor file changes
Inotifywatch is used to count the number of accesses to the file system and monitor the metadata of files.
L-M, that is, -- monitor, indicates that the event listening status is always maintained.
L-R, that is, -- Recursive, indicates the recursive query directory.
L-Q, that is, -- quiet, indicates that the monitoring event is printed.
L-e, that is, -- event. You can use this parameter to specify the event to be monitored. Common events include modify, delete, create, and attrib.
Work
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/47/5D/wKioL1P5u5XSk2wkAACqvZ9JZ9Y733.jpg "Title =" 36020140425180821577.jpg" alt = "wkiol1p5u5xsk2wkaacqvz9jz9y733.jpg"/>
Server Configuration
1. Download Software
Http://nchc.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/
Tar xf inotify-tools/3.13-C/usr/local
CD inotify
./Configure -- frefix =/usr/local/inotify
Mak & make install
Yum install xinetd-y
Vim/etc/xinetd. d/rsync
650) This. width = 650; "style =" border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; "Title =" wps_clip_image-21384 "border =" 0 "alt =" wps_clip_image-21384 "src =" http://img1.51cto.com/attachment/201408/24/8400375_1408873381hd3x.png "width =" 555 "Height =" 283 "/>
Vim/etc/rsyncd. conf
650) This. width = 650; "style =" border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; "Title =" wps_clip_image-15859 "border =" 0 "alt =" wps_clip_image-15859 "src =" http://img1.51cto.com/attachment/201408/24/8400375_1408873392B3f0.png "width =" 569 "Height =" 299 "/>
Service xinetd restart
SS-tnl check whether 873 is listening
Create directory mkdir/Momo
2. provides an update control script.
650) This. width = 650; "style =" border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; "Title =" wps_clip_image-23392 "border =" 0 "alt =" wps_clip_image-23392 "src =" http://img1.51cto.com/attachment/201408/24/8400375_1408873403VOh6.png "width =" 698 "Height =" 322 "/>
Script control push
Vim/bin/inotify. Sh
#! /Bin/bash
Trap 'echo "stop" 'int
Source =/Momo
Host = 172.16.1.13
DeST = tools
Host1 = 172.16.1.23
/Usr/local/bin/inotifywait-mrq-e modify, delete, create, attrib/Momo | while read line
Do
/Usr/bin/rsync-ahqz-e SSH -- delete -- Progress-V -- stats $ {source}/$ {Host }::$ dest
/Usr/bin/rsync-ahqz-e SSH -- delete -- Progress-V -- stats $ {source}/$ {host1} ::$ dest
Done
3. Key Authentication
Generate key
650) This. width = 650; "style =" border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; "Title =" wps_clip_image-6143 "border =" 0 "alt =" wps_clip_image-6143 "src =" http://img1.51cto.com/attachment/201408/24/8400375_1408873428n1xg.png "width =" 690 "Height =" 273 "/>
Authenticate the key to the client
650) This. width = 650; "style =" border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; "Title =" wps_clip_image-25248 "border =" 0 "alt =" wps_clip_image-25248 "src =" http://img1.51cto.com/attachment/201408/24/8400375_1408873446lh1h.png "width =" 696 "Height =" 291 "/>
Save it to the/etc/. sshd/authorized_keys file on the client.
Client Configuration
Install the following content in the service configuration in a unified manner, create a shared directory, restart, view the listener, the environment is very important, iptables,
Vim/etc/rsyncd. conf
# Global settings
Uid = nobady
Gid = nobady
Use chroot = No
Max connections = 10
Strict modes = Yes
PID file =/var/run/rsyncd. PID
Log File =/var/log/rsyncd. Log
# Directory to be synced
[Tools]
Path =/Momo
Ignore errors = Yes
Read Only = No
Write only = No
Hosts allow = 172.16.1.0/16
Hosts deny = *
List = true
Uid = root
Gid = root
For final verification, the server starts the script, adds the file to the shared directory of the server, modifies the file, and synchronously updates the file on the client. You can also run the command on the server to check the file.
Inotifywait, used to monitor file changes
Inotifywatch is used to count the number of accesses to the file system and monitor the metadata of files.
What should I pay attention to in the experiment? How to set up the environment, principles and ideas? The most important thing is script and troubleshooting.
Rsync + inotify