Linux rsync Command Detailed

Source: Internet
Author: User
Tags rsync

Servers often maintain a consistent set of files or directories, such as large software download sites, which typically use multiple servers to provide download services. When the file on a server is updated, other servers need to be updated, and should be updated only for new or modified files, or it will cause network bandwidth and time wasted. Rsync is an excellent software that can effectively keep files and directories consistent.

Rsync,remote Synchronize

As implies means that it is a remote synchronization software, it in the synchronization of files at the same time, can maintain the original file permissions, time, soft and hard links and other additional information, and can be transferred via SSH file, so its confidentiality is very good, in addition it is free software. RYSNC's official website: http://rsync.samba.org/, you can get the latest version from above. Of course, because rsync is such a useful piece of software, many distributions of Linux are included in it. You do not have Rsync installed on your Linux and you can install it yourself according to the following security methods:

First, the installation process

1. Download rsync

Currently (September 2003) The latest rsync version is 2.5.6, from the official website of RYSNC to download one back:

# wget http://ftp.samba.org/ftp/rsync/rsync-2.5.6.tar.gz

2. Unzip

# TAR-XZPVF Rsync-2.5.6.tar.gz

3. Compiling the installation

# CD RSYNC-2.5.6/

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

# make

# make Install

The above process does not appear to install, now there is the rsync command can be used, the rsync command placed in

/usr/local/rsync/bin. Use the rsync command to crawl data on a server running the Rsync service.

If you want to turn the current machine into an rsync server, you need to continue with some configuration.

Ii. Configuring Rsync Services

Configuring a simple Rsync service is not complicated and you need to modify or create some configuration files.

1.rsyncd.conf

# VI/ETC/RSYNCD.MOTD

Rsyncd.con is the main configuration file for the Rsync service, which controls the various properties of the Rsync service, giving a

Examples of rsyncd.conf files:

#先定义整体变量

Secrets file =/etc/rsyncd.secrets

MOTD file =/ETC/RSYNCD.MOTD

Read Only = yes

List = yes

UID = nobody

GID = Nobody

Hosts allow = 192.168.100.90 #哪些电脑可以访问rsync服务

Hosts deny = 192.168.100.0/24 #哪些电脑不可以访问rsync服务

Max connections = 2

Log file =/var/log/rsyncd.log

PID file =/var/run/rsyncd.pid

Lock file =/var/run/rsync.lock

#再定义要rsync目录

[Terry]

Comment = Terry ' s directory from 192.168.100.21

Path =/home/terry

Auth users = Terry,rsync

[Test]

Comment = Test rsync

Path =/home/test

In the configuration file above, only 192.168.100.90 of the machines in the 192.168.100.0/24 subnet are qualified to access the Rsync service of the rsync server. The following section of the configuration file defines two rsync directories, which are available only to people who know Terry and rsync two accounts, and the text directory is accessible without an account. Rsync also provides a number of other options for defining the directory, which can be more tightly controlled.

2.rsyncd.secrets

# vi/etc/rsyncd.secrets

Rsyncd.secrets is the user name and password that stores the Rsync service, it is a text file in plaintext, and here is an example of a rsyncd.secrets file:

terry:12345

Rsync:abcde

Because Rsyncd.secrets stores the user name and password for the rsync service, it is important that the properties of the file be

Set to 600, only the owner can read and write:

# chmod 600/etc/rsyncd.secrets

3.rsyncd.motd

# VI/ETC/RSYNCD.MOTD

RSYNCD.MOTD records a welcome message for the Rsync service, where you can enter any text information, such as:

Welcome to use the rsync services!

4.services

# vi/etc/services

Services is not a configuration file for rsync, and this step can also be done. The benefit of modifying the services file is that

The system knows that the 873 port pair is on the service name Rsync. The way to modify services is to ensure that services have the following two lines,

If you don't, you can join yourself:

rsync 873/tcp # rsync

rsync 873/UDP # rsync

5./etc/xinetd.d/rsync

# Vi/etc/xinetd.d/rsync

To create a file named/etc/xinetd.d/rsync, enter the following:

Service rsync

{

Disable = no

Socket_type = Stream

wait = no

user = root

Server =/usr/local/rsync/bin/rsync

Server_args =--daemon

Log_on_failure + = USERID

}

Once saved, you can run the Rsync service. Enter the following command:

#/etc/rc.d/init.d/xinetd Reload

So the Rsync service is running on this machine (192.168.100.21), and the next step is how to use it.

Iii. usage of the rsync command

After configuring the Rsync server, you can issue the rsync command from the client to achieve a variety of synchronous operations. Rsync has a very

