Use rsync in centos for scheduled Replication

Source: Internet
Author: User
Tags file copy rsync usage

1. Download and install rsync
* *** Rsync is enabled for normal RH installation, and the running file is stored in/usr/bin/rsync.

# Tar zxvf rsync-2.6.9.tar.gz

# Cd rsync-2.6.9

#./Configure -- prefix =/usr/local/rsync

# Make

# Make install

2. Configure rsync server

1. Start rsync

# Vi/etc/xinetd. d/rsync

Change disable's original yes to no

Service rsync

{

Disable = No

Socket_type = stream

Wait = No

User = root

Server =/usr/bin/rsync

Server_args = -- daemon

Log_on_failure + = userid

}

Start rsync with the System

# Chkconfig rsync on

2. Configure/etc/rsyncd. conf (manually generated)

The parameters of rsyncd. conf are written on the top as global parameters, and the parameters of modules are written on the top.

# Vi/etc/rsyncd. conf

Global Parameters

Uid = root # the user who runs the rsync daemon

Gid = root # group for running the rsync daemon

Use chroot = No # Do not use chroot

Max connections = 4 # the maximum number of connections is 4

Strict modes = yes # Check the permission of the password file

Port = 873 # default port 873



Module Parameters

[Backup] # The Authentication Module name must be specified on the client.

Path =/home/backup/# directory of the image to be created, which is indispensable!

Comment = This is a test # comment on this module

Ignore errors # ignore unrelated Io errors

Read Only = yes # Read-Only

List = No # column Files Not Allowed

Auth users = hening # The authenticated user name. If this row is not used, it indicates that the user is anonymous and has nothing to do with the system.

Secrets file =/etc/rsyncd. Pw # table of password and user name comparison. The password file is generated by yourself.

Hosts allow = 192.168.1.1, 10.10.10.10 # allow hosts

Hosts deny = 0.0.0.0/0 # disable hosts

# Transfer logging = Yes

Note: The following green files are automatically generated after the rsync service is installed.

PID file =/var/run/rsyncd. pid # storage location of PID files

Lock file =/var/run/rsyncd. Lock # lock file storage location

Log File =/var/log/rsyncd. log # storage location of Log File

================== Instance ==================

Uid = nobody

Gid = nobody

Use chroot = No

Max connections = 4

Stirict modes = Yes

Port = 873

PID file =/var/run/rsyncd. PID

Lock file =/var/run/rsyncd. Lock

Log File =/var/log/rsyncd. Log

[HTML]

Path =/home/html/

Comment = This is a html

Ignore errors

Read Only = No

List = No

Hosts allow = 192.168.152.155, 10.10.10.10

Hosts deny = 0.0.0.0/0

Auth users = devilzy

Secrets file =/etc/rsyncd. pw

[Index]

Path =/home/index/

Comment = This is a Index

Ignore errors

Read Only = No

List = No

Hosts allow = 192.168.152.155

Hosts deny = 0.0.0.0/0

Auth users = devilzy

Secrets file =/etc/rsyncd. pw

================== Instance ==================



3. Configure the rsync password (the path has been written in the preceding configuration file)/etc/rsyncd. pw (name can be written at will, as long as it is consistent with the above configuration file), format (one row per user)

Account: Password

# Vi/etc/rsyncd. pw

Example:

Hening: 111111

Permission: Because rsyncd. Pw stores the user name and password of the rsync service, it is very important. Set rsyncd. PW to root and the permission is 600.

# Cd/etc

# Chown root. Root rsyncd. pw

# Chmod 600 rsyncd. pw

3. rsyncd. motd (configuration welcome information, optional)

# Vi/etc/rsyncd. motd

Rsyncd. motd records the welcome information of the rsync service. You can enter any text information, such:

Welcome to use the rsync services!

4. Make the configuration take effect

# Service xinetd restart

================================================================ ==========

1. xinetd: unrecognized service

Perform the following operations:

Yum-y install xinetd

================================================================ ==========

3. Start rsync server

Two Methods for rsync server startup

1. Start the rsync server (independently started)

#/Usr/bin/rsync -- daemon

2. Start the rsync server (xinetd superprocess is enabled)

#/Etc/rc. d/init. d/xinetd reload

3. Restart Method: Kill-9 rsync

4. Add RC. Local

In various operating systems, RC files are stored in different locations and can be modified so that rsync-daemon can be loaded during system startup.

# Vi/etc/rc. Local

Add/usr/bin/rsync -- daemon

5. Check rsync

# Netstat-A | grep rsync

TCP 0 0 0.0.0.0: 873 0.0.0.0: * listen

6. enable port iptables

Iptables-A input-p tcp-s! 192.168.152.155 -- dport 873-J Drop

In this case, only the Client IP 11.22.33.44 can enter this rsync server.

7. Configure rsync Client

1. Set the password

# Vi/etc/rsyncd. pw

111111

Modify permissions

# Cd/etc

# Chown root. Root rsyncd. pw

# Chmod 600 rsyncd. pw

2. Connect the client to the server

Retrieving files from the server

/Usr/bin/rsync-vzrtopg -- Progress -- delete devilzy@192.168.152.154: Index/home/index -- password-file =/etc/rsyncd. pw

/Usr/bin/rsync-vzrtopg -- Progress -- delete devilzy@192.168.152.154: HTML/home/html -- password-file =/etc/rsyncd. pw

Upload files to the server

/Usr/bin/rsync-vzrtopg -- Progress -- password-file =/root/rsyncd. PW/home/index devilzy@192.168.152.154: Index

