


Remember to incrementally synchronize remote server files with rsync
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>
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>
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>
The parameters avr --delete respectively represent
<code>-a 归档(archive)模式,以递归方式传输文件,并保持文件属性 -v 输出同步的详细信息(verbose) -r 对子目录进行递归模式处理(recursive) --delete 删除源(SRC)中没有目标(DST)中有的文件</code>
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>
- Alice generates a public key and a private key and sends the public key to Bob.
- Alice uses her private key to generate a signature, which is encryption.
- Alice sends the signed information to Bob.
- Bob uses Alice’s public key to decrypt and verify the authenticity of the signature.
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>
- Bob generates his own public and private keys and sends the public key to Alice.
- Alice encrypts the message with Bob's public key.
- Alice sends the encrypted information to Bob.
- Bob uses his private key to decrypt and obtain the information sent by Alice.
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>
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>
Set up crontab
<code>0 */2 * * * rsync yanruitao@192.168.1.101:/export/wwwroot/hotdocs/files/ /export/wwwroot/hotdocs/files/ #设置完成,每天02:00分会执行一次增量同步</code>
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>
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Alipay PHP...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...
