Home > Backend Development > PHP Tutorial > Remember to incrementally synchronize remote server files with rsync

Remember to incrementally synchronize remote server files with rsync

WBOY
Release: 2016-08-08 09:24:37
Original
909 people have browsed it

rsync remote shell incrementally synchronizes data

There are two ways to synchronize files with rsync, one is the daemon method (rsync daemon) and the other is through the remote shell method (rsync remote shell).

The difference between the two methods

daemon mode. This method connects to the remote rsync daemon through TCP. You need to use a configuration file and enable the daemon process.

<code>rsync [OPTION] user@host::src dest
rsync [OPTION] src user@host::dest</code>
Copy after login

remote shell method, this method does not require the use of configuration files or daemon processes.

<code>rsync [OPTION] user@host:src dest
rsync [OPTION] src user@host:dest</code>
Copy after login

The daemon method is too troublesome. I have to set up the configuration file and daemon process, so I chose the remote shell method to synchronize as follows:

<code>  rsync -avr --delete yanruitao@123.123.123.123:/export/test/htdocs/files/ /export/test/htdocs/files/</code>
Copy after login

The parameters avr --delete respectively represent

<code>-a 归档(archive)模式,以递归方式传输文件,并保持文件属性
-v 输出同步的详细信息(verbose)
-r 对子目录进行递归模式处理(recursive)
--delete 删除源(SRC)中没有目标(DST)中有的文件</code>
Copy after login

If everything is normal, when you run the above synchronization command, you will be prompted to enter the password of the yanruitao user on the remote machine (the first time you will be prompted whether to establish a relationship (it seems to be, I forgot the details), just press Enter), after execution It will be synchronized according to the rules.

But there is a problem. I want to add a crontab task and perform incremental synchronization at 2 o'clock every day. I cannot enter the password at this time. At this time, I have to establish a trust relationship on the two machines

Establish trust relationship between two machines

Let’s take a look at encryption and authentication based on public and private keys before establishing a trust relationship

Private key signing process

The picture below is stolen, haha, it’s perfect to use this picture to understand the authentication process:

<code>消息-->[私钥]-->签名-->[公钥]-->认证
私钥数字签名,公钥验证</code>
Copy after login
  1. Alice generates a public key and a private key and sends the public key to Bob.
  2. Alice uses her private key to generate a signature, which is encryption.
  3. Alice sends the signed information to Bob.
  4. Bob uses Alice’s public key to decrypt and verify the authenticity of the signature.
    Remember to incrementally synchronize remote server files with rsync
Public key encryption process

The picture below is also stolen (it’s easier to understand if you have a picture). Here is Alice sending information to Bob through symmetric key technology:

<code>消息-->[公钥]-->签名后的消息-->私钥-->解密后的消息
公钥加密,私钥解密</code>
Copy after login
  1. Bob generates his own public and private keys and sends the public key to Alice.
  2. Alice encrypts the message with Bob's public key.
  3. Alice sends the encrypted information to Bob.
  4. Bob uses his private key to decrypt and obtain the information sent by Alice.
    Remember to incrementally synchronize remote server files with rsync

Establish trust relationship between hosts

Understanding the above knowledge of public keys and private keys, now let’s establish a trust relationship between two hosts,

, , to establish a trust relationship with 100 on 101 (that is, 100 does not need to enter a password when logging in to 101), this time the private key signature process is used:
Generate the public key and private key in the home directory of yanruitao192.168.1.100 (the existing ones can be ignored)
<code>ssh-keygen -t rsa
#执行完之后会在家目录下的.ssh文件夹下生成id_rsa、id_rsa.pub两个文件,后者是公钥。
scp .ssh/id_rsa.pub yanruitao@192.168.1.101:/home/yanruitao/
#scp同样是通过remote shell的方式传送文件,回车之后会提示输入密码,此时
#还未建立信任关系,因此需要输入密码。确认100的公钥发送给101</code>
Copy after login
Establish a trust relationship with yanruitao192.168.1.100 in the home directory of yanruitao192.168.1.101
<code>#将刚才传送的100机器的公钥写入101yanruitao及目录下的.ssh/authorized_keys文件
cat id_rsa.pub >> .ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
#至此就在yanruitao@192.168.1.101上对192.168.1.100建立了信任关系</code>
Copy after login
Set up crontab
<code>0 */2 * * * rsync yanruitao@192.168.1.101:/export/wwwroot/hotdocs/files/ /export/wwwroot/hotdocs/files/
#设置完成,每天02:00分会执行一次增量同步</code>
Copy after login
Problems encountered

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

<code>#修改文件夹所有者为mywife
sudo chown mywife:users /export/wwwroot/htdocs/files/</code>
Copy after login

Otherwise, an error may be reported (I forgot the specific error, I encountered it at the company). Although it does not seem to affect synchronization, $? will return 23, which will affect the following operations. Please pay attention here as well.

Reference article

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

The copyright of this article belongs to the author iforever (). Any form of reprinting is prohibited without the author's consent. After reprinting the article, the author and the original text link must be given in an obvious position on the article page, otherwise we reserve the right to pursue legal liability s right.

The above introduces the incremental synchronization of remote server files with rsync, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template