/Usr/bin/rsync-vzrtopg -- Progress -- password-file =/root/rsyncd. PW/home/html devilzy@192.168.152.154: HTML

This command backs up all files (including subdirectories) in the local machine/home/Backup Directory to the backup directory set by the backup module of rsync server (172.20.0.6.

Note that if the path ends with "/", it indicates backing up the stuff in the directory, but it does not create this directory. If it does not contain "/", it is created.

Rsync usage:

Rsync [Option]... [user @] HOST: SRC [DEST] # Back Up Files from rsync server to Local Machine

Rsync [Option]... SRC [SRC]... [user @] HOST: DEST # Back Up Files from local machines to rsync server

3. Automatic Operation

1) vi/usr/local/rsync/time. Sh // create a script file

Copy the following content

#! /Bin/bash

/Usr/bin/rsync-vzrtopg -- Progress -- delete devilzy@192.168.152.154: Index/home/index -- password-file =/etc/rsyncd. pw

/Usr/bin/rsync-vzrtopg -- Progress -- delete devilzy@192.168.152.154: HTML/home/html -- password-file =/etc/rsyncd. pw

# Chmod + x/usr/local/rsync/time. Sh

2) crontab-e

Add ***/usr/local/rsync/time. Sh // run the time. Sh script file every minute.

Add 55 */usr/local/rsync/time. Sh // run the time. Sh script every 55 minutes of an hour (run every hour)

Command Introduction:-rvlhpogdts

Rsync Command Parameters

-V indicates verbose details

-Z indicates Compression

-R indicates recursive Recursion

-T indicates the creation time of the original file.

-O indicates to keep the original file owner

-P indicates the parameter for retaining the original file.

-G indicates the group to which the original file belongs.

-A archive Mode

-P indicates the option function that replaces-partial and-progress.

-E create an encrypted connection through SSH.

-- Partial: prevents rsync from deleting the copied part during transmission interruption (if the transmission is interrupted during file copy, rsync's default operation is to undo the previous operation, that is, remove the copied part from the target machine.

Delete some copied files .)

-- Progress indicates that the detailed progress is displayed.

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

-- Exclude does not include the/INS directory

-- Size-only: the difference between the two folders is that there are some new files in the source folder, and there are no files with duplicate names and modified names, this type of file may be omitted because the content is modified to the same size. This parameter can greatly improve synchronization efficiency because it does not need to check whether the content of files with the same name is the same.

-- Password-file: Specifies the password file, which contains the password of the user specified by the server.

In this way, you can use the password in the script without entering the verification password interactively. Note that the permission attribute of this password file must be set to only the owner-readable.

Hening@192.168.0.217: Backup

Hening refers to the user specified for authentication on the server.

192.168.0.217 indicates the IP address of the server.

: Backup indicates the name of the module to be synchronized on the server;

/Home/quack/backup/$ date indicates the directory address of the synchronized file stored in the local machine.

/Var/log/rsync. $ date is the directory address of the Synchronized Log files stored in the local machine.

Note:

If you do not put/, the directory name also contains mirror. If you put/, There is only mirror in the directory.

Instance summary process:

1. Configure the master Console

# Vim/etc/rsyncd. conf

###################################

Uid = nobody

Gid = nobody

Use chroot = No

Max connections = 4

Stirict modes = Yes

Port = 873

[HTML]

Path =/home/html

Comment = This is a html

Ignore errors

Read Only = false

List = No

Hosts allow = 192.168.152 .*

Hosts deny = 0.0.0.0/0

Auth users = devilzy

Secrets file =/etc/rsyncd. pw

PID file =/var/run/rsyncd. PID

Lock file =/var/run/rsyncd. Lock

Log File =/var/log/rsyncd. Log

[Index]

Path =/home/Index

Comment = This is a Index

Ignore errors

Read Only = false

List = No

Hosts allow = 192.168.152 .*

Hosts deny = 0.0.0.0/0

Auth users = devilzy

Secrets file =/etc/rsyncd. pw

PID file =/var/run/rsyncd. PID

Lock file =/var/run/rsyncd. Lock

Log File =/var/log/rsyncd. Log

###################################

# Vim/etc/rsyncd. pw

###################################

Devilzy: 123456

###################################

# Cd/etc

# Chown root. Root rsyncd. pw

# Chmod 600 rsyncd. pw

Start rsync server

# Rsync -- daemon

Check whether port 873 is enabled

Join startup

# Echo "rsync -- daemon">/etc/rc. Local

Write Permission to the/usr/local/test directory

# Chown-r Nobody. Nobody/usr/local/test

# Chmod-r 770/usr/local/test

Master configuration complete

2. Client Configuration

# Vim/etc/rsyncd. pw

####################################

123456

####################################

# Chown root. Root/etc/rsyncd. pw

# Chmod 600/etc/rsyncd. pw

Then, use the command to directly update the data file on the server without the password.

/Usr/bin/rsync-vzrtopg -- Progress -- password-file =/root/rsyncd. PW/home/index devilzy@192.168.152.154: Index

/Usr/bin/rsync-vzrtopg -- Progress -- password-file =/root/rsyncd. PW/home/html devilzy@192.168.152.154: HTML

Note:

1. The index name here is [Index] In the conf configuration of the master. The index name must be the same.

2. devilzy is the devilzy In the conf configuration of the master, which can be named randomly, not a system user.

========================== Problems and Solutions ======================================

1. @ error: chdir failed

Rsync error: Error starting client-server protocol (Code 5) at main. C (1296) [handler ER = 2.6.8]

Cause and solution:

SELinux;

Setsebool-P rsync_disable_trans on

========================== Problems and Solutions ======================================

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.