Data Backup tool under Linux rsync

Source: Internet
Author: User
Tags rsync

Rsync is a file synchronization and Transfer tool under a Unix system. Rsync can not only synchronize data remotely (like an SCP), but can also synchronize data locally (similar to CP), but unlike CP or SCP, rsync does not overwrite previous data like CP/SCP (if the data already exists), It will first determine the difference between the existing data and the new data, and only the different parts will be overwritten.

1,linux installation Rsnync

# yum Install-y rsync

Start rsync

# rsync--daemon--config=/etc/rsyncd.conf

Common options for 2,rsync

-a archive mode, which means that the file is transferred recursively, and all properties are equal to-rlptgod, and the-a option can be followed by a--no-option, which indicates that one of the-rlptgod is closed, for example-a--no-l is equivalent to-rptgod

-R subdirectory in recursive mode, mainly for the directory, if a separate file does not need to add-r, but the transfer is the directory must be added to the-r option

-V Print some information, such as rate, number of files, etc.

-L reserved Soft link

-L handles soft links like regular files, and if there is a soft connection file in Src, then adding this option will copy the destination file pointed to by the soft link to the DST

-P Maintain file permissions

-O Keep File owner information

-G hold File group information

-D Keep Device file information

-T hold file time information

--delete Delete those files that are not in the DST SRC

--exclude=pattern specifies to exclude files that do not need to be transferred, the equals sign followed by the file name, can be a universal character pattern (such as *.txt)

--progress in the process of synchronization can see the process state of synchronization, such as statistics to synchronize the number of files, synchronized file transfer speed, etc.

-U plus this option will exclude new files in DST than SRC and will not overwrite

3,rsync syntax

rsync [OPTION] ... SRC DEST

rsync [OPTION] ... SRC [[Email protected]]host:dest

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

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

rsync [OPTION] ... SRC [[Email protected]]host::D EST



Example:

Local copy:

rsync [OPTION] ... SRC DEST Local Copy

# rsync-av 123.txt/tmp/sending incremental file list123.txtsent from bytes received to bytes 204.00 bytes/sectotal size is 0 Speedup is 0.00

The above example shows that the 123.txt in the current directory is synchronized to the/tmp/directory.

You can also change the name of the target file, changing the 123.txt to 234.txt

# Rsync-av 123.txt/tmp/234.txt


Remote copy, (a colon) is copied by SSH

rsync [OPTION] ... SRC [[Email protected]]host:DEST Remote Copy Local--Remote

# Rsync-av 123.txt 192.168.0.101:/data/the authenticity of host ' 192.168.0.101 (192.168.0.101) ' can ' t be established. RSA key fingerprint is b4:54:5f:73:ec:c2:60:5f:c3:79:c0:f9:51:e9:ac:e5. Is you sure want to continue connecting (yes/no)? yeswarning:permanently added ' 192.168.0.101 ' (RSA) to the list of known hosts. [email protected] ' s password:

No add [email protected] If no default refers to root.

The first connection will indicate if you want to continue the connection, we enter Yes to continue, when the connection is established, you need to enter a password. If it's okay to do this manually, but what if it's written in a script? This involves adding a trust relationship, which is described in more detail later in this section.


rsync [OPTION] ... [[email protected]] HOST:SRC DEST remote copy remote - local

# Rsync-av 192.168.0.101:/data/123.txt/tmp


Remote copy via SSH, if you want not to enter the password manually, then you need to use SSH key pair.

SSH Password-free login:

Production of certification documents

1: Public Key rsa.pub

2: Private key RSA

# Ssh-keygen

Return

Generating public/private RSA key pair.

Enter file in which to save the key (/ROOT/.SSH/ID_RSA):

Enter passphrase (empty for no passphrase):

Enter same Passphrase again:

Your identification has been saved In/root/.ssh/id_rsa.

Your public key has been saved in/root/.ssh/id_rsa.pub.

The key fingerprint is:

51:e9:2a:a0:76:a2:ef:f2:51:88:52:ae:25:b8:5f:7c [email protected]

# scp/root/.ssh/id_rsa.pub 192.168.0.101:/root/.ssh/authorized_keys

Send the public key to the IP machine to be remote, rename to Authorized_keys, then SSH connection no longer need password authentication.


remote Copy, (2 colons) belongs to Through background services the way to copy

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

rsync [OPTION] ... SRC [[Email protected]]host::D EST


This approach can be understood in this way, to establish an rsync server on the remote host, configure the various applications of rsync on the server, and then the machine as a client of rsync to connect to the remote rsync server. Here's how to configure an rsync server.

1. Set up and configure the rsync configuration file/etc/rsyncd.conf