Multi-function options, here's a look at common options:

The command format for rsync can be:

1. rsync [OPTION] ... src [src] ... [[email protected]] Host:dest]

2. rsync [OPTION] ... [[email protected]] HOST:SRC DEST]

3. rsync [OPTION] ... src [src] ... DEST]

4. rsync [OPTION] ... [[email protected]] HOST::SRC [DEST]

5. rsync [OPTION] ... src [src] ... [[email protected]] HOST:: DEST]

6. rsync [OPTION] ... rsync://[[email protected]]host[:P ort]/src [DEST]

Rsync has six different modes of operation:

1. Copy the local file and start this mode of operation when both the SRC and des path information do not contain a single colon ":" delimiter.

2. Use a remote shell program (such as rsh, SSH) to copy the contents of the local machine to the remote machine. When the DST

The path address contains a single colon ":" delimiter when the mode is started.

3. Use a remote shell program (such as rsh, SSH) to copy the contents of the remote machine to the local machine. When SRC

The address path contains a single colon ":" delimiter when the mode is started.

4. Copy files from the remote rsync server to the local machine. This mode is started when the SRC path information contains the "::" delimiter.

5. Copy files from the local machine to the remote rsync server. This mode is started when the DST path information contains the "::" delimiter.

6. List the files of the remote machine. This is similar to the rsync transfer, but only if the local machine information is omitted from the command.

The following examples illustrate:

# rsync-vazu-progress [Email protected]:/terry//Home

V Detailed Tips

A in archive mode, copy directory, symbolic connection

Z-Compression

You only update to prevent local new files from being rewritten, noting both machines ' clocks while

-progress refers to the display

The above command is to keep the/home/terry directory on the client 192.168.100.90 and the Terry directory on the rsync server

Step. This command will require you to enter the password for the Terry account before it is synchronized, this account is in front of us at rsyncd.secrets

defined in the file. If you want to write this command in a script and then execute it regularly, you can use the--password-file

Options, specific commands are as follows:

# rsync-vazu-progress--password-file=/etc/rsync.secret

[Email protected]:/terry//Home

To use the--password-file option, you must first create a file that holds the password, which is specified as/etc/rsync.secret.

Its content is simple, as follows:

terry:12345

Also modify the file attributes as follows:

# chmod 600/etc/rsyncd.secrets

Four, using rsync to maintain the Linux server file synchronization between instances

Now assume that there are two Linux servers A (192.168.100.21) and B (192.168.100.90), and Server A

Both directories in/home/terry and Server B need to be synchronized, that is, when the files in Server A

After the change, the file in Server B should also be changed.

We install rsync on Server A as described above, and configure it as an rsync server and/home/terry

The directory is configured as a directory shared by Rsync. Then install rsync on Server B because B is only a client, so there is no need to configure it.

Then, in Server B, create the following script:

#!/bin/bash

/usr/loca/rsync/bin/rsync-vazu-progress--delete

--password-file=/etc/rsync.secret [Email protected]:/terry//Home

Save the script as atob.sh and add the executable property:

# chmod 755/root/atob.sh

Then, with crontab settings, let the script run every 30 minutes. Execute command:

# CRONTAB-E

Enter the following line:

0,30 * * * */root/atob.sh

Save the exit so that Server B will run automatically 0 minutes and 30 ticks per hour atob.sh,atob.sh is responsible for

Keep Server B and server a synchronized. This ensures that all updates to server A are made after 30 minutes, and Server B also takes

Get the latest information like Server A.

V. Other applications

In addition to synchronizing files and directories, rsync can also use it to enable remote backup of remote Web sites. If you combine scripts and crontab, you can automate remote backups at regular intervals. It can achieve similar results with commercially available backup and mirror products, but is completely free.

Attached: Rsync has six different modes of operation:

1. Copy the local file and start this mode of operation when both the SRC and des path information do not contain a single colon ":" delimiter.

2. Use a remote shell program (such as rsh, SSH) to copy the contents of the local machine to the remote machine. When the DST

The path address contains a single colon ":" delimiter when the mode is started.

3. Use a remote shell program (such as rsh, SSH) to copy the contents of the remote machine to the local machine. When SRC

The address path contains a single colon ":" delimiter when the mode is started.

4. Copy files from the remote rsync server to the local machine. This mode is started when the SRC path information contains the "::" delimiter.

5. Copy files from the local machine to the remote rsync server. This mode is started when the DST path information contains the "::" delimiter.

6. List the files of the remote machine. This is similar to the rsync transfer, but only if the local machine information is omitted from the command.

Linux rsync Command Detailed

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.