Record an rsync incremental synchronization of remote server files

Source: Internet
Author: User
: This article describes how to record an rsync incremental synchronization of remote server files. For more information about PHP tutorials, see. Rsync remote shell incremental data synchronization

Rsync supports two methods to synchronize files. one is the daemon method (rsync daemon) and the other is the remote shell method (rsync remote shell ).

Differences between the two methods

Daemon mode. In this mode, remote rsync daemon is connected through TCP. you need to use the configuration file and enable the daemon process.

rsync [OPTION] user@host::src destrsync [OPTION] src user@host::dest

Remote shell, which does not require configuration files or daemon processes.

rsync [OPTION] user@host:src destrsync [OPTION] src user@host:dest

The daemon method is too troublesome, so you have to set the configuration file and daemon process. Therefore, I chose the remote shell method, just like the following:

  rsync -avr --delete yanruitao@123.123.123.123:/export/test/htdocs/files/ /export/test/htdocs/files/

The parameter avr -- delete indicates

-A archive mode: transfers files recursively, and maintains the file attribute-v output details of synchronization (verbose)-r performs recursive mode processing (recursive) on subdirectories) -- delete: delete files in the source (SRC) that do not have a destination (DST)

If everything is normal, when you run the above synchronization command, you will be prompted to enter the password of the user of the remote machine yanruitao (the first time, you will be prompted to establish a link (as if yes, forgot to do it ), directly press enter). after the execution, the synchronization will follow the rules.

But there is a problem. I want to add a crontab task, which is incrementally synchronized once at two o'clock every day. at this time, I cannot enter the password. at this time, I have to establish a trust relationship between the two machines.

Establish a trust relationship between two machines

Before establishing a trust relationship, let's look at encryption and authentication based on public and private keys.

Private key signature process

The figure below is stolen. haha, it is better to understand the authentication process with this figure:

Message --> [private key] --> signature --> [public key] --> authenticates the digital signature of the private key and verifies the public key.
  1. Alice generates the public key and private key, and sends the public key to Bob.
  2. Alice uses her own private key to generate a signature, that is, encryption.
  3. Alice sends the signature information to Bob.
  4. Bob decrypts the signature with Alice's public key to verify the authenticity of the signature.
Public key encryption process

The figure below is also stolen (the figure is easy to understand). Here Alice sends information to Bob through symmetric key technology:

Message --> [public key] --> signed message --> Private key --> decrypted message public key encryption, private key decryption
  1. Bob generates his own public key and private key, and sends the public key to Alice.
  2. Alice uses Bob's public key to encrypt the information.
  3. Alice sends encrypted information to Bob.
  4. Bob uses his private key for decryption and obtains the information sent by Alice.
Establish trust relationships between hosts

After learning about the public key and private key, we can establish a trust relationship between the two hosts, Assume there are two machines: yanruitao at 192 dot 168 dot 1 dot 100 , Yanruitao at 192 dot 168 dot 1 dot 101 To establish a trust relationship with 101 on 100 (that is, you do not need to enter a password when logging on to 100 on 101), the private key signature process is used at this time:

Generate the public key and private key in the home directory of yanruitao192.168.1.100 (some can be ignored)
Ssh-keygen-t rsa # after execution, the id_rsa and id_rsa.pub files are generated in the. ssh folder in the home directory. The latter is the public key. Scp. ssh/id_rsa.pub yanruitao@192.168.1.101:/home/yanruitao/# scp is also the way to transfer files through remote shell, press enter will prompt to enter the password, at this time # No trust relationship has been established, therefore, you must enter the password. Confirm that the public key of 100 is sent to 101
Create a trust relationship for yanruitao192.168.1.100 in the home directory of yanruitao192.168.1.101
# Write the public key of the 100 machine just transferred to the. ssh/authorized_keys file cat id_rsa.pub>. ssh/authorized_keyschmod 700 ~ /. Sshchmod 600 ~ /. Ssh/authorized_keys # the trust relationship between 192.168.1.100 on the yanruitao@192.168.1.101 has been established so far
Set crontab
0 */2 * rsync yanruitao@192.168.1.101:/export/wwwroot/hotdocs/files/# The setting is complete, and one incremental synchronization is executed at every day.
Problems encountered

Of course, I may encounter problems in actual operations. when I was doing this, the user names of the two machines were different. one was yanruitao and the other was mywife (haha, don't laugh ), in yanruitao, you need to re-operate the above trust relationship steps in the mywife home directory. Another problem is that the owner of the/export/wwwroot/htdocs/files/folder on the mywife machine must be mywife:

# Change the folder owner to mywifesudo chown mywife: users/export/wwwroot/htdocs/files/

Otherwise, an error may be reported (I forgot the specific error and encountered it in the company). although it does not seem to affect synchronization, but $? The return value is 23, which affects the following operations. pay attention to it here.

References

Http://www.williamlong.info/archives/837.html
Http://www.cnblogs.com/ymy124/archive/2012/04/04/2432432.html
Http://www.zhihu.com/question/25912483

This article is copyrighted by iforever ( Luluyrt at 163 dot com All, reprinting in any form is prohibited without the author's consent. after reprinting, the author and the original article must be clearly connected on the article page; otherwise, the right to pursue legal liability will be retained.

The above describes how to record an rsync incremental synchronization of remote server files, including the content, and hope to be helpful to friends who are interested in PHP tutorials.

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.