Linux Server Sync rsync sync server file _linux

Source: Internet
Author: User
Tags chmod ssh rsync
When files on one server are updated, other servers also need to be updated, and when they are updated
It should be updated only on new or modified files, otherwise it will cause network bandwidth and waste of time. Rsync is able to have
Excellent software that keeps files and directories consistent.
Rsync,remote Synchronize
As implies know it is a remote synchronization function of the software, it synchronized files at the same time, you can keep the original file
Additional information such as permissions, time, soft and hard links, and can be transmitted through SSH to the file, so that its confidentiality is also very good, another
It is also free of 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 software, so many Linux distributions are included in it. Your Linux
There is no rsync installed in, you can install it by the following safety method:
First, the installation process
1. Download rsync
Currently (September 2003) The latest version of Rsync is 2.5.6, from RYSNC's official website to download one back:
# wget http://ftp.samba.org/ftp/rsync/rsync-2.5.6.tar.gz
2. Decompression
# TAR-XZPVF Rsync-2.5.6.tar.gz

3. Compile and install
# CD RSYNC-2.5.6/
#./configure–prefix=/usr/local/rsync
# make
# make Install
The above process does not appear to be installed, now there is the rsync command can be used, the rsync command on
/usr/local/rsync/bin. Use the rsync command to crawl data on servers running Rsync services.
If you want to turn the current machine into an rsync server, you need to continue with some configuration.
Second, the configuration of rsync services
Configuring a simple Rsync service is not complicated and you need to modify or set up 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, given 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 above configuration file, 192.168.100.0/24 this subnet, only 192.168.100.90 of the machine can
To access the Rsync service for this rsync server. The following section of the configuration file defines the two rsync directories, the Terry directory
is only know Terry, rsync two account people can use, and the text directory is not required to access the account. Rsync
There are other options available 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, which is a plaintext text file, given a
Examples of rsyncd.secrets files:
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 must 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 the welcome information for the Rsync service, where you can enter any textual information, such as:
Welcome to use the rsync services!
4.services
# vi/etc/services
Services are not a configuration file for rsync, and this step is not possible. 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 make sure that there are two lines in services,
If not, join yourself:
rsync 873/tcp # rsync
rsync 873/UDP # rsync
5./etc/xinetd.d/rsync
# Vi/etc/xinetd.d/rsync
Create a file called/etc/xinetd.d/rsync, and 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. use of the rsync command
After the rsync server is configured, the Rsync commands can be issued from the client to implement various synchronization operations. Rsync has a very
Multi-functional options, here are some common options:
The command format for rsync can be:

1. rsync [OPTION] ... src [src] ... [user@] Hostdest
2. rsync [OPTION] ... [user@] HOST:SRC DEST
3. rsync [OPTION] ... src [src] ... DEST
4. rsync [OPTION] ... [user@] HOST::SRC [DEST]
5. rsync [OPTION] ... src [src] ... [user@] Host:dest
6. rsync [OPTION] ... rsync://[user@]host[port]/src [DEST]

Rsync has six different modes of operation:

1. Copy local files and start this mode of work when both 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 DST
This mode is started when the path address contains a single colon ":" Separator.
3. Use a remote shell program (such as rsh, SSH) to copy the contents of the remote machine to the local machine. When SRC
This mode is started when the address path contains a single colon ":" Separator.
4. Copy files from remote rsync server to local machine. This mode is started when the SRC path information contains the "::" delimiter.
5. Copy files from local machine to remote rsync server. This mode is started when the DST path information contains the "::" separator.
6. List of files for remote machines. This is similar to rsync transmission, but simply omitting the local machine information in the command.

Here is an example to illustrate:
# rsync-vazu-progress terry@192.168.100.21:/terry//home
V Detailed Tips
A in archive mode, copy directory, symbolic connection
Z Compression
U only update to prevent local new files from being rewritten, notice both machine clock
-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 your Terry account before you perform the synchronization, which is in front of us in 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
terry@192.168.100.21:/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.
The content is simple, as follows:
terry:12345
Also modify the file properties as follows:
# chmod 600/etc/rsyncd.secrets
Iv. using rsync to keep file synchronization instances between Linux servers
Now suppose there are two Linux servers A (192.168.100.21) and B (192.168.100.90), Server A
The/home/terry and/home/terry in Server B need to be synchronized, that is, when a file in Server a occurs
After the change, the files in Server B should be changed accordingly.
We use the above method to install Rsync on Server A and configure it as an rsync server and/home/terry
The directory is configured as an rsync shared directory. Then install rsync on Server B because B is only a client, so no configuration is required.
Then, on Server B, create the following script:
#!/bin/bash
/usr/loca/rsync/bin/rsync-vazu-progress–delete
–password-file=/etc/rsync.secret terry@192.168.100.21:/terry//home
Save this script as atob.sh and add the executable properties:
# chmod 755/root/atob.sh
Then, with the crontab setting, the script runs every 30 minutes. To execute a command:
# CRONTAB-E
Enter the following line:
0,30 * * * */root/atob.sh
Save exits so that Server B will automatically run once per hour with 0 points and 30 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 takes the same
Got the latest information like Server A.
V. Other applications
In addition to synchronizing files and directories, rsync can also use it to implement remote backups of remote Web sites. If the script is combined again
And crontab can realize the automatic remote backup of timing. It can achieve similar effects with commercial backup and mirror products, but completely
Free

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.