# Vim/etc/rsyncd.confport=873log File=/var/log/rsync.logpid File=/var/run/rsyncd.pid#address=192.168.0.10[test] Path=/root/rsyncuse Chroot=truemax connections=4ignore errorsread only=yeslist=trueuid=rootgid=rootauth users= Testsecrets file=/etc/rsyncd.passwdhosts allow=192.168.0.101

Where the configuration file is divided into two parts: the full configuration section and the Module configuration section, the global part is just a few parameters, like the above rsyncd.conf in port, log file, PID file, address these are the global configuration, and [test] The following sections are part of the module configuration. There can be more than one module in a configuration file, the module name is customized. In fact, some parameters in the module such as Use Chroot, max connections, UDI, GID, auth users, Secrets file, and hosts allow can all be configured as global parameters. The parameters given in the above configuration file are not all, you can get more information through the man rsyncd.conf. Here's a quick explanation of what these parameters mean:

PORT specifies on which port to start the RSYNCD service, default is 873

Log file specifies the logfile

PID file to specify the PID files, the role of this file related to the start of services and stop processes such as management operations

address Specifies the IP to start the RSYNCD service, and if your machine has multiple IPs, you can specify one of the boot RSYNCD services, which is started on all IPs by default

[Test] Specifies the module name, custom

path specifies where data is stored

Use Chroot True|false is true by default, meaning that the file is first chroot to the directory specified by the path parameter before it is transferred. The reason for this is to implement additional security protections, but the disadvantage is that you need to roots permissions, and you cannot back up the directory files that point to external symbolic connections. By default, the Chroot value is true, and it is recommended to set false if there is a soft connection file in your data.

Max connections Specifies the maximum number of connections, the default is 0, which is no limit

Ignore errors ignore errors that occur in synchronization

Read Only yes defines access mode as read-only

list Specifies whether the module is listed when the user queries the available modules on the server, set to True to list, and false to hide

UID/GID Specifies which user/group to transfer when transferring files

Auth users specifies the user name to use when transferring

The secrets file specifies the password files, which, together with the parameters above, do not use password authentication if not specified

Hosts allow specifies the host that is allowed to connect to the module, either IP or network segment, if multiple, separated by a space


Edit rsyncd.passwd password file, format user name: Password , after saving to give 600 permissions , if the permissions are not correct, cannot complete synchronization

# CAT/ETC/RSYNCD.PASSWD

Test:test123

Modify Permissions

# chmod 600/ETC/RSYNCD.PASSWD

Start the RSYNCD service

# rsync--daemon--config=/etc/rsyncd.conf


Test on another machine (must be the IP address allowed in hosts allow )

# RSYNC-AVL [Email protected]::test/tmp/test5/

Password:

Receiving incremental file list

Created DIRECTORY/TMP/TEST5

./

1

1.txt

2

2.txt

3

4

Note: This synchronization still needs to enter a password, if you do not want to enter a password, there are two methods

Method One: Synchronize the command to specify the password file

Edit the password file on the client side ( note, not on the rsync server )

Vim/etc/pass add password for test User:

Test123

Permissions to modify password files: (Incorrect permissions will be error)

# chmod 600/etc/pass

When synchronizing, specify the password file, you can save the steps to enter the password:

# RSYNC-AVL [Email protected]::test/tmp/test8/--password-file=/etc/pass

Receiving incremental file list

Created Directory/tmp/test8

./

1

1.txt

2

2.txt

3

4

Test.txt

Sent bytes received 451 bytes 1282.00 Bytes/sec

Total size was 0 Speedup is 0.00


Method Two: Do not specify the user on the rsync server side

Modify the configuration file rsyncd.conf on the rsync server to remove the configuration entries for the authentication account (auth user and Secrets file):

# sed-i ' S/auth users/#auth users/;s/secrets file/#secrets file/'/etc/rsyncd.conf

The above command is the "Auth users" and "Secrets File" the first two lines of the first add a "#", so that the two lines commented out, so that it loses meaning. Then we'll test on the client host:

# RSYNC-AVL 192.168.0.10::test/test1//tmp/test9/

Receiving incremental file list

Created Directory/tmp/test9

./

1

1.txt

2

2.txt

3

4

Test.txt

Sent 162 bytes received 410 bytes 1144.00 bytes/sec

Total size was 0 Speedup is 0.00

Note that there is no need to add a test to this user, the default is the identity of the root copy , and now do not have to enter a password.

This article is from the "Baiyubao blog" blog, make sure to keep this source http://baiyubao.blog.51cto.com/2845008/1660531

Data Backup tool under Linux rsync

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.